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