strophe.muc.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. *Plugin to implement the MUC extension.
  3. http://xmpp.org/extensions/xep-0045.html
  4. *Previous Author:
  5. Nathan Zorn <nathan.zorn@gmail.com>
  6. *Complete CoffeeScript rewrite:
  7. Andreas Guth <guth@dbis.rwth-aachen.de>
  8. *Cleanup, AMD/global registrations and bugfixes:
  9. JC Brand <jc@opkode.com>
  10. */
  11. // AMD/global registrations
  12. (function (root, factory) {
  13. if (typeof console === undefined || typeof console.log === undefined) {
  14. console = { log: function () {}, error: function () {} };
  15. }
  16. if (typeof define === 'function' && define.amd) {
  17. define([
  18. "Libraries/strophe"
  19. ], function () {
  20. if (console===undefined || console.log===undefined) {
  21. console = { log: function () {}, error: function () {} };
  22. }
  23. return factory(jQuery, console);
  24. }
  25. );
  26. } else {
  27. return factory(jQuery, console);
  28. }
  29. }(this, function ($, console) {
  30. (function() {
  31. var Occupant, RoomConfig, XmppRoom,
  32. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  33. Strophe.addConnectionPlugin('muc', {
  34. _connection: null,
  35. rooms: [],
  36. init: function(conn) {
  37. /* Initialize the MUC plugin. Sets the correct connection object and
  38. * extends the namesace.
  39. */
  40. this._connection = conn;
  41. this._muc_handler = null;
  42. Strophe.addNamespace('MUC_OWNER', Strophe.NS.MUC + "#owner");
  43. Strophe.addNamespace('MUC_ADMIN', Strophe.NS.MUC + "#admin");
  44. Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user");
  45. return Strophe.addNamespace('MUC_ROOMCONF', Strophe.NS.MUC + "#roomconfig");
  46. },
  47. join: function(room, nick, msg_handler_cb, pres_handler_cb, roster_cb, password) {
  48. /* Join a multi-user chat room
  49. *
  50. * Parameters:
  51. * (String) room - The multi-user chat room to join.
  52. * (String) nick - Optional nickname to use in the chat room.
  53. * (Function) msg_handler_cb - The function call to handle messages from the specified chat room.
  54. * (Function) pres_handler_cb - The function call back to handle presence in the chat room.
  55. * (String) password - The optional password to use. (password protected rooms only)
  56. */
  57. var msg, room_nick, _base,
  58. _this = this;
  59. room_nick = this.test_append_nick(room, nick);
  60. msg = $pres({
  61. from: this._connection.jid,
  62. to: room_nick
  63. }).c("x", {
  64. xmlns: Strophe.NS.MUC
  65. });
  66. if (password !== null) {
  67. msg.cnode(Strophe.xmlElement("password", [], password));
  68. }
  69. if (this._muc_handler === null) {
  70. this._muc_handler = this._connection.addHandler(function(stanza) {
  71. var from, handler, handlers, id, roomname, x, xmlns, xquery, _i, _len;
  72. from = stanza.getAttribute('from');
  73. roomname = from.split("/")[0];
  74. if (!_this.rooms[roomname]) { return true; }
  75. room = _this.rooms[roomname];
  76. handlers = {};
  77. if (stanza.nodeName === "message") {
  78. handlers = room._message_handlers;
  79. } else if (stanza.nodeName === "presence") {
  80. xquery = stanza.getElementsByTagName("x");
  81. if (xquery.length > 0) {
  82. for (_i = 0, _len = xquery.length; _i < _len; _i++) {
  83. x = xquery[_i];
  84. xmlns = x.getAttribute("xmlns");
  85. if (xmlns && xmlns.match(Strophe.NS.MUC)) {
  86. handlers = room._presence_handlers;
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. for (id in handlers) {
  93. handler = handlers[id];
  94. if (!handler(stanza, room)) { delete handlers[id]; }
  95. }
  96. return true;
  97. });
  98. }
  99. if ((_base = this.rooms)[room] === null) {
  100. _base[room] = new XmppRoom(this, room, nick, password);
  101. }
  102. if (pres_handler_cb) {
  103. this.rooms[room].addHandler('presence', pres_handler_cb);
  104. }
  105. if (msg_handler_cb) {
  106. this.rooms[room].addHandler('message', msg_handler_cb);
  107. }
  108. if (roster_cb) {
  109. this.rooms[room].addHandler('roster', roster_cb);
  110. }
  111. return this._connection.send(msg);
  112. },
  113. leave: function(room, nick, handler_cb, exit_msg) {
  114. /* Leave a multi-user chat room
  115. *
  116. * Parameters:
  117. * (String) room - The multi-user chat room to leave.
  118. * (String) nick - The nick name used in the room.
  119. * (Function) handler_cb - Optional function to handle the successful leave.
  120. * (String) exit_msg - optional exit message.
  121. * Returns:
  122. * iqid - The unique id for the room leave.
  123. */
  124. var presence, presenceid, room_nick;
  125. delete this.rooms[room];
  126. if (this.rooms.length === 0) {
  127. this._connection.deleteHandler(this._muc_handler);
  128. this._muc_handler = null;
  129. }
  130. room_nick = this.test_append_nick(room, nick);
  131. presenceid = this._connection.getUniqueId();
  132. presence = $pres({
  133. type: "unavailable",
  134. id: presenceid,
  135. from: this._connection.jid,
  136. to: room_nick
  137. });
  138. if (exit_msg !== null) {
  139. presence.c("status", exit_msg);
  140. }
  141. if (handler_cb !== null) {
  142. this._connection.addHandler(handler_cb, null, "presence", null, presenceid);
  143. }
  144. this._connection.send(presence);
  145. return presenceid;
  146. },
  147. message: function(room, nick, message, html_message, type) {
  148. /* Parameters:
  149. * (String) room - The multi-user chat room name.
  150. * (String) nick - The nick name used in the chat room.
  151. * (String) message - The plaintext message to send to the room.
  152. * (String) html_message - The message to send to the room with html markup.
  153. * (String) type - "groupchat" for group chat messages o
  154. * "chat" for private chat messages
  155. * Returns:
  156. * msgiq - the unique id used to send the message
  157. */
  158. var msg, msgid, parent, room_nick;
  159. room_nick = this.test_append_nick(room, nick);
  160. type = type || (nick !== null ? "chat" : "groupchat");
  161. msgid = this._connection.getUniqueId();
  162. msg = $msg({
  163. to: room_nick,
  164. from: this._connection.jid,
  165. type: type,
  166. id: msgid
  167. }).c("body", {
  168. xmlns: Strophe.NS.CLIENT
  169. }).t(message);
  170. msg.up();
  171. if (html_message !== null) {
  172. msg.c("html", {xmlns: Strophe.NS.XHTML_IM}).c("body", {xmlns: Strophe.NS.XHTML}).h(html_message);
  173. if (msg.node.childNodes.length === 0) {
  174. parent = msg.node.parentNode;
  175. msg.up().up();
  176. msg.node.removeChild(parent);
  177. } else {
  178. msg.up().up();
  179. }
  180. }
  181. msg.c("x", {
  182. xmlns: "jabber:x:event"
  183. }).c("composing");
  184. this._connection.send(msg);
  185. return msgid;
  186. },
  187. groupchat: function(room, message, html_message) {
  188. /* Convenience Function to send a Message to all Occupants
  189. * Parameters:
  190. * (String) room - The multi-user chat room name.
  191. * (String) message - The plaintext message to send to the room.
  192. * (String) html_message - The message to send to the room with html markup.
  193. * Returns:
  194. * msgiq - the unique id used to send the message
  195. */
  196. return this.message(room, null, message, html_message);
  197. },
  198. invite: function(room, receiver, reason) {
  199. /* Send a mediated invitation.
  200. * Parameters:
  201. * (String) room - The multi-user chat room name.
  202. * (String) receiver - The invitation's receiver.
  203. * (String) reason - Optional reason for joining the room.
  204. * Returns:
  205. * msgiq - the unique id used to send the invitation
  206. */
  207. var invitation, msgid;
  208. msgid = this._connection.getUniqueId();
  209. invitation = $msg({
  210. from: this._connection.jid,
  211. to: room,
  212. id: msgid
  213. }).c('x', {
  214. xmlns: Strophe.NS.MUC_USER
  215. }).c('invite', {
  216. to: receiver
  217. });
  218. if (reason !== null) {
  219. invitation.c('reason', reason);
  220. }
  221. this._connection.send(invitation);
  222. return msgid;
  223. },
  224. directInvite: function(room, receiver, reason, password) {
  225. /* Send a direct invitation.
  226. * Parameters:
  227. * (String) room - The multi-user chat room name.
  228. * (String) receiver - The invitation's receiver.
  229. * (String) reason - Optional reason for joining the room.
  230. * (String) password - Optional password for the room.
  231. * Returns:
  232. * msgiq - the unique id used to send the invitation
  233. */
  234. var attrs, invitation, msgid;
  235. msgid = this._connection.getUniqueId();
  236. attrs = {
  237. xmlns: 'jabber:x:conference',
  238. jid: room
  239. };
  240. if (reason !== null) { attrs.reason = reason; }
  241. if (password !== null) { attrs.password = password; }
  242. invitation = $msg({
  243. from: this._connection.jid,
  244. to: receiver,
  245. id: msgid
  246. }).c('x', attrs);
  247. this._connection.send(invitation);
  248. return msgid;
  249. },
  250. queryOccupants: function(room, success_cb, error_cb) {
  251. /* Queries a room for a list of occupants
  252. * (String) room - The multi-user chat room name.
  253. * (Function) success_cb - Optional function to handle the info.
  254. * (Function) error_cb - Optional function to handle an error.
  255. * Returns:
  256. * id - the unique id used to send the info request
  257. */
  258. var attrs, info;
  259. attrs = {
  260. xmlns: Strophe.NS.DISCO_ITEMS
  261. };
  262. info = $iq({
  263. from: this._connection.jid,
  264. to: room,
  265. type: 'get'
  266. }).c('query', attrs);
  267. return this._connection.sendIQ(info, success_cb, error_cb);
  268. },
  269. configure: function(room, handler_cb) {
  270. /* Start a room configuration.
  271. * Parameters:
  272. * (String) room - The multi-user chat room name.
  273. * (Function) handler_cb - Optional function to handle the config form.
  274. * Returns:
  275. * id - the unique id used to send the configuration request
  276. */
  277. var config, id, stanza;
  278. config = $iq({
  279. to: room,
  280. type: "get"
  281. }).c("query", {
  282. xmlns: Strophe.NS.MUC_OWNER
  283. });
  284. stanza = config.tree();
  285. id = this._connection.sendIQ(stanza);
  286. if (handler_cb !== null) {
  287. this._connection.addHandler(function(stanza) { handler_cb(stanza);
  288. return false;
  289. }, Strophe.NS.MUC_OWNER, "iq", null, id);
  290. }
  291. return id;
  292. },
  293. cancelConfigure: function(room) {
  294. /* Cancel the room configuration
  295. * Parameters:
  296. * (String) room - The multi-user chat room name.
  297. * Returns:
  298. * id - the unique id used to cancel the configuration.
  299. */
  300. var config, stanza;
  301. config = $iq({
  302. to: room,
  303. type: "set"
  304. }).c("query", {
  305. xmlns: Strophe.NS.MUC_OWNER
  306. }).c("x", {
  307. xmlns: "jabber:x:data",
  308. type: "cancel"
  309. });
  310. stanza = config.tree();
  311. return this._connection.sendIQ(stanza);
  312. },
  313. saveConfiguration: function(room, configarray) {
  314. /* Save a room configuration.
  315. * Parameters:
  316. * (String) room - The multi-user chat room name.
  317. * (Array) configarray - an array of form elements used to configure the room.
  318. * Returns:
  319. * id - the unique id used to save the configuration.
  320. */
  321. var conf, config, stanza, _i, _len;
  322. config = $iq({
  323. to: room,
  324. type: "set"
  325. }).c("query", {
  326. xmlns: Strophe.NS.MUC_OWNER
  327. }).c("x", {
  328. xmlns: "jabber:x:data",
  329. type: "submit"
  330. });
  331. for (_i = 0, _len = configarray.length; _i < _len; _i++) {
  332. conf = configarray[_i];
  333. config.cnode(conf).up();
  334. }
  335. stanza = config.tree();
  336. return this._connection.sendIQ(stanza);
  337. },
  338. createInstantRoom: function(room) {
  339. /* Parameters:
  340. * (String) room - The multi-user chat room name.
  341. * Returns:
  342. * id - the unique id used to create the chat room.
  343. */
  344. var roomiq;
  345. roomiq = $iq({
  346. to: room,
  347. type: "set"
  348. }).c("query", {
  349. xmlns: Strophe.NS.MUC_OWNER
  350. }).c("x", {
  351. xmlns: "jabber:x:data",
  352. type: "submit"
  353. });
  354. return this._connection.sendIQ(roomiq.tree());
  355. },
  356. setTopic: function(room, topic) {
  357. /* Set the topic of the chat room.
  358. * Parameters:
  359. * (String) room - The multi-user chat room name.
  360. * (String) topic - Topic message.
  361. */
  362. var msg;
  363. msg = $msg({
  364. to: room,
  365. from: this._connection.jid,
  366. type: "groupchat"
  367. }).c("subject", {
  368. xmlns: "jabber:client"
  369. }).t(topic);
  370. return this._connection.send(msg.tree());
  371. },
  372. _modifyPrivilege: function(room, item, reason, handler_cb, error_cb) {
  373. /* Internal Function that Changes the role or affiliation of a member
  374. * of a MUC room. This function is used by modifyRole and modifyAffiliation.
  375. * The modification can only be done by a room moderator. An error will be
  376. * returned if the user doesn't have permission.
  377. * Parameters:
  378. * (String) room - The multi-user chat room name.
  379. * (Object) item - Object with nick and role or jid and affiliation attribute
  380. * (String) reason - Optional reason for the change.
  381. * (Function) handler_cb - Optional callback for success
  382. * (Function) errer_cb - Optional callback for error
  383. * Returns:
  384. * iq - the id of the mode change request.
  385. */
  386. var iq;
  387. iq = $iq({
  388. to: room,
  389. type: "set"
  390. }).c("query", {
  391. xmlns: Strophe.NS.MUC_ADMIN
  392. }).cnode(item.node);
  393. if (reason !== null) { iq.c("reason", reason); }
  394. return this._connection.sendIQ(iq.tree(), handler_cb, error_cb);
  395. },
  396. modifyRole: function(room, nick, role, reason, handler_cb, error_cb) {
  397. /* Changes the role of a member of a MUC room.
  398. * The modification can only be done by a room moderator. An error will be
  399. * returned if the user doesn't have permission.
  400. * Parameters:
  401. * (String) room - The multi-user chat room name.
  402. * (String) nick - The nick name of the user to modify.
  403. * (String) role - The new role of the user.
  404. * (String) affiliation - The new affiliation of the user.
  405. * (String) reason - Optional reason for the change.
  406. * (Function) handler_cb - Optional callback for success
  407. * (Function) errer_cb - Optional callback for error
  408. * Returns:
  409. * iq - the id of the mode change request.
  410. */
  411. var item;
  412. item = $build("item", {
  413. nick: nick,
  414. role: role
  415. });
  416. return this._modifyPrivilege(room, item, reason, handler_cb, error_cb);
  417. },
  418. kick: function(room, nick, reason, handler_cb, error_cb) {
  419. return this.modifyRole(room, nick, 'none', reason, handler_cb, error_cb);
  420. },
  421. voice: function(room, nick, reason, handler_cb, error_cb) {
  422. return this.modifyRole(room, nick, 'participant', reason, handler_cb, error_cb);
  423. },
  424. mute: function(room, nick, reason, handler_cb, error_cb) {
  425. return this.modifyRole(room, nick, 'visitor', reason, handler_cb, error_cb);
  426. },
  427. op: function(room, nick, reason, handler_cb, error_cb) {
  428. return this.modifyRole(room, nick, 'moderator', reason, handler_cb, error_cb);
  429. },
  430. deop: function(room, nick, reason, handler_cb, error_cb) {
  431. return this.modifyRole(room, nick, 'participant', reason, handler_cb, error_cb);
  432. },
  433. modifyAffiliation: function(room, jid, affiliation, reason, handler_cb, error_cb) {
  434. /* Changes the affiliation of a member of a MUC room.
  435. * The modification can only be done by a room moderator. An error will be
  436. * returned if the user doesn't have permission.
  437. * Parameters:
  438. * (String) room - The multi-user chat room name.
  439. * (String) jid - The jid of the user to modify.
  440. * (String) affiliation - The new affiliation of the user.
  441. * (String) reason - Optional reason for the change.
  442. * (Function) handler_cb - Optional callback for success
  443. * (Function) errer_cb - Optional callback for error
  444. * Returns:
  445. * iq - the id of the mode change request.
  446. */
  447. var item;
  448. item = $build("item", {
  449. jid: jid,
  450. affiliation: affiliation
  451. });
  452. return this._modifyPrivilege(room, item, reason, handler_cb, error_cb);
  453. },
  454. ban: function(room, jid, reason, handler_cb, error_cb) {
  455. return this.modifyAffiliation(room, jid, 'outcast', reason, handler_cb, error_cb);
  456. },
  457. member: function(room, jid, reason, handler_cb, error_cb) {
  458. return this.modifyAffiliation(room, jid, 'member', reason, handler_cb, error_cb);
  459. },
  460. revoke: function(room, jid, reason, handler_cb, error_cb) {
  461. return this.modifyAffiliation(room, jid, 'none', reason, handler_cb, error_cb);
  462. },
  463. owner: function(room, jid, reason, handler_cb, error_cb) {
  464. return this.modifyAffiliation(room, jid, 'owner', reason, handler_cb, error_cb);
  465. },
  466. admin: function(room, jid, reason, handler_cb, error_cb) {
  467. return this.modifyAffiliation(room, jid, 'admin', reason, handler_cb, error_cb);
  468. },
  469. changeNick: function(room, user) {
  470. /* Change the current users nick name.
  471. * Parameters:
  472. * (String) room - The multi-user chat room name.
  473. * (String) user - The new nick name.
  474. */
  475. var presence, room_nick;
  476. room_nick = this.test_append_nick(room, user);
  477. presence = $pres({
  478. from: this._connection.jid,
  479. to: room_nick,
  480. id: this._connection.getUniqueId()
  481. });
  482. return this._connection.send(presence.tree());
  483. },
  484. setStatus: function(room, user, show, status) {
  485. /* Change the current users status.
  486. * Parameters:
  487. * (String) room - The multi-user chat room name.
  488. * (String) user - The current nick.
  489. * (String) show - The new show-text.
  490. * (String) status - The new status-text.
  491. */
  492. var presence, room_nick;
  493. room_nick = this.test_append_nick(room, user);
  494. presence = $pres({
  495. from: this._connection.jid,
  496. to: room_nick
  497. });
  498. if (show !== null) { presence.c('show', show).up(); }
  499. if (status !== null) { presence.c('status', status); }
  500. return this._connection.send(presence.tree());
  501. },
  502. listRooms: function(server, handle_cb) {
  503. /* List all chat room available on a server.
  504. * Parameters:
  505. * (String) server - name of chat server.
  506. * (String) handle_cb - Function to call for room list return.
  507. */
  508. var iq;
  509. iq = $iq({
  510. to: server,
  511. from: this._connection.jid,
  512. type: "get"
  513. }).c("query", {
  514. xmlns: Strophe.NS.DISCO_ITEMS
  515. });
  516. return this._connection.sendIQ(iq, handle_cb);
  517. },
  518. test_append_nick: function(room, nick) {
  519. return room + (nick !== null ? "/" + (Strophe.escapeNode(nick)) : "");
  520. }
  521. });
  522. XmppRoom = (function() {
  523. function XmppRoom(client, name, nick, password) {
  524. this.roster = {};
  525. this._message_handlers = {};
  526. this._presence_handlers = {};
  527. this._roster_handlers = {};
  528. this._handler_ids = 0;
  529. this.client = client;
  530. this.name = name;
  531. this.nick = nick;
  532. this.password = password;
  533. this._roomRosterHandler = __bind(this._roomRosterHandler, this);
  534. this._addOccupant = __bind(this._addOccupant, this);
  535. if (client.muc) { this.client = client.muc; }
  536. this.name = Strophe.getBareJidFromJid(name);
  537. this.client.rooms[this.name] = this;
  538. this.addHandler('presence', this._roomRosterHandler);
  539. }
  540. XmppRoom.prototype.join = function(msg_handler_cb, pres_handler_cb) {
  541. if (!this.client.rooms[this.name]) {
  542. return this.client.join(this.name, this.nick, msg_handler_cb, pres_handler_cb, this.password);
  543. }
  544. };
  545. XmppRoom.prototype.leave = function(handler_cb, message) {
  546. this.client.leave(this.name, this.nick, handler_cb, message);
  547. return delete this.client.rooms[this.name];
  548. };
  549. XmppRoom.prototype.message = function(nick, message, html_message, type) {
  550. return this.client.message(this.name, nick, message, html_message, type);
  551. };
  552. XmppRoom.prototype.groupchat = function(message, html_message) {
  553. return this.client.groupchat(this.name, message, html_message);
  554. };
  555. XmppRoom.prototype.invite = function(receiver, reason) {
  556. return this.client.invite(this.name, receiver, reason);
  557. };
  558. XmppRoom.prototype.directInvite = function(receiver, reason) {
  559. return this.client.directInvite(this.name, receiver, reason, this.password);
  560. };
  561. XmppRoom.prototype.configure = function(handler_cb) {
  562. return this.client.configure(this.name, handler_cb);
  563. };
  564. XmppRoom.prototype.cancelConfigure = function() {
  565. return this.client.cancelConfigure(this.name);
  566. };
  567. XmppRoom.prototype.saveConfiguration = function(configarray) {
  568. return this.client.saveConfiguration(this.name, configarray);
  569. };
  570. XmppRoom.prototype.queryOccupants = function(success_cb, error_cb) {
  571. return this.client.queryOccupants(this.name, success_cb, error_cb);
  572. };
  573. XmppRoom.prototype.setTopic = function(topic) {
  574. return this.client.setTopic(this.name, topic);
  575. };
  576. XmppRoom.prototype.modifyRole = function(nick, role, reason, success_cb, error_cb) {
  577. return this.client.modifyRole(this.name, nick, role, reason, success_cb, error_cb);
  578. };
  579. XmppRoom.prototype.kick = function(nick, reason, handler_cb, error_cb) {
  580. return this.client.kick(this.name, nick, reason, handler_cb, error_cb);
  581. };
  582. XmppRoom.prototype.voice = function(nick, reason, handler_cb, error_cb) {
  583. return this.client.voice(this.name, nick, reason, handler_cb, error_cb);
  584. };
  585. XmppRoom.prototype.mute = function(nick, reason, handler_cb, error_cb) {
  586. return this.client.mute(this.name, nick, reason, handler_cb, error_cb);
  587. };
  588. XmppRoom.prototype.op = function(nick, reason, handler_cb, error_cb) {
  589. return this.client.op(this.name, nick, reason, handler_cb, error_cb);
  590. };
  591. XmppRoom.prototype.deop = function(nick, reason, handler_cb, error_cb) {
  592. return this.client.deop(this.name, nick, reason, handler_cb, error_cb);
  593. };
  594. XmppRoom.prototype.modifyAffiliation = function(jid, affiliation, reason, success_cb, error_cb) {
  595. return this.client.modifyAffiliation(this.name, jid, affiliation, reason, success_cb, error_cb);
  596. };
  597. XmppRoom.prototype.ban = function(jid, reason, handler_cb, error_cb) {
  598. return this.client.ban(this.name, jid, reason, handler_cb, error_cb);
  599. };
  600. XmppRoom.prototype.member = function(jid, reason, handler_cb, error_cb) {
  601. return this.client.member(this.name, jid, reason, handler_cb, error_cb);
  602. };
  603. XmppRoom.prototype.revoke = function(jid, reason, handler_cb, error_cb) {
  604. return this.client.revoke(this.name, jid, reason, handler_cb, error_cb);
  605. };
  606. XmppRoom.prototype.owner = function(jid, reason, handler_cb, error_cb) {
  607. return this.client.owner(this.name, jid, reason, handler_cb, error_cb);
  608. };
  609. XmppRoom.prototype.admin = function(jid, reason, handler_cb, error_cb) {
  610. return this.client.admin(this.name, jid, reason, handler_cb, error_cb);
  611. };
  612. XmppRoom.prototype.changeNick = function(nick) {
  613. this.nick = nick;
  614. return this.client.changeNick(this.name, nick);
  615. };
  616. XmppRoom.prototype.setStatus = function(show, status) {
  617. return this.client.setStatus(this.name, this.nick, show, status);
  618. };
  619. XmppRoom.prototype.addHandler = function(handler_type, handler) {
  620. /* Adds a handler to the MUC room.
  621. * Parameters:
  622. * (String) handler_type - 'message', 'presence' or 'roster'.
  623. * (Function) handler - The handler function.
  624. * Returns:
  625. * id - the id of handler.
  626. */
  627. var id;
  628. id = this._handler_ids++;
  629. switch (handler_type) {
  630. case 'presence':
  631. this._presence_handlers[id] = handler;
  632. break;
  633. case 'message':
  634. this._message_handlers[id] = handler;
  635. break;
  636. case 'roster':
  637. this._roster_handlers[id] = handler;
  638. break;
  639. default:
  640. this._handler_ids--;
  641. return null;
  642. }
  643. return id;
  644. };
  645. XmppRoom.prototype.removeHandler = function(id) {
  646. /* Removes a handler from the MUC room.
  647. * This function takes ONLY ids returned by the addHandler function
  648. * of this room. passing handler ids returned by connection.addHandler
  649. * may brake things!
  650. * Parameters:
  651. * (number) id - the id of the handler
  652. */
  653. delete this._presence_handlers[id];
  654. delete this._message_handlers[id];
  655. return delete this._roster_handlers[id];
  656. };
  657. XmppRoom.prototype._addOccupant = function(data) {
  658. /* Creates and adds an Occupant to the Room Roster.
  659. * Parameters:
  660. * (Object) data - the data the Occupant is filled with
  661. * Returns:
  662. * occ - the created Occupant.
  663. */
  664. var occ;
  665. occ = new Occupant(data, this);
  666. this.roster[occ.nick] = occ;
  667. return occ;
  668. };
  669. XmppRoom.prototype._roomRosterHandler = function(pres) {
  670. /* The standard handler that managed the Room Roster.
  671. * Parameters:
  672. * (Object) pres - the presence stanza containing user information
  673. */
  674. var data, handler, id, newnick, nick, _ref;
  675. data = XmppRoom._parsePresence(pres);
  676. nick = data.nick;
  677. newnick = data.newnick || null;
  678. switch (data.type) {
  679. case 'error':
  680. return;
  681. case 'unavailable':
  682. if (newnick) {
  683. data.nick = newnick;
  684. if (this.roster[nick] && this.roster[newnick]) {
  685. this.roster[nick].update(this.roster[newnick]);
  686. this.roster[newnick] = this.roster[nick];
  687. }
  688. if (this.roster[nick] && !this.roster[newnick]) {
  689. this.roster[newnick] = this.roster[nick].update(data);
  690. }
  691. }
  692. delete this.roster[nick];
  693. break;
  694. default:
  695. if (this.roster[nick]) {
  696. this.roster[nick].update(data);
  697. } else {
  698. this._addOccupant(data);
  699. }
  700. }
  701. _ref = this._roster_handlers;
  702. for (id in _ref) {
  703. handler = _ref[id];
  704. if (!handler(this.roster, this)) { delete this._roster_handlers[id]; }
  705. }
  706. return true;
  707. };
  708. XmppRoom._parsePresence = function(pres) {
  709. /* Parses a presence stanza
  710. * Parameters:
  711. * (Object) data - the data extracted from the presence stanza
  712. */
  713. var a, c, c2, data, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
  714. data = {};
  715. a = pres.attributes;
  716. data.nick = Strophe.getResourceFromJid(a.from.textContent);
  717. data.type = ((_ref = a.type) != null ? _ref.textContent : void 0) || null;
  718. data.states = [];
  719. _ref2 = pres.childNodes;
  720. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  721. c = _ref2[_i];
  722. switch (c.nodeName) {
  723. case "status":
  724. data.status = c.textContent || null;
  725. break;
  726. case "show":
  727. data.show = c.textContent || null;
  728. break;
  729. case "x":
  730. a = c.attributes;
  731. if (((_ref3 = a.xmlns) != null ? _ref3.textContent : void 0) === Strophe.NS.MUC_USER) {
  732. _ref4 = c.childNodes;
  733. for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
  734. c2 = _ref4[_j];
  735. switch (c2.nodeName) {
  736. case "item":
  737. a = c2.attributes;
  738. data.affiliation = ((_ref5 = a.affiliation) != null ? _ref5.textContent : void 0) || null;
  739. data.role = ((_ref6 = a.role) != null ? _ref6.textContent : void 0) || null;
  740. data.jid = ((_ref7 = a.jid) != null ? _ref7.textContent : void 0) || null;
  741. data.newnick = ((_ref8 = a.nick) != null ? _ref8.textContent : void 0) || null;
  742. break;
  743. case "status":
  744. if (c2.attributes.code) {
  745. data.states.push(c2.attributes.code.textContent);
  746. }
  747. }
  748. }
  749. }
  750. }
  751. }
  752. return data;
  753. };
  754. return XmppRoom;
  755. })();
  756. RoomConfig = (function() {
  757. function RoomConfig(info) {
  758. this.parse = __bind(this.parse, this); if (info != null) this.parse(info);
  759. }
  760. RoomConfig.prototype.parse = function(result) {
  761. var attr, attrs, child, field, identity, query, _i, _j, _k, _len, _len2, _len3, _ref;
  762. query = result.getElementsByTagName("query")[0].childNodes;
  763. this.identities = [];
  764. this.features = [];
  765. this.x = [];
  766. for (_i = 0, _len = query.length; _i < _len; _i++) {
  767. child = query[_i];
  768. attrs = child.attributes;
  769. switch (child.nodeName) {
  770. case "identity":
  771. identity = {};
  772. for (_j = 0, _len2 = attrs.length; _j < _len2; _j++) {
  773. attr = attrs[_j];
  774. identity[attr.name] = attr.textContent;
  775. }
  776. this.identities.push(identity);
  777. break;
  778. case "feature":
  779. this.features.push(attrs["var"].textContent);
  780. break;
  781. case "x":
  782. attrs = child.childNodes[0].attributes;
  783. if ((!attrs["var"].textContent === 'FORM_TYPE') || (!attrs.type.textContent === 'hidden')) {
  784. break;
  785. }
  786. _ref = child.childNodes;
  787. for (_k = 0, _len3 = _ref.length; _k < _len3; _k++) {
  788. field = _ref[_k];
  789. if (!(!field.attributes.type)) continue;
  790. attrs = field.attributes;
  791. this.x.push({
  792. "var": attrs["var"].textContent,
  793. label: attrs.label.textContent || "",
  794. value: field.firstChild.textContent || ""
  795. });
  796. }
  797. }
  798. }
  799. return {
  800. "identities": this.identities,
  801. "features": this.features,
  802. "x": this.x
  803. };
  804. };
  805. return RoomConfig;
  806. })();
  807. Occupant = (function() {
  808. function Occupant(data, room) {
  809. this.room = room;
  810. this.update = __bind(this.update, this);
  811. this.admin = __bind(this.admin, this);
  812. this.owner = __bind(this.owner, this);
  813. this.revoke = __bind(this.revoke, this);
  814. this.member = __bind(this.member, this);
  815. this.ban = __bind(this.ban, this);
  816. this.modifyAffiliation = __bind(this.modifyAffiliation, this);
  817. this.deop = __bind(this.deop, this);
  818. this.op = __bind(this.op, this);
  819. this.mute = __bind(this.mute, this);
  820. this.voice = __bind(this.voice, this);
  821. this.kick = __bind(this.kick, this);
  822. this.modifyRole = __bind(this.modifyRole, this);
  823. this.update(data);
  824. }
  825. Occupant.prototype.modifyRole = function(role, reason, success_cb, error_cb) {
  826. return this.room.modifyRole(this.nick, role, reason, success_cb, error_cb);
  827. };
  828. Occupant.prototype.kick = function(reason, handler_cb, error_cb) {
  829. return this.room.kick(this.nick, reason, handler_cb, error_cb);
  830. };
  831. Occupant.prototype.voice = function(reason, handler_cb, error_cb) {
  832. return this.room.voice(this.nick, reason, handler_cb, error_cb);
  833. };
  834. Occupant.prototype.mute = function(reason, handler_cb, error_cb) {
  835. return this.room.mute(this.nick, reason, handler_cb, error_cb);
  836. };
  837. Occupant.prototype.op = function(reason, handler_cb, error_cb) {
  838. return this.room.op(this.nick, reason, handler_cb, error_cb);
  839. };
  840. Occupant.prototype.deop = function(reason, handler_cb, error_cb) {
  841. return this.room.deop(this.nick, reason, handler_cb, error_cb);
  842. };
  843. Occupant.prototype.modifyAffiliation = function(affiliation, reason, success_cb, error_cb) {
  844. return this.room.modifyAffiliation(this.jid, affiliation, reason, success_cb, error_cb);
  845. };
  846. Occupant.prototype.ban = function(reason, handler_cb, error_cb) {
  847. return this.room.ban(this.jid, reason, handler_cb, error_cb);
  848. };
  849. Occupant.prototype.member = function(reason, handler_cb, error_cb) {
  850. return this.room.member(this.jid, reason, handler_cb, error_cb);
  851. };
  852. Occupant.prototype.revoke = function(reason, handler_cb, error_cb) {
  853. return this.room.revoke(this.jid, reason, handler_cb, error_cb);
  854. };
  855. Occupant.prototype.owner = function(reason, handler_cb, error_cb) {
  856. return this.room.owner(this.jid, reason, handler_cb, error_cb);
  857. };
  858. Occupant.prototype.admin = function(reason, handler_cb, error_cb) {
  859. return this.room.admin(this.jid, reason, handler_cb, error_cb);
  860. };
  861. Occupant.prototype.update = function(data) {
  862. this.nick = data.nick || null;
  863. this.affiliation = data.affiliation || null;
  864. this.role = data.role || null;
  865. this.jid = data.jid || null;
  866. this.status = data.status || null;
  867. this.show = data.show || null;
  868. return this;
  869. };
  870. return Occupant;
  871. })();
  872. }).call(this);
  873. }));