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.0KB

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