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

Caps.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* global $ */
  2. import { b64_sha1, Strophe } from 'strophe.js'; // eslint-disable-line camelcase
  3. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  4. import Listenable from '../util/Listenable';
  5. /**
  6. * The property
  7. */
  8. const IDENTITY_PROPERTIES = [ 'category', 'type', 'lang', 'name' ];
  9. const IDENTITY_PROPERTIES_FOR_COMPARE = [ 'category', 'type', 'lang' ];
  10. const HASH = 'sha-1';
  11. /**
  12. *
  13. * @param a
  14. * @param b
  15. */
  16. function compareIdentities(a, b) {
  17. let res = 0;
  18. IDENTITY_PROPERTIES_FOR_COMPARE.some(key =>
  19. (res = ((a[key] > b[key]) && 1) || ((a[key] < b[key]) && -1)) !== 0
  20. );
  21. return res;
  22. }
  23. /**
  24. * Implements xep-0115 ( http://xmpp.org/extensions/xep-0115.html )
  25. */
  26. export default class Caps extends Listenable {
  27. /**
  28. * Constructs new Caps instance.
  29. * @param {Strophe.Connection} connection the strophe connection object
  30. * @param {String} node the value of the node attribute of the "c" xml node
  31. * that will be sent to the other participants
  32. */
  33. constructor(connection = {}, node = 'http://jitsi.org/jitsimeet') {
  34. super();
  35. this.node = node;
  36. this.disco = connection.disco;
  37. if (!this.disco) {
  38. throw new Error(
  39. 'Missing strophe-plugins '
  40. + '(disco plugin is required)!');
  41. }
  42. this.versionToCapabilities = Object.create(null);
  43. this.jidToVersion = Object.create(null);
  44. this.version = '';
  45. this.rooms = new Set();
  46. const emuc = connection.emuc;
  47. emuc.addListener(XMPPEvents.EMUC_ROOM_ADDED,
  48. room => this._addChatRoom(room));
  49. emuc.addListener(XMPPEvents.EMUC_ROOM_REMOVED,
  50. room => this._removeChatRoom(room));
  51. Object.keys(emuc.rooms).forEach(jid => {
  52. this._addChatRoom(emuc.rooms[jid]);
  53. });
  54. Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
  55. this.disco.addFeature(Strophe.NS.CAPS);
  56. connection.addHandler(this._handleCaps.bind(this), Strophe.NS.CAPS);
  57. this._onMucMemberLeft = this._removeJidToVersionEntry.bind(this);
  58. }
  59. /**
  60. * Adds new feature to the list of supported features for the local
  61. * participant
  62. * @param {String} feature the name of the feature.
  63. * @param {boolean} submit if true - new presence with updated "c" node
  64. * will be sent.
  65. */
  66. addFeature(feature, submit = false) {
  67. this.disco.addFeature(feature);
  68. this._generateVersion();
  69. if (submit) {
  70. this.submit();
  71. }
  72. }
  73. /**
  74. * Removes a feature from the list of supported features for the local
  75. * participant
  76. * @param {String} feature the name of the feature.
  77. * @param {boolean} submit if true - new presence with updated "c" node
  78. * will be sent.
  79. */
  80. removeFeature(feature, submit = false) {
  81. this.disco.removeFeature(feature);
  82. this._generateVersion();
  83. if (submit) {
  84. this.submit();
  85. }
  86. }
  87. /**
  88. * Sends new presence stanza for every room from the list of rooms.
  89. */
  90. submit() {
  91. this.rooms.forEach(room => room.sendPresence());
  92. }
  93. /**
  94. * Returns a set with the features for a participant.
  95. * @param {String} jid the jid of the participant
  96. * @param {int} timeout the timeout in ms for reply from the participant.
  97. * @returns {Promise<Set<String>, Error>}
  98. */
  99. getFeatures(jid, timeout = 5000) {
  100. const user
  101. = jid in this.jidToVersion ? this.jidToVersion[jid] : null;
  102. if (!user || !(user.version in this.versionToCapabilities)) {
  103. const node = user ? `${user.node}#${user.version}` : null;
  104. return this._getDiscoInfo(jid, node, timeout)
  105. .then(({ features }) => {
  106. if (user) {
  107. // TODO: Maybe use the version + node + hash as keys?
  108. this.versionToCapabilities[user.version] = features;
  109. }
  110. return features;
  111. });
  112. }
  113. return Promise.resolve(this.versionToCapabilities[user.version]);
  114. }
  115. /**
  116. * Returns a set with the features for a host.
  117. * @param {String} jid the jid of the host
  118. * @param {int} timeout the timeout in ms for reply from the host.
  119. * @returns {Promise<Set<String>, Error>}
  120. */
  121. getFeaturesAndIdentities(jid, timeout = 5000) {
  122. return this._getDiscoInfo(jid, null, timeout);
  123. }
  124. /**
  125. * Returns a set with the features and identities for a host.
  126. * @param {String} jid the jid of the host
  127. * @param {String|null} node the node to query
  128. * @param {int} timeout the timeout in ms for reply from the host.
  129. * @returns {Promise<Object>}
  130. * @private
  131. */
  132. _getDiscoInfo(jid, node, timeout) {
  133. return new Promise((resolve, reject) =>
  134. this.disco.info(jid, node, response => {
  135. const features = new Set();
  136. const identities = new Set();
  137. $(response)
  138. .find('>query>feature')
  139. .each(
  140. (_, el) => features.add(el.getAttribute('var')));
  141. $(response)
  142. .find('>query>identity')
  143. .each(
  144. (_, el) => identities.add({
  145. type: el.getAttribute('type'),
  146. name: el.getAttribute('name'),
  147. category: el.getAttribute('category')
  148. }));
  149. resolve({
  150. features,
  151. identities });
  152. }, reject, timeout)
  153. );
  154. }
  155. /**
  156. * Adds ChatRoom instance to the list of rooms. Adds listeners to the room
  157. * and adds "c" element to the presences of the room.
  158. * @param {ChatRoom} room the room.
  159. */
  160. _addChatRoom(room) {
  161. this.rooms.add(room);
  162. room.addListener(XMPPEvents.MUC_MEMBER_LEFT, this._onMucMemberLeft);
  163. this._fixChatRoomPresenceMap(room);
  164. }
  165. /**
  166. * Removes ChatRoom instance from the list of rooms. Removes listeners
  167. * added from the Caps class.
  168. * @param {ChatRoom} room the room.
  169. */
  170. _removeChatRoom(room) {
  171. this.rooms.delete(room);
  172. room.removeListener(XMPPEvents.MUC_MEMBER_LEFT, this._onMucMemberLeft);
  173. }
  174. /**
  175. * Creates/updates the "c" xml node into the presence of the passed room.
  176. * @param {ChatRoom} room the room.
  177. */
  178. _fixChatRoomPresenceMap(room) {
  179. room.addToPresence('c', {
  180. attributes: {
  181. xmlns: Strophe.NS.CAPS,
  182. hash: HASH,
  183. node: this.node,
  184. ver: this.version
  185. }
  186. });
  187. }
  188. /**
  189. * Handles this.version changes.
  190. */
  191. _notifyVersionChanged() {
  192. // update the version for all rooms
  193. this.rooms.forEach(room => this._fixChatRoomPresenceMap(room));
  194. this.submit();
  195. }
  196. /**
  197. * Generates the value for the "ver" attribute.
  198. */
  199. _generateVersion() {
  200. const identities
  201. = this.disco._identities.sort(compareIdentities).reduce(
  202. (accumulatedValue, identity) =>
  203. `${
  204. IDENTITY_PROPERTIES.reduce(
  205. (tmp, key, idx) =>
  206. tmp
  207. + (idx === 0 ? '' : '/')
  208. + identity[key],
  209. '')
  210. }<`,
  211. '');
  212. const features
  213. = this.disco._features.sort().reduce(
  214. (tmp, feature) => `${tmp + feature}<`, '');
  215. this.version = b64_sha1(identities + features);
  216. this._notifyVersionChanged();
  217. }
  218. /**
  219. * Parses the "c" xml node from presence.
  220. * @param {DOMElement} stanza the presence packet
  221. */
  222. _handleCaps(stanza) {
  223. const from = stanza.getAttribute('from');
  224. const caps = stanza.querySelector('c');
  225. const version = caps.getAttribute('ver');
  226. const node = caps.getAttribute('node');
  227. const oldVersion = this.jidToVersion[from];
  228. this.jidToVersion[from] = { version,
  229. node };
  230. if (oldVersion && oldVersion.version !== version) {
  231. this.eventEmitter.emit(XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  232. from);
  233. }
  234. // return true to not remove the handler from Strophe
  235. return true;
  236. }
  237. /**
  238. * Removes entry from this.jidToVersion map.
  239. * @param {String} jid the jid to be removed.
  240. */
  241. _removeJidToVersionEntry(jid) {
  242. if (jid in this.jidToVersion) {
  243. delete this.jidToVersion[jid];
  244. }
  245. }
  246. }