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.

277 lines
8.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <vector>
  28. #include <algorithm>
  29. #include "talk/base/common.h"
  30. #include "talk/xmpp/xmppengineimpl.h"
  31. #include "talk/xmpp/constants.h"
  32. namespace buzz {
  33. class XmppIqEntry {
  34. XmppIqEntry(const std::string & id, const std::string & to,
  35. XmppEngine * pxce, XmppIqHandler * iq_handler) :
  36. id_(id),
  37. to_(to),
  38. engine_(pxce),
  39. iq_handler_(iq_handler) {
  40. }
  41. private:
  42. friend class XmppEngineImpl;
  43. const std::string id_;
  44. const std::string to_;
  45. XmppEngine * const engine_;
  46. XmppIqHandler * const iq_handler_;
  47. };
  48. XmppReturnStatus
  49. XmppEngineImpl::SendIq(const XmlElement * element, XmppIqHandler * iq_handler,
  50. XmppIqCookie* cookie) {
  51. if (state_ == STATE_CLOSED)
  52. return XMPP_RETURN_BADSTATE;
  53. if (NULL == iq_handler)
  54. return XMPP_RETURN_BADARGUMENT;
  55. if (!element || element->Name() != QN_IQ)
  56. return XMPP_RETURN_BADARGUMENT;
  57. const std::string& type = element->Attr(QN_TYPE);
  58. if (type != "get" && type != "set")
  59. return XMPP_RETURN_BADARGUMENT;
  60. if (!element->HasAttr(QN_ID))
  61. return XMPP_RETURN_BADARGUMENT;
  62. const std::string& id = element->Attr(QN_ID);
  63. XmppIqEntry * iq_entry = new XmppIqEntry(id,
  64. element->Attr(QN_TO),
  65. this, iq_handler);
  66. iq_entries_->push_back(iq_entry);
  67. SendStanza(element);
  68. if (cookie)
  69. *cookie = iq_entry;
  70. return XMPP_RETURN_OK;
  71. }
  72. XmppReturnStatus
  73. XmppEngineImpl::RemoveIqHandler(XmppIqCookie cookie,
  74. XmppIqHandler ** iq_handler) {
  75. std::vector<XmppIqEntry*, std::allocator<XmppIqEntry*> >::iterator pos;
  76. pos = std::find(iq_entries_->begin(),
  77. iq_entries_->end(),
  78. reinterpret_cast<XmppIqEntry*>(cookie));
  79. if (pos == iq_entries_->end())
  80. return XMPP_RETURN_BADARGUMENT;
  81. XmppIqEntry* entry = *pos;
  82. iq_entries_->erase(pos);
  83. if (iq_handler)
  84. *iq_handler = entry->iq_handler_;
  85. delete entry;
  86. return XMPP_RETURN_OK;
  87. }
  88. void
  89. XmppEngineImpl::DeleteIqCookies() {
  90. for (size_t i = 0; i < iq_entries_->size(); i += 1) {
  91. XmppIqEntry * iq_entry_ = (*iq_entries_)[i];
  92. (*iq_entries_)[i] = NULL;
  93. delete iq_entry_;
  94. }
  95. iq_entries_->clear();
  96. }
  97. static void
  98. AecImpl(XmlElement * error_element, const QName & name,
  99. const char * type, const char * code) {
  100. error_element->AddElement(new XmlElement(QN_ERROR));
  101. error_element->AddAttr(QN_CODE, code, 1);
  102. error_element->AddAttr(QN_TYPE, type, 1);
  103. error_element->AddElement(new XmlElement(name, true), 1);
  104. }
  105. static void
  106. AddErrorCode(XmlElement * error_element, XmppStanzaError code) {
  107. switch (code) {
  108. case XSE_BAD_REQUEST:
  109. AecImpl(error_element, QN_STANZA_BAD_REQUEST, "modify", "400");
  110. break;
  111. case XSE_CONFLICT:
  112. AecImpl(error_element, QN_STANZA_CONFLICT, "cancel", "409");
  113. break;
  114. case XSE_FEATURE_NOT_IMPLEMENTED:
  115. AecImpl(error_element, QN_STANZA_FEATURE_NOT_IMPLEMENTED,
  116. "cancel", "501");
  117. break;
  118. case XSE_FORBIDDEN:
  119. AecImpl(error_element, QN_STANZA_FORBIDDEN, "auth", "403");
  120. break;
  121. case XSE_GONE:
  122. AecImpl(error_element, QN_STANZA_GONE, "modify", "302");
  123. break;
  124. case XSE_INTERNAL_SERVER_ERROR:
  125. AecImpl(error_element, QN_STANZA_INTERNAL_SERVER_ERROR, "wait", "500");
  126. break;
  127. case XSE_ITEM_NOT_FOUND:
  128. AecImpl(error_element, QN_STANZA_ITEM_NOT_FOUND, "cancel", "404");
  129. break;
  130. case XSE_JID_MALFORMED:
  131. AecImpl(error_element, QN_STANZA_JID_MALFORMED, "modify", "400");
  132. break;
  133. case XSE_NOT_ACCEPTABLE:
  134. AecImpl(error_element, QN_STANZA_NOT_ACCEPTABLE, "cancel", "406");
  135. break;
  136. case XSE_NOT_ALLOWED:
  137. AecImpl(error_element, QN_STANZA_NOT_ALLOWED, "cancel", "405");
  138. break;
  139. case XSE_PAYMENT_REQUIRED:
  140. AecImpl(error_element, QN_STANZA_PAYMENT_REQUIRED, "auth", "402");
  141. break;
  142. case XSE_RECIPIENT_UNAVAILABLE:
  143. AecImpl(error_element, QN_STANZA_RECIPIENT_UNAVAILABLE, "wait", "404");
  144. break;
  145. case XSE_REDIRECT:
  146. AecImpl(error_element, QN_STANZA_REDIRECT, "modify", "302");
  147. break;
  148. case XSE_REGISTRATION_REQUIRED:
  149. AecImpl(error_element, QN_STANZA_REGISTRATION_REQUIRED, "auth", "407");
  150. break;
  151. case XSE_SERVER_NOT_FOUND:
  152. AecImpl(error_element, QN_STANZA_REMOTE_SERVER_NOT_FOUND,
  153. "cancel", "404");
  154. break;
  155. case XSE_SERVER_TIMEOUT:
  156. AecImpl(error_element, QN_STANZA_REMOTE_SERVER_TIMEOUT, "wait", "502");
  157. break;
  158. case XSE_RESOURCE_CONSTRAINT:
  159. AecImpl(error_element, QN_STANZA_RESOURCE_CONSTRAINT, "wait", "500");
  160. break;
  161. case XSE_SERVICE_UNAVAILABLE:
  162. AecImpl(error_element, QN_STANZA_SERVICE_UNAVAILABLE, "cancel", "503");
  163. break;
  164. case XSE_SUBSCRIPTION_REQUIRED:
  165. AecImpl(error_element, QN_STANZA_SUBSCRIPTION_REQUIRED, "auth", "407");
  166. break;
  167. case XSE_UNDEFINED_CONDITION:
  168. AecImpl(error_element, QN_STANZA_UNDEFINED_CONDITION, "wait", "500");
  169. break;
  170. case XSE_UNEXPECTED_REQUEST:
  171. AecImpl(error_element, QN_STANZA_UNEXPECTED_REQUEST, "wait", "400");
  172. break;
  173. }
  174. }
  175. XmppReturnStatus
  176. XmppEngineImpl::SendStanzaError(const XmlElement * element_original,
  177. XmppStanzaError code,
  178. const std::string & text) {
  179. if (state_ == STATE_CLOSED)
  180. return XMPP_RETURN_BADSTATE;
  181. XmlElement error_element(element_original->Name());
  182. error_element.AddAttr(QN_TYPE, "error");
  183. // copy attrs, copy 'from' to 'to' and strip 'from'
  184. for (const XmlAttr * attribute = element_original->FirstAttr();
  185. attribute; attribute = attribute->NextAttr()) {
  186. QName name = attribute->Name();
  187. if (name == QN_TO)
  188. continue; // no need to put a from attr. Server will stamp stanza
  189. else if (name == QN_FROM)
  190. name = QN_TO;
  191. else if (name == QN_TYPE)
  192. continue;
  193. error_element.AddAttr(name, attribute->Value());
  194. }
  195. // copy children
  196. for (const XmlChild * child = element_original->FirstChild();
  197. child;
  198. child = child->NextChild()) {
  199. if (child->IsText()) {
  200. error_element.AddText(child->AsText()->Text());
  201. } else {
  202. error_element.AddElement(new XmlElement(*(child->AsElement())));
  203. }
  204. }
  205. // add error information
  206. AddErrorCode(&error_element, code);
  207. if (text != STR_EMPTY) {
  208. XmlElement * text_element = new XmlElement(QN_STANZA_TEXT, true);
  209. text_element->AddText(text);
  210. error_element.AddElement(text_element);
  211. }
  212. SendStanza(&error_element);
  213. return XMPP_RETURN_OK;
  214. }
  215. bool
  216. XmppEngineImpl::HandleIqResponse(const XmlElement * element) {
  217. if (iq_entries_->empty())
  218. return false;
  219. if (element->Name() != QN_IQ)
  220. return false;
  221. std::string type = element->Attr(QN_TYPE);
  222. if (type != "result" && type != "error")
  223. return false;
  224. if (!element->HasAttr(QN_ID))
  225. return false;
  226. std::string id = element->Attr(QN_ID);
  227. std::string from = element->Attr(QN_FROM);
  228. for (std::vector<XmppIqEntry *>::iterator it = iq_entries_->begin();
  229. it != iq_entries_->end(); it += 1) {
  230. XmppIqEntry * iq_entry = *it;
  231. if (iq_entry->id_ == id && iq_entry->to_ == from) {
  232. iq_entries_->erase(it);
  233. iq_entry->iq_handler_->IqResponse(iq_entry, element);
  234. delete iq_entry;
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. }