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

strophe.disco.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright 2010, François de Metz <francois@2metz.fr>
  3. */
  4. /**
  5. * Disco Strophe Plugin
  6. * Implement http://xmpp.org/extensions/xep-0030.html
  7. * TODO: manage node hierarchies, and node on info request
  8. */
  9. Strophe.addConnectionPlugin('disco',
  10. {
  11. _connection: null,
  12. _identities : [],
  13. _features : [],
  14. _items : [],
  15. /** Function: init
  16. * Plugin init
  17. *
  18. * Parameters:
  19. * (Strophe.Connection) conn - Strophe connection
  20. */
  21. init: function(conn)
  22. {
  23. this._connection = conn;
  24. this._identities = [];
  25. this._features = [];
  26. this._items = [];
  27. // disco info
  28. conn.addHandler(this._onDiscoInfo.bind(this), Strophe.NS.DISCO_INFO, 'iq', 'get', null, null);
  29. // disco items
  30. conn.addHandler(this._onDiscoItems.bind(this), Strophe.NS.DISCO_ITEMS, 'iq', 'get', null, null);
  31. },
  32. /** Function: addIdentity
  33. * See http://xmpp.org/registrar/disco-categories.html
  34. * Parameters:
  35. * (String) category - category of identity (like client, automation, etc ...)
  36. * (String) type - type of identity (like pc, web, bot , etc ...)
  37. * (String) name - name of identity in natural language
  38. * (String) lang - lang of name parameter
  39. *
  40. * Returns:
  41. * Boolean
  42. */
  43. addIdentity: function(category, type, name, 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. if (node)
  167. {
  168. attrs.node = node;
  169. }
  170. var iqresult = this._buildIQResult(stanza, attrs);
  171. for (var i=0; i<this._identities.length; i++)
  172. {
  173. var attrs = {category: this._identities[i].category,
  174. type : this._identities[i].type};
  175. if (this._identities[i].name)
  176. attrs.name = this._identities[i].name;
  177. if (this._identities[i].lang)
  178. attrs['xml:lang'] = this._identities[i].lang;
  179. iqresult.c('identity', attrs).up();
  180. }
  181. for (var i=0; i<this._features.length; i++)
  182. {
  183. iqresult.c('feature', {'var':this._features[i]}).up();
  184. }
  185. this._connection.send(iqresult.tree());
  186. return true;
  187. },
  188. /** PrivateFunction: _onDiscoItems
  189. * Called when receive items request
  190. */
  191. _onDiscoItems: function(stanza)
  192. {
  193. var query_attrs = {xmlns: Strophe.NS.DISCO_ITEMS};
  194. var node = stanza.getElementsByTagName('query')[0].getAttribute('node');
  195. if (node)
  196. {
  197. query_attrs.node = node;
  198. var items = [];
  199. for (var i = 0; i < this._items.length; i++)
  200. {
  201. if (this._items[i].node == node)
  202. {
  203. items = this._items[i].call_back(stanza);
  204. break;
  205. }
  206. }
  207. }
  208. else
  209. {
  210. var items = this._items;
  211. }
  212. var iqresult = this._buildIQResult(stanza, query_attrs);
  213. for (var i = 0; i < items.length; i++)
  214. {
  215. var attrs = {jid: items[i].jid};
  216. if (items[i].name)
  217. attrs.name = items[i].name;
  218. if (items[i].node)
  219. attrs.node = items[i].node;
  220. iqresult.c('item', attrs).up();
  221. }
  222. this._connection.send(iqresult.tree());
  223. return true;
  224. }
  225. });