Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 new Promise((resolve, reject) =>
  105. this.disco.info(jid, node, response => {
  106. const features = new Set();
  107. $(response)
  108. .find('>query>feature')
  109. .each(
  110. (idx, el) => features.add(el.getAttribute('var')));
  111. if (user) {
  112. // TODO: Maybe use the version + node + hash as keys?
  113. this.versionToCapabilities[user.version] = features;
  114. }
  115. resolve(features);
  116. }, reject, timeout)
  117. );
  118. }
  119. return Promise.resolve(this.versionToCapabilities[user.version]);
  120. }
  121. /**
  122. * Adds ChatRoom instance to the list of rooms. Adds listeners to the room
  123. * and adds "c" element to the presences of the room.
  124. * @param {ChatRoom} room the room.
  125. */
  126. _addChatRoom(room) {
  127. this.rooms.add(room);
  128. room.addListener(XMPPEvents.MUC_MEMBER_LEFT, this._onMucMemberLeft);
  129. this._fixChatRoomPresenceMap(room);
  130. }
  131. /**
  132. * Removes ChatRoom instance from the list of rooms. Removes listeners
  133. * added from the Caps class.
  134. * @param {ChatRoom} room the room.
  135. */
  136. _removeChatRoom(room) {
  137. this.rooms.delete(room);
  138. room.removeListener(XMPPEvents.MUC_MEMBER_LEFT, this._onMucMemberLeft);
  139. }
  140. /**
  141. * Creates/updates the "c" xml node into the presence of the passed room.
  142. * @param {ChatRoom} room the room.
  143. */
  144. _fixChatRoomPresenceMap(room) {
  145. room.addToPresence('c', {
  146. attributes: {
  147. xmlns: Strophe.NS.CAPS,
  148. hash: HASH,
  149. node: this.node,
  150. ver: this.version
  151. }
  152. });
  153. }
  154. /**
  155. * Handles this.version changes.
  156. */
  157. _notifyVersionChanged() {
  158. // update the version for all rooms
  159. this.rooms.forEach(room => this._fixChatRoomPresenceMap(room));
  160. this.submit();
  161. }
  162. /**
  163. * Generates the value for the "ver" attribute.
  164. */
  165. _generateVersion() {
  166. const identities
  167. = this.disco._identities.sort(compareIdentities).reduce(
  168. (accumulatedValue, identity) =>
  169. `${
  170. IDENTITY_PROPERTIES.reduce(
  171. (tmp, key, idx) =>
  172. tmp
  173. + (idx === 0 ? '' : '/')
  174. + identity[key],
  175. '')
  176. }<`,
  177. '');
  178. const features
  179. = this.disco._features.sort().reduce(
  180. (tmp, feature) => `${tmp + feature}<`, '');
  181. this.version = b64_sha1(identities + features);
  182. this._notifyVersionChanged();
  183. }
  184. /**
  185. * Parses the "c" xml node from presence.
  186. * @param {DOMElement} stanza the presence packet
  187. */
  188. _handleCaps(stanza) {
  189. const from = stanza.getAttribute('from');
  190. const caps = stanza.querySelector('c');
  191. const version = caps.getAttribute('ver');
  192. const node = caps.getAttribute('node');
  193. const oldVersion = this.jidToVersion[from];
  194. this.jidToVersion[from] = { version,
  195. node };
  196. if (oldVersion && oldVersion.version !== version) {
  197. this.eventEmitter.emit(XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  198. from);
  199. }
  200. // return true to not remove the handler from Strophe
  201. return true;
  202. }
  203. /**
  204. * Removes entry from this.jidToVersion map.
  205. * @param {String} jid the jid to be removed.
  206. */
  207. _removeJidToVersionEntry(jid) {
  208. if (jid in this.jidToVersion) {
  209. delete this.jidToVersion[jid];
  210. }
  211. }
  212. }