~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/examples/call/presenceouttask.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

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