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

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