1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
* libjingle
* Copyright 2004--2005, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#include <sstream>
#include "talk/base/stringencode.h"
#include "talk/xmpp/constants.h"
#include "talk/xmpp/presenceouttask.h"
#include "talk/xmpp/xmppclient.h"
namespace buzz {
XmppReturnStatus
PresenceOutTask::Send(const PresenceStatus & s) {
if (GetState() != STATE_INIT && GetState() != STATE_START)
return XMPP_RETURN_BADSTATE;
XmlElement * presence = TranslateStatus(s);
QueueStanza(presence);
delete presence;
return XMPP_RETURN_OK;
}
XmppReturnStatus
PresenceOutTask::SendDirected(const Jid & j, const PresenceStatus & s) {
if (GetState() != STATE_INIT && GetState() != STATE_START)
return XMPP_RETURN_BADSTATE;
XmlElement * presence = TranslateStatus(s);
presence->AddAttr(QN_TO, j.Str());
QueueStanza(presence);
delete presence;
return XMPP_RETURN_OK;
}
XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) {
if (GetState() != STATE_INIT && GetState() != STATE_START)
return XMPP_RETURN_BADSTATE;
XmlElement * presence = new XmlElement(QN_PRESENCE);
presence->AddAttr(QN_TO, jid.Str());
presence->AddAttr(QN_TYPE, "probe");
QueueStanza(presence);
delete presence;
return XMPP_RETURN_OK;
}
int
PresenceOutTask::ProcessStart() {
const XmlElement * stanza = NextStanza();
if (stanza == NULL)
return STATE_BLOCKED;
if (SendStanza(stanza) != XMPP_RETURN_OK)
return STATE_ERROR;
return STATE_START;
}
XmlElement *
PresenceOutTask::TranslateStatus(const PresenceStatus & s) {
XmlElement * result = new XmlElement(QN_PRESENCE);
if (!s.available()) {
result->AddAttr(QN_TYPE, STR_UNAVAILABLE);
}
else {
if (s.show() != PresenceStatus::SHOW_ONLINE &&
s.show() != PresenceStatus::SHOW_OFFLINE) {
result->AddElement(new XmlElement(QN_SHOW));
switch (s.show()) {
default:
result->AddText(STR_SHOW_AWAY, 1);
break;
case PresenceStatus::SHOW_XA:
result->AddText(STR_SHOW_XA, 1);
break;
case PresenceStatus::SHOW_DND:
result->AddText(STR_SHOW_DND, 1);
break;
case PresenceStatus::SHOW_CHAT:
result->AddText(STR_SHOW_CHAT, 1);
break;
}
}
result->AddElement(new XmlElement(QN_STATUS));
result->AddText(s.status(), 1);
if (!s.nick().empty()) {
result->AddElement(new XmlElement(QN_NICKNAME));
result->AddText(s.nick(), 1);
}
std::string pri;
talk_base::ToString(s.priority(), &pri);
result->AddElement(new XmlElement(QN_PRIORITY));
result->AddText(pri, 1);
if (s.know_capabilities()) {
result->AddElement(new XmlElement(QN_CAPS_C, true));
result->AddAttr(QN_NODE, s.caps_node(), 1);
result->AddAttr(QN_VER, s.version(), 1);
std::string caps;
caps.append(s.voice_capability() ? "voice-v1" : "");
caps.append(s.pmuc_capability() ? " pmuc-v1" : "");
caps.append(s.video_capability() ? " video-v1" : "");
caps.append(s.camera_capability() ? " camera-v1" : "");
result->AddAttr(QN_EXT, caps, 1);
}
// Put the delay mark on the presence according to JEP-0091
{
result->AddElement(new XmlElement(kQnDelayX, true));
// This here is why we *love* the C runtime
time_t current_time_seconds;
time(¤t_time_seconds);
struct tm* current_time = gmtime(¤t_time_seconds);
char output[256];
strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time);
result->AddAttr(kQnStamp, output, 1);
}
}
return result;
}
}
|