Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

muc.js 20KB

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