Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Caps.js 7.4KB

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