OLD | NEW |
(Empty) | |
| 1 // Copyright 2004 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <time.h> |
| 6 #include <sstream> |
| 7 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 8 #include "third_party/libjingle_xmpp/xmpp/presenceouttask.h" |
| 9 #include "third_party/libjingle_xmpp/xmpp/xmppclient.h" |
| 10 #include "webrtc/base/arraysize.h" |
| 11 #include "webrtc/base/stringencode.h" |
| 12 |
| 13 namespace buzz { |
| 14 |
| 15 XmppReturnStatus |
| 16 PresenceOutTask::Send(const PresenceStatus & s) { |
| 17 if (GetState() != STATE_INIT && GetState() != STATE_START) |
| 18 return XMPP_RETURN_BADSTATE; |
| 19 |
| 20 XmlElement * presence = TranslateStatus(s); |
| 21 QueueStanza(presence); |
| 22 delete presence; |
| 23 return XMPP_RETURN_OK; |
| 24 } |
| 25 |
| 26 XmppReturnStatus |
| 27 PresenceOutTask::SendDirected(const Jid & j, const PresenceStatus & s) { |
| 28 if (GetState() != STATE_INIT && GetState() != STATE_START) |
| 29 return XMPP_RETURN_BADSTATE; |
| 30 |
| 31 XmlElement * presence = TranslateStatus(s); |
| 32 presence->AddAttr(QN_TO, j.Str()); |
| 33 QueueStanza(presence); |
| 34 delete presence; |
| 35 return XMPP_RETURN_OK; |
| 36 } |
| 37 |
| 38 XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) { |
| 39 if (GetState() != STATE_INIT && GetState() != STATE_START) |
| 40 return XMPP_RETURN_BADSTATE; |
| 41 |
| 42 XmlElement * presence = new XmlElement(QN_PRESENCE); |
| 43 presence->AddAttr(QN_TO, jid.Str()); |
| 44 presence->AddAttr(QN_TYPE, "probe"); |
| 45 |
| 46 QueueStanza(presence); |
| 47 delete presence; |
| 48 return XMPP_RETURN_OK; |
| 49 } |
| 50 |
| 51 int |
| 52 PresenceOutTask::ProcessStart() { |
| 53 const XmlElement * stanza = NextStanza(); |
| 54 if (stanza == NULL) |
| 55 return STATE_BLOCKED; |
| 56 |
| 57 if (SendStanza(stanza) != XMPP_RETURN_OK) |
| 58 return STATE_ERROR; |
| 59 |
| 60 return STATE_START; |
| 61 } |
| 62 |
| 63 XmlElement * |
| 64 PresenceOutTask::TranslateStatus(const PresenceStatus & s) { |
| 65 XmlElement * result = new XmlElement(QN_PRESENCE); |
| 66 if (!s.available()) { |
| 67 result->AddAttr(QN_TYPE, STR_UNAVAILABLE); |
| 68 } |
| 69 else { |
| 70 if (s.show() != PresenceStatus::SHOW_ONLINE && |
| 71 s.show() != PresenceStatus::SHOW_OFFLINE) { |
| 72 result->AddElement(new XmlElement(QN_SHOW)); |
| 73 switch (s.show()) { |
| 74 default: |
| 75 result->AddText(STR_SHOW_AWAY, 1); |
| 76 break; |
| 77 case PresenceStatus::SHOW_XA: |
| 78 result->AddText(STR_SHOW_XA, 1); |
| 79 break; |
| 80 case PresenceStatus::SHOW_DND: |
| 81 result->AddText(STR_SHOW_DND, 1); |
| 82 break; |
| 83 case PresenceStatus::SHOW_CHAT: |
| 84 result->AddText(STR_SHOW_CHAT, 1); |
| 85 break; |
| 86 } |
| 87 } |
| 88 |
| 89 result->AddElement(new XmlElement(QN_STATUS)); |
| 90 result->AddText(s.status(), 1); |
| 91 |
| 92 if (!s.nick().empty()) { |
| 93 result->AddElement(new XmlElement(QN_NICKNAME)); |
| 94 result->AddText(s.nick(), 1); |
| 95 } |
| 96 |
| 97 std::string pri; |
| 98 rtc::ToString(s.priority(), &pri); |
| 99 |
| 100 result->AddElement(new XmlElement(QN_PRIORITY)); |
| 101 result->AddText(pri, 1); |
| 102 |
| 103 if (s.know_capabilities()) { |
| 104 result->AddElement(new XmlElement(QN_CAPS_C, true)); |
| 105 result->AddAttr(QN_NODE, s.caps_node(), 1); |
| 106 result->AddAttr(QN_VER, s.version(), 1); |
| 107 |
| 108 std::string caps; |
| 109 caps.append(s.voice_capability() ? "voice-v1" : ""); |
| 110 caps.append(s.pmuc_capability() ? " pmuc-v1" : ""); |
| 111 caps.append(s.video_capability() ? " video-v1" : ""); |
| 112 caps.append(s.camera_capability() ? " camera-v1" : ""); |
| 113 |
| 114 result->AddAttr(QN_EXT, caps, 1); |
| 115 } |
| 116 |
| 117 // Put the delay mark on the presence according to JEP-0091 |
| 118 { |
| 119 result->AddElement(new XmlElement(kQnDelayX, true)); |
| 120 |
| 121 // This here is why we *love* the C runtime |
| 122 time_t current_time_seconds; |
| 123 time(¤t_time_seconds); |
| 124 struct tm* current_time = gmtime(¤t_time_seconds); |
| 125 char output[256]; |
| 126 strftime(output, arraysize(output), "%Y%m%dT%H:%M:%S", current_time); |
| 127 result->AddAttr(kQnStamp, output, 1); |
| 128 } |
| 129 } |
| 130 |
| 131 return result; |
| 132 } |
| 133 |
| 134 |
| 135 } |
OLD | NEW |