-
Notifications
You must be signed in to change notification settings - Fork 1
/
roster.cpp
427 lines (379 loc) · 10.5 KB
/
roster.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/****************************************************************************
**
** Ireen — cross-platform OSCAR protocol library
**
** Copyright © 2012 Ruslan Nigmatullin <[email protected]>
** Alexey Prokhin <[email protected]>
**
*****************************************************************************
**
** $IREEN_BEGIN_LICENSE$
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as
** published by the Free Software Foundation, either version 3
** 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 Lesser General Public License
** along with this program. If not, see http://www.gnu.org/licenses/.
** $IREEN_END_LICENSE$
**
****************************************************************************/
#include "roster.h"
#include "feedbag.h"
#include "status.h"
#include "client.h"
#include "core/sessiondataitem.h"
#include "core/util.h"
namespace Ireen {
enum SsiBuddyTlvs
{
SsiBuddyReqAuth = 0x0066,
SsiBuddyProto = 0x0084,
SsiBuddyNick = 0x0131,
SsiBuddyComment = 0x013c
};
class ContactItemPrivate : public QSharedData
{
public:
void setFeedbagItem(const FeedbagItem &item);
private:
friend class ContactItem;
FeedbagItem feedbagItem;
};
class StatusItemPrivate : public QSharedData
{
public:
void setTlvs(const TLVMap &tlvs, bool online);
private:
friend class StatusItem;
bool online;
quint16 statusId;
quint16 statusFlags;
TLVMap tlvs;
SessionDataItemMap statusData;
};
class RosterPrivate
{
public:
Feedbag *feedbag;
};
ContactItem::ContactItem() :
d(new ContactItemPrivate)
{
}
ContactItem::ContactItem(const FeedbagItem &item) :
d(new ContactItemPrivate)
{
d->setFeedbagItem(item);
}
ContactItem::~ContactItem()
{
}
ContactItem::ContactItem(const ContactItem &other) :
d(other.d)
{
}
void ContactItem::operator=(const ContactItem &other)
{
d = other.d;
}
void ContactItemPrivate::setFeedbagItem(const FeedbagItem &item)
{
feedbagItem = item;
}
QString ContactItem::uin() const
{
return d->feedbagItem.name();
}
QString ContactItem::name() const
{
return d->feedbagItem.field<QString>(SsiBuddyNick);
}
void ContactItem::setName(const QString &name)
{
d->feedbagItem.setField(SsiBuddyNick, name);
}
quint16 ContactItem::groupId() const
{
return d->feedbagItem.groupId();
}
QString ContactItem::group() const
{
FeedbagItem group = d->feedbagItem.feedbag()->groupItem(groupId());
Q_ASSERT(!group.isNull());
return group.name();
}
QString ContactItem::protocol() const
{
return d->feedbagItem.field<QString>(SsiBuddyProto);
}
QString ContactItem::comment() const
{
return d->feedbagItem.field<QString>(SsiBuddyComment);
}
void ContactItem::setComment(const QString &comment)
{
d->feedbagItem.setField(SsiBuddyComment, comment);
}
bool ContactItem::isAuthorizedBy() const
{
return !d->feedbagItem.containsField(SsiBuddyReqAuth);
}
FeedbagItem &ContactItem::feedbagItem() const
{
return d->feedbagItem;
}
StatusItem::StatusItem() :
d(new StatusItemPrivate)
{
}
StatusItem::~StatusItem()
{
}
StatusItem::StatusItem(const StatusItem &other) :
d(other.d)
{
}
void StatusItem::operator=(const StatusItem &other)
{
d = other.d;
}
void StatusItemPrivate::setTlvs(const TLVMap &tlvs_, bool online_)
{
online = online_;
tlvs = tlvs_;
if (online && tlvs.contains(0x06)) {
DataUnit data(tlvs.value(0x06));
statusFlags = data.read<quint16>();
statusId = data.read<quint16>();
//if (!status.setStatusFlag(statusId & 0x0fff))
// status.setStatusType(Status::Online);
} else {
statusId = online ? Status::Online : Status::Offline;
}
statusData = tlvs.value(0x1D);
}
quint16 StatusItem::statusId() const
{
return d->statusId;
}
quint16 StatusItem::statusFlags() const
{
return d->statusFlags;
}
QDateTime StatusItem::statusTextUpdateTime() const
{
if (d->statusData.contains(0x0d)) {
quint16 time = d->statusData.value(0x0d).read<quint16>();
debug() << "Status note update time" << time;
return QDateTime::fromTime_t(time);
}
return QDateTime();
}
QString StatusItem::statusText() const
{
if (d->statusData.contains(0x02)) {
DataUnit data(d->statusData.value(0x02));
QByteArray note_data = data.read<QByteArray, quint16>();
QByteArray encoding = data.read<QByteArray, quint16>();
QTextCodec *codec = 0;
if (!encoding.isEmpty()) {
codec = QTextCodec::codecForName(encoding);
if (!codec)
debug() << "Server sent wrong encoding for status note";
}
if (!codec)
codec = Util::utf8Codec();
return codec->toUnicode(note_data);
}
return QString();
}
Capabilities StatusItem::capabilities() const
{
Capabilities caps;
if (d->tlvs.contains(0x000d)) {
DataUnit data(d->tlvs.value(0x000d));
while (data.dataSize() >= 16)
caps << data.read<Capability>();
}
if (d->tlvs.contains(0x0019)) {
DataUnit data(d->tlvs.value(0x0019));
while (data.dataSize() >= 2)
caps << Capability(data.readData(2));
}
return caps;
}
QDateTime StatusItem::onlineSince() const
{
if (d->tlvs.contains(0x000f))
return QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t() - d->tlvs.value<quint32>(0x000f));
else if (d->tlvs.contains(0x0003))
return QDateTime::fromTime_t(d->tlvs.value<quint32>(0x0003));
else
return QDateTime();
}
QDateTime StatusItem::awaySince() const
{
if (d->tlvs.contains(0x0004))
return QDateTime::currentDateTime().addSecs(-60 * d->tlvs.value<quint32>(0x0004));
else if (d->statusId != Status::Online && d->online)
return QDateTime::currentDateTime();
else
return QDateTime();
}
QDateTime StatusItem::registrationTime() const
{
if (d->tlvs.contains(0x0005))
return QDateTime::fromTime_t(d->tlvs.value<quint32>(0x0005));
else
return QDateTime();
}
SessionDataItemMap StatusItem::statusData() const
{
return d->statusData;
}
Roster::Roster(Client *client, Feedbag *feedbag)
{
m_infos << SNACInfo(ServiceFamily, ServiceServerAsksServices)
<< SNACInfo(BuddyFamily, UserOnline)
<< SNACInfo(BuddyFamily, UserOffline)
<< SNACInfo(BuddyFamily, UserSrvReplyBuddy)
<< SNACInfo(ListsFamily, ListsAuthRequest)
<< SNACInfo(ListsFamily, ListsSrvAuthResponse);
m_types << SsiBuddy << SsiGroup;
client->registerHandler(this);
feedbag->registerHandler(this);
client->registerInitializationSnac(BuddyFamily, UserCliReqBuddy);
}
bool Roster::handleFeedbagItem(Feedbag *feedbag, const FeedbagItem &item, Feedbag::ModifyType type, FeedbagError error)
{
Q_UNUSED(feedbag);
if (type != Feedbag::Remove && error == FeedbagError::RequiresAuthorization) {
// Failed to add the contact because it requires authorization.
// Try to readd the contact with SsiBuddyReqAuth field present.
Q_ASSERT(!item.isInList());
FeedbagItem itemCopy = item;
itemCopy.setId(item.feedbag()->uniqueItemId(SsiBuddy));
itemCopy.setField(SsiBuddyReqAuth);
itemCopy.add();
return true;
} else if (error != FeedbagError::NoError) {
return false;
}
if (type == Feedbag::Remove)
handleRemoveCLItem(item);
else
handleAddModifyCLItem(item, type);
return true;
}
void Roster::handleAddModifyCLItem(const FeedbagItem &item, Feedbag::ModifyType type)
{
switch (item.type()) {
case SsiBuddy: {
if (item.name().isEmpty())
break;
debug(DebugVerbose) << "The contact" << item.name() << "has been added or updated";
ContactItem newContact;
newContact.d->setFeedbagItem(item);
emit contactItemReceived(newContact);
break;
}
case SsiGroup: {
if (item.groupId() == 0) // Skip Root group
break;
if (type == Feedbag::Modify) {
FeedbagItem old = item.feedbag()->groupItem(item.groupId());
if (old.name() != item.name()) {
debug(DebugVerbose) << "The group" << old.name() << "has been renamed to" << item.name();
emit groupItemRenamed(item.name(), old.name());
}
} else {
debug(DebugVerbose) << "The group" << item.name() << "has been added";
emit groupItemAdded(item.name());
}
break;
}
}
}
void Roster::handleRemoveCLItem(const FeedbagItem &item)
{
switch (item.type()) {
case SsiBuddy: {
debug(DebugVerbose) << "The contact" << item.name() << "has been removed";
emit contactItemRemoved(item.name());
break;
}
case SsiGroup: {
if (item.groupId() == 0) // Skip Root group
break;
debug(DebugVerbose) << "The group" << item.name() << "has been removed";
emit groupItemRemoved(item.name());
break;
}
}
}
void Roster::handleSNAC(AbstractConnection *conn, const SNAC &sn)
{
switch ((sn.family() << 16) | sn.subtype()) {
case ServiceFamily << 16 | ServiceServerAsksServices: {
// Requesting client-side contactlist rights
SNAC snac(BuddyFamily, UserCliReqBuddy);
snac.appendTLV<quint16>(0x0005, 0x0f);
// snac.appendTLV(0x0006, QByteArray::fromHex("000000"));
// snac.appendTLV<quint8>(0x0007, 0x00);
// Request facebook contacts
snac.appendTLV<quint8>(0x0008, 1);
// Query flags: 1 = Enable Avatars
// 2 = Enable offline status message notification
// 4 = Enable Avatars for offline contacts
// 8 = Use reject for not authorized contacts
//snac.appendTLV<quint16>(0x05, 7);
conn->send(snac);
break;
}
case BuddyFamily << 16 | UserOnline:
handleNewStatus(sn, true);
break;
case BuddyFamily << 16 | UserOffline:
handleNewStatus(sn, false);
break;
case BuddyFamily << 16 | UserSrvReplyBuddy:
debug() << IMPLEMENT_ME << "BuddyFamily, UserSrvReplyBuddy";
break;
case ListsFamily << 16 | ListsAuthRequest: {
QString uin = sn.read<QString, quint8>();
QString reason = sn.read<QString, qint16>();
debug() << QString("Authorization request from \"%1\" with reason \"%2").arg(uin).arg(reason);
emit authorizationRequestReceived(uin, reason);
break;
}
case ListsFamily << 16 | ListsSrvAuthResponse: {
QString uin = sn.read<QString, qint8>();
bool isAccepted = sn.read<qint8>();
QString reason = sn.read<QString, qint16>();
QString verb = isAccepted ? "accepted" : "declined";
debug() << QString("Our authorization request to \"%1\" has been %2 with reason \"%3")
.arg(uin)
.arg(verb)
.arg(reason);
emit authorizationRequestReply(uin, isAccepted, reason);
break;
}
}
}
void Roster::handleNewStatus(const SNAC &snac, bool online)
{
QString uin = snac.read<QString, quint8>();
quint16 warning_level = snac.read<quint16>();
Q_UNUSED(warning_level);
TLVMap tlvs = snac.read<TLVMap, quint16>();
StatusItem status;
status.d->setTlvs(tlvs, online);
emit contactStatusUpdated(uin, status);
}
} // namespace Ireen