您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Caps.js 7.3KB

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