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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. role: null,
  16. init: function (conn) {
  17. this.connection = conn;
  18. },
  19. initPresenceMap: function (myroomjid) {
  20. this.presMap['to'] = myroomjid;
  21. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  22. },
  23. doJoin: function (jid, password) {
  24. this.myroomjid = jid;
  25. console.info("Joined MUC as " + this.myroomjid);
  26. this.initPresenceMap(this.myroomjid);
  27. if (!this.roomjid) {
  28. this.roomjid = Strophe.getBareJidFromJid(jid);
  29. // add handlers (just once)
  30. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  31. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  32. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  33. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  34. }
  35. if (password !== undefined) {
  36. this.presMap['password'] = password;
  37. }
  38. this.sendPresence();
  39. },
  40. doLeave: function() {
  41. console.log("do leave", this.myroomjid);
  42. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  43. this.presMap.length = 0;
  44. this.connection.send(pres);
  45. },
  46. onPresence: function (pres) {
  47. var from = pres.getAttribute('from');
  48. var type = pres.getAttribute('type');
  49. if (type != null) {
  50. return true;
  51. }
  52. // Parse etherpad tag.
  53. var etherpad = $(pres).find('>etherpad');
  54. if (etherpad.length) {
  55. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  56. }
  57. // Parse prezi tag.
  58. var presentation = $(pres).find('>prezi');
  59. if (presentation.length)
  60. {
  61. var url = presentation.attr('url');
  62. var current = presentation.find('>current').text();
  63. console.log('presentation info received from', from, url);
  64. if (this.preziMap[from] == null) {
  65. this.preziMap[from] = url;
  66. $(document).trigger('presentationadded.muc', [from, url, current]);
  67. }
  68. else {
  69. $(document).trigger('gotoslide.muc', [from, url, current]);
  70. }
  71. }
  72. else if (this.preziMap[from] != null) {
  73. var url = this.preziMap[from];
  74. delete this.preziMap[from];
  75. $(document).trigger('presentationremoved.muc', [from, url]);
  76. }
  77. // Parse audio info tag.
  78. var audioMuted = $(pres).find('>audiomuted');
  79. if (audioMuted.length) {
  80. $(document).trigger('audiomuted.muc', [from, audioMuted.text()]);
  81. }
  82. // Parse video info tag.
  83. var videoMuted = $(pres).find('>videomuted');
  84. if (videoMuted.length) {
  85. $(document).trigger('videomuted.muc', [from, videoMuted.text()]);
  86. }
  87. var stats = $(pres).find('>stats');
  88. if(stats.length)
  89. {
  90. var statsObj = {};
  91. Strophe.forEachChild(stats[0], "stat", function (el) {
  92. statsObj[el.getAttribute("name")] = el.getAttribute("value");
  93. });
  94. ConnectionQuality.updateRemoteStats(from, statsObj);
  95. }
  96. // Parse status.
  97. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  98. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  99. this.isOwner = true;
  100. var create = $iq({type: 'set', to: this.roomjid})
  101. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  102. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  103. this.connection.send(create); // fire away
  104. }
  105. // Parse roles.
  106. var member = {};
  107. member.show = $(pres).find('>show').text();
  108. member.status = $(pres).find('>status').text();
  109. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  110. member.affiliation = tmp.attr('affiliation');
  111. member.role = tmp.attr('role');
  112. // Focus recognition
  113. member.jid = tmp.attr('jid');
  114. member.isFocus = false;
  115. if (member.jid && member.jid.indexOf(config.focusUserJid + "/") == 0) {
  116. member.isFocus = true;
  117. }
  118. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  119. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  120. if (from == this.myroomjid) {
  121. if (member.affiliation == 'owner') this.isOwner = true;
  122. if (this.role !== member.role) {
  123. this.role = member.role;
  124. $(document).trigger('local.role.changed.muc', [from, member, pres]);
  125. }
  126. if (!this.joined) {
  127. this.joined = true;
  128. $(document).trigger('joined.muc', [from, member]);
  129. this.list_members.push(from);
  130. }
  131. } else if (this.members[from] === undefined) {
  132. // new participant
  133. this.members[from] = member;
  134. this.list_members.push(from);
  135. $(document).trigger('entered.muc', [from, member, pres]);
  136. } else {
  137. // Presence update for existing participant
  138. // Watch role change:
  139. if (this.members[from].role != member.role) {
  140. this.members[from].role = member.role
  141. $(document).trigger('role.changed.muc', [from, member, pres]);
  142. }
  143. }
  144. // Always trigger presence to update bindings
  145. console.log('presence change from', from, pres);
  146. $(document).trigger('presence.muc', [from, member, pres]);
  147. // Trigger status message update
  148. if (member.status) {
  149. $(document).trigger('presence.status.muc', [from, member, pres]);
  150. }
  151. return true;
  152. },
  153. onPresenceUnavailable: function (pres) {
  154. var from = pres.getAttribute('from');
  155. // Status code 110 indicates that this notification is "self-presence".
  156. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  157. delete this.members[from];
  158. this.list_members.splice(this.list_members.indexOf(from), 1);
  159. $(document).trigger('left.muc', [from]);
  160. }
  161. // If the status code is 110 this means we're leaving and we would like
  162. // to remove everyone else from our view, so we trigger the event.
  163. else if (this.list_members.length > 1) {
  164. for (var i = 0; i < this.list_members.length; i++) {
  165. var member = this.list_members[i];
  166. delete this.members[i];
  167. this.list_members.splice(i, 1);
  168. $(document).trigger('left.muc', member);
  169. }
  170. }
  171. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="307"]').length) {
  172. $(document).trigger('kicked.muc', [from]);
  173. }
  174. return true;
  175. },
  176. onPresenceError: function (pres) {
  177. var from = pres.getAttribute('from');
  178. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  179. $(document).trigger('passwordrequired.muc', [from]);
  180. } else if ($(pres).find(
  181. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  182. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  183. if(toDomain === config.hosts.anonymousdomain) {
  184. // we are connected with anonymous domain and only non anonymous users can create rooms
  185. // we must authorize the user
  186. $(document).trigger('passwordrequired.main');
  187. } else {
  188. console.warn('onPresError ', pres);
  189. messageHandler.openReportDialog(null,
  190. 'Oops! Something went wrong and we couldn`t connect to the conference.',
  191. pres);
  192. }
  193. } else {
  194. console.warn('onPresError ', pres);
  195. messageHandler.openReportDialog(null,
  196. 'Oops! Something went wrong and we couldn`t connect to the conference.',
  197. pres);
  198. }
  199. return true;
  200. },
  201. sendMessage: function (body, nickname) {
  202. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  203. msg.c('body', body).up();
  204. if (nickname) {
  205. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  206. }
  207. this.connection.send(msg);
  208. if(APIConnector.isEnabled() && APIConnector.isEventEnabled("outgoingMessage"))
  209. {
  210. APIConnector.triggerEvent("outgoingMessage", {"message": body});
  211. }
  212. },
  213. setSubject: function (subject){
  214. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  215. msg.c('subject', subject);
  216. this.connection.send(msg);
  217. console.log("topic changed to " + subject);
  218. },
  219. onMessage: function (msg) {
  220. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  221. var from = msg.getAttribute('from');
  222. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  223. var txt = $(msg).find('>body').text();
  224. var type = msg.getAttribute("type");
  225. if(type == "error")
  226. {
  227. Chat.chatAddError($(msg).find('>text').text(), txt);
  228. return true;
  229. }
  230. var subject = $(msg).find('>subject');
  231. if(subject.length)
  232. {
  233. var subjectText = subject.text();
  234. if(subjectText || subjectText == "") {
  235. Chat.chatSetSubject(subjectText);
  236. console.log("Subject is changed to " + subjectText);
  237. }
  238. }
  239. if (txt) {
  240. console.log('chat', nick, txt);
  241. Chat.updateChatConversation(from, nick, txt);
  242. if(APIConnector.isEnabled() && APIConnector.isEventEnabled("incomingMessage"))
  243. {
  244. if(from != this.myroomjid)
  245. APIConnector.triggerEvent("incomingMessage",
  246. {"from": from, "nick": nick, "message": txt});
  247. }
  248. }
  249. return true;
  250. },
  251. lockRoom: function (key) {
  252. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  253. var ob = this;
  254. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  255. function (res) {
  256. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  257. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  258. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  259. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  260. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  261. // Fixes a bug in prosody 0.9.+ https://code.google.com/p/lxmppd/issues/detail?id=373
  262. formsubmit.c('field', {'var': 'muc#roomconfig_whois'}).c('value').t('anyone').up().up();
  263. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  264. this.connection.sendIQ(formsubmit,
  265. function (res) {
  266. // password is required
  267. if (sharedKey)
  268. {
  269. console.log('set room password');
  270. Toolbar.lockLockButton();
  271. }
  272. else
  273. {
  274. console.log('removed room password');
  275. Toolbar.unlockLockButton();
  276. }
  277. },
  278. function (err) {
  279. console.warn('setting password failed', err);
  280. messageHandler.showError('Lock failed',
  281. 'Failed to lock conference.',
  282. err);
  283. setSharedKey('');
  284. }
  285. );
  286. } else {
  287. console.warn('room passwords not supported');
  288. messageHandler.showError('Warning',
  289. 'Room passwords are currently not supported.');
  290. setSharedKey('');
  291. }
  292. },
  293. function (err) {
  294. console.warn('setting password failed', err);
  295. messageHandler.showError('Lock failed',
  296. 'Failed to lock conference.',
  297. err);
  298. setSharedKey('');
  299. }
  300. );
  301. },
  302. kick: function (jid) {
  303. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  304. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  305. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  306. .c('reason').t('You have been kicked.').up().up().up();
  307. this.connection.sendIQ(
  308. kickIQ,
  309. function (result) {
  310. console.log('Kick participant with jid: ', jid, result);
  311. },
  312. function (error) {
  313. console.log('Kick participant error: ', error);
  314. });
  315. },
  316. sendPresence: function () {
  317. var pres = $pres({to: this.presMap['to'] });
  318. pres.c('x', {xmlns: this.presMap['xns']});
  319. if (this.presMap['password']) {
  320. pres.c('password').t(this.presMap['password']).up();
  321. }
  322. pres.up();
  323. // Send XEP-0115 'c' stanza that contains our capabilities info
  324. if (connection.caps) {
  325. connection.caps.node = config.clientNode;
  326. pres.c('c', connection.caps.generateCapsAttrs()).up();
  327. }
  328. if(this.presMap['bridgeIsDown']) {
  329. pres.c('bridgeIsDown').up();
  330. }
  331. if (this.presMap['displayName']) {
  332. // XEP-0172
  333. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
  334. .t(this.presMap['displayName']).up();
  335. }
  336. if (this.presMap['audions']) {
  337. pres.c('audiomuted', {xmlns: this.presMap['audions']})
  338. .t(this.presMap['audiomuted']).up();
  339. }
  340. if (this.presMap['videons']) {
  341. pres.c('videomuted', {xmlns: this.presMap['videons']})
  342. .t(this.presMap['videomuted']).up();
  343. }
  344. if(this.presMap['statsns'])
  345. {
  346. var stats = pres.c('stats', {xmlns: this.presMap['statsns']});
  347. for(var stat in this.presMap["stats"])
  348. if(this.presMap["stats"][stat] != null)
  349. stats.c("stat",{name: stat, value: this.presMap["stats"][stat]}).up();
  350. pres.up();
  351. }
  352. if (this.presMap['prezins']) {
  353. pres.c('prezi',
  354. {xmlns: this.presMap['prezins'],
  355. 'url': this.presMap['preziurl']})
  356. .c('current').t(this.presMap['prezicurrent']).up().up();
  357. }
  358. if (this.presMap['etherpadns']) {
  359. pres.c('etherpad', {xmlns: this.presMap['etherpadns']})
  360. .t(this.presMap['etherpadname']).up();
  361. }
  362. if (this.presMap['medians'])
  363. {
  364. pres.c('media', {xmlns: this.presMap['medians']});
  365. var sourceNumber = 0;
  366. Object.keys(this.presMap).forEach(function (key) {
  367. if (key.indexOf('source') >= 0) {
  368. sourceNumber++;
  369. }
  370. });
  371. if (sourceNumber > 0)
  372. for (var i = 1; i <= sourceNumber/3; i ++) {
  373. pres.c('source',
  374. {type: this.presMap['source' + i + '_type'],
  375. ssrc: this.presMap['source' + i + '_ssrc'],
  376. direction: this.presMap['source'+ i + '_direction']
  377. || 'sendrecv' }
  378. ).up();
  379. }
  380. }
  381. pres.up();
  382. connection.send(pres);
  383. },
  384. addDisplayNameToPresence: function (displayName) {
  385. this.presMap['displayName'] = displayName;
  386. },
  387. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  388. if (!this.presMap['medians'])
  389. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  390. this.presMap['source' + sourceNumber + '_type'] = mtype;
  391. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  392. this.presMap['source' + sourceNumber + '_direction'] = direction;
  393. },
  394. clearPresenceMedia: function () {
  395. var self = this;
  396. Object.keys(this.presMap).forEach( function(key) {
  397. if(key.indexOf('source') != -1) {
  398. delete self.presMap[key];
  399. }
  400. });
  401. },
  402. addPreziToPresence: function (url, currentSlide) {
  403. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  404. this.presMap['preziurl'] = url;
  405. this.presMap['prezicurrent'] = currentSlide;
  406. },
  407. removePreziFromPresence: function () {
  408. delete this.presMap['prezins'];
  409. delete this.presMap['preziurl'];
  410. delete this.presMap['prezicurrent'];
  411. },
  412. addCurrentSlideToPresence: function (currentSlide) {
  413. this.presMap['prezicurrent'] = currentSlide;
  414. },
  415. getPrezi: function (roomjid) {
  416. return this.preziMap[roomjid];
  417. },
  418. addEtherpadToPresence: function(etherpadName) {
  419. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  420. this.presMap['etherpadname'] = etherpadName;
  421. },
  422. addAudioInfoToPresence: function(isMuted) {
  423. this.presMap['audions'] = 'http://jitsi.org/jitmeet/audio';
  424. this.presMap['audiomuted'] = isMuted.toString();
  425. },
  426. addVideoInfoToPresence: function(isMuted) {
  427. this.presMap['videons'] = 'http://jitsi.org/jitmeet/video';
  428. this.presMap['videomuted'] = isMuted.toString();
  429. },
  430. addConnectionInfoToPresence: function(stats) {
  431. this.presMap['statsns'] = 'http://jitsi.org/jitmeet/stats';
  432. this.presMap['stats'] = stats;
  433. },
  434. findJidFromResource: function(resourceJid) {
  435. var peerJid = null;
  436. Object.keys(this.members).some(function (jid) {
  437. peerJid = jid;
  438. return Strophe.getResourceFromJid(jid) === resourceJid;
  439. });
  440. return peerJid;
  441. },
  442. addBridgeIsDownToPresence: function() {
  443. this.presMap['bridgeIsDown'] = true;
  444. },
  445. isModerator: function() {
  446. return this.role === 'moderator';
  447. }
  448. });