diff options
author | Marvin W <git@larma.de> | 2018-01-12 21:03:09 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2018-01-28 00:38:47 +0100 |
commit | 782ae4c049e2b6fab13d7453cbb0e74610e7d200 (patch) | |
tree | 1b4cd0a1689cee0c2e2cac2ae9a5fb8ebbe63621 /xmpp-vala/src/module/jid.vala | |
parent | d46d071e57e599e8cfb1780597cbecb36881c4d8 (diff) | |
download | dino-782ae4c049e2b6fab13d7453cbb0e74610e7d200.tar.gz dino-782ae4c049e2b6fab13d7453cbb0e74610e7d200.zip |
Move Jid class to xmpp-vala, partially refactor namespace
Diffstat (limited to 'xmpp-vala/src/module/jid.vala')
-rw-r--r-- | xmpp-vala/src/module/jid.vala | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/jid.vala b/xmpp-vala/src/module/jid.vala new file mode 100644 index 00000000..c20e0202 --- /dev/null +++ b/xmpp-vala/src/module/jid.vala @@ -0,0 +1,107 @@ +namespace Xmpp { + +public class Jid { + public string? localpart; + public string domainpart; + public string? resourcepart; + + public Jid bare_jid { + owned get { return is_bare() ? this : new Jid.components(localpart, domainpart, null); } + } + + public Jid domain_jid { + owned get { return is_domain() ? this : new Jid.components(null, domainpart, null); } + } + + private string jid; + + public Jid(string jid) { + Jid? parsed = Jid.parse(jid); + string? localpart = parsed != null ? (owned) parsed.localpart : null; + string domainpart = parsed != null ? (owned) parsed.domainpart : jid; + string? resourcepart = parsed != null ? (owned) parsed.resourcepart : null; + this.intern(jid, (owned) localpart, (owned) domainpart, (owned) resourcepart); + } + + private Jid.intern(owned string jid, owned string? localpart, owned string domainpart, owned string? resourcepart) { + this.jid = (owned) jid; + this.localpart = (owned) localpart; + this.domainpart = (owned) domainpart; + this.resourcepart = (owned) resourcepart; + } + + public Jid.components(owned string? localpart, owned string domainpart, owned string? resourcepart) { + string jid = domainpart; + if (localpart != null) { + jid = @"$localpart@$jid"; + } + if (resourcepart != null) { + jid = @"$jid/$resourcepart"; + } + this.jid = jid; + this.localpart = (owned) localpart; + this.domainpart = (owned) domainpart; + this.resourcepart = (owned) resourcepart; + } + + public static Jid? parse(string jid) { + int slash_index = jid.index_of("/"); + string resourcepart = slash_index == -1 ? null : jid.slice(slash_index + 1, jid.length); + string bare_jid = slash_index == -1 ? jid : jid.slice(0, slash_index); + int at_index = bare_jid.index_of("@"); + string localpart = at_index == -1 ? null : bare_jid.slice(0, at_index); + string domainpart = at_index == -1 ? bare_jid : bare_jid.slice(at_index + 1, bare_jid.length); + + if (domainpart == "") return null; + if (slash_index != -1 && resourcepart == "") return null; + if (at_index != -1 && localpart == "") return null; + + return new Jid.intern(jid, (owned) localpart, (owned) domainpart, (owned) resourcepart); + } + + public Jid with_resource(string? resourcepart) { + return new Jid.components(localpart, domainpart, resourcepart); + } + + public bool is_domain() { + return localpart == null && resourcepart == null; + } + + public bool is_bare() { + return localpart != null && resourcepart == null; + } + + public bool is_full() { + return localpart != null && resourcepart != null; + } + + public string to_string() { + return jid; + } + + public bool equals_bare(Jid? jid) { + return jid != null && equals_bare_func(this, jid); + } + + public bool equals(Jid? jid) { + return jid != null && equals_func(this, jid); + } + + public static new bool equals_bare_func(Jid jid1, Jid jid2) { + return jid1.bare_jid.to_string() == jid2.bare_jid.to_string(); + } + + public static bool equals_func(Jid jid1, Jid jid2) { + return jid1.to_string() == jid2.to_string(); + } + + public static new uint hash_bare_func(Jid jid) { + return jid.bare_jid.to_string().hash(); + } + + public static new uint hash_func(Jid jid) { + return jid.to_string().hash(); + } +} + +} |