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 7.2KB

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