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.

strophe.disco.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* eslint-disable */
  2. import { $iq, Strophe } from 'strophe.js';
  3. Strophe.addConnectionPlugin('disco',
  4. {
  5. _connection: null,
  6. _identities : [],
  7. _features : [],
  8. _items : [],
  9. /** Function: init
  10. * Plugin init
  11. *
  12. * Parameters:
  13. * (Strophe.Connection) conn - Strophe connection
  14. */
  15. init: function(conn)
  16. {
  17. this._connection = conn;
  18. this._identities = [];
  19. this._features = [];
  20. this._items = [];
  21. // disco info
  22. conn.addHandler(this._onDiscoInfo.bind(this), Strophe.NS.DISCO_INFO, 'iq', 'get', null, null);
  23. // disco items
  24. conn.addHandler(this._onDiscoItems.bind(this), Strophe.NS.DISCO_ITEMS, 'iq', 'get', null, null);
  25. },
  26. /** Function: addIdentity
  27. * See http://xmpp.org/registrar/disco-categories.html
  28. * Parameters:
  29. * (String) category - category of identity (like client, automation, etc ...)
  30. * (String) type - type of identity (like pc, web, bot , etc ...)
  31. * (String) name - name of identity in natural language
  32. * (String) lang - lang of name parameter
  33. *
  34. * Returns:
  35. * Boolean
  36. */
  37. addIdentity: function(category, type, name, lang)
  38. {
  39. if (typeof name === 'undefined') {
  40. name = '';
  41. }
  42. if (typeof lang === 'undefined') {
  43. lang = '';
  44. }
  45. for (var i=0; i<this._identities.length; i++)
  46. {
  47. if (this._identities[i].category == category &&
  48. this._identities[i].type == type &&
  49. this._identities[i].name == name &&
  50. this._identities[i].lang == lang)
  51. {
  52. return false;
  53. }
  54. }
  55. this._identities.push({category: category, type: type, name: name, lang: lang});
  56. return true;
  57. },
  58. /** Function: addFeature
  59. *
  60. * Parameters:
  61. * (String) var_name - feature name (like jabber:iq:version)
  62. *
  63. * Returns:
  64. * boolean
  65. */
  66. addFeature: function(var_name)
  67. {
  68. for (var i=0; i<this._features.length; i++)
  69. {
  70. if (this._features[i] == var_name)
  71. return false;
  72. }
  73. this._features.push(var_name);
  74. return true;
  75. },
  76. /** Function: removeFeature
  77. *
  78. * Parameters:
  79. * (String) var_name - feature name (like jabber:iq:version)
  80. *
  81. * Returns:
  82. * boolean
  83. */
  84. removeFeature: function(var_name)
  85. {
  86. for (var i=0; i<this._features.length; i++)
  87. {
  88. if (this._features[i] === var_name){
  89. this._features.splice(i,1);
  90. return true;
  91. }
  92. }
  93. return false;
  94. },
  95. /** Function: addItem
  96. *
  97. * Parameters:
  98. * (String) jid
  99. * (String) name
  100. * (String) node
  101. * (Function) call_back
  102. *
  103. * Returns:
  104. * boolean
  105. */
  106. addItem: function(jid, name, node, call_back)
  107. {
  108. if (node && !call_back)
  109. return false;
  110. this._items.push({jid: jid, name: name, node: node, call_back: call_back});
  111. return true;
  112. },
  113. /** Function: info
  114. * Info query
  115. *
  116. * Parameters:
  117. * (Function) call_back
  118. * (String) jid
  119. * (String) node
  120. */
  121. info: function(jid, node, success, error, timeout)
  122. {
  123. var attrs = {xmlns: Strophe.NS.DISCO_INFO};
  124. if (node)
  125. attrs.node = node;
  126. var info = $iq({from:this._connection.jid,
  127. to:jid, type:'get'}).c('query', attrs);
  128. this._connection.sendIQ(info, success, error, timeout);
  129. },
  130. /** Function: items
  131. * Items query
  132. *
  133. * Parameters:
  134. * (Function) call_back
  135. * (String) jid
  136. * (String) node
  137. */
  138. items: function(jid, node, success, error, timeout)
  139. {
  140. var attrs = {xmlns: Strophe.NS.DISCO_ITEMS};
  141. if (node)
  142. attrs.node = node;
  143. var items = $iq({from:this._connection.jid,
  144. to:jid, type:'get'}).c('query', attrs);
  145. this._connection.sendIQ(items, success, error, timeout);
  146. },
  147. /** PrivateFunction: _buildIQResult
  148. */
  149. _buildIQResult: function(stanza, query_attrs)
  150. {
  151. var id = stanza.getAttribute('id');
  152. var from = stanza.getAttribute('from');
  153. var iqresult = $iq({type: 'result', id: id});
  154. if (from !== null) {
  155. iqresult.attrs({to: from});
  156. }
  157. return iqresult.c('query', query_attrs);
  158. },
  159. /** PrivateFunction: _onDiscoInfo
  160. * Called when receive info request
  161. */
  162. _onDiscoInfo: function(stanza)
  163. {
  164. var node = stanza.getElementsByTagName('query')[0].getAttribute('node');
  165. var attrs = {xmlns: Strophe.NS.DISCO_INFO};
  166. var i;
  167. if (node)
  168. {
  169. attrs.node = node;
  170. }
  171. var iqresult = this._buildIQResult(stanza, attrs);
  172. for (i=0; i<this._identities.length; i++)
  173. {
  174. attrs = {category: this._identities[i].category,
  175. type : this._identities[i].type};
  176. if (this._identities[i].name)
  177. attrs.name = this._identities[i].name;
  178. if (this._identities[i].lang)
  179. attrs['xml:lang'] = this._identities[i].lang;
  180. iqresult.c('identity', attrs).up();
  181. }
  182. for (i=0; i<this._features.length; i++)
  183. {
  184. iqresult.c('feature', {'var':this._features[i]}).up();
  185. }
  186. this._connection.send(iqresult.tree());
  187. return true;
  188. },
  189. /** PrivateFunction: _onDiscoItems
  190. * Called when receive items request
  191. */
  192. _onDiscoItems: function(stanza)
  193. {
  194. var query_attrs = {xmlns: Strophe.NS.DISCO_ITEMS};
  195. var node = stanza.getElementsByTagName('query')[0].getAttribute('node');
  196. var items, i;
  197. if (node)
  198. {
  199. query_attrs.node = node;
  200. items = [];
  201. for (i = 0; i < this._items.length; i++)
  202. {
  203. if (this._items[i].node == node)
  204. {
  205. items = this._items[i].call_back(stanza);
  206. break;
  207. }
  208. }
  209. }
  210. else
  211. {
  212. items = this._items;
  213. }
  214. var iqresult = this._buildIQResult(stanza, query_attrs);
  215. for (i = 0; i < items.length; i++)
  216. {
  217. var attrs = {jid: items[i].jid};
  218. if (items[i].name)
  219. attrs.name = items[i].name;
  220. if (items[i].node)
  221. attrs.node = items[i].node;
  222. iqresult.c('item', attrs).up();
  223. }
  224. this._connection.send(iqresult.tree());
  225. return true;
  226. }
  227. });