js/xmpp_muc.js

Thu, 18 Mar 2010 16:05:59 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Mar 2010 16:05:59 +0000
changeset 20
bd1d350ab61c
parent 19
41a3c9d41e6a
child 25
2651c3a7e621
permissions
-rw-r--r--

Add to the MUC library support for sending private messages

19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 // Wraps a function so that its 'this' is always 'context' when called
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 var recontext = function (context, f) { return function () { return f.apply(context, arguments); }; };
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 var MUC = function (conn, callbacks)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 /* Set our properties */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 this.conn = conn;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 this.callbacks = callbacks;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 this.occupants = {};
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 /* Initialize stanza handlers */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 conn.addHandler(recontext(this, this.handle_join), null, "presence", null, null, null);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 conn.addHandler(recontext(this, this.handle_error), null, "presence", "error", null, null);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 /* Return newly-created object */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return this;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 MUC.prototype =
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 /** General methods */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 join: function (jid, nick, status_text)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 this.jid = jid;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 this.nick = nick;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 var pres = $pres({to: jid+'/'+nick});
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if(status)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 pres.c("status").t(status);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return this.send_stanza(pres);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 part: function (status_text)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 var pres = $pres({to: this.jid+'/'+this.nick, type: "unavailable"});
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if(status)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 pres.c("status").t(status);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return this.send_stanza(pres);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 send_message: function (message)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return this.send_stanza($msg({to: this.jid, type: "groupchat"})
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 .c("body").t(message||""));
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
20
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
48 send_private_message: function (nick, message)
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
49 {
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
50 return this.send_stanza($msg({to: this.jid+"/"+nick, type: "chat"})
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
51 .c("body").t(message||""));
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
52 },
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
53
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 send_stanza: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return this.conn.send(stanza.tree());
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 /** Incoming stanza handlers */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 handle_join: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 var nick = Strophe.getResourceFromJid(stanza.getAttribute("from"));
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if(stanza.getAttribute("type") != "unavailable" && stanza.getAttribute("type") != "error"
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if(!this.occupants[nick])
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 var text = stanza.getElementsByTagName("status")[0];
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 if(text) text = Strophe.getText(text);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 this.occupants[nick] = {};
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if(this.callbacks.joined)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 this.callbacks.joined(stanza, this, nick, text);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if(this.status_handler)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 this.status_handler(stanza);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return true;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 handle_error: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if(this.callbacks.error
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 var e = stanza.getElementsByTagName("error");
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if(e.length > 0)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 var err = null;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 Strophe.forEachChild(e[0], null, function (child)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if(child.getAttribute("xmlns") == "urn:ietf:params:xml:ns:xmpp-stanzas")
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 err = child.nodeName;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 });
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 this.callbacks.error(stanza, this, err);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return true;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 };

mercurial