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 "talk/examples/call/mucinviterecvtask.h"
29#include "talk/xmpp/constants.h"
30
31namespace buzz {
32
33const char* types[] = {
34  "unknown",
35  "audio",
36  "video",
37};
38
39const char* statuses[] = {
40  "unknown",
41  "sendrecv",
42  "sendonly",
43  "recvonly",
44  "inactive",
45};
46
47const char*
48AvailableMediaEntry::TypeAsString(type_t type) {
49  // The values of the constants have been chosen such that this is correct.
50  return types[type];
51}
52
53const char*
54AvailableMediaEntry::StatusAsString(status_t status) {
55  // The values of the constants have been chosen such that this is correct.
56  return statuses[status];
57}
58
59int bodytext_to_array_pos(const XmlElement* elem, const char* array[],
60    int len, int defval = -1) {
61  if (elem) {
62    const std::string& body(elem->BodyText());
63    for (int i = 0; i < len; ++i) {
64      if (body == array[i]) {
65        // Found it.
66        return i;
67      }
68    }
69  }
70  // If we get here, it's not any value in the array.
71  return defval;
72}
73
74bool
75MucInviteRecvTask::HandleStanza(const XmlElement* stanza) {
76  // Figuring out that we want to handle this is a lot of the work of
77  // actually handling it, so we handle it right here instead of queueing it.
78  const XmlElement* xstanza;
79  const XmlElement* invite;
80  if (stanza->Name() != QN_MESSAGE) return false;
81  xstanza = stanza->FirstNamed(QN_MUC_USER_X);
82  if (!xstanza) return false;
83  invite = xstanza->FirstNamed(QN_MUC_USER_INVITE);
84  if (!invite) return false;
85  // Else it's an invite and we definitely want to handle it. Parse the
86  // available-media, if any.
87  std::vector<AvailableMediaEntry> v;
88  const XmlElement* avail =
89    invite->FirstNamed(QN_GOOGLE_MUC_USER_AVAILABLE_MEDIA);
90  if (avail) {
91    for (const XmlElement* entry = avail->FirstNamed(QN_GOOGLE_MUC_USER_ENTRY);
92        entry;
93        entry = entry->NextNamed(QN_GOOGLE_MUC_USER_ENTRY)) {
94      AvailableMediaEntry tmp;
95      // In the interest of debugging, we accept as much valid-looking data
96      // as we can.
97      tmp.label = atoi(entry->Attr(QN_LABEL).c_str());
98      tmp.type = static_cast<AvailableMediaEntry::type_t>(
99          bodytext_to_array_pos(
100              entry->FirstNamed(QN_GOOGLE_MUC_USER_TYPE),
101              types,
102              sizeof(types)/sizeof(const char*),
103              AvailableMediaEntry::TYPE_UNKNOWN));
104      tmp.status = static_cast<AvailableMediaEntry::status_t>(
105          bodytext_to_array_pos(
106              entry->FirstNamed(QN_GOOGLE_MUC_USER_STATUS),
107              statuses,
108              sizeof(statuses)/sizeof(const char*),
109              AvailableMediaEntry::STATUS_UNKNOWN));
110      v.push_back(tmp);
111    }
112  }
113  SignalInviteReceived(Jid(invite->Attr(QN_FROM)), Jid(stanza->Attr(QN_FROM)),
114      v);
115  return true;
116}
117
118int
119MucInviteRecvTask::ProcessStart() {
120  // We never queue anything so we are always blocked.
121  return STATE_BLOCKED;
122}
123
124}
125