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.

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