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.

1084 lines
28 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 <string>
  29. #include <map>
  30. #include <algorithm>
  31. #include <sstream>
  32. #include <iostream>
  33. #include "talk/base/common.h"
  34. #include "talk/base/stringencode.h"
  35. #include "talk/xmpp/constants.h"
  36. #include "talk/xmpp/rostermoduleimpl.h"
  37. namespace buzz {
  38. // enum prase and persist helpers ----------------------------------------------
  39. static bool
  40. StringToPresenceShow(const std::string& input, XmppPresenceShow* show) {
  41. // If this becomes a perf issue we can use a hash or a map here
  42. if (STR_SHOW_AWAY == input)
  43. *show = XMPP_PRESENCE_AWAY;
  44. else if (STR_SHOW_DND == input)
  45. *show = XMPP_PRESENCE_DND;
  46. else if (STR_SHOW_XA == input)
  47. *show = XMPP_PRESENCE_XA;
  48. else if (STR_SHOW_CHAT == input)
  49. *show = XMPP_PRESENCE_CHAT;
  50. else if (STR_EMPTY == input)
  51. *show = XMPP_PRESENCE_DEFAULT;
  52. else
  53. return false;
  54. return true;
  55. }
  56. static bool
  57. PresenceShowToString(XmppPresenceShow show, const char** output) {
  58. switch(show) {
  59. case XMPP_PRESENCE_AWAY:
  60. *output = STR_SHOW_AWAY;
  61. return true;
  62. case XMPP_PRESENCE_CHAT:
  63. *output = STR_SHOW_CHAT;
  64. return true;
  65. case XMPP_PRESENCE_XA:
  66. *output = STR_SHOW_XA;
  67. return true;
  68. case XMPP_PRESENCE_DND:
  69. *output = STR_SHOW_DND;
  70. return true;
  71. case XMPP_PRESENCE_DEFAULT:
  72. *output = STR_EMPTY;
  73. return true;
  74. }
  75. *output = STR_EMPTY;
  76. return false;
  77. }
  78. static bool
  79. StringToSubscriptionState(const std::string& subscription,
  80. const std::string& ask,
  81. XmppSubscriptionState* state)
  82. {
  83. if (ask == "subscribe")
  84. {
  85. if (subscription == "none") {
  86. *state = XMPP_SUBSCRIPTION_NONE_ASKED;
  87. return true;
  88. }
  89. if (subscription == "from") {
  90. *state = XMPP_SUBSCRIPTION_FROM_ASKED;
  91. return true;
  92. }
  93. } else if (ask == STR_EMPTY)
  94. {
  95. if (subscription == "none") {
  96. *state = XMPP_SUBSCRIPTION_NONE;
  97. return true;
  98. }
  99. if (subscription == "from") {
  100. *state = XMPP_SUBSCRIPTION_FROM;
  101. return true;
  102. }
  103. if (subscription == "to") {
  104. *state = XMPP_SUBSCRIPTION_TO;
  105. return true;
  106. }
  107. if (subscription == "both") {
  108. *state = XMPP_SUBSCRIPTION_BOTH;
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. static bool
  115. StringToSubscriptionRequestType(const std::string& string,
  116. XmppSubscriptionRequestType* type)
  117. {
  118. if (string == "subscribe")
  119. *type = XMPP_REQUEST_SUBSCRIBE;
  120. else if (string == "unsubscribe")
  121. *type = XMPP_REQUEST_UNSUBSCRIBE;
  122. else if (string == "subscribed")
  123. *type = XMPP_REQUEST_SUBSCRIBED;
  124. else if (string == "unsubscribed")
  125. *type = XMPP_REQUEST_UNSUBSCRIBED;
  126. else
  127. return false;
  128. return true;
  129. }
  130. // XmppPresenceImpl class ------------------------------------------------------
  131. XmppPresence*
  132. XmppPresence::Create() {
  133. return new XmppPresenceImpl();
  134. }
  135. XmppPresenceImpl::XmppPresenceImpl() {
  136. }
  137. const Jid
  138. XmppPresenceImpl::jid() const {
  139. if (!raw_xml_)
  140. return Jid();
  141. return Jid(raw_xml_->Attr(QN_FROM));
  142. }
  143. XmppPresenceAvailable
  144. XmppPresenceImpl::available() const {
  145. if (!raw_xml_)
  146. return XMPP_PRESENCE_UNAVAILABLE;
  147. if (raw_xml_->Attr(QN_TYPE) == "unavailable")
  148. return XMPP_PRESENCE_UNAVAILABLE;
  149. else if (raw_xml_->Attr(QN_TYPE) == "error")
  150. return XMPP_PRESENCE_ERROR;
  151. else
  152. return XMPP_PRESENCE_AVAILABLE;
  153. }
  154. XmppReturnStatus
  155. XmppPresenceImpl::set_available(XmppPresenceAvailable available) {
  156. if (!raw_xml_)
  157. CreateRawXmlSkeleton();
  158. if (available == XMPP_PRESENCE_AVAILABLE)
  159. raw_xml_->ClearAttr(QN_TYPE);
  160. else if (available == XMPP_PRESENCE_UNAVAILABLE)
  161. raw_xml_->SetAttr(QN_TYPE, "unavailable");
  162. else if (available == XMPP_PRESENCE_ERROR)
  163. raw_xml_->SetAttr(QN_TYPE, "error");
  164. return XMPP_RETURN_OK;
  165. }
  166. XmppPresenceShow
  167. XmppPresenceImpl::presence_show() const {
  168. if (!raw_xml_)
  169. return XMPP_PRESENCE_DEFAULT;
  170. XmppPresenceShow show = XMPP_PRESENCE_DEFAULT;
  171. StringToPresenceShow(raw_xml_->TextNamed(QN_SHOW), &show);
  172. return show;
  173. }
  174. XmppReturnStatus
  175. XmppPresenceImpl::set_presence_show(XmppPresenceShow show) {
  176. if (!raw_xml_)
  177. CreateRawXmlSkeleton();
  178. const char* show_string;
  179. if(!PresenceShowToString(show, &show_string))
  180. return XMPP_RETURN_BADARGUMENT;
  181. raw_xml_->ClearNamedChildren(QN_SHOW);
  182. if (show!=XMPP_PRESENCE_DEFAULT) {
  183. raw_xml_->AddElement(new XmlElement(QN_SHOW));
  184. raw_xml_->AddText(show_string, 1);
  185. }
  186. return XMPP_RETURN_OK;
  187. }
  188. int
  189. XmppPresenceImpl::priority() const {
  190. if (!raw_xml_)
  191. return 0;
  192. int raw_priority = 0;
  193. if (!talk_base::FromString(raw_xml_->TextNamed(QN_PRIORITY), &raw_priority))
  194. raw_priority = 0;
  195. if (raw_priority < -128)
  196. raw_priority = -128;
  197. if (raw_priority > 127)
  198. raw_priority = 127;
  199. return raw_priority;
  200. }
  201. XmppReturnStatus
  202. XmppPresenceImpl::set_priority(int priority) {
  203. if (!raw_xml_)
  204. CreateRawXmlSkeleton();
  205. if (priority < -128 || priority > 127)
  206. return XMPP_RETURN_BADARGUMENT;
  207. raw_xml_->ClearNamedChildren(QN_PRIORITY);
  208. if (0 != priority) {
  209. std::string priority_string;
  210. if (talk_base::ToString(priority, &priority_string)) {
  211. raw_xml_->AddElement(new XmlElement(QN_PRIORITY));
  212. raw_xml_->AddText(priority_string, 1);
  213. }
  214. }
  215. return XMPP_RETURN_OK;
  216. }
  217. const std::string
  218. XmppPresenceImpl::status() const {
  219. if (!raw_xml_)
  220. return STR_EMPTY;
  221. XmlElement* status_element;
  222. XmlElement* element;
  223. // Search for a status element with no xml:lang attribute on it. if we can't
  224. // find that then just return the first status element in the stanza.
  225. for (status_element = element = raw_xml_->FirstNamed(QN_STATUS);
  226. element;
  227. element = element->NextNamed(QN_STATUS)) {
  228. if (!element->HasAttr(QN_XML_LANG)) {
  229. status_element = element;
  230. break;
  231. }
  232. }
  233. if (status_element) {
  234. return status_element->BodyText();
  235. }
  236. return STR_EMPTY;
  237. }
  238. XmppReturnStatus
  239. XmppPresenceImpl::set_status(const std::string& status) {
  240. if (!raw_xml_)
  241. CreateRawXmlSkeleton();
  242. raw_xml_->ClearNamedChildren(QN_STATUS);
  243. if (status != STR_EMPTY) {
  244. raw_xml_->AddElement(new XmlElement(QN_STATUS));
  245. raw_xml_->AddText(status, 1);
  246. }
  247. return XMPP_RETURN_OK;
  248. }
  249. XmppPresenceConnectionStatus
  250. XmppPresenceImpl::connection_status() const {
  251. if (!raw_xml_)
  252. return XMPP_CONNECTION_STATUS_UNKNOWN;
  253. XmlElement* con = raw_xml_->FirstNamed(QN_GOOGLE_PSTN_CONFERENCE_STATUS);
  254. if (con) {
  255. std::string status = con->Attr(QN_ATTR_STATUS);
  256. if (status == STR_PSTN_CONFERENCE_STATUS_CONNECTING)
  257. return XMPP_CONNECTION_STATUS_CONNECTING;
  258. else if (status == STR_PSTN_CONFERENCE_STATUS_CONNECTED)
  259. return XMPP_CONNECTION_STATUS_CONNECTED;
  260. else if (status == STR_PSTN_CONFERENCE_STATUS_HANGUP)
  261. return XMPP_CONNECTION_STATUS_HANGUP;
  262. }
  263. return XMPP_CONNECTION_STATUS_CONNECTED;
  264. }
  265. const std::string
  266. XmppPresenceImpl::google_user_id() const {
  267. if (!raw_xml_)
  268. return std::string();
  269. XmlElement* muc_user_x = raw_xml_->FirstNamed(QN_MUC_USER_X);
  270. if (muc_user_x) {
  271. XmlElement* muc_user_item = muc_user_x->FirstNamed(QN_MUC_USER_ITEM);
  272. if (muc_user_item) {
  273. return muc_user_item->Attr(QN_GOOGLE_USER_ID);
  274. }
  275. }
  276. return std::string();
  277. }
  278. const std::string
  279. XmppPresenceImpl::nickname() const {
  280. if (!raw_xml_)
  281. return std::string();
  282. XmlElement* nickname = raw_xml_->FirstNamed(QN_NICKNAME);
  283. if (nickname) {
  284. return nickname->BodyText();
  285. }
  286. return std::string();
  287. }
  288. const XmlElement*
  289. XmppPresenceImpl::raw_xml() const {
  290. if (!raw_xml_)
  291. const_cast<XmppPresenceImpl*>(this)->CreateRawXmlSkeleton();
  292. return raw_xml_.get();
  293. }
  294. XmppReturnStatus
  295. XmppPresenceImpl::set_raw_xml(const XmlElement * xml) {
  296. if (!xml ||
  297. xml->Name() != QN_PRESENCE)
  298. return XMPP_RETURN_BADARGUMENT;
  299. const std::string& type = xml->Attr(QN_TYPE);
  300. if (type != STR_EMPTY && type != "unavailable")
  301. return XMPP_RETURN_BADARGUMENT;
  302. raw_xml_.reset(new XmlElement(*xml));
  303. return XMPP_RETURN_OK;
  304. }
  305. void
  306. XmppPresenceImpl::CreateRawXmlSkeleton() {
  307. raw_xml_.reset(new XmlElement(QN_PRESENCE));
  308. }
  309. // XmppRosterContactImpl -------------------------------------------------------
  310. XmppRosterContact*
  311. XmppRosterContact::Create() {
  312. return new XmppRosterContactImpl();
  313. }
  314. XmppRosterContactImpl::XmppRosterContactImpl() {
  315. ResetGroupCache();
  316. }
  317. void
  318. XmppRosterContactImpl::SetXmlFromWire(const XmlElement* xml) {
  319. ResetGroupCache();
  320. if (xml)
  321. raw_xml_.reset(new XmlElement(*xml));
  322. else
  323. raw_xml_.reset(NULL);
  324. }
  325. void
  326. XmppRosterContactImpl::ResetGroupCache() {
  327. group_count_ = -1;
  328. group_index_returned_ = -1;
  329. group_returned_ = NULL;
  330. }
  331. const Jid
  332. XmppRosterContactImpl::jid() const {
  333. return Jid(raw_xml_->Attr(QN_JID));
  334. }
  335. XmppReturnStatus
  336. XmppRosterContactImpl::set_jid(const Jid& jid)
  337. {
  338. if (!raw_xml_)
  339. CreateRawXmlSkeleton();
  340. if (!jid.IsValid())
  341. return XMPP_RETURN_BADARGUMENT;
  342. raw_xml_->SetAttr(QN_JID, jid.Str());
  343. return XMPP_RETURN_OK;
  344. }
  345. const std::string
  346. XmppRosterContactImpl::name() const {
  347. return raw_xml_->Attr(QN_NAME);
  348. }
  349. XmppReturnStatus
  350. XmppRosterContactImpl::set_name(const std::string& name) {
  351. if (!raw_xml_)
  352. CreateRawXmlSkeleton();
  353. if (name == STR_EMPTY)
  354. raw_xml_->ClearAttr(QN_NAME);
  355. else
  356. raw_xml_->SetAttr(QN_NAME, name);
  357. return XMPP_RETURN_OK;
  358. }
  359. XmppSubscriptionState
  360. XmppRosterContactImpl::subscription_state() const {
  361. if (!raw_xml_)
  362. return XMPP_SUBSCRIPTION_NONE;
  363. XmppSubscriptionState state = XMPP_SUBSCRIPTION_NONE;
  364. if (StringToSubscriptionState(raw_xml_->Attr(QN_SUBSCRIPTION),
  365. raw_xml_->Attr(QN_ASK),
  366. &state))
  367. return state;
  368. return XMPP_SUBSCRIPTION_NONE;
  369. }
  370. size_t
  371. XmppRosterContactImpl::GetGroupCount() const {
  372. if (!raw_xml_)
  373. return 0;
  374. if (-1 == group_count_) {
  375. XmlElement *group_element = raw_xml_->FirstNamed(QN_ROSTER_GROUP);
  376. int group_count = 0;
  377. while(group_element) {
  378. group_count++;
  379. group_element = group_element->NextNamed(QN_ROSTER_GROUP);
  380. }
  381. ASSERT(group_count > 0); // protect the cast
  382. XmppRosterContactImpl * me = const_cast<XmppRosterContactImpl*>(this);
  383. me->group_count_ = group_count;
  384. }
  385. return group_count_;
  386. }
  387. const std::string
  388. XmppRosterContactImpl::GetGroup(size_t index) const {
  389. if (index >= GetGroupCount())
  390. return STR_EMPTY;
  391. // We cache the last group index and element that we returned. This way
  392. // going through the groups in order is order n and not n^2. This could be
  393. // enhanced if necessary by starting at the cached value if the index asked
  394. // is after the cached one.
  395. if (group_index_returned_ >= 0 &&
  396. index == static_cast<size_t>(group_index_returned_) + 1)
  397. {
  398. XmppRosterContactImpl * me = const_cast<XmppRosterContactImpl*>(this);
  399. me->group_returned_ = group_returned_->NextNamed(QN_ROSTER_GROUP);
  400. ASSERT(group_returned_ != NULL);
  401. me->group_index_returned_++;
  402. } else if (group_index_returned_ < 0 ||
  403. static_cast<size_t>(group_index_returned_) != index) {
  404. XmlElement * group_element = raw_xml_->FirstNamed(QN_ROSTER_GROUP);
  405. size_t group_index = 0;
  406. while(group_index < index) {
  407. ASSERT(group_element != NULL);
  408. group_index++;
  409. group_element = group_element->NextNamed(QN_ROSTER_GROUP);
  410. }
  411. XmppRosterContactImpl * me = const_cast<XmppRosterContactImpl*>(this);
  412. me->group_index_returned_ = static_cast<int>(group_index);
  413. me->group_returned_ = group_element;
  414. }
  415. return group_returned_->BodyText();
  416. }
  417. XmppReturnStatus
  418. XmppRosterContactImpl::AddGroup(const std::string& group) {
  419. if (group == STR_EMPTY)
  420. return XMPP_RETURN_BADARGUMENT;
  421. if (!raw_xml_)
  422. CreateRawXmlSkeleton();
  423. if (FindGroup(group, NULL, NULL))
  424. return XMPP_RETURN_OK;
  425. raw_xml_->AddElement(new XmlElement(QN_ROSTER_GROUP));
  426. raw_xml_->AddText(group, 1);
  427. ++group_count_;
  428. return XMPP_RETURN_OK;
  429. }
  430. XmppReturnStatus
  431. XmppRosterContactImpl::RemoveGroup(const std::string& group) {
  432. if (group == STR_EMPTY)
  433. return XMPP_RETURN_BADARGUMENT;
  434. if (!raw_xml_)
  435. return XMPP_RETURN_OK;
  436. XmlChild * child_before;
  437. if (FindGroup(group, NULL, &child_before)) {
  438. raw_xml_->RemoveChildAfter(child_before);
  439. ResetGroupCache();
  440. }
  441. return XMPP_RETURN_OK;
  442. }
  443. bool
  444. XmppRosterContactImpl::FindGroup(const std::string& group,
  445. XmlElement** element,
  446. XmlChild** child_before) {
  447. XmlChild * prev_child = NULL;
  448. XmlChild * next_child;
  449. XmlChild * child;
  450. for (child = raw_xml_->FirstChild(); child; child = next_child) {
  451. next_child = child->NextChild();
  452. if (!child->IsText() &&
  453. child->AsElement()->Name() == QN_ROSTER_GROUP &&
  454. child->AsElement()->BodyText() == group) {
  455. if (element)
  456. *element = child->AsElement();
  457. if (child_before)
  458. *child_before = prev_child;
  459. return true;
  460. }
  461. prev_child = child;
  462. }
  463. return false;
  464. }
  465. const XmlElement*
  466. XmppRosterContactImpl::raw_xml() const {
  467. if (!raw_xml_)
  468. const_cast<XmppRosterContactImpl*>(this)->CreateRawXmlSkeleton();
  469. return raw_xml_.get();
  470. }
  471. XmppReturnStatus
  472. XmppRosterContactImpl::set_raw_xml(const XmlElement* xml) {
  473. if (!xml ||
  474. xml->Name() != QN_ROSTER_ITEM ||
  475. xml->HasAttr(QN_SUBSCRIPTION) ||
  476. xml->HasAttr(QN_ASK))
  477. return XMPP_RETURN_BADARGUMENT;
  478. ResetGroupCache();
  479. raw_xml_.reset(new XmlElement(*xml));
  480. return XMPP_RETURN_OK;
  481. }
  482. void
  483. XmppRosterContactImpl::CreateRawXmlSkeleton() {
  484. raw_xml_.reset(new XmlElement(QN_ROSTER_ITEM));
  485. }
  486. // XmppRosterModuleImpl --------------------------------------------------------
  487. XmppRosterModule *
  488. XmppRosterModule::Create() {
  489. return new XmppRosterModuleImpl();
  490. }
  491. XmppRosterModuleImpl::XmppRosterModuleImpl() :
  492. roster_handler_(NULL),
  493. incoming_presence_map_(new JidPresenceVectorMap()),
  494. incoming_presence_vector_(new PresenceVector()),
  495. contacts_(new ContactVector()) {
  496. }
  497. XmppRosterModuleImpl::~XmppRosterModuleImpl() {
  498. DeleteIncomingPresence();
  499. DeleteContacts();
  500. }
  501. XmppReturnStatus
  502. XmppRosterModuleImpl::set_roster_handler(XmppRosterHandler * handler) {
  503. roster_handler_ = handler;
  504. return XMPP_RETURN_OK;
  505. }
  506. XmppRosterHandler*
  507. XmppRosterModuleImpl::roster_handler() {
  508. return roster_handler_;
  509. }
  510. XmppPresence*
  511. XmppRosterModuleImpl::outgoing_presence() {
  512. return &outgoing_presence_;
  513. }
  514. XmppReturnStatus
  515. XmppRosterModuleImpl::BroadcastPresence() {
  516. // Scrub the outgoing presence
  517. const XmlElement* element = outgoing_presence_.raw_xml();
  518. ASSERT(!element->HasAttr(QN_TO) &&
  519. !element->HasAttr(QN_FROM) &&
  520. (element->Attr(QN_TYPE) == STR_EMPTY ||
  521. element->Attr(QN_TYPE) == "unavailable"));
  522. if (!engine())
  523. return XMPP_RETURN_BADSTATE;
  524. return engine()->SendStanza(element);
  525. }
  526. XmppReturnStatus
  527. XmppRosterModuleImpl::SendDirectedPresence(const XmppPresence* presence,
  528. const Jid& to_jid) {
  529. if (!presence)
  530. return XMPP_RETURN_BADARGUMENT;
  531. if (!engine())
  532. return XMPP_RETURN_BADSTATE;
  533. XmlElement element(*(presence->raw_xml()));
  534. if (element.Name() != QN_PRESENCE ||
  535. element.HasAttr(QN_TO) ||
  536. element.HasAttr(QN_FROM))
  537. return XMPP_RETURN_BADARGUMENT;
  538. if (element.HasAttr(QN_TYPE)) {
  539. if (element.Attr(QN_TYPE) != STR_EMPTY &&
  540. element.Attr(QN_TYPE) != "unavailable") {
  541. return XMPP_RETURN_BADARGUMENT;
  542. }
  543. }
  544. element.SetAttr(QN_TO, to_jid.Str());
  545. return engine()->SendStanza(&element);
  546. }
  547. size_t
  548. XmppRosterModuleImpl::GetIncomingPresenceCount() {
  549. return incoming_presence_vector_->size();
  550. }
  551. const XmppPresence*
  552. XmppRosterModuleImpl::GetIncomingPresence(size_t index) {
  553. if (index >= incoming_presence_vector_->size())
  554. return NULL;
  555. return (*incoming_presence_vector_)[index];
  556. }
  557. size_t
  558. XmppRosterModuleImpl::GetIncomingPresenceForJidCount(const Jid& jid)
  559. {
  560. // find the vector in the map
  561. JidPresenceVectorMap::iterator pos;
  562. pos = incoming_presence_map_->find(jid);
  563. if (pos == incoming_presence_map_->end())
  564. return 0;
  565. ASSERT(pos->second != NULL);
  566. return pos->second->size();
  567. }
  568. const XmppPresence*
  569. XmppRosterModuleImpl::GetIncomingPresenceForJid(const Jid& jid,
  570. size_t index) {
  571. JidPresenceVectorMap::iterator pos;
  572. pos = incoming_presence_map_->find(jid);
  573. if (pos == incoming_presence_map_->end())
  574. return NULL;
  575. ASSERT(pos->second != NULL);
  576. if (index >= pos->second->size())
  577. return NULL;
  578. return (*pos->second)[index];
  579. }
  580. XmppReturnStatus
  581. XmppRosterModuleImpl::RequestRosterUpdate() {
  582. if (!engine())
  583. return XMPP_RETURN_BADSTATE;
  584. XmlElement roster_get(QN_IQ);
  585. roster_get.AddAttr(QN_TYPE, "get");
  586. roster_get.AddAttr(QN_ID, engine()->NextId());
  587. roster_get.AddElement(new XmlElement(QN_ROSTER_QUERY, true));
  588. return engine()->SendIq(&roster_get, this, NULL);
  589. }
  590. size_t
  591. XmppRosterModuleImpl::GetRosterContactCount() {
  592. return contacts_->size();
  593. }
  594. const XmppRosterContact*
  595. XmppRosterModuleImpl::GetRosterContact(size_t index) {
  596. if (index >= contacts_->size())
  597. return NULL;
  598. return (*contacts_)[index];
  599. }
  600. class RosterPredicate {
  601. public:
  602. explicit RosterPredicate(const Jid& jid) : jid_(jid) {
  603. }
  604. bool operator() (XmppRosterContactImpl *& contact) {
  605. return contact->jid() == jid_;
  606. }
  607. private:
  608. Jid jid_;
  609. };
  610. const XmppRosterContact*
  611. XmppRosterModuleImpl::FindRosterContact(const Jid& jid) {
  612. ContactVector::iterator pos;
  613. pos = std::find_if(contacts_->begin(),
  614. contacts_->end(),
  615. RosterPredicate(jid));
  616. if (pos == contacts_->end())
  617. return NULL;
  618. return *pos;
  619. }
  620. XmppReturnStatus
  621. XmppRosterModuleImpl::RequestRosterChange(
  622. const XmppRosterContact* contact) {
  623. if (!contact)
  624. return XMPP_RETURN_BADARGUMENT;
  625. Jid jid = contact->jid();
  626. if (!jid.IsValid())
  627. return XMPP_RETURN_BADARGUMENT;
  628. if (!engine())
  629. return XMPP_RETURN_BADSTATE;
  630. const XmlElement* contact_xml = contact->raw_xml();
  631. if (contact_xml->Name() != QN_ROSTER_ITEM ||
  632. contact_xml->HasAttr(QN_SUBSCRIPTION) ||
  633. contact_xml->HasAttr(QN_ASK))
  634. return XMPP_RETURN_BADARGUMENT;
  635. XmlElement roster_add(QN_IQ);
  636. roster_add.AddAttr(QN_TYPE, "set");
  637. roster_add.AddAttr(QN_ID, engine()->NextId());
  638. roster_add.AddElement(new XmlElement(QN_ROSTER_QUERY, true));
  639. roster_add.AddElement(new XmlElement(*contact_xml), 1);
  640. return engine()->SendIq(&roster_add, this, NULL);
  641. }
  642. XmppReturnStatus
  643. XmppRosterModuleImpl::RequestRosterRemove(const Jid& jid) {
  644. if (!jid.IsValid())
  645. return XMPP_RETURN_BADARGUMENT;
  646. if (!engine())
  647. return XMPP_RETURN_BADSTATE;
  648. XmlElement roster_add(QN_IQ);
  649. roster_add.AddAttr(QN_TYPE, "set");
  650. roster_add.AddAttr(QN_ID, engine()->NextId());
  651. roster_add.AddElement(new XmlElement(QN_ROSTER_QUERY, true));
  652. roster_add.AddAttr(QN_JID, jid.Str(), 1);
  653. roster_add.AddAttr(QN_SUBSCRIPTION, "remove", 1);
  654. return engine()->SendIq(&roster_add, this, NULL);
  655. }
  656. XmppReturnStatus
  657. XmppRosterModuleImpl::RequestSubscription(const Jid& jid) {
  658. return SendSubscriptionRequest(jid, "subscribe");
  659. }
  660. XmppReturnStatus
  661. XmppRosterModuleImpl::CancelSubscription(const Jid& jid) {
  662. return SendSubscriptionRequest(jid, "unsubscribe");
  663. }
  664. XmppReturnStatus
  665. XmppRosterModuleImpl::ApproveSubscriber(const Jid& jid) {
  666. return SendSubscriptionRequest(jid, "subscribed");
  667. }
  668. XmppReturnStatus
  669. XmppRosterModuleImpl::CancelSubscriber(const Jid& jid) {
  670. return SendSubscriptionRequest(jid, "unsubscribed");
  671. }
  672. void
  673. XmppRosterModuleImpl::IqResponse(XmppIqCookie, const XmlElement * stanza) {
  674. // The only real Iq response that we expect to recieve are initial roster
  675. // population
  676. if (stanza->Attr(QN_TYPE) == "error")
  677. {
  678. if (roster_handler_)
  679. roster_handler_->RosterError(this, stanza);
  680. return;
  681. }
  682. ASSERT(stanza->Attr(QN_TYPE) == "result");
  683. InternalRosterItems(stanza);
  684. }
  685. bool
  686. XmppRosterModuleImpl::HandleStanza(const XmlElement * stanza)
  687. {
  688. ASSERT(engine() != NULL);
  689. // There are two types of stanzas that we care about: presence and roster push
  690. // Iqs
  691. if (stanza->Name() == QN_PRESENCE) {
  692. const std::string& jid_string = stanza->Attr(QN_FROM);
  693. Jid jid(jid_string);
  694. if (!jid.IsValid())
  695. return false; // if the Jid isn't valid, don't process
  696. const std::string& type = stanza->Attr(QN_TYPE);
  697. XmppSubscriptionRequestType request_type;
  698. if (StringToSubscriptionRequestType(type, &request_type))
  699. InternalSubscriptionRequest(jid, stanza, request_type);
  700. else if (type == "unavailable" || type == STR_EMPTY)
  701. InternalIncomingPresence(jid, stanza);
  702. else if (type == "error")
  703. InternalIncomingPresenceError(jid, stanza);
  704. else
  705. return false;
  706. return true;
  707. } else if (stanza->Name() == QN_IQ) {
  708. const XmlElement * roster_query = stanza->FirstNamed(QN_ROSTER_QUERY);
  709. if (!roster_query || stanza->Attr(QN_TYPE) != "set")
  710. return false;
  711. InternalRosterItems(stanza);
  712. // respond to the IQ
  713. XmlElement result(QN_IQ);
  714. result.AddAttr(QN_TYPE, "result");
  715. result.AddAttr(QN_TO, stanza->Attr(QN_FROM));
  716. result.AddAttr(QN_ID, stanza->Attr(QN_ID));
  717. engine()->SendStanza(&result);
  718. return true;
  719. }
  720. return false;
  721. }
  722. void
  723. XmppRosterModuleImpl::DeleteIncomingPresence() {
  724. // Clear out the vector of all presence notifications
  725. {
  726. PresenceVector::iterator pos;
  727. for (pos = incoming_presence_vector_->begin();
  728. pos < incoming_presence_vector_->end();
  729. ++pos) {
  730. XmppPresenceImpl * presence = *pos;
  731. *pos = NULL;
  732. delete presence;
  733. }
  734. incoming_presence_vector_->clear();
  735. }
  736. // Clear out all of the small presence vectors per Jid
  737. {
  738. JidPresenceVectorMap::iterator pos;
  739. for (pos = incoming_presence_map_->begin();
  740. pos != incoming_presence_map_->end();
  741. ++pos) {
  742. PresenceVector* presence_vector = pos->second;
  743. pos->second = NULL;
  744. delete presence_vector;
  745. }
  746. incoming_presence_map_->clear();
  747. }
  748. }
  749. void
  750. XmppRosterModuleImpl::DeleteContacts() {
  751. ContactVector::iterator pos;
  752. for (pos = contacts_->begin();
  753. pos < contacts_->end();
  754. ++pos) {
  755. XmppRosterContact* contact = *pos;
  756. *pos = NULL;
  757. delete contact;
  758. }
  759. contacts_->clear();
  760. }
  761. XmppReturnStatus
  762. XmppRosterModuleImpl::SendSubscriptionRequest(const Jid& jid,
  763. const std::string& type) {
  764. if (!jid.IsValid())
  765. return XMPP_RETURN_BADARGUMENT;
  766. if (!engine())
  767. return XMPP_RETURN_BADSTATE;
  768. XmlElement presence_request(QN_PRESENCE);
  769. presence_request.AddAttr(QN_TO, jid.Str());
  770. presence_request.AddAttr(QN_TYPE, type);
  771. return engine()->SendStanza(&presence_request);
  772. }
  773. void
  774. XmppRosterModuleImpl::InternalSubscriptionRequest(const Jid& jid,
  775. const XmlElement* stanza,
  776. XmppSubscriptionRequestType
  777. request_type) {
  778. if (roster_handler_)
  779. roster_handler_->SubscriptionRequest(this, jid, request_type, stanza);
  780. }
  781. class PresencePredicate {
  782. public:
  783. explicit PresencePredicate(const Jid& jid) : jid_(jid) {
  784. }
  785. bool operator() (XmppPresenceImpl *& contact) {
  786. return contact->jid() == jid_;
  787. }
  788. private:
  789. Jid jid_;
  790. };
  791. void
  792. XmppRosterModuleImpl::InternalIncomingPresence(const Jid& jid,
  793. const XmlElement* stanza) {
  794. bool added = false;
  795. Jid bare_jid = jid.BareJid();
  796. // First add the presence to the map
  797. JidPresenceVectorMap::iterator pos;
  798. pos = incoming_presence_map_->find(jid.BareJid());
  799. if (pos == incoming_presence_map_->end()) {
  800. // Insert a new entry into the map. Get the position of this new entry
  801. pos = (incoming_presence_map_->insert(
  802. std::make_pair(bare_jid, new PresenceVector()))).first;
  803. }
  804. PresenceVector * presence_vector = pos->second;
  805. ASSERT(presence_vector != NULL);
  806. // Try to find this jid in the bare jid bucket
  807. PresenceVector::iterator presence_pos;
  808. XmppPresenceImpl* presence;
  809. presence_pos = std::find_if(presence_vector->begin(),
  810. presence_vector->end(),
  811. PresencePredicate(jid));
  812. // Update/add it to the bucket
  813. if (presence_pos == presence_vector->end()) {
  814. presence = new XmppPresenceImpl();
  815. if (XMPP_RETURN_OK == presence->set_raw_xml(stanza)) {
  816. added = true;
  817. presence_vector->push_back(presence);
  818. } else {
  819. delete presence;
  820. presence = NULL;
  821. }
  822. } else {
  823. presence = *presence_pos;
  824. presence->set_raw_xml(stanza);
  825. }
  826. // now add to the comprehensive vector
  827. if (added)
  828. incoming_presence_vector_->push_back(presence);
  829. // Call back to the user with the changed presence information
  830. if (roster_handler_)
  831. roster_handler_->IncomingPresenceChanged(this, presence);
  832. }
  833. void
  834. XmppRosterModuleImpl::InternalIncomingPresenceError(const Jid& jid,
  835. const XmlElement* stanza) {
  836. if (roster_handler_)
  837. roster_handler_->SubscriptionError(this, jid, stanza);
  838. }
  839. void
  840. XmppRosterModuleImpl::InternalRosterItems(const XmlElement* stanza) {
  841. const XmlElement* result_data = stanza->FirstNamed(QN_ROSTER_QUERY);
  842. if (!result_data)
  843. return; // unknown stuff in result!
  844. bool all_new = contacts_->empty();
  845. for (const XmlElement* roster_item = result_data->FirstNamed(QN_ROSTER_ITEM);
  846. roster_item;
  847. roster_item = roster_item->NextNamed(QN_ROSTER_ITEM))
  848. {
  849. const std::string& jid_string = roster_item->Attr(QN_JID);
  850. Jid jid(jid_string);
  851. if (!jid.IsValid())
  852. continue;
  853. // This algorithm is N^2 on the number of incoming contacts after the
  854. // initial load. There is no way to do this faster without allowing
  855. // duplicates, introducing more data structures or write a custom data
  856. // structure. We'll see if this becomes a perf problem and fix it if it
  857. // does.
  858. ContactVector::iterator pos = contacts_->end();
  859. if (!all_new) {
  860. pos = std::find_if(contacts_->begin(),
  861. contacts_->end(),
  862. RosterPredicate(jid));
  863. }
  864. if (pos != contacts_->end()) { // Update/remove a current contact
  865. if (roster_item->Attr(QN_SUBSCRIPTION) == "remove") {
  866. XmppRosterContact* contact = *pos;
  867. contacts_->erase(pos);
  868. if (roster_handler_)
  869. roster_handler_->ContactRemoved(this, contact,
  870. std::distance(contacts_->begin(), pos));
  871. delete contact;
  872. } else {
  873. XmppRosterContact* old_contact = *pos;
  874. *pos = new XmppRosterContactImpl();
  875. (*pos)->SetXmlFromWire(roster_item);
  876. if (roster_handler_)
  877. roster_handler_->ContactChanged(this, old_contact,
  878. std::distance(contacts_->begin(), pos));
  879. delete old_contact;
  880. }
  881. } else { // Add a new contact
  882. XmppRosterContactImpl* contact = new XmppRosterContactImpl();
  883. contact->SetXmlFromWire(roster_item);
  884. contacts_->push_back(contact);
  885. if (roster_handler_ && !all_new)
  886. roster_handler_->ContactsAdded(this, contacts_->size() - 1, 1);
  887. }
  888. }
  889. // Send a consolidated update if all contacts are new
  890. if (roster_handler_ && all_new)
  891. roster_handler_->ContactsAdded(this, 0, contacts_->size());
  892. }
  893. }