Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strophe.emuc.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  39. /**
  40. *
  41. * @param jid
  42. * @param password
  43. * @param options
  44. */
  45. createRoom(jid, password, options) {
  46. const roomJid = Strophe.getBareJidFromJid(jid);
  47. if (this.rooms[roomJid]) {
  48. const errmsg = 'You are already in the room!';
  49. logger.error(errmsg);
  50. throw new Error(errmsg);
  51. }
  52. this.rooms[roomJid] = new ChatRoom(this.connection, jid,
  53. password, this.xmpp, options);
  54. this.eventEmitter.emit(
  55. XMPPEvents.EMUC_ROOM_ADDED, this.rooms[roomJid]);
  56. return this.rooms[roomJid];
  57. }
  58. /**
  59. *
  60. * @param jid
  61. */
  62. doLeave(jid) {
  63. this.eventEmitter.emit(
  64. XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
  65. delete this.rooms[jid];
  66. }
  67. /**
  68. *
  69. * @param pres
  70. */
  71. onPresence(pres) {
  72. const from = pres.getAttribute('from');
  73. // What is this for? A workaround for something?
  74. if (pres.getAttribute('type')) {
  75. return true;
  76. }
  77. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  78. if (!room) {
  79. return true;
  80. }
  81. // Parse status.
  82. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
  83. + '>status[code="201"]').length) {
  84. room.createNonAnonymousRoom();
  85. }
  86. room.onPresence(pres);
  87. return true;
  88. }
  89. /**
  90. *
  91. * @param pres
  92. */
  93. onPresenceUnavailable(pres) {
  94. const from = pres.getAttribute('from');
  95. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  96. if (!room) {
  97. return true;
  98. }
  99. room.onPresenceUnavailable(pres, from);
  100. return true;
  101. }
  102. /**
  103. *
  104. * @param pres
  105. */
  106. onPresenceError(pres) {
  107. const from = pres.getAttribute('from');
  108. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  109. if (!room) {
  110. return true;
  111. }
  112. room.onPresenceError(pres, from);
  113. return true;
  114. }
  115. /**
  116. *
  117. * @param msg
  118. */
  119. onMessage(msg) {
  120. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  121. const from = msg.getAttribute('from');
  122. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  123. if (!room) {
  124. return true;
  125. }
  126. room.onMessage(msg, from);
  127. return true;
  128. }
  129. /**
  130. * TODO: Document
  131. * @param iq
  132. */
  133. onMute(iq) {
  134. const from = iq.getAttribute('from');
  135. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  136. // Returning false would result in the listener being deregistered by Strophe
  137. if (!room) {
  138. return true;
  139. }
  140. room.onMute(iq);
  141. return true;
  142. }
  143. }