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.

muc.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* jshint -W117 */
  2. /* a simple MUC connection plugin
  3. * can only handle a single MUC room
  4. */
  5. Strophe.addConnectionPlugin('emuc', {
  6. connection: null,
  7. roomjid: null,
  8. myroomjid: null,
  9. members: {},
  10. isOwner: false,
  11. init: function (conn) {
  12. this.connection = conn;
  13. },
  14. doJoin: function (jid, password) {
  15. this.myroomjid = jid;
  16. if (!this.roomjid) {
  17. this.roomjid = Strophe.getBareJidFromJid(jid);
  18. // add handlers (just once)
  19. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  20. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  21. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  22. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  23. }
  24. var join = $pres({to: this.myroomjid }).c('x', {xmlns: 'http://jabber.org/protocol/muc'});
  25. if (password !== undefined) {
  26. join.c('password').t(password);
  27. }
  28. this.connection.send(join);
  29. },
  30. onPresence: function (pres) {
  31. var from = pres.getAttribute('from');
  32. var type = pres.getAttribute('type');
  33. if (type != null) {
  34. return true;
  35. }
  36. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  37. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  38. this.isOwner = true;
  39. var create = $iq({type: 'set', to: this.roomjid})
  40. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  41. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  42. this.connection.send(create); // fire away
  43. }
  44. var member = {};
  45. member.show = $(pres).find('>show').text();
  46. member.status = $(pres).find('>status').text();
  47. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  48. member.affilication = tmp.attr('affiliation');
  49. member.role = tmp.attr('role');
  50. if (from == this.myroomjid) {
  51. $(document).trigger('joined.muc', [from, member]);
  52. } else if (this.members[from] === undefined) {
  53. // new participant
  54. this.members[from] = member;
  55. $(document).trigger('entered.muc', [from, member]);
  56. } else {
  57. console.log('presence change from', from);
  58. }
  59. return true;
  60. },
  61. onPresenceUnavailable: function (pres) {
  62. var from = pres.getAttribute('from');
  63. delete this.members[from];
  64. $(document).trigger('left.muc', [from]);
  65. return true;
  66. },
  67. onPresenceError: function (pres) {
  68. var from = pres.getAttribute('from');
  69. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  70. $(document).trigger('passwordrequired.muc', [from]);
  71. // FIXME: remove once moved to passwordrequired which should reuse dojoin
  72. var ob = this;
  73. window.setTimeout(function () {
  74. var given = window.prompt('Password required');
  75. if (given != null) {
  76. // FIXME: reuse doJoin?
  77. ob.connection.send($pres({to: ob.myroomjid }).c('x', {xmlns: 'http://jabber.org/protocol/muc'}).c('password').t(given));
  78. } else {
  79. // user aborted
  80. }
  81. }, 50);
  82. } else {
  83. console.warn('onPresError ', pres);
  84. }
  85. return true;
  86. },
  87. sendMessage: function (body, nickname) {
  88. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  89. msg.c('body', body).up();
  90. if (nickname) {
  91. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  92. }
  93. this.connection.send(msg);
  94. },
  95. onMessage: function (msg) {
  96. var txt = $(msg).find('>body').text();
  97. // TODO: <subject/>
  98. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  99. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(msg.getAttribute('from'));
  100. if (txt) {
  101. console.log('chat', nick, txt);
  102. updateChatConversation(nick, txt);
  103. }
  104. return true;
  105. },
  106. lockRoom: function (key) {
  107. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  108. var ob = this;
  109. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  110. function (res) {
  111. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  112. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  113. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  114. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  115. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  116. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  117. this.connection.sendIQ(formsubmit,
  118. function (res) {
  119. console.log('set room password');
  120. },
  121. function (err) {
  122. console.warn('setting password failed', err);
  123. }
  124. );
  125. } else {
  126. console.warn('room passwords not supported');
  127. }
  128. },
  129. function (err) {
  130. console.warn('setting password failed', err);
  131. }
  132. );
  133. }
  134. });