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