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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. } else {
  77. console.warn('onPresError ', pres);
  78. }
  79. return true;
  80. },
  81. sendMessage: function (body, nickname) {
  82. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  83. msg.c('body', body).up();
  84. if (nickname) {
  85. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  86. }
  87. this.connection.send(msg);
  88. },
  89. onMessage: function (msg) {
  90. var txt = $(msg).find('>body').text();
  91. // TODO: <subject/>
  92. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  93. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(msg.getAttribute('from'));
  94. if (txt) {
  95. console.log('chat', nick, txt);
  96. updateChatConversation(nick, txt);
  97. }
  98. return true;
  99. },
  100. lockRoom: function (key) {
  101. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  102. var ob = this;
  103. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  104. function (res) {
  105. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  106. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  107. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  108. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  109. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  110. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  111. this.connection.sendIQ(formsubmit,
  112. function (res) {
  113. console.log('set room password');
  114. },
  115. function (err) {
  116. console.warn('setting password failed', err);
  117. }
  118. );
  119. } else {
  120. console.warn('room passwords not supported');
  121. }
  122. },
  123. function (err) {
  124. console.warn('setting password failed', err);
  125. }
  126. );
  127. }
  128. });