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

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