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/xmpp/constants.h" 32 #include "talk/xmpp/presenceouttask.h" 33 #include "talk/xmpp/xmppclient.h" 34 35 namespace buzz { 36 37 XmppReturnStatus 38 PresenceOutTask::Send(const PresenceStatus & 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 PresenceStatus & 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 PresenceStatus & 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() != PresenceStatus::SHOW_ONLINE && 93 s.show() != PresenceStatus::SHOW_OFFLINE) { 94 result->AddElement(new XmlElement(QN_SHOW)); 95 switch (s.show()) { 96 default: 97 result->AddText(STR_SHOW_AWAY, 1); 98 break; 99 case PresenceStatus::SHOW_XA: 100 result->AddText(STR_SHOW_XA, 1); 101 break; 102 case PresenceStatus::SHOW_DND: 103 result->AddText(STR_SHOW_DND, 1); 104 break; 105 case PresenceStatus::SHOW_CHAT: 106 result->AddText(STR_SHOW_CHAT, 1); 107 break; 108 } 109 } 110 111 result->AddElement(new XmlElement(QN_STATUS)); 112 result->AddText(s.status(), 1); 113 114 if (!s.nick().empty()) { 115 result->AddElement(new XmlElement(QN_NICKNAME)); 116 result->AddText(s.nick(), 1); 117 } 118 119 std::string pri; 120 talk_base::ToString(s.priority(), &pri); 121 122 result->AddElement(new XmlElement(QN_PRIORITY)); 123 result->AddText(pri, 1); 124 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); 129 130 std::string caps; 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" : ""); 135 136 result->AddAttr(QN_EXT, caps, 1); 137 } 138 139 // Put the delay mark on the presence according to JEP-0091 140 { 141 result->AddElement(new XmlElement(kQnDelayX, true)); 142 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); 147 char output[256]; 148 strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time); 149 result->AddAttr(kQnStamp, output, 1); 150 } 151 } 152 153 return result; 154 } 155 156 157 } 158