您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. init: function (conn) {
  16. this.connection = conn;
  17. },
  18. initPresenceMap: function (myroomjid) {
  19. this.presMap['to'] = myroomjid;
  20. this.presMap['xns'] = 'http://jabber.org/protocol/muc';
  21. },
  22. doJoin: function (jid, password) {
  23. this.myroomjid = jid;
  24. console.info("Joined MUC as " + this.myroomjid);
  25. this.initPresenceMap(this.myroomjid);
  26. if (!this.roomjid) {
  27. this.roomjid = Strophe.getBareJidFromJid(jid);
  28. // add handlers (just once)
  29. this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
  30. this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
  31. this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
  32. this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
  33. }
  34. if (password !== undefined) {
  35. this.presMap['password'] = password;
  36. }
  37. this.sendPresence();
  38. },
  39. doLeave: function() {
  40. console.log("do leave", this.myroomjid);
  41. var pres = $pres({to: this.myroomjid, type: 'unavailable' });
  42. this.presMap.length = 0;
  43. this.connection.send(pres);
  44. },
  45. onPresence: function (pres) {
  46. var from = pres.getAttribute('from');
  47. var type = pres.getAttribute('type');
  48. if (type != null) {
  49. return true;
  50. }
  51. // Parse etherpad tag.
  52. var etherpad = $(pres).find('>etherpad');
  53. if (etherpad.length) {
  54. $(document).trigger('etherpadadded.muc', [from, etherpad.text()]);
  55. }
  56. // Parse prezi tag.
  57. var presentation = $(pres).find('>prezi');
  58. if (presentation.length)
  59. {
  60. var url = presentation.attr('url');
  61. var current = presentation.find('>current').text();
  62. console.log('presentation info received from', from, url);
  63. if (this.preziMap[from] == null) {
  64. this.preziMap[from] = url;
  65. $(document).trigger('presentationadded.muc', [from, url, current]);
  66. }
  67. else {
  68. $(document).trigger('gotoslide.muc', [from, url, current]);
  69. }
  70. }
  71. else if (this.preziMap[from] != null) {
  72. var url = this.preziMap[from];
  73. delete this.preziMap[from];
  74. $(document).trigger('presentationremoved.muc', [from, url]);
  75. }
  76. // Parse audio info tag.
  77. var audioMuted = $(pres).find('>audiomuted');
  78. if (audioMuted.length) {
  79. $(document).trigger('audiomuted.muc', [from, audioMuted.text()]);
  80. }
  81. // Parse video info tag.
  82. var videoMuted = $(pres).find('>videomuted');
  83. if (videoMuted.length) {
  84. $(document).trigger('videomuted.muc', [from, videoMuted.text()]);
  85. }
  86. // Parse status.
  87. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
  88. // http://xmpp.org/extensions/xep-0045.html#createroom-instant
  89. this.isOwner = true;
  90. var create = $iq({type: 'set', to: this.roomjid})
  91. .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
  92. .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  93. this.connection.send(create); // fire away
  94. }
  95. // Parse roles.
  96. var member = {};
  97. member.show = $(pres).find('>show').text();
  98. member.status = $(pres).find('>status').text();
  99. var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
  100. member.affiliation = tmp.attr('affiliation');
  101. member.role = tmp.attr('role');
  102. var nicktag = $(pres).find('>nick[xmlns="http://jabber.org/protocol/nick"]');
  103. member.displayName = (nicktag.length > 0 ? nicktag.text() : null);
  104. if (from == this.myroomjid) {
  105. if (member.affiliation == 'owner') this.isOwner = true;
  106. if (!this.joined) {
  107. this.joined = true;
  108. $(document).trigger('joined.muc', [from, member]);
  109. this.list_members.push(from);
  110. }
  111. } else if (this.members[from] === undefined) {
  112. // new participant
  113. this.members[from] = member;
  114. this.list_members.push(from);
  115. $(document).trigger('entered.muc', [from, member, pres]);
  116. }
  117. // Always trigger presence to update bindings
  118. console.log('presence change from', from);
  119. $(document).trigger('presence.muc', [from, member, pres]);
  120. // Trigger status message update
  121. if (member.status) {
  122. $(document).trigger('presence.status.muc', [from, member, pres]);
  123. }
  124. return true;
  125. },
  126. onPresenceUnavailable: function (pres) {
  127. var from = pres.getAttribute('from');
  128. // Status code 110 indicates that this notification is "self-presence".
  129. if (!$(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="110"]').length) {
  130. delete this.members[from];
  131. this.list_members.splice(this.list_members.indexOf(from), 1);
  132. $(document).trigger('left.muc', [from]);
  133. }
  134. // If the status code is 110 this means we're leaving and we would like
  135. // to remove everyone else from our view, so we trigger the event.
  136. else if (this.list_members.length > 1) {
  137. for (var i = 0; i < this.list_members.length; i++) {
  138. var member = this.list_members[i];
  139. delete this.members[i];
  140. this.list_members.splice(i, 1);
  141. $(document).trigger('left.muc', member);
  142. }
  143. }
  144. return true;
  145. },
  146. onPresenceError: function (pres) {
  147. var from = pres.getAttribute('from');
  148. if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  149. $(document).trigger('passwordrequired.muc', [from]);
  150. } else if ($(pres).find(
  151. '>error[type="cancel"]>not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
  152. var toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  153. if(toDomain === config.hosts.anonymousdomain) {
  154. // we are connected with anonymous domain and only non anonymous users can create rooms
  155. // we must authorize the user
  156. $(document).trigger('passwordrequired.main');
  157. }
  158. else
  159. console.warn('onPresError ', pres);
  160. } else {
  161. console.warn('onPresError ', pres);
  162. }
  163. return true;
  164. },
  165. sendMessage: function (body, nickname) {
  166. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  167. msg.c('body', body).up();
  168. if (nickname) {
  169. msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
  170. }
  171. this.connection.send(msg);
  172. },
  173. setSubject: function (subject){
  174. var msg = $msg({to: this.roomjid, type: 'groupchat'});
  175. msg.c('subject', subject);
  176. this.connection.send(msg);
  177. console.log("topic changed to " + subject);
  178. },
  179. onMessage: function (msg) {
  180. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  181. var from = msg.getAttribute('from');
  182. var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(from);
  183. var txt = $(msg).find('>body').text();
  184. var type = msg.getAttribute("type");
  185. if(type == "error")
  186. {
  187. Chat.chatAddError($(msg).find('>text').text(), txt);
  188. return true;
  189. }
  190. var subject = $(msg).find('>subject');
  191. if(subject.length)
  192. {
  193. var subjectText = subject.text();
  194. if(subjectText || subjectText == "") {
  195. Chat.chatSetSubject(subjectText);
  196. console.log("Subject is changed to " + subjectText);
  197. }
  198. }
  199. if (txt) {
  200. console.log('chat', nick, txt);
  201. Chat.updateChatConversation(from, nick, txt);
  202. }
  203. return true;
  204. },
  205. lockRoom: function (key) {
  206. //http://xmpp.org/extensions/xep-0045.html#roomconfig
  207. var ob = this;
  208. this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
  209. function (res) {
  210. if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
  211. var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
  212. formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
  213. formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
  214. formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
  215. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  216. this.connection.sendIQ(formsubmit,
  217. function (res) {
  218. console.log('set room password');
  219. },
  220. function (err) {
  221. console.warn('setting password failed', err);
  222. }
  223. );
  224. } else {
  225. console.warn('room passwords not supported');
  226. }
  227. },
  228. function (err) {
  229. console.warn('setting password failed', err);
  230. }
  231. );
  232. },
  233. kick: function (jid) {
  234. var kickIQ = $iq({to: this.roomjid, type: 'set'})
  235. .c('query', {xmlns: 'http://jabber.org/protocol/muc#admin'})
  236. .c('item', {nick: Strophe.getResourceFromJid(jid), role: 'none'})
  237. .c('reason').t('You have been kicked.').up().up().up();
  238. this.connection.sendIQ(
  239. kickIQ,
  240. function (result) {
  241. console.log('Kick participant with jid: ', jid, result);
  242. },
  243. function (error) {
  244. console.log('Kick participant error: ', error);
  245. });
  246. },
  247. sendPresence: function () {
  248. var pres = $pres({to: this.presMap['to'] });
  249. pres.c('x', {xmlns: this.presMap['xns']});
  250. if (this.presMap['password']) {
  251. pres.c('password').t(this.presMap['password']).up();
  252. }
  253. pres.up();
  254. if (this.presMap['displayName']) {
  255. // XEP-0172
  256. pres.c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
  257. .t(this.presMap['displayName']).up();
  258. }
  259. if (this.presMap['audions']) {
  260. pres.c('audiomuted', {xmlns: this.presMap['audions']})
  261. .t(this.presMap['audiomuted']).up();
  262. }
  263. if (this.presMap['videons']) {
  264. pres.c('videomuted', {xmlns: this.presMap['videons']})
  265. .t(this.presMap['videomuted']).up();
  266. }
  267. if (this.presMap['prezins']) {
  268. pres.c('prezi',
  269. {xmlns: this.presMap['prezins'],
  270. 'url': this.presMap['preziurl']})
  271. .c('current').t(this.presMap['prezicurrent']).up().up();
  272. }
  273. if (this.presMap['etherpadns']) {
  274. pres.c('etherpad', {xmlns: this.presMap['etherpadns']})
  275. .t(this.presMap['etherpadname']).up();
  276. }
  277. if (this.presMap['medians'])
  278. {
  279. pres.c('media', {xmlns: this.presMap['medians']});
  280. var sourceNumber = 0;
  281. Object.keys(this.presMap).forEach(function (key) {
  282. if (key.indexOf('source') >= 0) {
  283. sourceNumber++;
  284. }
  285. });
  286. if (sourceNumber > 0)
  287. for (var i = 1; i <= sourceNumber/3; i ++) {
  288. pres.c('source',
  289. {type: this.presMap['source' + i + '_type'],
  290. ssrc: this.presMap['source' + i + '_ssrc'],
  291. direction: this.presMap['source'+ i + '_direction']
  292. || 'sendrecv' }
  293. ).up();
  294. }
  295. }
  296. pres.up();
  297. connection.send(pres);
  298. },
  299. addDisplayNameToPresence: function (displayName) {
  300. this.presMap['displayName'] = displayName;
  301. },
  302. addMediaToPresence: function (sourceNumber, mtype, ssrcs, direction) {
  303. if (!this.presMap['medians'])
  304. this.presMap['medians'] = 'http://estos.de/ns/mjs';
  305. this.presMap['source' + sourceNumber + '_type'] = mtype;
  306. this.presMap['source' + sourceNumber + '_ssrc'] = ssrcs;
  307. this.presMap['source' + sourceNumber + '_direction'] = direction;
  308. },
  309. clearPresenceMedia: function () {
  310. var self = this;
  311. Object.keys(this.presMap).forEach( function(key) {
  312. if(key.indexOf('source') != -1) {
  313. delete self.presMap[key];
  314. }
  315. });
  316. },
  317. addPreziToPresence: function (url, currentSlide) {
  318. this.presMap['prezins'] = 'http://jitsi.org/jitmeet/prezi';
  319. this.presMap['preziurl'] = url;
  320. this.presMap['prezicurrent'] = currentSlide;
  321. },
  322. removePreziFromPresence: function () {
  323. delete this.presMap['prezins'];
  324. delete this.presMap['preziurl'];
  325. delete this.presMap['prezicurrent'];
  326. },
  327. addCurrentSlideToPresence: function (currentSlide) {
  328. this.presMap['prezicurrent'] = currentSlide;
  329. },
  330. getPrezi: function (roomjid) {
  331. return this.preziMap[roomjid];
  332. },
  333. addEtherpadToPresence: function(etherpadName) {
  334. this.presMap['etherpadns'] = 'http://jitsi.org/jitmeet/etherpad';
  335. this.presMap['etherpadname'] = etherpadName;
  336. },
  337. addAudioInfoToPresence: function(isMuted) {
  338. this.presMap['audions'] = 'http://jitsi.org/jitmeet/audio';
  339. this.presMap['audiomuted'] = isMuted.toString();
  340. },
  341. addVideoInfoToPresence: function(isMuted) {
  342. this.presMap['videons'] = 'http://jitsi.org/jitmeet/video';
  343. this.presMap['videomuted'] = isMuted.toString();
  344. },
  345. findJidFromResource: function(resourceJid) {
  346. var peerJid = null;
  347. Object.keys(this.members).some(function (jid) {
  348. peerJid = jid;
  349. return Strophe.getResourceFromJid(jid) === resourceJid;
  350. });
  351. return peerJid;
  352. }
  353. });