js/xmpp_muc.js

Thu, 18 Mar 2010 15:53:39 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Mar 2010 15:53:39 +0000
changeset 19
41a3c9d41e6a
child 20
bd1d350ab61c
permissions
-rw-r--r--

Add MUC library to be used for conversing with helpers

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
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 send_stanza: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 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
51 },
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 /** Incoming stanza handlers */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 handle_join: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 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
59 && 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
60 if(!this.occupants[nick])
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 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
63 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
64 this.occupants[nick] = {};
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if(this.callbacks.joined)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67 if(this.status_handler)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 this.status_handler(stanza);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return true;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 },
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 handle_error: function (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 if(this.callbacks.error
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 && 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
79 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 var e = stanza.getElementsByTagName("error");
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if(e.length > 0)
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 var err = null;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 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
85 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 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
87 err = child.nodeName;
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 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
90 }
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 return true;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 };

mercurial