summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cc
blob: cee7058a7c0267582d2561ea8064497dc6bce854 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Jingle call example
 * Copyright 2004--2005, Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "talk/examples/call/presencepushtask.h"
#include "talk/xmpp/constants.h"
#include <sstream>


namespace buzz {

// string helper functions -----------------------------------------------------
template <class T> static
bool FromString(const std::string& s,
                 T * t) {
  std::istringstream iss(s);
  return !(iss>>*t).fail();
}

template <class T> static
bool ToString(const T &t,
               std::string* s) {
  std::ostringstream oss;
  oss << t;
  *s = oss.str();
  return !oss.fail();
}

static bool
IsXmlSpace(int ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}

static bool
ListContainsToken(const std::string & list, const std::string & token) {
  size_t i = list.find(token);
  if (i == std::string::npos || token.empty())
    return false;
  bool boundary_before = (i == 0 || IsXmlSpace(list[i - 1]));
  bool boundary_after = (i == list.length() - token.length() || IsXmlSpace(list[i + token.length()]));
  return boundary_before && boundary_after;
}


bool
PresencePushTask::HandleStanza(const XmlElement * stanza) {
  if (stanza->Name() != TQN_PRESENCE)
    return false;
  if (stanza->HasAttr(TQN_TYPE) && stanza->Attr(TQN_TYPE) != STR_UNAVAILABLE)
    return false;
  QueueStanza(stanza);
  return true;
}

static bool IsUtf8FirstByte(int c) {
  return (((c)&0x80)==0) || // is single byte
    ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte
}

int
PresencePushTask::ProcessStart() {
  const XmlElement * stanza = NextStanza();
  if (stanza == NULL)
    return STATE_BLOCKED;
  tqStatus s;

  s.set_jid(Jid(stanza->Attr(TQN_FROM)));

  if (stanza->Attr(TQN_TYPE) == STR_UNAVAILABLE) {
    s.set_available(false);
    SignalStatusUpdate(s);
  }
  else {
    s.set_available(true);
    const XmlElement * status = stanza->FirstNamed(TQN_STATUS);
    if (status != NULL) {
      s.set_status(status->BodyText());

      // Truncate status messages longer than 300 bytes
      if (s.status().length() > 300) {
        size_t len = 300;

        // Be careful not to split legal utf-8 chars in half
        while (!IsUtf8FirstByte(s.status()[len]) && len > 0) {
          len -= 1;
        }
        std::string truncated(s.status(), 0, len);
        s.set_status(truncated);
      }
    }

    const XmlElement * priority = stanza->FirstNamed(TQN_PRIORITY);
    if (priority != NULL) {
      int pri;
      if (FromString(priority->BodyText(), &pri)) {
        s.set_priority(pri);
      }
    }

    const XmlElement * show = stanza->FirstNamed(TQN_SHOW);
    if (show == NULL || show->FirstChild() == NULL) {
      s.set_show(tqStatus::SHOW_ONLINE);
    }
    else {
      if (show->BodyText() == "away") {
        s.set_show(tqStatus::SHOW_AWAY);
      }
      else if (show->BodyText() == "xa") {
        s.set_show(tqStatus::SHOW_XA);
      }
      else if (show->BodyText() == "dnd") {
        s.set_show(tqStatus::SHOW_DND);
      }
      else if (show->BodyText() == "chat") {
        s.set_show(tqStatus::SHOW_CHAT);
      }
      else {
        s.set_show(tqStatus::SHOW_ONLINE);
      }
    }

    const XmlElement * caps = stanza->FirstNamed(TQN_CAPS_C);
    if (caps != NULL) {
      std::string node = caps->Attr(TQN_NODE);
      std::string ver = caps->Attr(TQN_VER);
      std::string exts = caps->Attr(TQN_EXT);

      s.set_know_capabilities(true);

      if (node == GOOGLE_CLIENT_NODE) {
        s.set_is_google_client(true);
        s.set_version(ver);
        if (ListContainsToken(exts, "voice-v1")) {
          s.set_phone_capability(true);
        }
      }
    }

    const XmlElement* delay = stanza->FirstNamed(kQnDelayX);
    if (delay != NULL) {
      // Ideally we would parse this according to the Psuedo ISO-8601 rules
      // that are laid out in JEP-0082:
      // http://www.jabber.org/jeps/jep-0082.html
      std::string stamp = delay->Attr(kQnStamp);
      s.set_sent_time(stamp);
    }

    SignalStatusUpdate(s);
  }

  return STATE_START;
}


}