Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

strophe.emuc.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* jshint -W117 */
  2. /* a simple MUC connection plugin
  3. * can only handle a single MUC room
  4. */
  5. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  6. var Moderator = require("./moderator");
  7. var RTC = require("../RTC/RTC");
  8. var EventEmitter = require("events");
  9. var parser = {
  10. packet2JSON: function (packet, nodes) {
  11. var self = this;
  12. $(packet).children().each(function (index) {
  13. var tagName = $(this).prop("tagName");
  14. var node = {}
  15. node["tagName"] = tagName;
  16. node.attributes = {};
  17. $($(this)[0].attributes).each(function( index, attr ) {
  18. node.attributes[ attr.name ] = attr.value;
  19. } );
  20. var text = Strophe.getText($(this)[0]);
  21. if(text)
  22. node.value = text;
  23. node.children = [];
  24. nodes.push(node);
  25. self.packet2JSON($(this), node.children);
  26. })
  27. },
  28. JSON2packet: function (nodes, packet) {
  29. for(var i = 0; i < nodes.length; i++)
  30. {
  31. var node = nodes[i];
  32. if(!node || node === null){
  33. continue;
  34. }
  35. packet.c(node.tagName, node.attributes);
  36. if(node.value)
  37. packet.t(node.value);
  38. if(node.children)
  39. this.JSON2packet(node.children, packet);
  40. packet.up();
  41. }
  42. packet.up();
  43. }
  44. };
  45. function ChatRoom(connection, jid, password, XMPP, eventEmitter)
  46. {
  47. this.eventEmitter = eventEmitter;
  48. this.roomEmitter = new EventEmitter();
  49. this.xmpp = XMPP;
  50. this.connection = connection;
  51. this.roomjid = Strophe.getBareJidFromJid(jid);
  52. this.myroomjid = jid;
  53. this.password = password;
  54. console.info("Joined MUC as " + this.myroomjid);
  55. this.members = {};
  56. this.presMap = {};
  57. this.presHandlers = {};
  58. this.joined = false;
  59. this.role = null;
  60. this.focusMucJid = null;
  61. this.bridgeIsDown = false;
  62. this.moderator = new Moderator(this.roomjid, this.xmpp, eventEmitter);
  63. this.initPresenceMap();
  64. this.readyToJoin = false;
  65. this.joinRequested = false;
  66. var self = this;
  67. this.moderator.allocateConferenceFocus(function()
  68. {
  69. self.readyToJoin = true;
  70. if(self.joinRequested)
  71. {
  72. self.join();
  73. }
  74. });
  75. }
  76. ChatRoom.prototype.initPresenceMap = function () {
  77. this.presMap['to'] = this.myroomjid;
  78. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  79. this.presMap["nodes"] = [];
  80. if (RTC.localAudio && RTC.localAudio.isMuted()) {
  81. this.nodes.push({
  82. tagName: "audiomuted",
  83. attributes: {xmlns: "http://jitsi.org/jitmeet/audio"},
  84. value: "true"});
  85. }
  86. if (RTC.localVideo && RTC.localVideo.isMuted()) {
  87. this.nodes.push({
  88. tagName: "videomuted",
  89. attributes: {xmlns: "http://jitsi.org/jitmeet/video"},
  90. value: "true"});
  91. }
  92. this.presMap["nodes"].push( {
  93. "tagName": "user-agent",
  94. "value": navigator.userAgent,
  95. "attributes": {xmlns: 'http://jitsi.org/jitmeet/user-agent'}
  96. });
  97. };
  98. ChatRoom.prototype.join = function (password) {
  99. if(password)
  100. this.password = password;
  101. if(!this.readyToJoin)
  102. {
  103. this.joinRequested = true;
  104. return;
  105. }
  106. this.joinRequested = false;
  107. this.sendPresence();
  108. }
  109. ChatRoom.prototype.sendPresence = function () {
  110. if (!this.presMap['to']) {
  111. // Too early to send presence - not initialized
  112. return;
  113. }
  114. var pres = $pres({to: this.presMap['to'] });
  115. pres.c('x', {xmlns: this.presMap['xns']});
  116. if (this.password) {
  117. pres.c('password').t(this.password).up();
  118. }
  119. pres.up();
  120. // Send XEP-0115 'c' stanza that contains our capabilities info
  121. if (this.connection.caps) {
  122. this.connection.caps.node = this.xmpp.options.clientNode;
  123. pres.c('c', this.connection.caps.generateCapsAttrs()).up();
  124. }
  125. parser.JSON2packet(this.presMap.nodes, pres);
  126. this.connection.send(pres);
  127. };
  128. ChatRoom.prototype.doLeave = function () {
  129. console.log("do leave", this.myroomjid);
  130. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  131. this.presMap.length = 0;
  132. this.connection.send(pres);
  133. };
  134. ChatRoom.prototype.createNonAnonymousRoom = function () {
  135. // http://xmpp.org/extensions/xep-0045.html#createroom-reserved
  136. var getForm = $iq({type: 'get', to: this.roomjid})
  137. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  138. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  139. var self = this;
  140. this.connection.sendIQ(getForm, function (form) {
  141. if (!$(form).find(
  142. '>query>x[xmlns="jabber:x:data"]' +
  143. '>field[var="muc#roomconfig_whois"]').length) {
  144. console.error('non-anonymous rooms not supported');
  145. return;
  146. }
  147. var formSubmit = $iq({to: this.roomjid, type: 'set'})
  148. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  149. formSubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  150. formSubmit.c('field', {'var': 'FORM_TYPE'})
  151. .c('value')
  152. .t('http://jabber.org/protocol/muc#roomconfig').up().up();
  153. formSubmit.c('field', {'var': 'muc#roomconfig_whois'})
  154. .c('value').t('anyone').up().up();
  155. self.connection.sendIQ(formSubmit);
  156. }, function (error) {
  157. console.error("Error getting room configuration form");
  158. });
  159. };
  160. ChatRoom.prototype.onPresence = function (pres) {
  161. var from = pres.getAttribute('from');
  162. // Parse roles.
  163. var member = {};
  164. member.show = $(pres).find('>show').text();
  165. member.status = $(pres).find('>status').text();
  166. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  167. member.affiliation = tmp.attr('affiliation');
  168. member.role = tmp.attr('role');
  169. // Focus recognition
  170. member.jid = tmp.attr('jid');
  171. member.isFocus = false;
  172. if (member.jid
  173. && member.jid.indexOf(this.moderator.getFocusUserJid() + "/") == 0) {
  174. member.isFocus = true;
  175. }
  176. $(pres).find(">x").remove();
  177. var nodes = [];
  178. parser.packet2JSON(pres, nodes);
  179. for(var i = 0; i < nodes.length; i++)
  180. {
  181. var node = nodes[i];
  182. switch(node.tagName)
  183. {
  184. case "nick":
  185. member.nick = node.value;
  186. if(!member.isFocus) {
  187. var displayName = !this.xmpp.options.displayJids
  188. ? member.nick : Strophe.getResourceFromJid(from);
  189. if (displayName && displayName.length > 0) {
  190. this.eventEmitter.emit(XMPPEvents.DISPLAY_NAME_CHANGED, from, displayName);
  191. }
  192. console.info("Display name: " + displayName, pres);
  193. }
  194. break;
  195. case "userId":
  196. member.id = node.value;
  197. break;
  198. case "email":
  199. member.email = node.value;
  200. break;
  201. case "bridgeIsDown":
  202. if(!this.bridgeIsDown) {
  203. this.bridgeIsDown = true;
  204. this.eventEmitter.emit(XMPPEvents.BRIDGE_DOWN);
  205. }
  206. break;
  207. default :
  208. this.processNode(node, from);
  209. }
  210. }
  211. if (from == this.myroomjid) {
  212. if (member.affiliation == 'owner')
  213. if (this.role !== member.role) {
  214. this.role = member.role;
  215. this.eventEmitter.emit(XMPPEvents.LOCAL_ROLE_CHANGED,
  216. member, this.isModerator());
  217. }
  218. if (!this.joined) {
  219. this.joined = true;
  220. this.eventEmitter.emit(XMPPEvents.MUC_JOINED, from, member);
  221. }
  222. } else if (this.members[from] === undefined) {
  223. // new participant
  224. this.members[from] = member;
  225. console.log('entered', from, member);
  226. if (member.isFocus) {
  227. this.focusMucJid = from;
  228. console.info("Ignore focus: " + from + ", real JID: " + member.jid);
  229. }
  230. else {
  231. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_JOINED, from, member.id || member.email, member.nick);
  232. }
  233. } else {
  234. // Presence update for existing participant
  235. // Watch role change:
  236. if (this.members[from].role != member.role) {
  237. this.members[from].role = member.role;
  238. this.eventEmitter.emit(XMPPEvents.MUC_ROLE_CHANGED,
  239. member.role, member.nick);
  240. }
  241. }
  242. if(!member.isFocus)
  243. this.eventEmitter.emit(XMPPEvents.USER_ID_CHANGED, from, member.id || member.email);
  244. // Trigger status message update
  245. if (member.status) {
  246. this.eventEmitter.emit(XMPPEvents.PRESENCE_STATUS, from, member);
  247. }
  248. };
  249. ChatRoom.prototype.processNode = function (node, from) {
  250. if(this.presHandlers[node.tagName])
  251. this.presHandlers[node.tagName](node, from);
  252. };
  253. ChatRoom.prototype.sendMessage = function (body, nickname) {
  254. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  255. msg.c('body', body).up();
  256. if (nickname) {
  257. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  258. }
  259. this.connection.send(msg);
  260. this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, body);
  261. };
  262. ChatRoom.prototype.setSubject = function (subject) {
  263. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  264. msg.c('subject', subject);
  265. this.connection.send(msg);
  266. console.log("topic changed to " + subject);
  267. };
  268. ChatRoom.prototype.onParticipantLeft = function (jid) {
  269. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_LEFT, jid);
  270. this.moderator.onMucMemberLeft(jid);
  271. };
  272. ChatRoom.prototype.onPresenceUnavailable = function (pres, from) {
  273. // room destroyed ?
  274. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  275. '>destroy').length) {
  276. var reason;
  277. var reasonSelect = $(pres).find(
  278. '>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  279. '>destroy>reason');
  280. if (reasonSelect.length) {
  281. reason = reasonSelect.text();
  282. }
  283. this.xmpp.disposeConference(false);
  284. this.eventEmitter.emit(XMPPEvents.MUC_DESTROYED, reason);
  285. delete this.connection.emuc.rooms[Strophe.getBareJidFromJid(jid)];
  286. return true;
  287. }
  288. // Status code 110 indicates that this notification is "self-presence".
  289. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  290. delete this.members[from];
  291. this.onParticipantLeft(from);
  292. }
  293. // If the status code is 110 this means we're leaving and we would like
  294. // to remove everyone else from our view, so we trigger the event.
  295. else if (Object.keys(this.members).length > 1) {
  296. for (var i in this.members) {
  297. var member = this.members[i];
  298. delete this.members[i];
  299. this.onParticipantLeft(member);
  300. }
  301. }
  302. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="307"]').length) {
  303. if (this.myroomjid === from) {
  304. this.xmpp.disposeConference(false);
  305. this.eventEmitter.emit(XMPPEvents.KICKED);
  306. }
  307. }
  308. };
  309. ChatRoom.prototype.onMessage = function (msg, from) {
  310. var nick =
  311. $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]')
  312. .text() ||
  313. Strophe.getResourceFromJid(from);
  314. var txt = $(msg).find('>body').text();
  315. var type = msg.getAttribute("type");
  316. if (type == "error") {
  317. this.eventEmitter.emit(XMPPEvents.CHAT_ERROR_RECEIVED,
  318. $(msg).find('>text').text(), txt);
  319. return true;
  320. }
  321. var subject = $(msg).find('>subject');
  322. if (subject.length) {
  323. var subjectText = subject.text();
  324. if (subjectText || subjectText == "") {
  325. this.eventEmitter.emit(XMPPEvents.SUBJECT_CHANGED, subjectText);
  326. console.log("Subject is changed to " + subjectText);
  327. }
  328. }
  329. // xep-0203 delay
  330. var stamp = $(msg).find('>delay').attr('stamp');
  331. if (!stamp) {
  332. // or xep-0091 delay, UTC timestamp
  333. stamp = $(msg).find('>[xmlns="jabber:x:delay"]').attr('stamp');
  334. if (stamp) {
  335. // the format is CCYYMMDDThh:mm:ss
  336. var dateParts = stamp.match(/(\d{4})(\d{2})(\d{2}T\d{2}:\d{2}:\d{2})/);
  337. stamp = dateParts[1] + "-" + dateParts[2] + "-" + dateParts[3] + "Z";
  338. }
  339. }
  340. if (txt) {
  341. console.log('chat', nick, txt);
  342. this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,
  343. from, nick, txt, this.myroomjid, stamp);
  344. }
  345. }
  346. ChatRoom.prototype.onPresenceError = function (pres, from) {
  347. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  348. console.log('on password required', from);
  349. this.eventEmitter.emit(XMPPEvents.PASSWORD_REQUIRED);
  350. } else if ($(pres).find(
  351. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  352. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  353. if (toDomain === this.xmpp.options.hosts.anonymousdomain) {
  354. // enter the room by replying with 'not-authorized'. This would
  355. // result in reconnection from authorized domain.
  356. // We're either missing Jicofo/Prosody config for anonymous
  357. // domains or something is wrong.
  358. this.eventEmitter.emit(XMPPEvents.ROOM_JOIN_ERROR, pres);
  359. } else {
  360. console.warn('onPresError ', pres);
  361. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  362. }
  363. } else {
  364. console.warn('onPresError ', pres);
  365. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR, pres);
  366. }
  367. };
  368. ChatRoom.prototype.kick = function (jid) {
  369. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  370. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  371. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  372. .c('reason').t('You have been kicked.').up().up().up();
  373. this.connection.sendIQ(
  374. kickIQ,
  375. function (result) {
  376. console.log('Kick participant with jid: ', jid, result);
  377. },
  378. function (error) {
  379. console.log('Kick participant error: ', error);
  380. });
  381. };
  382. ChatRoom.prototype.lockRoom = function (key, onSuccess, onError, onNotSupported) {
  383. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  384. var ob = this;
  385. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  386. function (res) {
  387. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  388. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  389. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  390. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  391. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  392. // Fixes a bug in prosody 0.9.+ https://code.google.com/p/lxmppd/issues/detail?id=373
  393. formsubmit.c('field', {'var': 'muc#roomconfig_whois'}).c('value').t('anyone').up().up();
  394. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  395. ob.connection.sendIQ(formsubmit,
  396. onSuccess,
  397. onError);
  398. } else {
  399. onNotSupported();
  400. }
  401. }, onError);
  402. };
  403. ChatRoom.prototype.addToPresence = function (key, values) {
  404. values.tagName = key;
  405. this.presMap["nodes"].push(values);
  406. };
  407. ChatRoom.prototype.removeFromPresence = function (key) {
  408. for(var i = 0; i < this.presMap.nodes.length; i++)
  409. {
  410. if(key === this.presMap.nodes[i].tagName)
  411. this.presMap.nodes.splice(i, 1);
  412. }
  413. };
  414. ChatRoom.prototype.addPresenceListener = function (name, handler) {
  415. this.presHandlers[name] = handler;
  416. }
  417. ChatRoom.prototype.removePresenceListener = function (name) {
  418. delete this.presHandlers[name];
  419. }
  420. ChatRoom.prototype.isModerator = function (jid) {
  421. return this.role === 'moderator';
  422. };
  423. ChatRoom.prototype.getMemberRole = function (peerJid) {
  424. if (this.members[peerJid]) {
  425. return this.members[peerJid].role;
  426. }
  427. return null;
  428. };
  429. module.exports = function(XMPP) {
  430. Strophe.addConnectionPlugin('emuc', {
  431. connection: null,
  432. rooms: {},//map with the rooms
  433. init: function (conn) {
  434. this.connection = conn;
  435. // add handlers (just once)
  436. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, null, null);
  437. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null);
  438. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null);
  439. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null);
  440. },
  441. createRoom: function (jid, password, eventEmitter) {
  442. var roomJid = Strophe.getBareJidFromJid(jid);
  443. if (this.rooms[roomJid]) {
  444. console.error("You are already in the room!");
  445. return;
  446. }
  447. this.rooms[roomJid] = new ChatRoom(this.connection, jid, password, XMPP, eventEmitter);
  448. return this.rooms[roomJid];
  449. },
  450. doLeave: function (jid) {
  451. this.rooms[jid].doLeave();
  452. delete this.rooms[jid];
  453. },
  454. onPresence: function (pres) {
  455. var from = pres.getAttribute('from');
  456. // What is this for? A workaround for something?
  457. if (pres.getAttribute('type')) {
  458. return true;
  459. }
  460. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  461. if(!room)
  462. return;
  463. // Parse status.
  464. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  465. room.createNonAnonymousRoom();
  466. }
  467. room.onPresence(pres);
  468. return true;
  469. },
  470. onPresenceUnavailable: function (pres) {
  471. var from = pres.getAttribute('from');
  472. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  473. if(!room)
  474. return;
  475. room.onPresenceUnavailable(pres, from);
  476. return true;
  477. },
  478. onPresenceError: function (pres) {
  479. var from = pres.getAttribute('from');
  480. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  481. if(!room)
  482. return;
  483. room.onPresenceError(pres, from);
  484. return true;
  485. },
  486. onMessage: function (msg) {
  487. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  488. var from = msg.getAttribute('from');
  489. var room = this.rooms[Strophe.getBareJidFromJid(from)];
  490. if(!room)
  491. return;
  492. room.onMessage(msg, from);
  493. return true;
  494. }
  495. });
  496. };