Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

strophe.emuc.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. this.connection.addHandler(this.onVisitors.bind(this),
  41. 'jitsi:visitors', 'iq', 'set', null, null);
  42. }
  43. /**
  44. *
  45. * @param jid
  46. * @param password
  47. * @param options
  48. */
  49. createRoom(jid, password, options) {
  50. const roomJid = Strophe.getBareJidFromJid(jid);
  51. if (this.isRoomCreated(roomJid)) {
  52. const errmsg = 'You are already in the room!';
  53. logger.error(errmsg);
  54. throw new Error(errmsg);
  55. }
  56. this.rooms[roomJid] = new ChatRoom(this.connection, jid,
  57. password, this.xmpp, options);
  58. this.eventEmitter.emit(
  59. XMPPEvents.EMUC_ROOM_ADDED, this.rooms[roomJid]);
  60. return this.rooms[roomJid];
  61. }
  62. /**
  63. * Check if a room with the passed JID is already created.
  64. *
  65. * @param {string} roomJid - The JID of the room.
  66. * @returns {boolean}
  67. */
  68. isRoomCreated(roomJid) {
  69. return roomJid in this.rooms;
  70. }
  71. /**
  72. *
  73. * @param jid
  74. */
  75. doLeave(jid) {
  76. this.eventEmitter.emit(
  77. XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
  78. delete this.rooms[jid];
  79. }
  80. /**
  81. *
  82. * @param pres
  83. */
  84. onPresence(pres) {
  85. const from = pres.getAttribute('from');
  86. // What is this for? A workaround for something?
  87. if (pres.getAttribute('type')) {
  88. return true;
  89. }
  90. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  91. if (!room) {
  92. return true;
  93. }
  94. // Parse status.
  95. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
  96. + '>status[code="201"]').length) {
  97. room.createNonAnonymousRoom();
  98. }
  99. room.onPresence(pres);
  100. return true;
  101. }
  102. /**
  103. *
  104. * @param pres
  105. */
  106. onPresenceUnavailable(pres) {
  107. const from = pres.getAttribute('from');
  108. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  109. if (!room) {
  110. return true;
  111. }
  112. room.onPresenceUnavailable(pres, from);
  113. return true;
  114. }
  115. /**
  116. *
  117. * @param pres
  118. */
  119. onPresenceError(pres) {
  120. const from = pres.getAttribute('from');
  121. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  122. if (!room) {
  123. return true;
  124. }
  125. room.onPresenceError(pres, from);
  126. return true;
  127. }
  128. /**
  129. *
  130. * @param msg
  131. */
  132. onMessage(msg) {
  133. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  134. const from = msg.getAttribute('from');
  135. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  136. if (!room) {
  137. return true;
  138. }
  139. room.onMessage(msg, from);
  140. return true;
  141. }
  142. /**
  143. * TODO: Document
  144. * @param iq
  145. */
  146. onMute(iq) {
  147. const from = iq.getAttribute('from');
  148. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  149. // Returning false would result in the listener being deregistered by Strophe
  150. if (!room) {
  151. return true;
  152. }
  153. room.onMute(iq);
  154. return true;
  155. }
  156. /**
  157. * TODO: Document
  158. * @param iq
  159. */
  160. onMuteVideo(iq) {
  161. const from = iq.getAttribute('from');
  162. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  163. // Returning false would result in the listener being deregistered by Strophe
  164. if (!room) {
  165. return true;
  166. }
  167. room.onMuteVideo(iq);
  168. return true;
  169. }
  170. /**
  171. * A visitor IQ is received, pass it to the room.
  172. * @param iq The received iq.
  173. * @returns {boolean}
  174. */
  175. onVisitors(iq) {
  176. const from = iq.getAttribute('from');
  177. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  178. room?.onVisitorIQ(iq);
  179. return true;
  180. }
  181. }