js/xmpp_muc.js

changeset 36
562ff07a3968
parent 35
43152bfafcc4
child 37
010783d24970
equal deleted inserted replaced
35:43152bfafcc4 36:562ff07a3968
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 conn.addHandler(recontext(this, this.handle_message), null, "message", "groupchat", null, null);
16
17 /* Return newly-created object */
18 return this;
19 }
20
21 MUC.prototype =
22 {
23 /** General methods */
24
25 join: function (jid, nick, status_text)
26 {
27 this.jid = jid;
28 this.nick = nick;
29 var pres = $pres({to: jid+'/'+nick});
30 if(status)
31 pres.c("status").t(status);
32 return this.send_stanza(pres);
33 },
34
35 part: function (status_text)
36 {
37 var pres = $pres({to: this.jid+'/'+this.nick, type: "unavailable"});
38 if(status)
39 pres.c("status").t(status);
40 return this.send_stanza(pres);
41 },
42
43 send_message: function (message)
44 {
45 return this.send_stanza($msg({to: this.jid, type: "groupchat"})
46 .c("body").t(message||""));
47 },
48
49 send_private_message: function (nick, message)
50 {
51 return this.send_stanza($msg({to: this.jid+"/"+nick, type: "chat"})
52 .c("body").t(message||""));
53 },
54
55 send_stanza: function (stanza)
56 {
57 return this.conn.send(stanza.tree());
58 },
59
60 /** Incoming stanza handlers */
61
62 handle_join: function (stanza)
63 {
64 var nick = Strophe.getResourceFromJid(stanza.getAttribute("from"));
65 if(stanza.getAttribute("type") != "unavailable" && stanza.getAttribute("type") != "error"
66 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
67 if(!this.occupants[nick])
68 {
69 var text = stanza.getElementsByTagName("status")[0];
70 if(text) text = Strophe.getText(text);
71 this.occupants[nick] = {};
72 if(this.callbacks.joined)
73 this.callbacks.joined(stanza, this, nick, text);
74 if(this.status_handler)
75 {
76 this.status_handler(stanza);
77 }
78 }
79 return true;
80 },
81
82 handle_error: function (stanza)
83 {
84 if(this.callbacks.error
85 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
86 {
87 var e = stanza.getElementsByTagName("error");
88 if(e.length > 0)
89 {
90 var err = null;
91 Strophe.forEachChild(e[0], null, function (child)
92 {
93 if(child.getAttribute("xmlns") == "urn:ietf:params:xml:ns:xmpp-stanzas")
94 err = child.nodeName;
95 });
96 this.callbacks.error(stanza, this, err);
97 }
98 }
99 return true;
100 },
101
102 handle_message: function (stanza)
103 {
104 if(!this.callbacks.message)
105 return true;
106
107 if(stanza.getAttribute("type") == "groupchat" && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
108 {
109 var body = stanza.getElementsByTagName("body");
110 if(body.length > 0 && stanza.getElementsByTagName("delay").length == 0)
111 this.callbacks.message(stanza, this, Strophe.getResourceFromJid(stanza.getAttribute("from")), Strophe.getText(body[0]));
112 }
113 return true;
114 }
115
116 };

mercurial