3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #include "webrtc/libjingle/xmpp/constants.h"
31 #include "webrtc/libjingle/xmpp/presenceouttask.h"
32 #include "webrtc/libjingle/xmpp/xmppclient.h"
33 #include "webrtc/base/stringencode.h"
38 PresenceOutTask::Send(const PresenceStatus & s) {
39 if (GetState() != STATE_INIT && GetState() != STATE_START)
40 return XMPP_RETURN_BADSTATE;
42 XmlElement * presence = TranslateStatus(s);
43 QueueStanza(presence);
45 return XMPP_RETURN_OK;
49 PresenceOutTask::SendDirected(const Jid & j, const PresenceStatus & s) {
50 if (GetState() != STATE_INIT && GetState() != STATE_START)
51 return XMPP_RETURN_BADSTATE;
53 XmlElement * presence = TranslateStatus(s);
54 presence->AddAttr(QN_TO, j.Str());
55 QueueStanza(presence);
57 return XMPP_RETURN_OK;
60 XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) {
61 if (GetState() != STATE_INIT && GetState() != STATE_START)
62 return XMPP_RETURN_BADSTATE;
64 XmlElement * presence = new XmlElement(QN_PRESENCE);
65 presence->AddAttr(QN_TO, jid.Str());
66 presence->AddAttr(QN_TYPE, "probe");
68 QueueStanza(presence);
70 return XMPP_RETURN_OK;
74 PresenceOutTask::ProcessStart() {
75 const XmlElement * stanza = NextStanza();
79 if (SendStanza(stanza) != XMPP_RETURN_OK)
86 PresenceOutTask::TranslateStatus(const PresenceStatus & s) {
87 XmlElement * result = new XmlElement(QN_PRESENCE);
89 result->AddAttr(QN_TYPE, STR_UNAVAILABLE);
92 if (s.show() != PresenceStatus::SHOW_ONLINE &&
93 s.show() != PresenceStatus::SHOW_OFFLINE) {
94 result->AddElement(new XmlElement(QN_SHOW));
97 result->AddText(STR_SHOW_AWAY, 1);
99 case PresenceStatus::SHOW_XA:
100 result->AddText(STR_SHOW_XA, 1);
102 case PresenceStatus::SHOW_DND:
103 result->AddText(STR_SHOW_DND, 1);
105 case PresenceStatus::SHOW_CHAT:
106 result->AddText(STR_SHOW_CHAT, 1);
111 result->AddElement(new XmlElement(QN_STATUS));
112 result->AddText(s.status(), 1);
114 if (!s.nick().empty()) {
115 result->AddElement(new XmlElement(QN_NICKNAME));
116 result->AddText(s.nick(), 1);
120 rtc::ToString(s.priority(), &pri);
122 result->AddElement(new XmlElement(QN_PRIORITY));
123 result->AddText(pri, 1);
125 if (s.know_capabilities()) {
126 result->AddElement(new XmlElement(QN_CAPS_C, true));
127 result->AddAttr(QN_NODE, s.caps_node(), 1);
128 result->AddAttr(QN_VER, s.version(), 1);
131 caps.append(s.voice_capability() ? "voice-v1" : "");
132 caps.append(s.pmuc_capability() ? " pmuc-v1" : "");
133 caps.append(s.video_capability() ? " video-v1" : "");
134 caps.append(s.camera_capability() ? " camera-v1" : "");
136 result->AddAttr(QN_EXT, caps, 1);
139 // Put the delay mark on the presence according to JEP-0091
141 result->AddElement(new XmlElement(kQnDelayX, true));
143 // This here is why we *love* the C runtime
144 time_t current_time_seconds;
145 time(¤t_time_seconds);
146 struct tm* current_time = gmtime(¤t_time_seconds);
148 strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time);
149 result->AddAttr(kQnStamp, output, 1);