| 1 | // Wraps a function so that its 'this' is always 'context' when called |
| 2 | var recontext = function (context, f) { return function () { return f.apply(context, arguments); }; }; |
| 3 | |
| 4 | // Returns a child element given a tag name and xmlns |
| 5 | function get_child(stanza, name, xmlns) |
| 6 | { |
| 7 | var children = stanza.getElementsByTagName(name); |
| 8 | for(var i=0;i<=children.length;i++) |
| 9 | { |
| 10 | if(children[i].getAttribute("xmlns") == xmlns || !xmlns) |
| 11 | return children[i]; |
| 12 | } |
| 13 | return null; |
| 14 | } |
| 15 | |
| 16 | |
| 17 | |
| 18 | var MUC = function (conn, callbacks) |
| 19 | { |
| 20 | /* Set our properties */ |
| 21 | this.conn = conn; |
| 22 | this.callbacks = callbacks; |
| 23 | |
| 24 | this.occupants = {}; |
| 25 | |
| 26 | /* Initialize stanza handlers */ |
| 27 | conn.addHandler(recontext(this, this.handle_join), null, "presence", null, null, null); |
| 28 | conn.addHandler(recontext(this, this.handle_leave), null, "presence", "unavailable", null, null); |
| 29 | conn.addHandler(recontext(this, this.handle_error), null, "presence", "error", null, null); |
| 30 | conn.addHandler(recontext(this, this.handle_message), null, "message", "groupchat", null, null); |
| 31 | |
| 32 | /* Return newly-created object */ |
| 33 | return this; |
| 34 | } |
| 35 | |
| 36 | MUC.prototype = |
| 37 | { |
| 38 | /** General methods */ |
| 39 | |
| 40 | join: function (jid, nick, status_text) |
| 41 | { |
| 42 | this.jid = jid; |
| 43 | this.nick = nick; |
| 44 | var pres = $pres({to: jid+'/'+nick}); |
| 45 | if(status) |
| 46 | pres.c("status").t(status); |
| 47 | return this.send_stanza(pres); |
| 48 | }, |
| 49 | |
| 50 | part: function (status_text) |
| 51 | { |
| 52 | var pres = $pres({to: this.jid+'/'+this.nick, type: "unavailable"}); |
| 53 | if(status) |
| 54 | pres.c("status").t(status); |
| 55 | return this.send_stanza(pres); |
| 56 | }, |
| 57 | |
| 58 | send_message: function (message) |
| 59 | { |
| 60 | return this.send_stanza($msg({to: this.jid, type: "groupchat"}) |
| 61 | .c("body").t(message||"")); |
| 62 | }, |
| 63 | |
| 64 | send_private_message: function (nick, message) |
| 65 | { |
| 66 | return this.send_stanza($msg({to: this.jid+"/"+nick, type: "chat"}) |
| 67 | .c("body").t(message||"")); |
| 68 | }, |
| 69 | |
| 70 | send_invite: function (jid, message) |
| 71 | { |
| 72 | return this.send_stanza($msg({to: jid}) |
| 73 | .c("x", { xmlns: "http://jabber.org/protocol/muc#user" }) |
| 74 | .c("invite", { to: jid }) |
| 75 | .c("reason").t(message||"")); |
| 76 | }, |
| 77 | |
| 78 | send_stanza: function (stanza) |
| 79 | { |
| 80 | return this.conn.send(stanza.tree()); |
| 81 | }, |
| 82 | |
| 83 | /** Incoming stanza handlers */ |
| 84 | |
| 85 | handle_join: function (stanza) |
| 86 | { |
| 87 | var nick = Strophe.getResourceFromJid(stanza.getAttribute("from")); |
| 88 | if(stanza.getAttribute("type") != "unavailable" && stanza.getAttribute("type") != "error" |
| 89 | && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
| 90 | if(!this.occupants[nick]) |
| 91 | { |
| 92 | var text = stanza.getElementsByTagName("status")[0]; |
| 93 | if(text) text = Strophe.getText(text); |
| 94 | this.occupants[nick] = { nick: nick, status: text }; |
| 95 | var item = get_child(stanza, "x", "http://jabber.org/protocol/muc#user"); |
| 96 | if(item) |
| 97 | { |
| 98 | item = item.getElementsByTagName("item")[0]; |
| 99 | this.occupants[nick].affiliation = item.getAttribute("affiliation"); |
| 100 | this.occupants[nick].role = item.getAttribute("role"); |
| 101 | } |
| 102 | if(this.callbacks.joined) |
| 103 | this.callbacks.joined(stanza, this, this.occupants[nick]); |
| 104 | if(this.status_handler) |
| 105 | { |
| 106 | this.status_handler(stanza); |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | }, |
| 111 | |
| 112 | handle_leave: function (stanza) |
| 113 | { |
| 114 | var nick = Strophe.getResourceFromJid(stanza.getAttribute("from")); |
| 115 | if(Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
| 116 | if(this.occupants[nick]) |
| 117 | { |
| 118 | var text = stanza.getElementsByTagName("status")[0]; |
| 119 | if(text) |
| 120 | { |
| 121 | text = Strophe.getText(text); |
| 122 | this.occupants[nick].status = text; |
| 123 | } |
| 124 | if(this.callbacks.left) |
| 125 | this.callbacks.left(stanza, this, this.occupants[nick]); |
| 126 | if(this.status_handler) |
| 127 | { |
| 128 | this.status_handler(stanza); |
| 129 | } |
| 130 | delete this.occupants[nick]; |
| 131 | } |
| 132 | return true; |
| 133 | }, |
| 134 | |
| 135 | handle_error: function (stanza) |
| 136 | { |
| 137 | if(this.callbacks.error |
| 138 | && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
| 139 | { |
| 140 | var e = stanza.getElementsByTagName("error"); |
| 141 | if(e.length > 0) |
| 142 | { |
| 143 | var err = null; |
| 144 | Strophe.forEachChild(e[0], null, function (child) |
| 145 | { |
| 146 | if(child.getAttribute("xmlns") == "urn:ietf:params:xml:ns:xmpp-stanzas") |
| 147 | err = child.nodeName; |
| 148 | }); |
| 149 | this.callbacks.error(stanza, this, err); |
| 150 | } |
| 151 | } |
| 152 | return true; |
| 153 | }, |
| 154 | |
| 155 | handle_message: function (stanza) |
| 156 | { |
| 157 | if(!this.callbacks.message) |
| 158 | return true; |
| 159 | |
| 160 | if(stanza.getAttribute("type") == "groupchat" && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
| 161 | { |
| 162 | var body = stanza.getElementsByTagName("body"); |
| 163 | if(body.length > 0 && stanza.getElementsByTagName("delay").length == 0) |
| 164 | this.callbacks.message(stanza, this, Strophe.getResourceFromJid(stanza.getAttribute("from")), Strophe.getText(body[0])); |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | }; |