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.

strophe.emuc.js 18KB

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