src/js/xmpp_muc.js

Thu, 01 Apr 2010 16:07:16 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 01 Apr 2010 16:07:16 +0100
changeset 36
562ff07a3968
parent 25
js/xmpp_muc.js@2651c3a7e621
child 41
d22dee1f3dba
permissions
-rw-r--r--

Large commit - reorganises directory structure, lots of small fixes

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);
25
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
15 conn.addHandler(recontext(this, this.handle_message), null, "message", "groupchat", null, null);
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 /* Return newly-created object */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return this;
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
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 MUC.prototype =
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 /** General methods */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 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
26 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 this.jid = jid;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 this.nick = nick;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 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
30 if(status)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 pres.c("status").t(status);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return this.send_stanza(pres);
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
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 part: function (status_text)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
38 if(status)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 pres.c("status").t(status);
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return this.send_stanza(pres);
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
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 send_message: function (message)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 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
46 .c("body").t(message||""));
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
20
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
49 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
50 {
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
51 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
52 .c("body").t(message||""));
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
53 },
bd1d350ab61c Add to the MUC library support for sending private messages
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
54
36
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
55 send_invite: function (jid, message)
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
56 {
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
57 return this.send_stanza($msg({to: jid})
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
58 .c("x", { xmlns: "http://jabber.org/protocol/muc#user" })
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
59 .c("invite", { to: jid })
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
60 .c("reason").t(message||""));
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
61 },
562ff07a3968 Large commit - reorganises directory structure, lots of small fixes
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
62
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 send_stanza: function (stanza)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 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
66 },
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 /** Incoming stanza handlers */
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 handle_join: function (stanza)
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 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
73 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
74 && 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
75 if(!this.occupants[nick])
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 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
78 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
79 this.occupants[nick] = {};
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if(this.callbacks.joined)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 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
82 if(this.status_handler)
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 this.status_handler(stanza);
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 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 return true;
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
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 handle_error: function (stanza)
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(this.callbacks.error
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 && 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
94 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 var e = stanza.getElementsByTagName("error");
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if(e.length > 0)
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 var err = null;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 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
100 {
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 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
102 err = child.nodeName;
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 });
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 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
105 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 }
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 return true;
25
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
108 },
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
109
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
110 handle_message: function (stanza)
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
111 {
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
112 if(!this.callbacks.message)
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
113 return true;
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
114
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
115 if(stanza.getAttribute("type") == "groupchat" && Strophe.getBareJidFromJid(stanza.getAttribute("from")) == this.jid)
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
116 {
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
117 var body = stanza.getElementsByTagName("body");
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
118 if(body.length > 0 && stanza.getElementsByTagName("delay").length == 0)
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
119 this.callbacks.message(stanza, this, Strophe.getResourceFromJid(stanza.getAttribute("from")), Strophe.getText(body[0]));
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
120 }
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
121 return true;
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 }
25
2651c3a7e621 Add support for handling incoming messages to the MUC library
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
123
19
41a3c9d41e6a Add MUC library to be used for conversing with helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 };

mercurial