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.

Caps.js 8.2KB

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