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.

157 lines
4.9 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 <time.h>
  28. #include <sstream>
  29. #include "talk/base/stringencode.h"
  30. #include "talk/xmpp/constants.h"
  31. #include "talk/xmpp/presenceouttask.h"
  32. #include "talk/xmpp/xmppclient.h"
  33. namespace buzz {
  34. XmppReturnStatus
  35. PresenceOutTask::Send(const PresenceStatus & s) {
  36. if (GetState() != STATE_INIT && GetState() != STATE_START)
  37. return XMPP_RETURN_BADSTATE;
  38. XmlElement * presence = TranslateStatus(s);
  39. QueueStanza(presence);
  40. delete presence;
  41. return XMPP_RETURN_OK;
  42. }
  43. XmppReturnStatus
  44. PresenceOutTask::SendDirected(const Jid & j, const PresenceStatus & s) {
  45. if (GetState() != STATE_INIT && GetState() != STATE_START)
  46. return XMPP_RETURN_BADSTATE;
  47. XmlElement * presence = TranslateStatus(s);
  48. presence->AddAttr(QN_TO, j.Str());
  49. QueueStanza(presence);
  50. delete presence;
  51. return XMPP_RETURN_OK;
  52. }
  53. XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) {
  54. if (GetState() != STATE_INIT && GetState() != STATE_START)
  55. return XMPP_RETURN_BADSTATE;
  56. XmlElement * presence = new XmlElement(QN_PRESENCE);
  57. presence->AddAttr(QN_TO, jid.Str());
  58. presence->AddAttr(QN_TYPE, "probe");
  59. QueueStanza(presence);
  60. delete presence;
  61. return XMPP_RETURN_OK;
  62. }
  63. int
  64. PresenceOutTask::ProcessStart() {
  65. const XmlElement * stanza = NextStanza();
  66. if (stanza == NULL)
  67. return STATE_BLOCKED;
  68. if (SendStanza(stanza) != XMPP_RETURN_OK)
  69. return STATE_ERROR;
  70. return STATE_START;
  71. }
  72. XmlElement *
  73. PresenceOutTask::TranslateStatus(const PresenceStatus & s) {
  74. XmlElement * result = new XmlElement(QN_PRESENCE);
  75. if (!s.available()) {
  76. result->AddAttr(QN_TYPE, STR_UNAVAILABLE);
  77. }
  78. else {
  79. if (s.show() != PresenceStatus::SHOW_ONLINE &&
  80. s.show() != PresenceStatus::SHOW_OFFLINE) {
  81. result->AddElement(new XmlElement(QN_SHOW));
  82. switch (s.show()) {
  83. default:
  84. result->AddText(STR_SHOW_AWAY, 1);
  85. break;
  86. case PresenceStatus::SHOW_XA:
  87. result->AddText(STR_SHOW_XA, 1);
  88. break;
  89. case PresenceStatus::SHOW_DND:
  90. result->AddText(STR_SHOW_DND, 1);
  91. break;
  92. case PresenceStatus::SHOW_CHAT:
  93. result->AddText(STR_SHOW_CHAT, 1);
  94. break;
  95. }
  96. }
  97. result->AddElement(new XmlElement(QN_STATUS));
  98. result->AddText(s.status(), 1);
  99. if (!s.nick().empty()) {
  100. result->AddElement(new XmlElement(QN_NICKNAME));
  101. result->AddText(s.nick(), 1);
  102. }
  103. std::string pri;
  104. talk_base::ToString(s.priority(), &pri);
  105. result->AddElement(new XmlElement(QN_PRIORITY));
  106. result->AddText(pri, 1);
  107. if (s.know_capabilities()) {
  108. result->AddElement(new XmlElement(QN_CAPS_C, true));
  109. result->AddAttr(QN_NODE, s.caps_node(), 1);
  110. result->AddAttr(QN_VER, s.version(), 1);
  111. std::string caps;
  112. caps.append(s.voice_capability() ? "voice-v1" : "");
  113. caps.append(s.pmuc_capability() ? " pmuc-v1" : "");
  114. caps.append(s.video_capability() ? " video-v1" : "");
  115. caps.append(s.camera_capability() ? " camera-v1" : "");
  116. result->AddAttr(QN_EXT, caps, 1);
  117. }
  118. // Put the delay mark on the presence according to JEP-0091
  119. {
  120. result->AddElement(new XmlElement(kQnDelayX, true));
  121. // This here is why we *love* the C runtime
  122. time_t current_time_seconds;
  123. time(&current_time_seconds);
  124. struct tm* current_time = gmtime(&current_time_seconds);
  125. char output[256];
  126. strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time);
  127. result->AddAttr(kQnStamp, output, 1);
  128. }
  129. }
  130. return result;
  131. }
  132. }