Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

muc.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. list_members: [], // so we can elect a new focus
  11. presMap: {},
  12. preziMap: {},
  13. joined: false,
  14. isOwner: false,
  15. init: function (conn) {
  16. this.connection = conn;
  17. },
  18. initPresenceMap: function (myroomjid) {
  19. this.presMap['to'] = myroomjid;
  20. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  21. },
  22. doJoin: function (jid, password) {
  23. this.myroomjid = jid;
  24. console.info("Joined MUC as " + this.myroomjid);
  25. this.initPresenceMap(this.myroomjid);
  26. if (!this.roomjid) {
  27. this.roomjid = Strophe.getBareJidFromJid(jid);
  28. // add handlers (just once)
  29. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  30. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  31. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  32. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  33. }
  34. if (password !== undefined) {
  35. this.presMap['password'] = password;
  36. }
  37. this.sendPresence();
  38. },
  39. doLeave: function() {
  40. console.log("do leave", this.myroomjid);
  41. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  42. this.presMap.length = 0;
  43. this.connection.send(pres);
  44. },
  45. onPresence: function (pres) {
  46. var from = pres.getAttribute('from');
  47. var type = pres.getAttribute('type');
  48. if (type != null) {
  49. return true;
  50. }
  51. // Parse etherpad tag.
  52. var etherpad = $(pres).find('>etherpad');
  53. if (etherpad.length) {
  54. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  55. }
  56. // Parse prezi tag.
  57. var presentation = $(pres).find('>prezi');
  58. if (presentation.length)
  59. {
  60. var url = presentation.attr('url');
  61. var current = presentation.find('>current').text();
  62. console.log('presentation info received from', from, url);
  63. if (this.preziMap[from] == null) {
  64. this.preziMap[from] = url;
  65. $(document).trigger('presentationadded.muc', [from, url, current]);
  66. }
  67. else {
  68. $(document).trigger('gotoslide.muc', [from, url, current]);
  69. }
  70. }
  71. else if (this.preziMap[from] != null) {
  72. var url = this.preziMap[from];
  73. delete this.preziMap[from];
  74. $(document).trigger('presentationremoved.muc', [from, url]);
  75. }
  76. // Parse audio info tag.
  77. var audioMuted = $(pres).find('>audiomuted');
  78. if (audioMuted.length) {
  79. $(document).trigger('audiomuted.muc', [from, audioMuted.text()]);
  80. }
  81. // Parse video info tag.
  82. var videoMuted = $(pres).find('>videomuted');
  83. if (videoMuted.length) {
  84. $(document).trigger('videomuted.muc', [from, videoMuted.text()]);
  85. }
  86. // Parse status.
  87. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  88. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  89. this.isOwner = true;
  90. var create = $iq({type: 'set', to: this.roomjid})
  91. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  92. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  93. this.connection.send(create); // fire away
  94. }
  95. // Parse roles.
  96. var member = {};
  97. member.show = $(pres).find('>show').text();
  98. member.status = $(pres).find('>status').text();
  99. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  100. member.affiliation = tmp.attr('affiliation');
  101. member.role = tmp.attr('role');
  102. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  103. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  104. if (from == this.myroomjid) {
  105. if (member.affiliation == 'owner') this.isOwner = true;
  106. if (!this.joined) {
  107. this.joined = true;
  108. $(document).trigger('joined.muc', [from, member]);
  109. this.list_members.push(from);
  110. }
  111. } else if (this.members[from] === undefined) {
  112. // new participant
  113. this.members[from] = member;
  114. this.list_members.push(from);
  115. $(document).trigger('entered.muc', [from, member, pres]);
  116. }
  117. // Always trigger presence to update bindings
  118. console.log('presence change from', from);
  119. $(document).trigger('presence.muc', [from, member, pres]);
  120. return true;
  121. },
  122. onPresenceUnavailable: function (pres) {
  123. var from = pres.getAttribute('from');
  124. // Status code 110 indicates that this notification is "self-presence".
  125. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  126. delete this.members[from];
  127. this.list_members.splice(this.list_members.indexOf(from), 1);
  128. $(document).trigger('left.muc', [from]);
  129. }
  130. // If the status code is 110 this means we're leaving and we would like
  131. // to remove everyone else from our view, so we trigger the event.
  132. else if (this.list_members.length > 1) {
  133. for (var i = 0; i < this.list_members.length; i++) {
  134. var member = this.list_members[i];
  135. delete this.members[i];
  136. this.list_members.splice(i, 1);
  137. $(document).trigger('left.muc', member);
  138. }
  139. }
  140. return true;
  141. },
  142. onPresenceError: function (pres) {
  143. var from = pres.getAttribute('from');
  144. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  145. $(document).trigger('passwordrequired.muc', [from]);
  146. } else {
  147. console.warn('onPresError ', pres);
  148. }
  149. return true;
  150. },
  151. sendMessage: function (body, nickname) {
  152. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  153. msg.c('body', body).up();
  154. if (nickname) {
  155. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  156. }
  157. this.connection.send(msg);
  158. },
  159. onMessage: function (msg) {
  160. var txt = $(msg).find('>body').text();
  161. // TODO: <subject/>
  162. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  163. var from = msg.getAttribute('from');
  164. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  165. if (txt) {
  166. console.log('chat', nick, txt);
  167. Chat.updateChatConversation(from, nick, txt);
  168. }
  169. return true;
  170. },
  171. lockRoom: function (key) {
  172. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  173. var ob = this;
  174. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  175. function (res) {
  176. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  177. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  178. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  179. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  180. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  181. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  182. this.connection.sendIQ(formsubmit,
  183. function (res) {
  184. console.log('set room password');
  185. },
  186. function (err) {
  187. console.warn('setting password failed', err);
  188. }
  189. );
  190. } else {
  191. console.warn('room passwords not supported');
  192. }
  193. },
  194. function (err) {
  195. console.warn('setting password failed', err);
  196. }
  197. );
  198. },
  199. kick: function (jid) {
  200. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  201. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  202. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  203. .c('reason').t('You have been kicked.').up().up().up();
  204. this.connection.sendIQ(
  205. kickIQ,
  206. function (result) {
  207. console.log('Kick participant with jid: ', jid, result);
  208. },
  209. function (error) {
  210. console.log('Kick participant error: ', error);
  211. });
  212. },
  213. sendPresence: function () {
  214. var pres = $pres({to: this.presMap['to'] });
  215. pres.c('x', {xmlns: this.presMap['xns']});
  216. if (this.presMap['password']) {
  217. pres.c('password').t(this.presMap['password']).up();
  218. }
  219. pres.up();
  220. if (this.presMap['displayName']) {
  221. // XEP-0172
  222. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
  223. .t(this.presMap['displayName']).up();
  224. }
  225. if (this.presMap['audions']) {
  226. pres.c('audiomuted', {xmlns: this.presMap['audions']})
  227. .t(this.presMap['audiomuted']).up();
  228. }
  229. if (this.presMap['videons']) {
  230. pres.c('videomuted', {xmlns: this.presMap['videons']})
  231. .t(this.presMap['videomuted']).up();
  232. }
  233. if (this.presMap['prezins']) {
  234. pres.c('prezi',
  235. {xmlns: this.presMap['prezins'],
  236. 'url': this.presMap['preziurl']})
  237. .c('current').t(this.presMap['prezicurrent']).up().up();
  238. }
  239. if (this.presMap['etherpadns']) {
  240. pres.c('etherpad', {xmlns: this.presMap['etherpadns']})
  241. .t(this.presMap['etherpadname']).up();
  242. }
  243. if (this.presMap['medians'])
  244. {
  245. pres.c('media', {xmlns: this.presMap['medians']});
  246. var sourceNumber = 0;
  247. Object.keys(this.presMap).forEach(function (key) {
  248. if (key.indexOf('source') >= 0) {
  249. sourceNumber++;
  250. }
  251. });
  252. if (sourceNumber > 0)
  253. for (var i = 1; i <= sourceNumber/3; i ++) {
  254. pres.c('source',
  255. {type: this.presMap['source' + i + '_type'],
  256. ssrc: this.presMap['source' + i + '_ssrc'],
  257. direction: this.presMap['source'+ i + '_direction']
  258. || 'sendrecv' }
  259. ).up();
  260. }
  261. }
  262. pres.up();
  263. connection.send(pres);
  264. },
  265. addDisplayNameToPresence: function (displayName) {
  266. this.presMap['displayName'] = displayName;
  267. },
  268. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  269. if (!this.presMap['medians'])
  270. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  271. this.presMap['source' + sourceNumber + '_type'] = mtype;
  272. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  273. this.presMap['source' + sourceNumber + '_direction'] = direction;
  274. },
  275. clearPresenceMedia: function () {
  276. var self = this;
  277. Object.keys(this.presMap).forEach( function(key) {
  278. if(key.indexOf('source') != -1) {
  279. delete self.presMap[key];
  280. }
  281. });
  282. },
  283. addPreziToPresence: function (url, currentSlide) {
  284. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  285. this.presMap['preziurl'] = url;
  286. this.presMap['prezicurrent'] = currentSlide;
  287. },
  288. removePreziFromPresence: function () {
  289. delete this.presMap['prezins'];
  290. delete this.presMap['preziurl'];
  291. delete this.presMap['prezicurrent'];
  292. },
  293. addCurrentSlideToPresence: function (currentSlide) {
  294. this.presMap['prezicurrent'] = currentSlide;
  295. },
  296. getPrezi: function (roomjid) {
  297. return this.preziMap[roomjid];
  298. },
  299. addEtherpadToPresence: function(etherpadName) {
  300. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  301. this.presMap['etherpadname'] = etherpadName;
  302. },
  303. addAudioInfoToPresence: function(isMuted) {
  304. this.presMap['audions'] = 'http://jitsi.org/jitmeet/audio';
  305. this.presMap['audiomuted'] = isMuted.toString();
  306. },
  307. addVideoInfoToPresence: function(isMuted) {
  308. this.presMap['videons'] = 'http://jitsi.org/jitmeet/video';
  309. this.presMap['videomuted'] = isMuted.toString();
  310. }
  311. });