Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

strophe.emuc.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* a simple MUC connection plugin
  2. * can only handle a single MUC room
  3. */
  4. /* global $, Strophe */
  5. import { getLogger } from 'jitsi-meet-logger';
  6. const logger = getLogger(__filename);
  7. import ChatRoom from './ChatRoom';
  8. import { ConnectionPluginListenable } from './ConnectionPlugin';
  9. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  10. /**
  11. *
  12. */
  13. class MucConnectionPlugin extends ConnectionPluginListenable {
  14. /**
  15. *
  16. * @param xmpp
  17. */
  18. constructor(xmpp) {
  19. super();
  20. this.xmpp = xmpp;
  21. this.rooms = {};
  22. }
  23. /**
  24. *
  25. * @param connection
  26. */
  27. init(connection) {
  28. super.init(connection);
  29. // add handlers (just once)
  30. this.connection.addHandler(this.onPresence.bind(this), null,
  31. 'presence', null, null, null, null);
  32. this.connection.addHandler(this.onPresenceUnavailable.bind(this),
  33. null, 'presence', 'unavailable', null);
  34. this.connection.addHandler(this.onPresenceError.bind(this), null,
  35. 'presence', 'error', null);
  36. this.connection.addHandler(this.onMessage.bind(this), null,
  37. 'message', null, null);
  38. this.connection.addHandler(this.onMute.bind(this),
  39. 'http://jitsi.org/jitmeet/audio', '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;
  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;
  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;
  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;
  127. }
  128. room.onMessage(msg, from);
  129. return true;
  130. }
  131. /**
  132. *
  133. * @param iq
  134. */
  135. onMute(iq) {
  136. const from = iq.getAttribute('from');
  137. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  138. if (!room) {
  139. return;
  140. }
  141. room.onMute(iq);
  142. return true;
  143. }
  144. }
  145. /**
  146. *
  147. * @param XMPP
  148. */
  149. export default function(XMPP) {
  150. Strophe.addConnectionPlugin('emuc', new MucConnectionPlugin(XMPP));
  151. }