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.

222 lines
7.0 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/examples/call/presencepushtask.h"
  28. #include "talk/base/stringencode.h"
  29. #include "talk/examples/call/muc.h"
  30. #include "talk/xmpp/constants.h"
  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 ListContainsToken(const std::string & list,
  38. 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() ||
  44. IsXmlSpace(list[i + token.length()]));
  45. return boundary_before && boundary_after;
  46. }
  47. bool PresencePushTask::HandleStanza(const XmlElement * stanza) {
  48. if (stanza->Name() != QN_PRESENCE)
  49. return false;
  50. QueueStanza(stanza);
  51. return true;
  52. }
  53. static bool IsUtf8FirstByte(int c) {
  54. return (((c)&0x80)==0) || // is single byte
  55. ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte
  56. }
  57. int PresencePushTask::ProcessStart() {
  58. const XmlElement * stanza = NextStanza();
  59. if (stanza == NULL)
  60. return STATE_BLOCKED;
  61. Jid from(stanza->Attr(QN_FROM));
  62. std::map<Jid, buzz::Muc*>::const_iterator elem =
  63. client_->mucs().find(from.BareJid());
  64. if (elem == client_->mucs().end()) {
  65. HandlePresence(from, stanza);
  66. } else {
  67. HandleMucPresence(elem->second, from, stanza);
  68. }
  69. return STATE_START;
  70. }
  71. void PresencePushTask::HandlePresence(const Jid& from,
  72. const XmlElement* stanza) {
  73. if (stanza->Attr(QN_TYPE) == STR_ERROR)
  74. return;
  75. PresenceStatus s;
  76. FillStatus(from, stanza, &s);
  77. SignalStatusUpdate(s);
  78. }
  79. void PresencePushTask::HandleMucPresence(buzz::Muc* muc,
  80. const Jid& from,
  81. const XmlElement* stanza) {
  82. if (from == muc->local_jid()) {
  83. if (!stanza->HasAttr(QN_TYPE)) {
  84. // We joined the MUC.
  85. const XmlElement* elem = stanza->FirstNamed(QN_MUC_USER_X);
  86. // Status code=110 or 100 is not guaranteed to be present, so we
  87. // only check the item element and Muc join status.
  88. if (elem) {
  89. if (elem->FirstNamed(QN_MUC_USER_ITEM) &&
  90. muc->state() == buzz::Muc::MUC_JOINING) {
  91. SignalMucJoined(muc->jid());
  92. }
  93. }
  94. } else {
  95. // We've been kicked. Bye.
  96. int error = 0;
  97. if (stanza->Attr(QN_TYPE) == STR_ERROR) {
  98. const XmlElement* elem = stanza->FirstNamed(QN_ERROR);
  99. if (elem && elem->HasAttr(QN_CODE)) {
  100. error = atoi(elem->Attr(QN_CODE).c_str());
  101. }
  102. }
  103. SignalMucLeft(muc->jid(), error);
  104. }
  105. } else {
  106. MucPresenceStatus s;
  107. FillMucStatus(from, stanza, &s);
  108. SignalMucStatusUpdate(muc->jid(), s);
  109. }
  110. }
  111. void PresencePushTask::FillStatus(const Jid& from, const XmlElement* stanza,
  112. PresenceStatus* s) {
  113. s->set_jid(from);
  114. if (stanza->Attr(QN_TYPE) == STR_UNAVAILABLE) {
  115. s->set_available(false);
  116. } else {
  117. s->set_available(true);
  118. const XmlElement * status = stanza->FirstNamed(QN_STATUS);
  119. if (status != NULL) {
  120. s->set_status(status->BodyText());
  121. // Truncate status messages longer than 300 bytes
  122. if (s->status().length() > 300) {
  123. size_t len = 300;
  124. // Be careful not to split legal utf-8 chars in half
  125. while (!IsUtf8FirstByte(s->status()[len]) && len > 0) {
  126. len -= 1;
  127. }
  128. std::string truncated(s->status(), 0, len);
  129. s->set_status(truncated);
  130. }
  131. }
  132. const XmlElement * priority = stanza->FirstNamed(QN_PRIORITY);
  133. if (priority != NULL) {
  134. int pri;
  135. if (talk_base::FromString(priority->BodyText(), &pri)) {
  136. s->set_priority(pri);
  137. }
  138. }
  139. const XmlElement * show = stanza->FirstNamed(QN_SHOW);
  140. if (show == NULL || show->FirstChild() == NULL) {
  141. s->set_show(PresenceStatus::SHOW_ONLINE);
  142. }
  143. else {
  144. if (show->BodyText() == "away") {
  145. s->set_show(PresenceStatus::SHOW_AWAY);
  146. }
  147. else if (show->BodyText() == "xa") {
  148. s->set_show(PresenceStatus::SHOW_XA);
  149. }
  150. else if (show->BodyText() == "dnd") {
  151. s->set_show(PresenceStatus::SHOW_DND);
  152. }
  153. else if (show->BodyText() == "chat") {
  154. s->set_show(PresenceStatus::SHOW_CHAT);
  155. }
  156. else {
  157. s->set_show(PresenceStatus::SHOW_ONLINE);
  158. }
  159. }
  160. const XmlElement * caps = stanza->FirstNamed(QN_CAPS_C);
  161. if (caps != NULL) {
  162. std::string node = caps->Attr(QN_NODE);
  163. std::string ver = caps->Attr(QN_VER);
  164. std::string exts = caps->Attr(QN_EXT);
  165. s->set_know_capabilities(true);
  166. s->set_caps_node(node);
  167. s->set_version(ver);
  168. if (ListContainsToken(exts, "voice-v1")) {
  169. s->set_voice_capability(true);
  170. }
  171. if (ListContainsToken(exts, "video-v1")) {
  172. s->set_video_capability(true);
  173. }
  174. }
  175. const XmlElement* delay = stanza->FirstNamed(kQnDelayX);
  176. if (delay != NULL) {
  177. // Ideally we would parse this according to the Psuedo ISO-8601 rules
  178. // that are laid out in JEP-0082:
  179. // http://www.jabber.org/jeps/jep-0082.html
  180. std::string stamp = delay->Attr(kQnStamp);
  181. s->set_sent_time(stamp);
  182. }
  183. const XmlElement* nick = stanza->FirstNamed(QN_NICKNAME);
  184. if (nick) {
  185. s->set_nick(nick->BodyText());
  186. }
  187. }
  188. }
  189. void PresencePushTask::FillMucStatus(const Jid& from, const XmlElement* stanza,
  190. MucPresenceStatus* s) {
  191. FillStatus(from, stanza, s);
  192. }
  193. }