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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* global $ */
  2. import { getLogger } from 'jitsi-meet-logger';
  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.rooms[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. *
  62. * @param jid
  63. */
  64. doLeave(jid) {
  65. this.eventEmitter.emit(
  66. XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
  67. delete this.rooms[jid];
  68. }
  69. /**
  70. *
  71. * @param pres
  72. */
  73. onPresence(pres) {
  74. const from = pres.getAttribute('from');
  75. // What is this for? A workaround for something?
  76. if (pres.getAttribute('type')) {
  77. return true;
  78. }
  79. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  80. if (!room) {
  81. return true;
  82. }
  83. // Parse status.
  84. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
  85. + '>status[code="201"]').length) {
  86. room.createNonAnonymousRoom();
  87. }
  88. room.onPresence(pres);
  89. return true;
  90. }
  91. /**
  92. *
  93. * @param pres
  94. */
  95. onPresenceUnavailable(pres) {
  96. const from = pres.getAttribute('from');
  97. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  98. if (!room) {
  99. return true;
  100. }
  101. room.onPresenceUnavailable(pres, from);
  102. return true;
  103. }
  104. /**
  105. *
  106. * @param pres
  107. */
  108. onPresenceError(pres) {
  109. const from = pres.getAttribute('from');
  110. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  111. if (!room) {
  112. return true;
  113. }
  114. room.onPresenceError(pres, from);
  115. return true;
  116. }
  117. /**
  118. *
  119. * @param msg
  120. */
  121. onMessage(msg) {
  122. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  123. const from = msg.getAttribute('from');
  124. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  125. if (!room) {
  126. return true;
  127. }
  128. room.onMessage(msg, from);
  129. return true;
  130. }
  131. /**
  132. * TODO: Document
  133. * @param iq
  134. */
  135. onMute(iq) {
  136. const from = iq.getAttribute('from');
  137. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  138. // Returning false would result in the listener being deregistered by Strophe
  139. if (!room) {
  140. return true;
  141. }
  142. room.onMute(iq);
  143. return true;
  144. }
  145. /**
  146. * TODO: Document
  147. * @param iq
  148. */
  149. onMuteVideo(iq) {
  150. const from = iq.getAttribute('from');
  151. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  152. // Returning false would result in the listener being deregistered by Strophe
  153. if (!room) {
  154. return true;
  155. }
  156. room.onMuteVideo(iq);
  157. return true;
  158. }
  159. }