summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/xmpp/xmpptask.cc
blob: 38ed1994083d1d9a65d26a2b995a50294e746d57 (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
/*
 * 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 "talk/xmpp/xmpptask.h"
#include "talk/xmpp/xmppclient.h"
#include "talk/xmpp/xmppengine.h"
#include "talk/xmpp/constants.h"

namespace buzz {

XmppTask::XmppTask(Task * tqparent, XmppEngine::HandlerLevel level)
 : Task(tqparent), client_(NULL) {
  XmppClient * client = (XmppClient*)tqparent->GetParent(XMPP_CLIENT_TASK_CODE);
  client_ = client;
  id_ = client->NextId();
  client->AddXmppTask(this, level);
  client->SignalDisconnected.connect(this, &XmppTask::OnDisconnect);
}

XmppTask::~XmppTask() {
  StopImpl();
}

void
XmppTask::StopImpl() {
  while (NextStanza() != NULL) {}
  if (client_) {
    client_->RemoveXmppTask(this);
    client_->SignalDisconnected.disconnect(this);
    client_ = NULL;
  }
}

XmppReturntqStatus
XmppTask::SendStanza(const XmlElement * stanza) {
  if (client_ == NULL)
    return XMPP_RETURN_BADSTATE;
  return client_->SendStanza(stanza);
}

XmppReturntqStatus
XmppTask::SendStanzaError(const XmlElement * element_original,
                                XmppStanzaError code,
                                const std::string & text) {
  if (client_ == NULL)
    return XMPP_RETURN_BADSTATE;
  return client_->SendStanzaError(element_original, code, text);
}

void
XmppTask::Stop() {
  StopImpl();
  Task::Stop();
}

void
XmppTask::OnDisconnect() {
  Error();
}

void
XmppTask::QueueStanza(const XmlElement * stanza) {
  stanza_queue_.push_back(new XmlElement(*stanza));
  Wake();
}

const XmlElement *
XmppTask::NextStanza() {
  XmlElement * result = NULL;
  if (!stanza_queue_.empty()) {
    result = stanza_queue_.front();
    stanza_queue_.pop_front();
  }
  next_stanza_.reset(result);
  return result;
}

XmlElement *
XmppTask::MakeIq(const std::string & type,
  const buzz::Jid & to, const std::string id) {
  XmlElement * result = new XmlElement(TQN_IQ);
  if (!type.empty())
    result->AddAttr(TQN_TYPE, type);
  if (to != JID_EMPTY)
    result->AddAttr(TQN_TO, to.Str());
  if (!id.empty())
    result->AddAttr(TQN_ID, id);
  return result;
}

XmlElement *
XmppTask::MakeIqResult(const XmlElement * query) {
  XmlElement * result = new XmlElement(TQN_IQ);
  result->AddAttr(TQN_TYPE, STR_RESULT);
  if (query->HasAttr(TQN_FROM)) {
    result->AddAttr(TQN_TO, query->Attr(TQN_FROM));
  }
  result->AddAttr(TQN_ID, query->Attr(TQN_ID));
  return result;
}

bool
XmppTask::MatchResponseIq(const XmlElement * stanza,
    const Jid & to, const std::string & id) {
  if (stanza->Name() != TQN_IQ)
    return false;

  if (stanza->Attr(TQN_ID) != id)
    return false;

  Jid from(stanza->Attr(TQN_FROM));
  if (from != to) {
    Jid me = client_->jid();
    // we address the server as "", but it is legal for the server
    // to identify itself with "domain" or "myself@domain"
    if (to != JID_EMPTY) {
      return false;
    }

    if (from != Jid(me.domain()) && from != me.BareJid()) {
      return false;
    }
  }


  return true;
}

bool
XmppTask::MatchRequestIq(const XmlElement * stanza,
    const std::string & type, const TQName & qn) {
  if (stanza->Name() != TQN_IQ)
    return false;

  if (stanza->Attr(TQN_TYPE) != type)
    return false;

  if (stanza->FirstNamed(qn) == NULL)
    return false;

  return true;
}

}