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.

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