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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { getLogger } from '@jitsi/logger';
  2. import $ from 'jquery';
  3. import { Strophe } from 'strophe.js';
  4. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  5. import ChatRoom from './ChatRoom';
  6. import { ConnectionPluginListenable } from './ConnectionPlugin';
  7. const logger = getLogger(__filename);
  8. /**
  9. * MUC connection plugin.
  10. */
  11. export default class MucConnectionPlugin extends ConnectionPluginListenable {
  12. /**
  13. *
  14. * @param xmpp
  15. */
  16. constructor(xmpp) {
  17. super();
  18. this.xmpp = xmpp;
  19. this.rooms = {};
  20. }
  21. /**
  22. *
  23. * @param connection
  24. */
  25. init(connection) {
  26. super.init(connection);
  27. // add handlers (just once)
  28. this.connection.addHandler(this.onPresence.bind(this), null,
  29. 'presence', null, null, null, null);
  30. this.connection.addHandler(this.onPresenceUnavailable.bind(this),
  31. null, 'presence', 'unavailable', null);
  32. this.connection.addHandler(this.onPresenceError.bind(this), null,
  33. 'presence', 'error', null);
  34. this.connection.addHandler(this.onMessage.bind(this), null,
  35. 'message', null, null);
  36. this.connection.addHandler(this.onMute.bind(this),
  37. 'http://jitsi.org/jitmeet/audio', 'iq', 'set', null, null);
  38. this.connection.addHandler(this.onMuteVideo.bind(this),
  39. 'http://jitsi.org/jitmeet/video', 'iq', 'set', null, null);
  40. }
  41. /**
  42. *
  43. * @param jid
  44. * @param password
  45. * @param options
  46. */
  47. createRoom(jid, password, options) {
  48. const roomJid = Strophe.getBareJidFromJid(jid);
  49. if (this.isRoomCreated(roomJid)) {
  50. const errmsg = 'You are already in the room!';
  51. logger.error(errmsg);
  52. throw new Error(errmsg);
  53. }
  54. this.rooms[roomJid] = new ChatRoom(this.connection, jid,
  55. password, this.xmpp, options);
  56. this.eventEmitter.emit(
  57. XMPPEvents.EMUC_ROOM_ADDED, this.rooms[roomJid]);
  58. return this.rooms[roomJid];
  59. }
  60. /**
  61. * Check if a room with the passed JID is already created.
  62. *
  63. * @param {string} roomJid - The JID of the room.
  64. * @returns {boolean}
  65. */
  66. isRoomCreated(roomJid) {
  67. return roomJid in this.rooms;
  68. }
  69. /**
  70. *
  71. * @param jid
  72. */
  73. doLeave(jid) {
  74. this.eventEmitter.emit(
  75. XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
  76. delete this.rooms[jid];
  77. }
  78. /**
  79. *
  80. * @param pres
  81. */
  82. onPresence(pres) {
  83. const from = pres.getAttribute('from');
  84. // What is this for? A workaround for something?
  85. if (pres.getAttribute('type')) {
  86. return true;
  87. }
  88. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  89. if (!room) {
  90. return true;
  91. }
  92. // Parse status.
  93. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
  94. + '>status[code="201"]').length) {
  95. room.createNonAnonymousRoom();
  96. }
  97. room.onPresence(pres);
  98. return true;
  99. }
  100. /**
  101. *
  102. * @param pres
  103. */
  104. onPresenceUnavailable(pres) {
  105. const from = pres.getAttribute('from');
  106. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  107. if (!room) {
  108. return true;
  109. }
  110. room.onPresenceUnavailable(pres, from);
  111. return true;
  112. }
  113. /**
  114. *
  115. * @param pres
  116. */
  117. onPresenceError(pres) {
  118. const from = pres.getAttribute('from');
  119. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  120. if (!room) {
  121. return true;
  122. }
  123. room.onPresenceError(pres, from);
  124. return true;
  125. }
  126. /**
  127. *
  128. * @param msg
  129. */
  130. onMessage(msg) {
  131. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  132. const from = msg.getAttribute('from');
  133. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  134. if (!room) {
  135. return true;
  136. }
  137. room.onMessage(msg, from);
  138. return true;
  139. }
  140. /**
  141. * TODO: Document
  142. * @param iq
  143. */
  144. onMute(iq) {
  145. const from = iq.getAttribute('from');
  146. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  147. // Returning false would result in the listener being deregistered by Strophe
  148. if (!room) {
  149. return true;
  150. }
  151. room.onMute(iq);
  152. return true;
  153. }
  154. /**
  155. * TODO: Document
  156. * @param iq
  157. */
  158. onMuteVideo(iq) {
  159. const from = iq.getAttribute('from');
  160. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  161. // Returning false would result in the listener being deregistered by Strophe
  162. if (!room) {
  163. return true;
  164. }
  165. room.onMuteVideo(iq);
  166. return true;
  167. }
  168. }