Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Caps.js 8.2KB

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