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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* jshint -W117 */
  2. /* a simple MUC connection plugin
  3. * can only handle a single MUC room
  4. */
  5. import {getLogger} from "jitsi-meet-logger";
  6. const logger = getLogger(__filename);
  7. import ChatRoom from "./ChatRoom";
  8. import ConnectionPlugin from "./ConnectionPlugin";
  9. class MucConnectionPlugin extends ConnectionPlugin {
  10. constructor(xmpp) {
  11. super();
  12. this.xmpp = xmpp;
  13. this.rooms = {};
  14. }
  15. init (connection) {
  16. super.init(connection);
  17. // add handlers (just once)
  18. this.connection.addHandler(this.onPresence.bind(this), null,
  19. 'presence', null, null, null, null);
  20. this.connection.addHandler(this.onPresenceUnavailable.bind(this),
  21. null, 'presence', 'unavailable', null);
  22. this.connection.addHandler(this.onPresenceError.bind(this), null,
  23. 'presence', 'error', null);
  24. this.connection.addHandler(this.onMessage.bind(this), null,
  25. 'message', null, null);
  26. this.connection.addHandler(this.onMute.bind(this),
  27. 'http://jitsi.org/jitmeet/audio', 'iq', 'set',null,null);
  28. }
  29. createRoom (jid, password, options, settings) {
  30. const roomJid = Strophe.getBareJidFromJid(jid);
  31. if (this.rooms[roomJid]) {
  32. const errmsg = "You are already in the room!";
  33. logger.error(errmsg);
  34. throw new Error(errmsg);
  35. return;
  36. }
  37. this.rooms[roomJid] = new ChatRoom(this.connection, jid,
  38. password, this.xmpp, options, settings);
  39. return this.rooms[roomJid];
  40. }
  41. doLeave (jid) {
  42. delete this.rooms[jid];
  43. }
  44. onPresence (pres) {
  45. const from = pres.getAttribute('from');
  46. // What is this for? A workaround for something?
  47. if (pres.getAttribute('type')) {
  48. return true;
  49. }
  50. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  51. if(!room)
  52. return;
  53. // Parse status.
  54. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  55. '>status[code="201"]').length) {
  56. room.createNonAnonymousRoom();
  57. }
  58. room.onPresence(pres);
  59. return true;
  60. }
  61. onPresenceUnavailable (pres) {
  62. const from = pres.getAttribute('from');
  63. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  64. if(!room)
  65. return;
  66. room.onPresenceUnavailable(pres, from);
  67. return true;
  68. }
  69. onPresenceError (pres) {
  70. const from = pres.getAttribute('from');
  71. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  72. if(!room)
  73. return;
  74. room.onPresenceError(pres, from);
  75. return true;
  76. }
  77. onMessage (msg) {
  78. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  79. const from = msg.getAttribute('from');
  80. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  81. if(!room)
  82. return;
  83. room.onMessage(msg, from);
  84. return true;
  85. }
  86. setJingleSession (from, session) {
  87. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  88. if(!room)
  89. return;
  90. room.setJingleSession(session);
  91. }
  92. onMute(iq) {
  93. const from = iq.getAttribute('from');
  94. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  95. if(!room)
  96. return;
  97. room.onMute(iq);
  98. return true;
  99. }
  100. }
  101. export default function(XMPP) {
  102. Strophe.addConnectionPlugin('emuc', new MucConnectionPlugin(XMPP));
  103. }