|
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 var MUC = function (conn, callbacks) |
|
5 { |
|
6 /* Set our properties */ |
|
7 this.conn = conn; |
|
8 this.callbacks = callbacks; |
|
9 |
|
10 this.occupants = {}; |
|
11 |
|
12 /* Initialize stanza handlers */ |
|
13 conn.addHandler(recontext(this, this.handle_join), null, "presence", null, null, null); |
|
14 conn.addHandler(recontext(this, this.handle_error), null, "presence", "error", null, null); |
|
15 |
|
16 /* Return newly-created object */ |
|
17 return this; |
|
18 } |
|
19 |
|
20 MUC.prototype = |
|
21 { |
|
22 /** General methods */ |
|
23 |
|
24 join: function (jid, nick, status_text) |
|
25 { |
|
26 this.jid = jid; |
|
27 this.nick = nick; |
|
28 var pres = $pres({to: jid+'/'+nick}); |
|
29 if(status) |
|
30 pres.c("status").t(status); |
|
31 return this.send_stanza(pres); |
|
32 }, |
|
33 |
|
34 part: function (status_text) |
|
35 { |
|
36 var pres = $pres({to: this.jid+'/'+this.nick, type: "unavailable"}); |
|
37 if(status) |
|
38 pres.c("status").t(status); |
|
39 return this.send_stanza(pres); |
|
40 }, |
|
41 |
|
42 send_message: function (message) |
|
43 { |
|
44 return this.send_stanza($msg({to: this.jid, type: "groupchat"}) |
|
45 .c("body").t(message||"")); |
|
46 }, |
|
47 |
|
48 send_stanza: function (stanza) |
|
49 { |
|
50 return this.conn.send(stanza.tree()); |
|
51 }, |
|
52 |
|
53 /** Incoming stanza handlers */ |
|
54 |
|
55 handle_join: function (stanza) |
|
56 { |
|
57 var nick = Strophe.getResourceFromJid(stanza.getAttribute("from")); |
|
58 if(stanza.getAttribute("type") != "unavailable" && stanza.getAttribute("type") != "error" |
|
59 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
|
60 if(!this.occupants[nick]) |
|
61 { |
|
62 var text = stanza.getElementsByTagName("status")[0]; |
|
63 if(text) text = Strophe.getText(text); |
|
64 this.occupants[nick] = {}; |
|
65 if(this.callbacks.joined) |
|
66 this.callbacks.joined(stanza, this, nick, text); |
|
67 if(this.status_handler) |
|
68 { |
|
69 this.status_handler(stanza); |
|
70 } |
|
71 } |
|
72 return true; |
|
73 }, |
|
74 |
|
75 handle_error: function (stanza) |
|
76 { |
|
77 if(this.callbacks.error |
|
78 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid) |
|
79 { |
|
80 var e = stanza.getElementsByTagName("error"); |
|
81 if(e.length > 0) |
|
82 { |
|
83 var err = null; |
|
84 Strophe.forEachChild(e[0], null, function (child) |
|
85 { |
|
86 if(child.getAttribute("xmlns") == "urn:ietf:params:xml:ns:xmpp-stanzas") |
|
87 err = child.nodeName; |
|
88 }); |
|
89 this.callbacks.error(stanza, this, err); |
|
90 } |
|
91 } |
|
92 return true; |
|
93 } |
|
94 }; |