You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.emuc.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* jshint -W117 */
  2. /* a simple MUC connection plugin
  3. * can only handle a single MUC room
  4. */
  5. var ChatRoom = require("./ChatRoom");
  6. module.exports = function(XMPP) {
  7. Strophe.addConnectionPlugin('emuc', {
  8. connection: null,
  9. rooms: {},//map with the rooms
  10. init: function (conn) {
  11. this.connection = conn;
  12. // add handlers (just once)
  13. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, null, null);
  14. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null);
  15. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null);
  16. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null);
  17. },
  18. createRoom: function (jid, password, options) {
  19. var roomJid = Strophe.getBareJidFromJid(jid);
  20. if (this.rooms[roomJid]) {
  21. console.error("You are already in the room!");
  22. return;
  23. }
  24. this.rooms[roomJid] = new ChatRoom(this.connection, jid, password, XMPP, options);
  25. return this.rooms[roomJid];
  26. },
  27. doLeave: function (jid) {
  28. this.rooms[jid].doLeave();
  29. delete this.rooms[jid];
  30. },
  31. onPresence: function (pres) {
  32. var from = pres.getAttribute('from');
  33. // What is this for? A workaround for something?
  34. if (pres.getAttribute('type')) {
  35. return true;
  36. }
  37. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  38. if(!room)
  39. return;
  40. // Parse status.
  41. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  42. room.createNonAnonymousRoom();
  43. }
  44. room.onPresence(pres);
  45. return true;
  46. },
  47. onPresenceUnavailable: function (pres) {
  48. var from = pres.getAttribute('from');
  49. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  50. if(!room)
  51. return;
  52. room.onPresenceUnavailable(pres, from);
  53. return true;
  54. },
  55. onPresenceError: function (pres) {
  56. var from = pres.getAttribute('from');
  57. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  58. if(!room)
  59. return;
  60. room.onPresenceError(pres, from);
  61. return true;
  62. },
  63. onMessage: function (msg) {
  64. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  65. var from = msg.getAttribute('from');
  66. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  67. if(!room)
  68. return;
  69. room.onMessage(msg, from);
  70. return true;
  71. },
  72. setJingleSession: function (from, session) {
  73. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  74. if(!room)
  75. return;
  76. room.setJingleSession(session);
  77. }
  78. });
  79. };