Commit cc99f4f9 authored by JC Brand's avatar JC Brand

Cleanup and indented. Add AMD/global registrations

parent 9ef94886
// Generated by CoffeeScript 1.3.3
/* /*
*Plugin to implement the MUC extension. *Plugin to implement the MUC extension.
http://xmpp.org/extensions/xep-0045.html http://xmpp.org/extensions/xep-0045.html
*Previous Author: *Previous Author:
Nathan Zorn <nathan.zorn@gmail.com> Nathan Zorn <nathan.zorn@gmail.com>
*Complete CoffeeScript rewrite: *Complete CoffeeScript rewrite:
Andreas Guth <guth@dbis.rwth-aachen.de> Andreas Guth <guth@dbis.rwth-aachen.de>
*Cleanup, AMD/global registrations and bugfixes:
JC Brand <jc@opkode.com>
*/ */
var Occupant, RoomConfig, XmppRoom, // AMD/global registrations
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([
"Libraries/strophe"
], function () {
if (console===undefined || console.log===undefined) {
console = { log: function () {}, error: function () {} };
}
return factory(jQuery, console);
}
);
}
}(this, function ($, console) {
(function() {
var Occupant, RoomConfig, XmppRoom,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Strophe.addConnectionPlugin('muc', { Strophe.addConnectionPlugin('muc', {
_connection: null, _connection: null,
rooms: [], rooms: [],
/*Function
Initialize the MUC plugin. Sets the correct connection object and
extends the namesace.
*/
init: function(conn) { init: function(conn) {
/* Initialize the MUC plugin. Sets the correct connection object and
* extends the namesace.
*/
this._connection = conn; this._connection = conn;
this._muc_handler = null; this._muc_handler = null;
Strophe.addNamespace('MUC_OWNER', Strophe.NS.MUC + "#owner"); Strophe.addNamespace('MUC_OWNER', Strophe.NS.MUC + "#owner");
...@@ -27,21 +43,18 @@ Strophe.addConnectionPlugin('muc', { ...@@ -27,21 +43,18 @@ Strophe.addConnectionPlugin('muc', {
Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user"); Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user");
return Strophe.addNamespace('MUC_ROOMCONF', Strophe.NS.MUC + "#roomconfig"); return Strophe.addNamespace('MUC_ROOMCONF', Strophe.NS.MUC + "#roomconfig");
}, },
/*Function
Join a multi-user chat room
Parameters:
(String) room - The multi-user chat room to join.
(String) nick - The nickname to use in the chat room. Optional
(Function) msg_handler_cb - The function call to handle messages from the
specified chat room.
(Function) pres_handler_cb - The function call back to handle presence
in the chat room.
(String) password - The optional password to use. (password protected
rooms only)
*/
join: function(room, nick, msg_handler_cb, pres_handler_cb, roster_cb, password) { join: function(room, nick, msg_handler_cb, pres_handler_cb, roster_cb, password) {
var msg, room_nick, _base, _ref, _ref1, /* Join a multi-user chat room
*
* Parameters:
* (String) room - The multi-user chat room to join.
* (String) nick - Optional nickname to use in the chat room.
* (Function) msg_handler_cb - The function call to handle messages from the specified chat room.
* (Function) pres_handler_cb - The function call back to handle presence in the chat room.
* (String) password - The optional password to use. (password protected rooms only)
*/
var msg, room_nick, _base,
_this = this; _this = this;
room_nick = this.test_append_nick(room, nick); room_nick = this.test_append_nick(room, nick);
msg = $pres({ msg = $pres({
...@@ -53,17 +66,12 @@ Strophe.addConnectionPlugin('muc', { ...@@ -53,17 +66,12 @@ Strophe.addConnectionPlugin('muc', {
if (password != null) { if (password != null) {
msg.cnode(Strophe.xmlElement("password", [], password)); msg.cnode(Strophe.xmlElement("password", [], password));
} }
if ((_ref = this._muc_handler) == null) { if (this._muc_handler == null) {
this._muc_handler = this._connection.addHandler(function(stanza) { this._muc_handler = this._connection.addHandler(function(stanza) {
var from, handler, handlers, id, roomname, x, xmlns, xquery, _i, _len; var from, handler, handlers, id, roomname, x, xmlns, xquery, _i, _len;
from = stanza.getAttribute('from'); from = stanza.getAttribute('from');
if (!from) {
return true;
}
roomname = from.split("/")[0]; roomname = from.split("/")[0];
if (!_this.rooms[roomname]) { if (!_this.rooms[roomname]) return true;
return true;
}
room = _this.rooms[roomname]; room = _this.rooms[roomname];
handlers = {}; handlers = {};
if (stanza.nodeName === "message") { if (stanza.nodeName === "message") {
...@@ -83,39 +91,33 @@ Strophe.addConnectionPlugin('muc', { ...@@ -83,39 +91,33 @@ Strophe.addConnectionPlugin('muc', {
} }
for (id in handlers) { for (id in handlers) {
handler = handlers[id]; handler = handlers[id];
if (!handler(stanza, room)) { if (!handler(stanza, room)) delete handlers[id];
delete handlers[id];
}
} }
return true; return true;
}); });
} }
if ((_ref1 = (_base = this.rooms)[room]) == null) { if ((_base = this.rooms)[room] == null) {
_base[room] = new XmppRoom(this, room, nick, password); _base[room] = new XmppRoom(this, room, nick, password);
} }
if (pres_handler_cb) { if (pres_handler_cb) {
this.rooms[room].addHandler('presence', pres_handler_cb); this.rooms[room].addHandler('presence', pres_handler_cb);
} }
if (msg_handler_cb) { if (msg_handler_cb) this.rooms[room].addHandler('message', msg_handler_cb);
this.rooms[room].addHandler('message', msg_handler_cb); if (roster_cb) this.rooms[room].addHandler('roster', roster_cb);
}
if (roster_cb) {
this.rooms[room].addHandler('roster', roster_cb);
}
return this._connection.send(msg); return this._connection.send(msg);
}, },
/*Function
Leave a multi-user chat room
Parameters:
(String) room - The multi-user chat room to leave.
(String) nick - The nick name used in the room.
(Function) handler_cb - Optional function to handle the successful leave.
(String) exit_msg - optional exit message.
Returns:
iqid - The unique id for the room leave.
*/
leave: function(room, nick, handler_cb, exit_msg) { leave: function(room, nick, handler_cb, exit_msg) {
/* Leave a multi-user chat room
*
* Parameters:
* (String) room - The multi-user chat room to leave.
* (String) nick - The nick name used in the room.
* (Function) handler_cb - Optional function to handle the successful leave.
* (String) exit_msg - optional exit message.
* Returns:
* iqid - The unique id for the room leave.
*/
var presence, presenceid, room_nick; var presence, presenceid, room_nick;
delete this.rooms[room]; delete this.rooms[room];
if (this.rooms.length === 0) { if (this.rooms.length === 0) {
...@@ -130,28 +132,25 @@ Strophe.addConnectionPlugin('muc', { ...@@ -130,28 +132,25 @@ Strophe.addConnectionPlugin('muc', {
from: this._connection.jid, from: this._connection.jid,
to: room_nick to: room_nick
}); });
if (exit_msg != null) { if (exit_msg != null) presence.c("status", exit_msg);
presence.c("status", exit_msg);
}
if (handler_cb != null) { if (handler_cb != null) {
this._connection.addHandler(handler_cb, null, "presence", null, presenceid); this._connection.addHandler(handler_cb, null, "presence", null, presenceid);
} }
this._connection.send(presence); this._connection.send(presence);
return presenceid; return presenceid;
}, },
/*Function
Parameters:
(String) room - The multi-user chat room name.
(String) nick - The nick name used in the chat room.
(String) message - The plaintext message to send to the room.
(String) html_message - The message to send to the room with html markup.
(String) type - "groupchat" for group chat messages o
"chat" for private chat messages
Returns:
msgiq - the unique id used to send the message
*/
message: function(room, nick, message, html_message, type) { message: function(room, nick, message, html_message, type) {
/* Parameters:
* (String) room - The multi-user chat room name.
* (String) nick - The nick name used in the chat room.
* (String) message - The plaintext message to send to the room.
* (String) html_message - The message to send to the room with html markup.
* (String) type - "groupchat" for group chat messages o
* "chat" for private chat messages
* Returns:
* msgiq - the unique id used to send the message
*/
var msg, msgid, parent, room_nick; var msg, msgid, parent, room_nick;
room_nick = this.test_append_nick(room, nick); room_nick = this.test_append_nick(room, nick);
type = type || (nick != null ? "chat" : "groupchat"); type = type || (nick != null ? "chat" : "groupchat");
...@@ -185,30 +184,28 @@ Strophe.addConnectionPlugin('muc', { ...@@ -185,30 +184,28 @@ Strophe.addConnectionPlugin('muc', {
this._connection.send(msg); this._connection.send(msg);
return msgid; return msgid;
}, },
/*Function
Convenience Function to send a Message to all Occupants
Parameters:
(String) room - The multi-user chat room name.
(String) message - The plaintext message to send to the room.
(String) html_message - The message to send to the room with html markup.
Returns:
msgiq - the unique id used to send the message
*/
groupchat: function(room, message, html_message) { groupchat: function(room, message, html_message) {
/* Convenience Function to send a Message to all Occupants
* Parameters:
* (String) room - The multi-user chat room name.
* (String) message - The plaintext message to send to the room.
* (String) html_message - The message to send to the room with html markup.
* Returns:
* msgiq - the unique id used to send the message
*/
return this.message(room, null, message, html_message); return this.message(room, null, message, html_message);
}, },
/*Function
Send a mediated invitation.
Parameters:
(String) room - The multi-user chat room name.
(String) receiver - The invitation's receiver.
(String) reason - Optional reason for joining the room.
Returns:
msgiq - the unique id used to send the invitation
*/
invite: function(room, receiver, reason) { invite: function(room, receiver, reason) {
/* Send a mediated invitation.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) receiver - The invitation's receiver.
* (String) reason - Optional reason for joining the room.
* Returns:
* msgiq - the unique id used to send the invitation
*/
var invitation, msgid; var invitation, msgid;
msgid = this._connection.getUniqueId(); msgid = this._connection.getUniqueId();
invitation = $msg({ invitation = $msg({
...@@ -220,36 +217,30 @@ Strophe.addConnectionPlugin('muc', { ...@@ -220,36 +217,30 @@ Strophe.addConnectionPlugin('muc', {
}).c('invite', { }).c('invite', {
to: receiver to: receiver
}); });
if (reason != null) { if (reason != null) invitation.c('reason', reason);
invitation.c('reason', reason);
}
this._connection.send(invitation); this._connection.send(invitation);
return msgid; return msgid;
}, },
/*Function
Send a direct invitation.
Parameters:
(String) room - The multi-user chat room name.
(String) receiver - The invitation's receiver.
(String) reason - Optional reason for joining the room.
(String) password - Optional password for the room.
Returns:
msgiq - the unique id used to send the invitation
*/
directInvite: function(room, receiver, reason, password) { directInvite: function(room, receiver, reason, password) {
/* Send a direct invitation.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) receiver - The invitation's receiver.
* (String) reason - Optional reason for joining the room.
* (String) password - Optional password for the room.
* Returns:
* msgiq - the unique id used to send the invitation
*/
var attrs, invitation, msgid; var attrs, invitation, msgid;
msgid = this._connection.getUniqueId(); msgid = this._connection.getUniqueId();
attrs = { attrs = {
xmlns: 'jabber:x:conference', xmlns: 'jabber:x:conference',
jid: room jid: room
}; };
if (reason != null) { if (reason != null) attrs.reason = reason;
attrs.reason = reason; if (password != null) attrs.password = password;
}
if (password != null) {
attrs.password = password;
}
invitation = $msg({ invitation = $msg({
from: this._connection.jid, from: this._connection.jid,
to: receiver, to: receiver,
...@@ -258,16 +249,15 @@ Strophe.addConnectionPlugin('muc', { ...@@ -258,16 +249,15 @@ Strophe.addConnectionPlugin('muc', {
this._connection.send(invitation); this._connection.send(invitation);
return msgid; return msgid;
}, },
/*Function
Queries a room for a list of occupants
(String) room - The multi-user chat room name.
(Function) success_cb - Optional function to handle the info.
(Function) error_cb - Optional function to handle an error.
Returns:
id - the unique id used to send the info request
*/
queryOccupants: function(room, success_cb, error_cb) { queryOccupants: function(room, success_cb, error_cb) {
/* Queries a room for a list of occupants
* (String) room - The multi-user chat room name.
* (Function) success_cb - Optional function to handle the info.
* (Function) error_cb - Optional function to handle an error.
* Returns:
* id - the unique id used to send the info request
*/
var attrs, info; var attrs, info;
attrs = { attrs = {
xmlns: Strophe.NS.DISCO_ITEMS xmlns: Strophe.NS.DISCO_ITEMS
...@@ -279,17 +269,17 @@ Strophe.addConnectionPlugin('muc', { ...@@ -279,17 +269,17 @@ Strophe.addConnectionPlugin('muc', {
}).c('query', attrs); }).c('query', attrs);
return this._connection.sendIQ(info, success_cb, error_cb); return this._connection.sendIQ(info, success_cb, error_cb);
}, },
/*Function
Start a room configuration. configure: function(room, handler_cb) {
Parameters: /* Start a room configuration.
(String) room - The multi-user chat room name. * Parameters:
(Function) handler_cb - Optional function to handle the config form. * (String) room - The multi-user chat room name.
Returns: * (Function) handler_cb - Optional function to handle the config form.
id - the unique id used to send the configuration request * Returns:
* id - the unique id used to send the configuration request
*/ */
configure: function(room, handler_cb, error_cb) { var config, id, stanza;
var config, stanza;
config = $iq({ config = $iq({
to: room, to: room,
type: "get" type: "get"
...@@ -297,17 +287,22 @@ Strophe.addConnectionPlugin('muc', { ...@@ -297,17 +287,22 @@ Strophe.addConnectionPlugin('muc', {
xmlns: Strophe.NS.MUC_OWNER xmlns: Strophe.NS.MUC_OWNER
}); });
stanza = config.tree(); stanza = config.tree();
return this._connection.sendIQ(stanza, handler_cb, error_cb); id = this._connection.sendIQ(stanza);
if (handler_cb != null) {
this._connection.addHandler(function(stanza) { handler_cb(stanza);
return false;
}, Strophe.NS.MUC_OWNER, "iq", null, id);
}
return id;
}, },
/*Function
Cancel the room configuration
Parameters:
(String) room - The multi-user chat room name.
Returns:
id - the unique id used to cancel the configuration.
*/
cancelConfigure: function(room) { cancelConfigure: function(room) {
/* Cancel the room configuration
* Parameters:
* (String) room - The multi-user chat room name.
* Returns:
* id - the unique id used to cancel the configuration.
*/
var config, stanza; var config, stanza;
config = $iq({ config = $iq({
to: room, to: room,
...@@ -321,47 +316,39 @@ Strophe.addConnectionPlugin('muc', { ...@@ -321,47 +316,39 @@ Strophe.addConnectionPlugin('muc', {
stanza = config.tree(); stanza = config.tree();
return this._connection.sendIQ(stanza); return this._connection.sendIQ(stanza);
}, },
/*Function
Save a room configuration.
Parameters:
(String) room - The multi-user chat room name.
(Array) config- Form Object or an array of form elements used to configure the room.
Returns:
id - the unique id used to save the configuration.
*/
saveConfiguration: function(room, config, success_cb, error_cb) { saveConfiguration: function(room, configarray) {
var conf, iq, stanza, _i, _len; /* Save a room configuration.
iq = $iq({ * Parameters:
* (String) room - The multi-user chat room name.
* (Array) configarray - an array of form elements used to configure the room.
* Returns:
* id - the unique id used to save the configuration.
*/
var conf, config, stanza, _i, _len;
config = $iq({
to: room, to: room,
type: "set" type: "set"
}).c("query", { }).c("query", {
xmlns: Strophe.NS.MUC_OWNER xmlns: Strophe.NS.MUC_OWNER
}); }).c("x", {
if (config instanceof Form) {
config.type = "submit";
iq.cnode(config.toXML());
} else {
iq.c("x", {
xmlns: "jabber:x:data", xmlns: "jabber:x:data",
type: "submit" type: "submit"
}); });
for (_i = 0, _len = config.length; _i < _len; _i++) { for (_i = 0, _len = configarray.length; _i < _len; _i++) {
conf = config[_i]; conf = configarray[_i];
iq.cnode(conf).up(); config.cnode(conf).up();
}
} }
stanza = iq.tree(); stanza = config.tree();
return this._connection.sendIQ(stanza, success_cb, error_cb); return this._connection.sendIQ(stanza);
}, },
/*Function
Parameters:
(String) room - The multi-user chat room name.
Returns:
id - the unique id used to create the chat room.
*/
createInstantRoom: function(room, success_cb, error_cb) { createInstantRoom: function(room) {
/* Parameters:
* (String) room - The multi-user chat room name.
* Returns:
* id - the unique id used to create the chat room.
*/
var roomiq; var roomiq;
roomiq = $iq({ roomiq = $iq({
to: room, to: room,
...@@ -372,16 +359,15 @@ Strophe.addConnectionPlugin('muc', { ...@@ -372,16 +359,15 @@ Strophe.addConnectionPlugin('muc', {
xmlns: "jabber:x:data", xmlns: "jabber:x:data",
type: "submit" type: "submit"
}); });
return this._connection.sendIQ(roomiq.tree(), success_cb, error_cb); return this._connection.sendIQ(roomiq.tree());
}, },
/*Function
Set the topic of the chat room.
Parameters:
(String) room - The multi-user chat room name.
(String) topic - Topic message.
*/
setTopic: function(room, topic) { setTopic: function(room, topic) {
/* Set the topic of the chat room.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) topic - Topic message.
*/
var msg; var msg;
msg = $msg({ msg = $msg({
to: room, to: room,
...@@ -392,22 +378,21 @@ Strophe.addConnectionPlugin('muc', { ...@@ -392,22 +378,21 @@ Strophe.addConnectionPlugin('muc', {
}).t(topic); }).t(topic);
return this._connection.send(msg.tree()); return this._connection.send(msg.tree());
}, },
/*Function
Internal Function that Changes the role or affiliation of a member
of a MUC room. This function is used by modifyRole and modifyAffiliation.
The modification can only be done by a room moderator. An error will be
returned if the user doesn't have permission.
Parameters:
(String) room - The multi-user chat room name.
(Object) item - Object with nick and role or jid and affiliation attribute
(String) reason - Optional reason for the change.
(Function) handler_cb - Optional callback for success
(Function) error_cb - Optional callback for error
Returns:
iq - the id of the mode change request.
*/
_modifyPrivilege: function(room, item, reason, handler_cb, error_cb) { _modifyPrivilege: function(room, item, reason, handler_cb, error_cb) {
/* Internal Function that Changes the role or affiliation of a member
* of a MUC room. This function is used by modifyRole and modifyAffiliation.
* The modification can only be done by a room moderator. An error will be
* returned if the user doesn't have permission.
* Parameters:
* (String) room - The multi-user chat room name.
* (Object) item - Object with nick and role or jid and affiliation attribute
* (String) reason - Optional reason for the change.
* (Function) handler_cb - Optional callback for success
* (Function) errer_cb - Optional callback for error
* Returns:
* iq - the id of the mode change request.
*/
var iq; var iq;
iq = $iq({ iq = $iq({
to: room, to: room,
...@@ -415,28 +400,25 @@ Strophe.addConnectionPlugin('muc', { ...@@ -415,28 +400,25 @@ Strophe.addConnectionPlugin('muc', {
}).c("query", { }).c("query", {
xmlns: Strophe.NS.MUC_ADMIN xmlns: Strophe.NS.MUC_ADMIN
}).cnode(item.node); }).cnode(item.node);
if (reason != null) { if (reason != null) iq.c("reason", reason);
iq.c("reason", reason);
}
return this._connection.sendIQ(iq.tree(), handler_cb, error_cb); return this._connection.sendIQ(iq.tree(), handler_cb, error_cb);
}, },
/*Function
Changes the role of a member of a MUC room.
The modification can only be done by a room moderator. An error will be
returned if the user doesn't have permission.
Parameters:
(String) room - The multi-user chat room name.
(String) nick - The nick name of the user to modify.
(String) role - The new role of the user.
(String) affiliation - The new affiliation of the user.
(String) reason - Optional reason for the change.
(Function) handler_cb - Optional callback for success
(Function) error_cb - Optional callback for error
Returns:
iq - the id of the mode change request.
*/
modifyRole: function(room, nick, role, reason, handler_cb, error_cb) { modifyRole: function(room, nick, role, reason, handler_cb, error_cb) {
/* Changes the role of a member of a MUC room.
* The modification can only be done by a room moderator. An error will be
* returned if the user doesn't have permission.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) nick - The nick name of the user to modify.
* (String) role - The new role of the user.
* (String) affiliation - The new affiliation of the user.
* (String) reason - Optional reason for the change.
* (Function) handler_cb - Optional callback for success
* (Function) errer_cb - Optional callback for error
* Returns:
* iq - the id of the mode change request.
*/
var item; var item;
item = $build("item", { item = $build("item", {
nick: nick, nick: nick,
...@@ -444,6 +426,7 @@ Strophe.addConnectionPlugin('muc', { ...@@ -444,6 +426,7 @@ Strophe.addConnectionPlugin('muc', {
}); });
return this._modifyPrivilege(room, item, reason, handler_cb, error_cb); return this._modifyPrivilege(room, item, reason, handler_cb, error_cb);
}, },
kick: function(room, nick, reason, handler_cb, error_cb) { kick: function(room, nick, reason, handler_cb, error_cb) {
return this.modifyRole(room, nick, 'none', reason, handler_cb, error_cb); return this.modifyRole(room, nick, 'none', reason, handler_cb, error_cb);
}, },
...@@ -459,22 +442,21 @@ Strophe.addConnectionPlugin('muc', { ...@@ -459,22 +442,21 @@ Strophe.addConnectionPlugin('muc', {
deop: function(room, nick, reason, handler_cb, error_cb) { deop: function(room, nick, reason, handler_cb, error_cb) {
return this.modifyRole(room, nick, 'participant', reason, handler_cb, error_cb); return this.modifyRole(room, nick, 'participant', reason, handler_cb, error_cb);
}, },
/*Function
Changes the affiliation of a member of a MUC room.
The modification can only be done by a room moderator. An error will be
returned if the user doesn't have permission.
Parameters:
(String) room - The multi-user chat room name.
(String) jid - The jid of the user to modify.
(String) affiliation - The new affiliation of the user.
(String) reason - Optional reason for the change.
(Function) handler_cb - Optional callback for success
(Function) error_cb - Optional callback for error
Returns:
iq - the id of the mode change request.
*/
modifyAffiliation: function(room, jid, affiliation, reason, handler_cb, error_cb) { modifyAffiliation: function(room, jid, affiliation, reason, handler_cb, error_cb) {
/* Changes the affiliation of a member of a MUC room.
* The modification can only be done by a room moderator. An error will be
* returned if the user doesn't have permission.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) jid - The jid of the user to modify.
* (String) affiliation - The new affiliation of the user.
* (String) reason - Optional reason for the change.
* (Function) handler_cb - Optional callback for success
* (Function) errer_cb - Optional callback for error
* Returns:
* iq - the id of the mode change request.
*/
var item; var item;
item = $build("item", { item = $build("item", {
jid: jid, jid: jid,
...@@ -497,14 +479,13 @@ Strophe.addConnectionPlugin('muc', { ...@@ -497,14 +479,13 @@ Strophe.addConnectionPlugin('muc', {
admin: function(room, jid, reason, handler_cb, error_cb) { admin: function(room, jid, reason, handler_cb, error_cb) {
return this.modifyAffiliation(room, jid, 'admin', reason, handler_cb, error_cb); return this.modifyAffiliation(room, jid, 'admin', reason, handler_cb, error_cb);
}, },
/*Function
Change the current users nick name.
Parameters:
(String) room - The multi-user chat room name.
(String) user - The new nick name.
*/
changeNick: function(room, user) { changeNick: function(room, user) {
/* Change the current users nick name.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) user - The new nick name.
*/
var presence, room_nick; var presence, room_nick;
room_nick = this.test_append_nick(room, user); room_nick = this.test_append_nick(room, user);
presence = $pres({ presence = $pres({
...@@ -514,39 +495,32 @@ Strophe.addConnectionPlugin('muc', { ...@@ -514,39 +495,32 @@ Strophe.addConnectionPlugin('muc', {
}); });
return this._connection.send(presence.tree()); return this._connection.send(presence.tree());
}, },
/*Function
Change the current users status.
Parameters:
(String) room - The multi-user chat room name.
(String) user - The current nick.
(String) show - The new show-text.
(String) status - The new status-text.
*/
setStatus: function(room, user, show, status) { setStatus: function(room, user, show, status) {
/* Change the current users status.
* Parameters:
* (String) room - The multi-user chat room name.
* (String) user - The current nick.
* (String) show - The new show-text.
* (String) status - The new status-text.
*/
var presence, room_nick; var presence, room_nick;
room_nick = this.test_append_nick(room, user); room_nick = this.test_append_nick(room, user);
presence = $pres({ presence = $pres({
from: this._connection.jid, from: this._connection.jid,
to: room_nick to: room_nick
}); });
if (show != null) { if (show != null) presence.c('show', show).up();
presence.c('show', show).up(); if (status != null) presence.c('status', status);
}
if (status != null) {
presence.c('status', status);
}
return this._connection.send(presence.tree()); return this._connection.send(presence.tree());
}, },
/*Function
List all chat room available on a server.
Parameters:
(String) server - name of chat server.
(String) handle_cb - Function to call for room list return.
(String) error_cb - Function to call on error.
*/
listRooms: function(server, handle_cb, error_cb) { listRooms: function(server, handle_cb) {
/* List all chat room available on a server.
* Parameters:
* (String) server - name of chat server.
* (String) handle_cb - Function to call for room list return.
*/
var iq; var iq;
iq = $iq({ iq = $iq({
to: server, to: server,
...@@ -555,38 +529,37 @@ Strophe.addConnectionPlugin('muc', { ...@@ -555,38 +529,37 @@ Strophe.addConnectionPlugin('muc', {
}).c("query", { }).c("query", {
xmlns: Strophe.NS.DISCO_ITEMS xmlns: Strophe.NS.DISCO_ITEMS
}); });
return this._connection.sendIQ(iq, handle_cb, error_cb); return this._connection.sendIQ(iq, handle_cb);
}, },
test_append_nick: function(room, nick) { test_append_nick: function(room, nick) {
return room + (nick != null ? "/" + (Strophe.escapeNode(nick)) : ""); return room + (nick != null ? "/" + (Strophe.escapeNode(nick)) : "");
} }
}); });
XmppRoom = (function() {
XmppRoom = (function() {
function XmppRoom(client, name, nick, password) { function XmppRoom(client, name, nick, password) {
this.roster = {}; this.roster = {};
this._message_handlers = {}; this._message_handlers = {};
this._presence_handlers = {}; this._presence_handlers = {};
this._roster_handlers = {}; this._roster_handlers = {};
this._handler_ids = 0; this._handler_ids = 0;
this.client = client; this.client = client;
this.name = name; this.name = name;
this.nick = nick; this.nick = nick;
this.password = password; this.password = password;
this._roomRosterHandler = __bind(this._roomRosterHandler, this); this._roomRosterHandler = __bind(this._roomRosterHandler, this);
this._addOccupant = __bind(this._addOccupant, this); this._addOccupant = __bind(this._addOccupant, this);
if (client.muc) this.client = client.muc;
if (client.muc) {
this.client = client.muc;
}
this.name = Strophe.getBareJidFromJid(name); this.name = Strophe.getBareJidFromJid(name);
this.client.rooms[this.name] = this; this.client.rooms[this.name] = this;
this.addHandler('presence', this._roomRosterHandler); this.addHandler('presence', this._roomRosterHandler);
} }
XmppRoom.prototype.join = function(msg_handler_cb, pres_handler_cb, roster_cb) { XmppRoom.prototype.join = function(msg_handler_cb, pres_handler_cb) {
return this.client.join(this.name, this.nick, msg_handler_cb, pres_handler_cb, roster_cb, this.password); if (!this.client.rooms[this.name]) {
return this.client.join(this.name, this.nick, msg_handler_cb, pres_handler_cb, this.password);
}
}; };
XmppRoom.prototype.leave = function(handler_cb, message) { XmppRoom.prototype.leave = function(handler_cb, message) {
...@@ -618,8 +591,8 @@ XmppRoom = (function() { ...@@ -618,8 +591,8 @@ XmppRoom = (function() {
return this.client.cancelConfigure(this.name); return this.client.cancelConfigure(this.name);
}; };
XmppRoom.prototype.saveConfiguration = function(config) { XmppRoom.prototype.saveConfiguration = function(configarray) {
return this.client.saveConfiguration(this.name, config); return this.client.saveConfiguration(this.name, configarray);
}; };
XmppRoom.prototype.queryOccupants = function(success_cb, error_cb) { XmppRoom.prototype.queryOccupants = function(success_cb, error_cb) {
...@@ -687,17 +660,14 @@ XmppRoom = (function() { ...@@ -687,17 +660,14 @@ XmppRoom = (function() {
return this.client.setStatus(this.name, this.nick, show, status); return this.client.setStatus(this.name, this.nick, show, status);
}; };
/*Function
Adds a handler to the MUC room.
Parameters:
(String) handler_type - 'message', 'presence' or 'roster'.
(Function) handler - The handler function.
Returns:
id - the id of handler.
*/
XmppRoom.prototype.addHandler = function(handler_type, handler) { XmppRoom.prototype.addHandler = function(handler_type, handler) {
/* Adds a handler to the MUC room.
* Parameters:
* (String) handler_type - 'message', 'presence' or 'roster'.
* (Function) handler - The handler function.
* Returns:
* id - the id of handler.
*/
var id; var id;
id = this._handler_ids++; id = this._handler_ids++;
switch (handler_type) { switch (handler_type) {
...@@ -717,46 +687,37 @@ XmppRoom = (function() { ...@@ -717,46 +687,37 @@ XmppRoom = (function() {
return id; return id;
}; };
/*Function
Removes a handler from the MUC room.
This function takes ONLY ids returned by the addHandler function
of this room. passing handler ids returned by connection.addHandler
may brake things!
Parameters:
(number) id - the id of the handler
*/
XmppRoom.prototype.removeHandler = function(id) { XmppRoom.prototype.removeHandler = function(id) {
/* Removes a handler from the MUC room.
* This function takes ONLY ids returned by the addHandler function
* of this room. passing handler ids returned by connection.addHandler
* may brake things!
* Parameters:
* (number) id - the id of the handler
*/
delete this._presence_handlers[id]; delete this._presence_handlers[id];
delete this._message_handlers[id]; delete this._message_handlers[id];
return delete this._roster_handlers[id]; return delete this._roster_handlers[id];
}; };
/*Function
Creates and adds an Occupant to the Room Roster.
Parameters:
(Object) data - the data the Occupant is filled with
Returns:
occ - the created Occupant.
*/
XmppRoom.prototype._addOccupant = function(data) { XmppRoom.prototype._addOccupant = function(data) {
/* Creates and adds an Occupant to the Room Roster.
* Parameters:
* (Object) data - the data the Occupant is filled with
* Returns:
* occ - the created Occupant.
*/
var occ; var occ;
occ = new Occupant(data, this); occ = new Occupant(data, this);
this.roster[occ.nick] = occ; this.roster[occ.nick] = occ;
return occ; return occ;
}; };
/*Function
The standard handler that managed the Room Roster.
Parameters:
(Object) pres - the presence stanza containing user information
*/
XmppRoom.prototype._roomRosterHandler = function(pres) { XmppRoom.prototype._roomRosterHandler = function(pres) {
/* The standard handler that managed the Room Roster.
* Parameters:
* (Object) pres - the presence stanza containing user information
*/
var data, handler, id, newnick, nick, _ref; var data, handler, id, newnick, nick, _ref;
data = XmppRoom._parsePresence(pres); data = XmppRoom._parsePresence(pres);
nick = data.nick; nick = data.nick;
...@@ -787,30 +748,25 @@ XmppRoom = (function() { ...@@ -787,30 +748,25 @@ XmppRoom = (function() {
_ref = this._roster_handlers; _ref = this._roster_handlers;
for (id in _ref) { for (id in _ref) {
handler = _ref[id]; handler = _ref[id];
if (!handler(this.roster, this)) { if (!handler(this.roster, this)) delete this._roster_handlers[id];
delete this._roster_handlers[id];
}
} }
return true; return true;
}; };
/*Function
Parses a presence stanza
Parameters:
(Object) data - the data extracted from the presence stanza
*/
XmppRoom._parsePresence = function(pres) { XmppRoom._parsePresence = function(pres) {
var a, c, c2, data, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; /* Parses a presence stanza
* Parameters:
* (Object) data - the data extracted from the presence stanza
*/
var a, c, c2, data, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
data = {}; data = {};
a = pres.attributes; a = pres.attributes;
data.nick = Strophe.getResourceFromJid(a.from.textContent); data.nick = Strophe.getResourceFromJid(a.from.textContent);
data.type = ((_ref = a.type) != null ? _ref.textContent : void 0) || null; data.type = ((_ref = a.type) != null ? _ref.textContent : void 0) || null;
data.states = []; data.states = [];
_ref1 = pres.childNodes; _ref2 = pres.childNodes;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
c = _ref1[_i]; c = _ref2[_i];
switch (c.nodeName) { switch (c.nodeName) {
case "status": case "status":
data.status = c.textContent || null; data.status = c.textContent || null;
...@@ -820,17 +776,17 @@ XmppRoom = (function() { ...@@ -820,17 +776,17 @@ XmppRoom = (function() {
break; break;
case "x": case "x":
a = c.attributes; a = c.attributes;
if (((_ref2 = a.xmlns) != null ? _ref2.textContent : void 0) === Strophe.NS.MUC_USER) { if (((_ref3 = a.xmlns) != null ? _ref3.textContent : void 0) === Strophe.NS.MUC_USER) {
_ref3 = c.childNodes; _ref4 = c.childNodes;
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
c2 = _ref3[_j]; c2 = _ref4[_j];
switch (c2.nodeName) { switch (c2.nodeName) {
case "item": case "item":
a = c2.attributes; a = c2.attributes;
data.affiliation = ((_ref4 = a.affiliation) != null ? _ref4.textContent : void 0) || null; data.affiliation = ((_ref5 = a.affiliation) != null ? _ref5.textContent : void 0) || null;
data.role = ((_ref5 = a.role) != null ? _ref5.textContent : void 0) || null; data.role = ((_ref6 = a.role) != null ? _ref6.textContent : void 0) || null;
data.jid = ((_ref6 = a.jid) != null ? _ref6.textContent : void 0) || null; data.jid = ((_ref7 = a.jid) != null ? _ref7.textContent : void 0) || null;
data.newnick = ((_ref7 = a.nick) != null ? _ref7.textContent : void 0) || null; data.newnick = ((_ref8 = a.nick) != null ? _ref8.textContent : void 0) || null;
break; break;
case "status": case "status":
if (c2.attributes.code) { if (c2.attributes.code) {
...@@ -846,19 +802,16 @@ XmppRoom = (function() { ...@@ -846,19 +802,16 @@ XmppRoom = (function() {
return XmppRoom; return XmppRoom;
})(); })();
RoomConfig = (function() { RoomConfig = (function() {
function RoomConfig(info) { function RoomConfig(info) {
this.parse = __bind(this.parse, this); this.parse = __bind(this.parse, this); if (info != null) this.parse(info);
if (info != null) {
this.parse(info);
}
} }
RoomConfig.prototype.parse = function(result) { RoomConfig.prototype.parse = function(result) {
var attr, attrs, child, field, identity, query, _i, _j, _k, _len, _len1, _len2, _ref; var attr, attrs, child, field, identity, query, _i, _j, _k, _len, _len2, _len3, _ref;
query = result.getElementsByTagName("query")[0].childNodes; query = result.getElementsByTagName("query")[0].childNodes;
this.identities = []; this.identities = [];
this.features = []; this.features = [];
...@@ -869,7 +822,7 @@ RoomConfig = (function() { ...@@ -869,7 +822,7 @@ RoomConfig = (function() {
switch (child.nodeName) { switch (child.nodeName) {
case "identity": case "identity":
identity = {}; identity = {};
for (_j = 0, _len1 = attrs.length; _j < _len1; _j++) { for (_j = 0, _len2 = attrs.length; _j < _len2; _j++) {
attr = attrs[_j]; attr = attrs[_j];
identity[attr.name] = attr.textContent; identity[attr.name] = attr.textContent;
} }
...@@ -884,11 +837,9 @@ RoomConfig = (function() { ...@@ -884,11 +837,9 @@ RoomConfig = (function() {
break; break;
} }
_ref = child.childNodes; _ref = child.childNodes;
for (_k = 0, _len2 = _ref.length; _k < _len2; _k++) { for (_k = 0, _len3 = _ref.length; _k < _len3; _k++) {
field = _ref[_k]; field = _ref[_k];
if (!(!field.attributes.type)) { if (!(!field.attributes.type)) continue;
continue;
}
attrs = field.attributes; attrs = field.attributes;
this.x.push({ this.x.push({
"var": attrs["var"].textContent, "var": attrs["var"].textContent,
...@@ -907,38 +858,25 @@ RoomConfig = (function() { ...@@ -907,38 +858,25 @@ RoomConfig = (function() {
return RoomConfig; return RoomConfig;
})(); })();
Occupant = (function() { Occupant = (function() {
function Occupant(data, room) { function Occupant(data, room) {
this.room = room; this.room = room;
this.update = __bind(this.update, this); this.update = __bind(this.update, this);
this.admin = __bind(this.admin, this); this.admin = __bind(this.admin, this);
this.owner = __bind(this.owner, this); this.owner = __bind(this.owner, this);
this.revoke = __bind(this.revoke, this); this.revoke = __bind(this.revoke, this);
this.member = __bind(this.member, this); this.member = __bind(this.member, this);
this.ban = __bind(this.ban, this); this.ban = __bind(this.ban, this);
this.modifyAffiliation = __bind(this.modifyAffiliation, this); this.modifyAffiliation = __bind(this.modifyAffiliation, this);
this.deop = __bind(this.deop, this); this.deop = __bind(this.deop, this);
this.op = __bind(this.op, this); this.op = __bind(this.op, this);
this.mute = __bind(this.mute, this); this.mute = __bind(this.mute, this);
this.voice = __bind(this.voice, this); this.voice = __bind(this.voice, this);
this.kick = __bind(this.kick, this); this.kick = __bind(this.kick, this);
this.modifyRole = __bind(this.modifyRole, this); this.modifyRole = __bind(this.modifyRole, this);
this.update(data); this.update(data);
} }
...@@ -999,7 +937,9 @@ Occupant = (function() { ...@@ -999,7 +937,9 @@ Occupant = (function() {
this.show = data.show || null; this.show = data.show || null;
return this; return this;
}; };
return Occupant; return Occupant;
})(); })();
}).call(this);
}));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment