~ubuntu-branches/ubuntu/feisty/psi/feisty

« back to all changes in this revision

Viewing changes to src/jabtasks.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2002-04-19 02:28:44 UTC
  • Revision ID: james.westby@ubuntu.com-20020419022844-za7xgai5qyfd9xv6
Tags: upstream-0.8.5
ImportĀ upstreamĀ versionĀ 0.8.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** jabtasks.cpp - basic tasks for use with the JabIO task system
 
3
** Copyright (C) 2001, 2002  Justin Karneges
 
4
**
 
5
** This program is free software; you can redistribute it and/or
 
6
** modify it under the terms of the GNU General Public License
 
7
** as published by the Free Software Foundation; either version 2
 
8
** of the License, or (at your option) any later version.
 
9
**
 
10
** This program is distributed in the hope that it will be useful,
 
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
** GNU General Public License for more details.
 
14
**
 
15
** You should have received a copy of the GNU General Public License
 
16
** along with this program; if not, write to the Free Software
 
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 
18
**
 
19
****************************************************************************/
 
20
 
 
21
#include"jabtasks.h"
 
22
#include"common.h"
 
23
 
 
24
 
 
25
/****************************************************************************
 
26
  JT_Auth
 
27
****************************************************************************/
 
28
JT_Auth::JT_Auth(JabTask *parent, const QString &user, const QString &pass, const QString &sid, const QString &resource, bool plain)
 
29
:JabTask(parent)
 
30
{
 
31
        iq = createIQ("set", "", id());
 
32
        QDomElement query = doc()->createElement("query");
 
33
        query.setAttribute("xmlns", "jabber:iq:auth");
 
34
        iq.appendChild(query);
 
35
        query.appendChild(textTag("username", user));
 
36
        if(plain)
 
37
                query.appendChild(textTag("password", pass));
 
38
        else {
 
39
                QString hash = SHA1::digest(sid + pass);
 
40
                query.appendChild(textTag("digest", hash));
 
41
        }
 
42
        query.appendChild(textTag("resource", resource));
 
43
}
 
44
 
 
45
void JT_Auth::go()
 
46
{
 
47
        send(iq);
 
48
}
 
49
 
 
50
bool JT_Auth::take(const QDomElement &x)
 
51
{
 
52
        if(x.attribute("id") != id())
 
53
                return FALSE;
 
54
 
 
55
        if(x.attribute("type") == "result") {
 
56
                // login success
 
57
                setSuccess(TRUE);
 
58
        }
 
59
        else {
 
60
                // login fail
 
61
                setError(getErrorString(x));
 
62
                setSuccess(FALSE);
 
63
        }
 
64
 
 
65
        return TRUE;
 
66
}
 
67
 
 
68
 
 
69
/****************************************************************************
 
70
  JT_Register
 
71
****************************************************************************/
 
72
JT_Register::JT_Register(JabTask *parent)
 
73
:JabTask(parent)
 
74
{
 
75
}
 
76
 
 
77
void JT_Register::reg(const QString &user, const QString &pass)
 
78
{
 
79
        iq = createIQ("set", "", id());
 
80
        QDomElement query = doc()->createElement("query");
 
81
        query.setAttribute("xmlns", "jabber:iq:register");
 
82
        iq.appendChild(query);
 
83
        query.appendChild(textTag("username", user));
 
84
        query.appendChild(textTag("password", pass));
 
85
}
 
86
 
 
87
void JT_Register::go()
 
88
{
 
89
        send(iq);
 
90
}
 
91
 
 
92
bool JT_Register::take(const QDomElement &x)
 
93
{
 
94
        if(x.attribute("id") != id())
 
95
                return FALSE;
 
96
 
 
97
        if(x.attribute("type") == "result") {
 
98
                setSuccess(TRUE);
 
99
        }
 
100
        else {
 
101
                setError(getErrorString(x));
 
102
                setSuccess(FALSE);
 
103
        }
 
104
 
 
105
        return TRUE;
 
106
}
 
107
 
 
108
 
 
109
/****************************************************************************
 
110
  JT_Roster
 
111
****************************************************************************/
 
112
JT_Roster::JT_Roster(JabTask *parent)
 
113
:JabTask(parent)
 
114
{
 
115
        type = -1;
 
116
}
 
117
 
 
118
void JT_Roster::get()
 
119
{
 
120
        type = 0;
 
121
 
 
122
        iq = createIQ("get", "", id());
 
123
        QDomElement query = doc()->createElement("query");
 
124
        query.setAttribute("xmlns", "jabber:iq:roster");
 
125
        iq.appendChild(query);
 
126
}
 
127
 
 
128
void JT_Roster::set(const QString &_jid, const QString &_nick, const QStringList &_groups)
 
129
{
 
130
        type = 1;
 
131
        jid = _jid;
 
132
        nick = _nick;
 
133
        groups = _groups;
 
134
 
 
135
        iq = createIQ("set", "", id());
 
136
        QDomElement query = doc()->createElement("query");
 
137
        query.setAttribute("xmlns", "jabber:iq:roster");
 
138
        iq.appendChild(query);
 
139
 
 
140
        QDomElement item = doc()->createElement("item");
 
141
        item.setAttribute("jid", jid);
 
142
        if(!nick.isEmpty())
 
143
                item.setAttribute("name", nick);
 
144
        for(QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
 
145
                item.appendChild(textTag("group", *it));
 
146
        query.appendChild(item);
 
147
}
 
148
 
 
149
void JT_Roster::remove(const QString &jid)
 
150
{
 
151
        type = 2;
 
152
 
 
153
        iq = createIQ("set", "", id());
 
154
        QDomElement query = doc()->createElement("query");
 
155
        query.setAttribute("xmlns", "jabber:iq:roster");
 
156
        iq.appendChild(query);
 
157
 
 
158
        QDomElement item = doc()->createElement("item");
 
159
        item.setAttribute("jid", jid);
 
160
        item.setAttribute("subscription", "remove");
 
161
        query.appendChild(item);
 
162
}
 
163
 
 
164
void JT_Roster::go()
 
165
{
 
166
        send(iq);
 
167
}
 
168
 
 
169
QString JT_Roster::toString()
 
170
{
 
171
        if(type != 1)
 
172
                return "";
 
173
 
 
174
        QStringList list;
 
175
        list += "JT_Roster";
 
176
        list += jid;
 
177
        list += nick;
 
178
        list += lineEncodeList(groups);
 
179
        return lineEncodeList(list);
 
180
}
 
181
 
 
182
bool JT_Roster::fromString(const QString &str)
 
183
{
 
184
        QStringList args = lineDecodeList(str);
 
185
 
 
186
        if(args.count() != 4)
 
187
                return FALSE;
 
188
 
 
189
        QStringList::Iterator i = args.begin();
 
190
        QString arg0;
 
191
        arg0 = *(i++);
 
192
 
 
193
        if(arg0 == "JT_Roster") {
 
194
                QString jid = *(i++);
 
195
                QString nick = *(i++);
 
196
                QStringList groups = lineDecodeList(*(i++));
 
197
                set(jid, nick, groups);
 
198
                return TRUE;
 
199
        }
 
200
 
 
201
        return FALSE;
 
202
}
 
203
 
 
204
bool JT_Roster::take(const QDomElement &x)
 
205
{
 
206
        if(x.attribute("id") != id())
 
207
                return FALSE;
 
208
 
 
209
        if(type == 0) {
 
210
                if(x.attribute("type") == "result") {
 
211
                        QDomElement q = queryTag(x);
 
212
                        roster = xmlReadRoster(q);
 
213
                        setSuccess(TRUE);
 
214
                }
 
215
                else {
 
216
                        setError(getErrorString(x));
 
217
                        setSuccess(FALSE);
 
218
                }
 
219
 
 
220
                return TRUE;
 
221
        }
 
222
        else if(type == 1) {
 
223
                if(x.attribute("type") == "result")
 
224
                        setSuccess(TRUE);
 
225
                else {
 
226
                        setError(getErrorString(x));
 
227
                        setSuccess(FALSE);
 
228
                }
 
229
 
 
230
                return TRUE;
 
231
        }
 
232
        else if(type == 2) {
 
233
                pdb(DEBUG_JABBER, "[Remove successful]\n");
 
234
 
 
235
                setSuccess(TRUE);
 
236
                return TRUE;
 
237
        }
 
238
 
 
239
        return FALSE;
 
240
}
 
241
 
 
242
 
 
243
/****************************************************************************
 
244
  JT_Presence
 
245
****************************************************************************/
 
246
JT_Presence::JT_Presence(JabTask *parent)
 
247
:JabTask(parent)
 
248
{
 
249
}
 
250
 
 
251
void JT_Presence::pres(int _status, const QString &_statusString, int _priority)
 
252
{
 
253
        type = 0;
 
254
 
 
255
        status = _status;
 
256
        statusString = _statusString;
 
257
        priority = _priority;
 
258
 
 
259
        tag = doc()->createElement("presence");
 
260
        if(status == STATUS_OFFLINE) {
 
261
                tag.setAttribute("type", "unavailable");
 
262
                if(!statusString.isEmpty())
 
263
                        tag.appendChild(textTag("status", statusString));
 
264
        }
 
265
        else {
 
266
                if(status != STATUS_ONLINE)
 
267
                        tag.appendChild(textTag("show", status2jabprestxt(status)));
 
268
                if(!statusString.isEmpty())
 
269
                        tag.appendChild(textTag("status", statusString));
 
270
                tag.appendChild(textTag("priority", QString("%1").arg(priority)));
 
271
        }
 
272
}
 
273
 
 
274
void JT_Presence::sub(const Jid &to, int subType)
 
275
{
 
276
        type = 1;
 
277
 
 
278
        tag = doc()->createElement("presence");
 
279
        tag.setAttribute("to", to.full());
 
280
        if(subType == JABSUB_SUBSCRIBE)
 
281
                tag.setAttribute("type", "subscribe");
 
282
        else if(subType == JABSUB_SUBSCRIBED)
 
283
                tag.setAttribute("type", "subscribed");
 
284
        else if(subType == JABSUB_UNSUBSCRIBE)
 
285
                tag.setAttribute("type", "unsubscribe");
 
286
        else if(subType == JABSUB_UNSUBSCRIBED)
 
287
                tag.setAttribute("type", "unsubscribed");
 
288
}
 
289
 
 
290
void JT_Presence::go()
 
291
{
 
292
        send(tag);
 
293
        if(type == 0 && status != STATUS_OFFLINE)
 
294
                receivePresence(status, statusString);
 
295
        setSuccess(TRUE);
 
296
}
 
297
 
 
298
 
 
299
/****************************************************************************
 
300
  JT_Login
 
301
****************************************************************************/
 
302
JT_Login::JT_Login(JabTask *parent)
 
303
:JabTask(parent)
 
304
{
 
305
        reg = 0;
 
306
        auth = 0;
 
307
        rost = 0;
 
308
        pres = 0;
 
309
        type = -1;
 
310
}
 
311
 
 
312
void JT_Login::login(const QString &_user, const QString &_pass, const QString &_sid, const QString &_resource, int _status, const QString &_statusString, int _priority, bool plain)
 
313
{
 
314
        user = _user;
 
315
        pass = _pass;
 
316
        sid = _sid;
 
317
        resource = _resource;
 
318
        status = _status;
 
319
        statusString = _statusString;
 
320
        priority = _priority;
 
321
        usePlain = plain;
 
322
        type = 0;
 
323
}
 
324
 
 
325
void JT_Login::create(const QString &_user, const QString &_pass, const QString &_sid, const QString &_resource, int _status, const QString &_statusString, int _priority, bool plain)
 
326
{
 
327
        user = _user;
 
328
        pass = _pass;
 
329
        sid = _sid;
 
330
        resource = _resource;
 
331
        status = _status;
 
332
        statusString = _statusString;
 
333
        priority = _priority;
 
334
        usePlain = plain;
 
335
        type = 1;
 
336
}
 
337
 
 
338
void JT_Login::go()
 
339
{
 
340
        if(type == 0) {
 
341
                auth = new JT_Auth(this, user, pass, sid, resource, usePlain);
 
342
                stepChanged();
 
343
                auth->go();
 
344
        }
 
345
        else if(type == 1) {
 
346
                reg = new JT_Register(this);
 
347
                reg->reg(user, pass);
 
348
                stepChanged();
 
349
                reg->go();
 
350
        }
 
351
}
 
352
 
 
353
void JT_Login::done(JabTask *p)
 
354
{
 
355
        if(reg && p->id() == reg->id()) {
 
356
                if(reg->success()) {
 
357
                        //printf("JT_Login: Created.\n");
 
358
                        delete reg;
 
359
                        reg = 0;
 
360
 
 
361
                        auth = new JT_Auth(this, user, pass, sid, resource, usePlain);
 
362
                        stepChanged();
 
363
                        auth->go();
 
364
                }
 
365
                else {
 
366
                        setError(reg->errorString());
 
367
                        setSuccess(FALSE);
 
368
                        return;
 
369
                }
 
370
        }
 
371
        else if(auth && p->id() == auth->id()) {
 
372
                if(auth->success()) {
 
373
                        //printf("JT_Login: Authorized.\n");
 
374
                        receiveAuth();
 
375
                        delete auth;
 
376
                        auth = 0;
 
377
 
 
378
                        rost = new JT_Roster(this);
 
379
                        rost->get();
 
380
                        stepChanged();
 
381
                        rost->go();
 
382
                }
 
383
                else {
 
384
                        setError(auth->errorString());
 
385
                        setSuccess(FALSE);
 
386
                        return;
 
387
                }
 
388
        }
 
389
        else if(rost && p->id() == rost->id()) {
 
390
                //printf("JT_Login: Got roster.\n");
 
391
                receiveRoster(rost->roster);
 
392
                delete rost;
 
393
                rost = 0;
 
394
 
 
395
                pres = new JT_Presence(this);
 
396
                pres->pres(status, statusString, priority);
 
397
                pres->go();
 
398
        }
 
399
        else if(pres && p->id() == pres->id()) {
 
400
                //printf("JT_Login: Sent presence.\n");
 
401
                receivePresence(pres->status, pres->statusString);
 
402
                delete pres;
 
403
                pres = 0;
 
404
 
 
405
                setSuccess(TRUE);
 
406
        }
 
407
}
 
408
 
 
409
 
 
410
/****************************************************************************
 
411
  JT_PushPresence
 
412
****************************************************************************/
 
413
JT_PushPresence::JT_PushPresence(JabTask *parent)
 
414
:JabTask(parent)
 
415
{
 
416
}
 
417
 
 
418
bool JT_PushPresence::take(const QDomElement &e)
 
419
{
 
420
        if(e.tagName() != "presence")
 
421
                return FALSE;
 
422
 
 
423
        Info p;
 
424
        p.jid = e.attribute("from");
 
425
        p.status = STATUS_ONLINE;
 
426
        p.priority = 0;
 
427
        p.statusString = "";
 
428
        p.songTitle = ""; // Gabber music
 
429
        p.timeStamp = QDateTime::currentDateTime();
 
430
 
 
431
        if(e.hasAttribute("type")) {
 
432
                QString type = e.attribute("type");
 
433
                if(type == "unavailable") {
 
434
                        p.status = STATUS_OFFLINE;
 
435
                }
 
436
                else {
 
437
                        int subType = -1;
 
438
                        if(type == "subscribe") {
 
439
                                subType = JABSUB_SUBSCRIBE;
 
440
                        }
 
441
                        else if(type == "subscribed") {
 
442
                                subType = JABSUB_SUBSCRIBED;
 
443
                        }
 
444
                        else if(type == "unsubscribe") {
 
445
                                subType = JABSUB_UNSUBSCRIBE;
 
446
                        }
 
447
                        else if(type == "unsubscribed") {
 
448
                                subType = JABSUB_UNSUBSCRIBED;
 
449
                        }
 
450
                        if(subType == -1)
 
451
                                return TRUE;
 
452
 
 
453
                        subscription(p.jid, subType);
 
454
                        return TRUE;
 
455
                }
 
456
        }
 
457
 
 
458
        QDomElement tag;
 
459
        bool found;
 
460
 
 
461
        tag = findSubTag(e, "status", &found);
 
462
        if(found)
 
463
                p.statusString = tagContent(tag);
 
464
        tag = findSubTag(e, "show", &found);
 
465
        if(found)
 
466
                p.status = jabprestxt2status(tagContent(tag));
 
467
        tag = findSubTag(e, "priority", &found);
 
468
        if(found)
 
469
                p.priority = tagContent(tag).toInt();
 
470
 
 
471
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
472
                QDomElement i = n.toElement();
 
473
                if(i.isNull())
 
474
                        continue;
 
475
 
 
476
                if(i.tagName() == "x" && i.attribute("xmlns") == "jabber:x:delay") {
 
477
                        if(i.hasAttribute("stamp")) {
 
478
                                if(stamp2TS(i.attribute("stamp"), &p.timeStamp))
 
479
                                        p.timeStamp = p.timeStamp.addSecs(getTZOffset() * 3600);
 
480
                        }
 
481
                }
 
482
 
 
483
                if(i.tagName() == "x" && i.attribute("xmlns") == "gabber:x:music:info") {
 
484
                        QDomElement t;
 
485
                        bool found;
 
486
                        QString title, state;
 
487
 
 
488
                        t = findSubTag(i, "title", &found);
 
489
                        if(found)
 
490
                                title = tagContent(t);
 
491
                        t = findSubTag(i, "state", &found);
 
492
                        if(found)
 
493
                                state = tagContent(t);
 
494
 
 
495
                        if(!title.isEmpty() && state == "playing")
 
496
                                p.songTitle = tagContent(t);
 
497
                }
 
498
        }
 
499
 
 
500
        presence(p);
 
501
 
 
502
        return TRUE;
 
503
}
 
504
 
 
505
 
 
506
/****************************************************************************
 
507
  JT_PushRoster
 
508
****************************************************************************/
 
509
JT_PushRoster::JT_PushRoster(JabTask *parent)
 
510
:JabTask(parent)
 
511
{
 
512
}
 
513
 
 
514
bool JT_PushRoster::take(const QDomElement &e)
 
515
{
 
516
        // must be an iq tag
 
517
        if(e.tagName() != "iq")
 
518
                return FALSE;
 
519
 
 
520
        // let's not steal non-push responses
 
521
        if(e.hasAttribute("id"))
 
522
                return FALSE;
 
523
 
 
524
        if(queryNS(e) != "jabber:iq:roster")
 
525
                return FALSE;
 
526
 
 
527
        QDomElement q = queryTag(e);
 
528
        JabRoster r = xmlReadRoster(q, TRUE);
 
529
        roster(r);
 
530
 
 
531
        return TRUE;
 
532
}
 
533
 
 
534
 
 
535
/****************************************************************************
 
536
  JT_Message
 
537
****************************************************************************/
 
538
JT_Message::JT_Message(JabTask *parent, const JabMessage &_jmsg)
 
539
:JabTask(parent)
 
540
{
 
541
        jmsg = _jmsg;
 
542
}
 
543
 
 
544
void JT_Message::go()
 
545
{
 
546
        QDomElement message = doc()->createElement("message");
 
547
        message.setAttribute("to", jmsg.to.full());
 
548
        if(jmsg.type == JABMESSAGE_CHAT)
 
549
                message.setAttribute("type", "chat");
 
550
        if(!jmsg.subject.isEmpty())
 
551
                message.appendChild(textTag("subject", jmsg.subject));
 
552
        message.appendChild(textTag("body", jmsg.body));
 
553
        if(!jmsg.url.isEmpty()) {
 
554
                QDomElement x = doc()->createElement("x");
 
555
                x.setAttribute("xmlns", "jabber:x:oob");
 
556
                x.appendChild(textTag("url", jmsg.url));
 
557
                x.appendChild(textTag("desc", jmsg.url_desc));
 
558
                message.appendChild(x);
 
559
        }
 
560
        send(message);
 
561
        setSuccess(TRUE);
 
562
}
 
563
 
 
564
 
 
565
/****************************************************************************
 
566
  JT_PushMessage
 
567
****************************************************************************/
 
568
JT_PushMessage::JT_PushMessage(JabTask *parent)
 
569
:JabTask(parent)
 
570
{
 
571
}
 
572
 
 
573
bool JT_PushMessage::take(const QDomElement &e)
 
574
{
 
575
        if(e.tagName() != "message")
 
576
                return FALSE;
 
577
 
 
578
        QDomElement tag;
 
579
        bool found;
 
580
        JabMessage msg;
 
581
        msg.late = FALSE;
 
582
        msg.timeStamp = QDateTime::currentDateTime();
 
583
        QString errorString;
 
584
 
 
585
        QString type = e.attribute("type");
 
586
        QString from = e.attribute("from");
 
587
        msg.from = from;
 
588
        msg.type = JABMESSAGE_NORM;
 
589
 
 
590
        if(type == "chat")
 
591
                msg.type = JABMESSAGE_CHAT;
 
592
        else if(type == "error")
 
593
                msg.type = JABMESSAGE_ERROR;
 
594
        else if(type == "headline")
 
595
                msg.type = JABMESSAGE_HEADLINE;
 
596
 
 
597
        tag = findSubTag(e, "body", &found);
 
598
        if(found)
 
599
                msg.body = tagContent(tag);
 
600
 
 
601
        // no body tag?  no message!
 
602
        if(!found && msg.type != JABMESSAGE_ERROR)
 
603
                return FALSE;
 
604
 
 
605
        tag = findSubTag(e, "subject", &found);
 
606
        if(found)
 
607
                msg.subject = tagContent(tag);
 
608
        tag = findSubTag(e, "error", &found);
 
609
        if(found)
 
610
                errorString = tagContent(tag);
 
611
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
612
                QDomElement i = n.toElement();
 
613
                if(i.isNull())
 
614
                        continue;
 
615
 
 
616
                if(i.tagName() == "x" && i.attribute("xmlns") == "jabber:x:delay") {
 
617
                        if(i.hasAttribute("stamp")) {
 
618
                                if(stamp2TS(i.attribute("stamp"), &msg.timeStamp)) {
 
619
                                        msg.timeStamp = msg.timeStamp.addSecs(getTZOffset() * 3600);
 
620
                                        msg.late = TRUE;
 
621
                                }
 
622
                        }
 
623
                }
 
624
 
 
625
                if(i.tagName() == "x" && i.attribute("xmlns") == "jabber:x:oob") {
 
626
                        QDomElement tag;
 
627
                        bool found;
 
628
 
 
629
                        tag = findSubTag(i, "url", &found);
 
630
                        if(found)
 
631
                                msg.url = tagContent(tag);
 
632
                        tag = findSubTag(i, "desc", &found);
 
633
                        if(found)
 
634
                                msg.url_desc = tagContent(tag);
 
635
                }
 
636
        }
 
637
 
 
638
        msg.originLocal = FALSE;
 
639
        if(msg.type == JABMESSAGE_ERROR)
 
640
                msg.body = QString("%1\n------\n%2").arg(errorString).arg(msg.body);
 
641
 
 
642
        message(msg);
 
643
 
 
644
        return TRUE;
 
645
}
 
646
 
 
647
 
 
648
/****************************************************************************
 
649
  JT_Queue
 
650
****************************************************************************/
 
651
JT_Queue::JT_Queue(JabTask *parent)
 
652
:JabTask(parent)
 
653
{
 
654
        active = 0;
 
655
        going = FALSE;
 
656
}
 
657
 
 
658
void JT_Queue::addTask(JabTask *j)
 
659
{
 
660
        list.append(j);
 
661
 
 
662
        //printf("JT_Queue: count=%d\n", count());
 
663
        countChanged();
 
664
 
 
665
        if(!active)
 
666
                doNext();
 
667
}
 
668
 
 
669
void JT_Queue::done(JabTask *j)
 
670
{
 
671
        itemDone(j);
 
672
        QTimer::singleShot(0, this, SLOT(afterDone()));
 
673
}
 
674
 
 
675
void JT_Queue::go()
 
676
{
 
677
        going = TRUE;
 
678
        if(!active)
 
679
                doNext();
 
680
}
 
681
 
 
682
void JT_Queue::stop()
 
683
{
 
684
        going = FALSE;
 
685
 
 
686
        // check the active
 
687
        if(active) {
 
688
                if(active->isDone()) {
 
689
                        list.remove(active);
 
690
                        delete active;
 
691
                }
 
692
                else
 
693
                        active->stop();
 
694
                active = 0;
 
695
        }
 
696
}
 
697
 
 
698
void JT_Queue::afterDone()
 
699
{
 
700
        if(active) {
 
701
                list.remove(active);
 
702
 
 
703
                // should be safe to delete now
 
704
                delete active;
 
705
                active = 0;
 
706
        }
 
707
        countChanged();
 
708
        doNext();
 
709
}
 
710
 
 
711
void JT_Queue::doNext()
 
712
{
 
713
        if(going) {
 
714
                //printf("JT_Queue: count=%d\n", count());
 
715
                active = list.getFirst();
 
716
                if(active)
 
717
                        active->go();
 
718
        }
 
719
}
 
720
 
 
721
int JT_Queue::count()
 
722
{
 
723
        return list.count();
 
724
}
 
725
 
 
726
QString JT_Queue::toString()
 
727
{
 
728
        QStringList all;
 
729
        QPtrListIterator<JabTask> it(list);
 
730
        for(JabTask *j; (j = it.current()); ++it) {
 
731
                QString s = j->toString();
 
732
                if(!s.isEmpty())
 
733
                        all.append(s);
 
734
        }
 
735
        return lineEncodeList(all);
 
736
}
 
737
 
 
738
bool JT_Queue::fromString(const QString &str)
 
739
{
 
740
        QStringList all = lineDecodeList(str);
 
741
 
 
742
        for(QStringList::Iterator it = all.begin(); it != all.end(); ++it) {
 
743
                // only type that is supported is roster
 
744
                JT_Roster *r = new JT_Roster(this);
 
745
                //printf("trying to add: [%s]\n", (*it).latin1());
 
746
                if(!r->fromString(*it)) {
 
747
                        delete r;
 
748
                        continue;
 
749
                }
 
750
                addTask(r);
 
751
                //printf("success\n");
 
752
        }
 
753
        //printf("JT_Queue: count=%d\n", count());
 
754
        doNext();
 
755
        return TRUE;
 
756
}
 
757
 
 
758
 
 
759
/****************************************************************************
 
760
  JT_GetServices
 
761
****************************************************************************/
 
762
JT_GetServices::JT_GetServices(JabTask *parent, const QString &host)
 
763
:JabTask(parent)
 
764
{
 
765
        iq = createIQ("get", host, id());
 
766
        QDomElement query = doc()->createElement("query");
 
767
        query.setAttribute("xmlns", "jabber:iq:agents");
 
768
        iq.appendChild(query);
 
769
}
 
770
 
 
771
void JT_GetServices::go()
 
772
{
 
773
        send(iq);
 
774
}
 
775
 
 
776
bool JT_GetServices::take(const QDomElement &x)
 
777
{
 
778
        if(x.attribute("id") != id())
 
779
                return FALSE;
 
780
 
 
781
        if(x.attribute("type") == "result") {
 
782
                QDomElement q = queryTag(x);
 
783
 
 
784
                // agents
 
785
                for(QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
786
                        QDomElement i = n.toElement();
 
787
                        if(i.isNull())
 
788
                                continue;
 
789
 
 
790
                        if(i.tagName() == "agent") {
 
791
                                JabRosterEntry entry;
 
792
 
 
793
                                entry.jid = i.attribute("jid");
 
794
 
 
795
                                QDomElement tag;
 
796
                                bool found;
 
797
 
 
798
                                tag = findSubTag(i, "name", &found);
 
799
                                if(found)
 
800
                                        entry.nick = tagContent(tag);
 
801
                                tag = findSubTag(i, "register", &found);
 
802
                                if(found)
 
803
                                        entry.isRegisterable = TRUE;
 
804
                                tag = findSubTag(i, "search", &found);
 
805
                                if(found)
 
806
                                        entry.isSearchable = TRUE;
 
807
                                tag = findSubTag(i, "groupchat", &found);
 
808
                                if(found)
 
809
                                        entry.isGCCapable = TRUE;
 
810
                                tag = findSubTag(i, "transport", &found);
 
811
                                if(found)
 
812
                                        entry.isTransport = TRUE;
 
813
 
 
814
                                JabRosterEntry *tmp = new JabRosterEntry(entry);
 
815
                                services.add(tmp);
 
816
                        }
 
817
                }
 
818
 
 
819
                setSuccess(TRUE);
 
820
        }
 
821
        else {
 
822
                setError(getErrorString(x));
 
823
                setSuccess(FALSE);
 
824
        }
 
825
 
 
826
        return TRUE;
 
827
}
 
828
 
 
829
 
 
830
/****************************************************************************
 
831
  JT_VCard
 
832
****************************************************************************/
 
833
JT_VCard::JT_VCard(JabTask *parent)
 
834
:JabTask(parent)
 
835
{
 
836
        type = -1;
 
837
}
 
838
 
 
839
void JT_VCard::get(const QString &_jid)
 
840
{
 
841
        type = 0;
 
842
        jid = _jid;
 
843
        iq = createIQ("get", jid, id());
 
844
        QDomElement v = doc()->createElement("vCard");
 
845
        v.setAttribute("xmlns", "vcard-temp");
 
846
        v.setAttribute("version", "3.0");
 
847
        v.setAttribute("prodid", "-//HandGen//NONSGML vGen v1.0//EN");
 
848
        iq.appendChild(v);
 
849
}
 
850
 
 
851
void JT_VCard::set(VCard &card)
 
852
{
 
853
        type = 1;
 
854
        iq = createIQ("set", "", id());
 
855
        iq.appendChild(card.toXml());
 
856
}
 
857
 
 
858
void JT_VCard::go()
 
859
{
 
860
        send(iq);
 
861
}
 
862
 
 
863
bool JT_VCard::take(const QDomElement &x)
 
864
{
 
865
        if(x.attribute("id") != id())
 
866
                return FALSE;
 
867
 
 
868
        if(x.attribute("type") == "result") {
 
869
                if(type == 0) {
 
870
                        QDomElement q = x.firstChild().toElement();
 
871
                        if(q.tagName() == "vcard") {
 
872
                                if(vcard.fromXml(q)) {
 
873
                                        setSuccess(TRUE);
 
874
                                        return TRUE;
 
875
                                }
 
876
                        }
 
877
 
 
878
                        setSuccess(FALSE);
 
879
                        return TRUE;
 
880
                }
 
881
                else {
 
882
                        setSuccess(TRUE);
 
883
                        return TRUE;
 
884
                }
 
885
        }
 
886
        else {
 
887
                setError(getErrorString(x));
 
888
                setSuccess(FALSE);
 
889
        }
 
890
 
 
891
        return TRUE;
 
892
}
 
893
 
 
894
 
 
895
/****************************************************************************
 
896
  JT_RegForm
 
897
****************************************************************************/
 
898
JT_RegForm::JT_RegForm(JabTask *parent)
 
899
:JabTask(parent)
 
900
{
 
901
        type = -1;
 
902
}
 
903
 
 
904
void JT_RegForm::get(const QString &_jid)
 
905
{
 
906
        type = 0;
 
907
        jid = _jid;
 
908
        iq = createIQ("get", jid, id());
 
909
        QDomElement query = doc()->createElement("query");
 
910
        query.setAttribute("xmlns", "jabber:iq:register");
 
911
        iq.appendChild(query);
 
912
}
 
913
 
 
914
void JT_RegForm::set(const JabForm &form)
 
915
{
 
916
        type = 1;
 
917
        jid = form.jid.full();
 
918
        iq = createIQ("set", form.jid.full(), id());
 
919
        QDomElement query = doc()->createElement("query");
 
920
        query.setAttribute("xmlns", "jabber:iq:register");
 
921
        iq.appendChild(query);
 
922
 
 
923
        // key?
 
924
        if(!form.key.isEmpty())
 
925
                query.appendChild(textTag("key", form.key));
 
926
        // fields
 
927
        QPtrListIterator<JabFormField> it(form);
 
928
        for(JabFormField *f; (f = it.current()); ++it)
 
929
                query.appendChild(textTag(f->realName(), f->value()));
 
930
}
 
931
 
 
932
void JT_RegForm::go()
 
933
{
 
934
        send(iq);
 
935
}
 
936
 
 
937
bool JT_RegForm::take(const QDomElement &x)
 
938
{
 
939
        if(x.attribute("id") != id())
 
940
                return FALSE;
 
941
 
 
942
        if(x.attribute("type") == "result") {
 
943
                if(type == 0) {
 
944
                        form.jid = x.attribute("from");
 
945
                        QDomElement q = queryTag(x);
 
946
                        for(QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
947
                                QDomElement i = n.toElement();
 
948
                                if(i.isNull())
 
949
                                        continue;
 
950
 
 
951
                                if(i.tagName() == "instructions")
 
952
                                        form.instructions = tagContent(i);
 
953
                                else if(i.tagName() == "key")
 
954
                                        form.key = tagContent(i);
 
955
                                else {
 
956
                                        JabFormField *f = new JabFormField;
 
957
                                        if(f->setType(i.tagName())) {
 
958
                                                f->setValue(tagContent(i));
 
959
                                                form.append(f);
 
960
                                        }
 
961
                                        else {
 
962
                                                delete f;
 
963
                                        }
 
964
                                }
 
965
                        }
 
966
                        setSuccess(TRUE);
 
967
                        return TRUE;
 
968
                }
 
969
                else {
 
970
                        jid = x.attribute("from");
 
971
                        setSuccess(TRUE);
 
972
                        return TRUE;
 
973
                }
 
974
        }
 
975
        else {
 
976
                setError(getErrorString(x));
 
977
                setSuccess(FALSE);
 
978
        }
 
979
 
 
980
        return TRUE;
 
981
}
 
982
 
 
983
 
 
984
/****************************************************************************
 
985
  JT_Search
 
986
****************************************************************************/
 
987
JT_Search::JT_Search(JabTask *parent)
 
988
:JabTask(parent)
 
989
{
 
990
        type = -1;
 
991
}
 
992
 
 
993
void JT_Search::get(const QString &_jid)
 
994
{
 
995
        type = 0;
 
996
        jid = _jid;
 
997
        iq = createIQ("get", jid, id());
 
998
        QDomElement query = doc()->createElement("query");
 
999
        query.setAttribute("xmlns", "jabber:iq:search");
 
1000
        iq.appendChild(query);
 
1001
}
 
1002
 
 
1003
void JT_Search::set(const JabForm &form)
 
1004
{
 
1005
        type = 1;
 
1006
        jid = form.jid.full();
 
1007
        iq = createIQ("set", form.jid.full(), id());
 
1008
        QDomElement query = doc()->createElement("query");
 
1009
        query.setAttribute("xmlns", "jabber:iq:search");
 
1010
        iq.appendChild(query);
 
1011
 
 
1012
        // key?
 
1013
        if(!form.key.isEmpty())
 
1014
                query.appendChild(textTag("key", form.key));
 
1015
        // fields
 
1016
        QPtrListIterator<JabFormField> it(form);
 
1017
        for(JabFormField *f; (f = it.current()); ++it) {
 
1018
                if(f->value().isEmpty())
 
1019
                        continue;
 
1020
 
 
1021
                query.appendChild(textTag(f->realName(), f->value()));
 
1022
        }
 
1023
}
 
1024
 
 
1025
void JT_Search::go()
 
1026
{
 
1027
        send(iq);
 
1028
}
 
1029
 
 
1030
bool JT_Search::take(const QDomElement &x)
 
1031
{
 
1032
        if(x.attribute("id") != id())
 
1033
                return FALSE;
 
1034
 
 
1035
        if(x.attribute("type") == "result") {
 
1036
                if(type == 0) {
 
1037
                        form.jid = x.attribute("from");
 
1038
                        QDomElement q = queryTag(x);
 
1039
                        for(QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
1040
                                QDomElement i = n.toElement();
 
1041
                                if(i.isNull())
 
1042
                                        continue;
 
1043
 
 
1044
                                if(i.tagName() == "instructions")
 
1045
                                        form.instructions = tagContent(i);
 
1046
                                else if(i.tagName() == "key")
 
1047
                                        form.key = tagContent(i);
 
1048
                                else {
 
1049
                                        JabFormField *f = new JabFormField;
 
1050
                                        if(f->setType(i.tagName())) {
 
1051
                                                f->setValue(tagContent(i));
 
1052
                                                form.append(f);
 
1053
                                        }
 
1054
                                        else {
 
1055
                                                delete f;
 
1056
                                        }
 
1057
                                }
 
1058
                        }
 
1059
                        setSuccess(TRUE);
 
1060
                        return TRUE;
 
1061
                }
 
1062
                else {
 
1063
                        jid = x.attribute("from");
 
1064
                        QDomElement q = queryTag(x);
 
1065
                        for(QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
1066
                                QDomElement i = n.toElement();
 
1067
                                if(i.isNull())
 
1068
                                        continue;
 
1069
 
 
1070
                                if(i.tagName() == "item") {
 
1071
                                        JabRosterEntry entry;
 
1072
 
 
1073
                                        entry.jid = i.attribute("jid");
 
1074
 
 
1075
                                        QDomElement tag;
 
1076
                                        bool found;
 
1077
 
 
1078
                                        tag = findSubTag(i, "nick", &found);
 
1079
                                        if(found)
 
1080
                                                entry.nick = tagContent(tag);
 
1081
                                        tag = findSubTag(i, "first", &found);
 
1082
                                        if(found)
 
1083
                                                entry.first = tagContent(tag);
 
1084
                                        tag = findSubTag(i, "last", &found);
 
1085
                                        if(found)
 
1086
                                                entry.last = tagContent(tag);
 
1087
                                        tag = findSubTag(i, "email", &found);
 
1088
                                        if(found)
 
1089
                                                entry.email = tagContent(tag);
 
1090
 
 
1091
                                        JabRosterEntry *tmp = new JabRosterEntry(entry);
 
1092
                                        roster.add(tmp);
 
1093
                                }
 
1094
                        }
 
1095
                        setSuccess(TRUE);
 
1096
                        return TRUE;
 
1097
                }
 
1098
        }
 
1099
        else {
 
1100
                setError(getErrorString(x));
 
1101
                setSuccess(FALSE);
 
1102
        }
 
1103
 
 
1104
        return TRUE;
 
1105
}
 
1106
 
 
1107
 
 
1108
/****************************************************************************
 
1109
  JT_ClientVersion
 
1110
****************************************************************************/
 
1111
JT_ClientVersion::JT_ClientVersion(JabTask *parent, const Jid &_j)
 
1112
:JabTask(parent)
 
1113
{
 
1114
        j = _j;
 
1115
        iq = createIQ("get", j.full(), id());
 
1116
        QDomElement query = doc()->createElement("query");
 
1117
        query.setAttribute("xmlns", "jabber:iq:version");
 
1118
        iq.appendChild(query);
 
1119
}
 
1120
 
 
1121
void JT_ClientVersion::go()
 
1122
{
 
1123
        send(iq);
 
1124
}
 
1125
 
 
1126
bool JT_ClientVersion::take(const QDomElement &x)
 
1127
{
 
1128
        if(x.attribute("id") != id())
 
1129
                return FALSE;
 
1130
 
 
1131
        if(x.attribute("type") == "result") {
 
1132
                bool found;
 
1133
                QDomElement q = queryTag(x);
 
1134
                QDomElement tag;
 
1135
                tag = findSubTag(x, "name", &found);
 
1136
                if(found)
 
1137
                        name = tagContent(tag);
 
1138
                tag = findSubTag(x, "version", &found);
 
1139
                if(found)
 
1140
                        version = tagContent(tag);
 
1141
                tag = findSubTag(x, "os", &found);
 
1142
                if(found)
 
1143
                        os = tagContent(tag);
 
1144
 
 
1145
                setSuccess(FALSE);
 
1146
        }
 
1147
        else {
 
1148
                setError(getErrorString(x));
 
1149
                setSuccess(FALSE);
 
1150
        }
 
1151
 
 
1152
        return TRUE;
 
1153
}
 
1154
 
 
1155
 
 
1156
/****************************************************************************
 
1157
  JT_ClientTime
 
1158
****************************************************************************/
 
1159
JT_ClientTime::JT_ClientTime(JabTask *parent, const Jid &_j)
 
1160
:JabTask(parent)
 
1161
{
 
1162
        j = _j;
 
1163
        iq = createIQ("get", j.full(), id());
 
1164
        QDomElement query = doc()->createElement("query");
 
1165
        query.setAttribute("xmlns", "jabber:iq:time");
 
1166
        iq.appendChild(query);
 
1167
}
 
1168
 
 
1169
void JT_ClientTime::go()
 
1170
{
 
1171
        send(iq);
 
1172
}
 
1173
 
 
1174
bool JT_ClientTime::take(const QDomElement &x)
 
1175
{
 
1176
        if(x.attribute("id") != id())
 
1177
                return FALSE;
 
1178
 
 
1179
        if(x.attribute("type") == "result") {
 
1180
                bool found;
 
1181
                QDomElement q = queryTag(x);
 
1182
                QDomElement tag;
 
1183
                tag = findSubTag(x, "utc", &found);
 
1184
                if(found)
 
1185
                        stamp2TS(tagContent(tag), &utc);
 
1186
                tag = findSubTag(x, "tz", &found);
 
1187
                if(found)
 
1188
                        timezone = tagContent(tag);
 
1189
                tag = findSubTag(x, "display", &found);
 
1190
                if(found)
 
1191
                        display = tagContent(tag);
 
1192
 
 
1193
                setSuccess(FALSE);
 
1194
        }
 
1195
        else {
 
1196
                setError(getErrorString(x));
 
1197
                setSuccess(FALSE);
 
1198
        }
 
1199
 
 
1200
        return TRUE;
 
1201
}
 
1202
 
 
1203
 
 
1204
/****************************************************************************
 
1205
  JT_ServInfo
 
1206
****************************************************************************/
 
1207
JT_ServInfo::JT_ServInfo(JabTask *parent)
 
1208
:JabTask(parent)
 
1209
{
 
1210
}
 
1211
 
 
1212
bool JT_ServInfo::take(const QDomElement &e)
 
1213
{
 
1214
        if(e.tagName() != "iq")
 
1215
                return FALSE;
 
1216
 
 
1217
        if(e.attribute("type") != "get")
 
1218
                return FALSE;
 
1219
 
 
1220
        QString ns = queryNS(e);
 
1221
        if(ns == "jabber:iq:version") {
 
1222
                QDomElement iq = createIQ("result", e.attribute("from"), e.attribute("id"));
 
1223
                QDomElement query = doc()->createElement("query");
 
1224
                query.setAttribute("xmlns", "jabber:iq:version");
 
1225
                iq.appendChild(query);
 
1226
                query.appendChild(textTag("name", PROG_NAME));
 
1227
                query.appendChild(textTag("version", PROG_VERSION));
 
1228
                query.appendChild(textTag("os", getOSName()));
 
1229
                send(iq);
 
1230
                return TRUE;
 
1231
        }
 
1232
        else if(ns == "jabber:iq:time") {
 
1233
                QDomElement iq = createIQ("result", e.attribute("from"), e.attribute("id"));
 
1234
                QDomElement query = doc()->createElement("query");
 
1235
                query.setAttribute("xmlns", "jabber:iq:time");
 
1236
                iq.appendChild(query);
 
1237
                QDateTime local = QDateTime::currentDateTime();
 
1238
                QDateTime utc = local.addSecs(-getTZOffset() * 3600);
 
1239
                QString str = getTZString();
 
1240
                query.appendChild(textTag("utc", TS2stamp(utc)));
 
1241
                query.appendChild(textTag("tz", str));
 
1242
                query.appendChild(textTag("display", QString("%1 %2").arg(local.toString()).arg(str)));
 
1243
                send(iq);
 
1244
                return TRUE;
 
1245
        }
 
1246
        else
 
1247
                return FALSE;
 
1248
}
 
1249
 
 
1250
 
 
1251
/****************************************************************************
 
1252
  JT_Search
 
1253
****************************************************************************/
 
1254
JT_Gateway::JT_Gateway(JabTask *parent)
 
1255
:JabTask(parent)
 
1256
{
 
1257
        type = -1;
 
1258
}
 
1259
 
 
1260
void JT_Gateway::get(const QString &_jid)
 
1261
{
 
1262
        type = 0;
 
1263
        jid = _jid;
 
1264
        iq = createIQ("get", jid, id());
 
1265
        QDomElement query = doc()->createElement("query");
 
1266
        query.setAttribute("xmlns", "jabber:iq:gateway");
 
1267
        iq.appendChild(query);
 
1268
}
 
1269
 
 
1270
void JT_Gateway::set(const QString &_jid, const QString &prompt)
 
1271
{
 
1272
        type = 1;
 
1273
        jid = _jid;
 
1274
        iq = createIQ("set", jid, id());
 
1275
        QDomElement query = doc()->createElement("query");
 
1276
        query.setAttribute("xmlns", "jabber:iq:gateway");
 
1277
        iq.appendChild(query);
 
1278
        query.appendChild(textTag("prompt", prompt));
 
1279
}
 
1280
 
 
1281
void JT_Gateway::go()
 
1282
{
 
1283
        send(iq);
 
1284
}
 
1285
 
 
1286
bool JT_Gateway::take(const QDomElement &x)
 
1287
{
 
1288
        if(x.attribute("id") != id())
 
1289
                return FALSE;
 
1290
 
 
1291
        if(x.attribute("type") == "result") {
 
1292
                if(type == 0) {
 
1293
                        QDomElement query = queryTag(x);
 
1294
                        bool found;
 
1295
                        QDomElement tag;
 
1296
                        tag = findSubTag(query, "desc", &found);
 
1297
                        if(found)
 
1298
                                desc = tagContent(tag);
 
1299
                        tag = findSubTag(query, "prompt", &found);
 
1300
                        if(found)
 
1301
                                prompt = tagContent(tag);
 
1302
                }
 
1303
                else {
 
1304
                        QDomElement query = queryTag(x);
 
1305
                        bool found;
 
1306
                        QDomElement tag;
 
1307
                        tag = findSubTag(query, "prompt", &found);
 
1308
                        if(found)
 
1309
                                prompt = tagContent(tag);
 
1310
                }
 
1311
 
 
1312
                setSuccess(TRUE);
 
1313
        }
 
1314
        else {
 
1315
                setError(getErrorString(x));
 
1316
                setSuccess(FALSE);
 
1317
        }
 
1318
 
 
1319
        return TRUE;
 
1320
}