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.

201 lines
6.1 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 "talk/base/stringencode.h"
  28. #include "presencepushtask.h"
  29. #include "talk/xmpp/constants.h"
  30. #include <sstream>
  31. namespace buzz {
  32. // string helper functions -----------------------------------------------------
  33. static bool
  34. IsXmlSpace(int ch) {
  35. return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  36. }
  37. static bool
  38. ListContainsToken(const std::string & list, const std::string & token) {
  39. size_t i = list.find(token);
  40. if (i == std::string::npos || token.empty())
  41. return false;
  42. bool boundary_before = (i == 0 || IsXmlSpace(list[i - 1]));
  43. bool boundary_after = (i == list.length() - token.length() || IsXmlSpace(list[i + token.length()]));
  44. return boundary_before && boundary_after;
  45. }
  46. bool
  47. PresencePushTask::HandleStanza(const XmlElement * stanza) {
  48. if (stanza->Name() != QN_PRESENCE)
  49. return false;
  50. if (stanza->HasAttr(QN_TYPE) && stanza->Attr(QN_TYPE) != STR_UNAVAILABLE) {
  51. if (stanza->Attr(QN_TYPE) == STR_ERROR) {
  52. // Pass on the error.
  53. const XmlElement* error_xml_elem = stanza->FirstNamed(QN_ERROR);
  54. if (!error_xml_elem) {
  55. return false;
  56. }
  57. SignalStatusError(*error_xml_elem);
  58. return true;
  59. }
  60. }
  61. QueueStanza(stanza);
  62. return true;
  63. }
  64. static bool IsUtf8FirstByte(int c) {
  65. return (((c)&0x80)==0) || // is single byte
  66. ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte
  67. }
  68. int
  69. PresencePushTask::ProcessStart() {
  70. const XmlElement * stanza = NextStanza();
  71. if (stanza == NULL)
  72. return STATE_BLOCKED;
  73. Status s;
  74. s.set_jid(Jid(stanza->Attr(QN_FROM)));
  75. if (stanza->Attr(QN_TYPE) == STR_UNAVAILABLE) {
  76. s.set_available(false);
  77. SignalStatusUpdate(s);
  78. }
  79. else {
  80. s.set_available(true);
  81. const XmlElement * status = stanza->FirstNamed(QN_STATUS);
  82. if (status != NULL) {
  83. s.set_status(status->BodyText());
  84. // Truncate status messages longer than 300 bytes
  85. if (s.status().length() > 300) {
  86. size_t len = 300;
  87. // Be careful not to split legal utf-8 chars in half
  88. while (!IsUtf8FirstByte(s.status()[len]) && len > 0) {
  89. len -= 1;
  90. }
  91. std::string truncated(s.status(), 0, len);
  92. s.set_status(truncated);
  93. }
  94. }
  95. const XmlElement * priority = stanza->FirstNamed(QN_PRIORITY);
  96. if (priority != NULL) {
  97. int pri;
  98. if (talk_base::FromString(priority->BodyText(), &pri)) {
  99. s.set_priority(pri);
  100. }
  101. }
  102. const XmlElement * show = stanza->FirstNamed(QN_SHOW);
  103. if (show == NULL || show->FirstChild() == NULL) {
  104. s.set_show(Status::SHOW_ONLINE);
  105. }
  106. else {
  107. if (show->BodyText() == "away") {
  108. s.set_show(Status::SHOW_AWAY);
  109. }
  110. else if (show->BodyText() == "xa") {
  111. s.set_show(Status::SHOW_XA);
  112. }
  113. else if (show->BodyText() == "dnd") {
  114. s.set_show(Status::SHOW_DND);
  115. }
  116. else if (show->BodyText() == "chat") {
  117. s.set_show(Status::SHOW_CHAT);
  118. }
  119. else {
  120. s.set_show(Status::SHOW_ONLINE);
  121. }
  122. }
  123. const XmlElement * caps = stanza->FirstNamed(QN_CAPS_C);
  124. if (caps != NULL) {
  125. std::string node = caps->Attr(QN_NODE);
  126. std::string ver = caps->Attr(QN_VER);
  127. std::string exts = caps->Attr(QN_EXT);
  128. s.set_know_capabilities(true);
  129. std::string capability;
  130. std::stringstream ss(exts);
  131. while (ss >> capability) {
  132. s.AddCapability(capability);
  133. }
  134. s->set_caps_node(node);
  135. s->set_version(ver);
  136. }
  137. const XmlElement* delay = stanza->FirstNamed(kQnDelayX);
  138. if (delay != NULL) {
  139. // Ideally we would parse this according to the Psuedo ISO-8601 rules
  140. // that are laid out in JEP-0082:
  141. // http://www.jabber.org/jeps/jep-0082.html
  142. std::string stamp = delay->Attr(kQnStamp);
  143. s.set_sent_time(stamp);
  144. }
  145. const XmlElement *nick = stanza->FirstNamed(kQnNickname);
  146. if (nick) {
  147. std::string user_nick = nick->BodyText();
  148. s.set_user_nick(user_nick);
  149. }
  150. const XmlElement *plugin = stanza->FirstNamed(QN_PLUGIN);
  151. if (plugin) {
  152. const XmlElement *api_cap = plugin->FirstNamed(QN_CAPABILITY);
  153. if (api_cap) {
  154. const std::string &api_capability = api_cap->BodyText();
  155. s.set_api_capability(api_capability);
  156. }
  157. const XmlElement *api_msg = plugin->FirstNamed(QN_DATA);
  158. if (api_msg) {
  159. const std::string &api_message = api_msg->BodyText();
  160. s.set_api_message(api_message);
  161. }
  162. }
  163. const XmlElement* data_x = stanza->FirstNamed(QN_MUC_USER_X);
  164. if (data_x != NULL) {
  165. const XmlElement* item = data_x->FirstNamed(QN_MUC_USER_ITEM);
  166. if (item != NULL) {
  167. s.set_muc_role(item->Attr(QN_ROLE));
  168. }
  169. }
  170. SignalStatusUpdate(s);
  171. }
  172. return STATE_START;
  173. }
  174. }