| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2023-07-03 18:29:39 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2023-07-03 18:29:52 UTC |
| parent | 60a5556b553aca0408eb9cd5729443a15b745d9d |
| xmpp/ChatMessage.hx | +1 | -1 |
| xmpp/JID.hx | +37 | -13 |
diff --git a/xmpp/ChatMessage.hx b/xmpp/ChatMessage.hx index 742bd41..279f3d5 100644 --- a/xmpp/ChatMessage.hx +++ b/xmpp/ChatMessage.hx @@ -39,7 +39,7 @@ class ChatMessage { msg.text = stanza.getChildText("body"); msg.to = stanza.attr.get("to"); msg.from = stanza.attr.get("from"); - var domain = JID.split(localJid).domain; + final domain = JID.parse(localJid).domain; for (stanzaId in stanza.allTags("stanza-id", "urn:xmpp:sid:0")) { if (stanzaId.attr.get("by") == domain) { msg.serverId = stanzaId.attr.get("id"); diff --git a/xmpp/JID.hx b/xmpp/JID.hx index ae2718c..7241470 100644 --- a/xmpp/JID.hx +++ b/xmpp/JID.hx @@ -1,22 +1,46 @@ package xmpp; -typedef SplitJID = { - var ?node : String; - var domain : String; - var ?resource : String; -}; - class JID { - public static function split(jid:String):SplitJID { + public final node : Null<String>; + public final domain : String; + public final resource : Null<String>; + + public function new(?node:String, domain:String, ?resource:String) { + this.node = node; + this.domain = domain; + this.resource = resource; + } + + public static function parse(jid:String):JID { var resourceDelimiter = jid.indexOf("/"); var nodeDelimiter = jid.indexOf("@"); - if(nodeDelimiter >= resourceDelimiter) { + if(resourceDelimiter > 0 && nodeDelimiter >= resourceDelimiter) { nodeDelimiter = -1; } - return { - node: (nodeDelimiter>0)?jid.substr(0, nodeDelimiter):null, - domain: jid.substring((nodeDelimiter == -1)?0:nodeDelimiter+1, (resourceDelimiter == -1)?jid.length+1:resourceDelimiter), - resource: (resourceDelimiter == -1)?null:jid.substring(resourceDelimiter+1), - }; + return new JID( + (nodeDelimiter>0)?jid.substr(0, nodeDelimiter):null, + jid.substring((nodeDelimiter == -1)?0:nodeDelimiter+1, (resourceDelimiter == -1)?jid.length+1:resourceDelimiter), + (resourceDelimiter == -1)?null:jid.substring(resourceDelimiter+1) + ); + } + + public function asBare():JID { + return new JID(this.node, this.domain); + } + + public function equals(rhs:JID):Bool { + return ( + this.node == rhs.node && + this.domain == rhs.domain && + this.resource == rhs.resource + ); + } + + public function asString():String { + return ( + (this.node != null ? this.node + "@" : "") + + this.domain + + (this.resource != null ? "/" + this.resource : "") + ); } }