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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
/*
Kopete Oscar Protocol
ssimodifytask.cpp - Handles all the ssi modification stuff
Copyright (c) 2004 by Kopete Developers <[email protected]>
Based on code Copyright (c) 2004 SuSE Linux AG <http://www.suse.com>
Based on Iris, Copyright (C) 2003 Justin Karneges
Kopete (c) 2002-2004 by the Kopete developers <[email protected]>
*************************************************************************
* *
* This library 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 2 of the License, or (at your option) any later version. *
* *
*************************************************************************
*/
#include "ssimodifytask.h"
#include <kdebug.h>
#include <klocale.h>
#include <tqstring.h>
#include "connection.h"
#include "oscarutils.h"
#include "transfer.h"
SSIModifyTask::SSIModifyTask( Task* tqparent, bool staticTask ) : Task( tqparent )
{
m_ssiManager = tqparent->client()->ssiManager();
m_static = staticTask;
m_opType = NoType;
m_opSubject = NoSubject;
m_id = 0;
}
SSIModifyTask::~SSIModifyTask()
{
}
void SSIModifyTask::onGo()
{
sendSSIUpdate();
}
bool SSIModifyTask::take( Transfer* transfer )
{
if ( forMe( transfer ) )
{
SnacTransfer* st = dynamic_cast<SnacTransfer*>( transfer );
if ( st )
{
setTransfer( transfer );
if ( st->snacSubtype() == 0x0008 )
handleSSIAdd();
else if ( st->snacSubtype() == 0x0009 )
handleSSIUpdate();
else if ( st->snacSubtype() == 0x000A )
handleSSIRemove();
else if ( st->snacSubtype() == 0x000E )
handleSSIAck();
setTransfer( 0 );
}
return true;
}
else
return false;
}
bool SSIModifyTask::addContact( const TQString& contact, const TQString& group, bool requiresAuth )
{
m_opType = Add;
m_opSubject = Contact;
TQString newContact = Oscar::normalize( contact );
Oscar::SSI oldItem = m_ssiManager->findContact( newContact );
Oscar::SSI groupItem = m_ssiManager->findGroup( group );
if ( !groupItem )
{
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "group " << group << " does not exist on SSI. Aborting" << endl;
return false;
}
//create new SSI item and populate the TLV list
TQValueList<TLV> tlvList;
if ( requiresAuth )
{
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "This contact requires auth. adding appropriate tlv" << endl;
TLV t( 0x0066, 0, 0 );
tlvList.append( t );
}
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "creating new SSI item for " << contact << " in group " << group << endl;
Oscar::SSI newItem( newContact, groupItem.gid(), m_ssiManager->nextContactId(), ROSTER_CONTACT, tlvList );
m_newItem = newItem;
return true;
}
bool SSIModifyTask::removeContact( const TQString& contact )
{
m_opType = Remove;
m_opSubject = Contact;
m_oldItem = m_ssiManager->findContact( Oscar::normalize( contact ) );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Scheduling" << m_oldItem.name() << " for removal" << endl;
return true;
}
bool SSIModifyTask::changeGroup( const TQString& contact, const TQString& newGroup )
{
m_opType = Change;
m_opSubject = Group;
m_oldItem = m_ssiManager->findContact( Oscar::normalize( contact ) );
Oscar::SSI oldGroupItem;
if ( m_oldItem.isValid() )
oldGroupItem = m_ssiManager->findGroup( newGroup );
else
return false;
if ( m_oldItem.gid() == oldGroupItem.gid() )
{ //buddy already exists in this group
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "contact " << contact << " already exists in group " << oldGroupItem.name() << ". Aborting." << endl;
return false;
}
m_groupItem = m_ssiManager->findGroup( newGroup );
if ( !m_groupItem )
{ //couldn't find group
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "new group " << newGroup << " not found in SSI. Aborting" << endl;
return false;
}
//create a new SSI item for the buddy in the new group
Oscar::SSI newItem( m_oldItem.name(), m_groupItem.gid(), m_oldItem.bid(), ROSTER_CONTACT, m_oldItem.tlvList() );
m_newItem = newItem;
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Moving '" << m_oldItem.name() << "' to group " << m_groupItem.name() << endl;
return true;
}
bool SSIModifyTask::addGroup( const TQString& groupName )
{
m_opType = Add;
m_opSubject = Group;
m_newItem = m_ssiManager->findGroup( groupName );
TQValueList<TLV> dummy;
Oscar::SSI newItem( groupName, m_ssiManager->nextGroupId(), 0, ROSTER_GROUP, dummy );
m_newItem = newItem;
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding group '" << m_newItem.name() << "' to SSI" << endl;
return true;
}
bool SSIModifyTask::removeGroup( const TQString& groupName )
{
m_opType = Remove;
m_opSubject = Group;
m_oldItem = m_ssiManager->findGroup( groupName );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Scheduling group '" << m_oldItem.name() << "' for removal. " << endl;
return true;
}
bool SSIModifyTask::renameGroup( const TQString& oldName, const TQString & newName )
{
m_opType = Rename;
m_opSubject = Group;
if ( oldName == newName )
return false;
m_oldItem = m_ssiManager->findGroup( oldName );
Oscar::SSI newItem( newName, m_oldItem.gid(), m_oldItem.bid(), ROSTER_GROUP, m_oldItem.tlvList() );
m_newItem = newItem;
return true;
}
bool SSIModifyTask::addItem( const Oscar::SSI& item )
{
m_opType = Add;
m_opSubject = NoSubject;
m_newItem = item;
return true;
}
bool SSIModifyTask::removeItem( const Oscar::SSI& item )
{
m_opType = Remove;
m_opSubject = NoSubject;
m_oldItem = item;
return true;
}
bool SSIModifyTask::modifyItem( const Oscar::SSI& oldItem, const Oscar::SSI& newItem )
{
if ( !m_ssiManager->hasItem( oldItem ) )
return false;
//make sure there are some common things between the two items
if ( oldItem.type() != newItem.type() )
return false;
m_oldItem = oldItem;
m_newItem = newItem;
m_opType = Change;
m_opSubject = NoSubject;
return true;
}
bool SSIModifyTask::forMe( const Transfer * transfer ) const
{
const SnacTransfer* st = dynamic_cast<const SnacTransfer*>( transfer );
if ( !st )
return false;
if ( st->snacService() == 0x0013 )
{
WORD subtype = st->snacSubtype();
if ( m_static )
{
if ( subtype == 0x0008 || subtype == 0x0009 || subtype == 0x000A )
return true;
}
else
{
if ( subtype == 0x000E && m_id == st->snac().id )
return true;
}
}
return false;
}
void SSIModifyTask::handleSSIAck()
{
Buffer* b = transfer()->buffer();
int numItems = b->length() / 2;
for( int i = 0; i < numItems; ++i )
{
WORD ackCode = b->getWord();
kdDebug(OSCAR_RAW_DEBUG) << "Acknowledgement code is " << ackCode << endl;
if ( ackCode != 0x0000 )
freeIdOnError();
switch( ackCode )
{
case 0x0000:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "SSI Update successful" << endl;
updateSSIManager();
break;
case 0x0002:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Item to modify not found in list" << endl;
setSuccess( 0, TQString() );
break;
case 0x0003:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Item already exists in SSI" << endl;
setSuccess( 0, TQString() );
break;
case 0x000A:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Error adding item ( invalid id, already in list, invalid data )" << endl;
setSuccess( 0, TQString() );
break;
case 0x000C:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Can't add item. Limit exceeded." << endl;
setSuccess( 0, TQString() );
break;
case 0x000D:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Can't add ICQ item to AIM list ( and vice versa )" << endl;
setSuccess( 0, TQString() );
break;
case 0x000E:
{
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Can't add item because contact requires authorization" << endl;
Oscar::SSI groupItem = m_ssiManager->findGroup( m_newItem.gid() );
TQString groupName = groupItem.name();
addContact( m_newItem.name(), groupName, true );
go();
break;
}
default:
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Unknown acknowledgement code" << endl;
setSuccess( 0, TQString() );
break;
}
};
}
void SSIModifyTask::sendSSIUpdate()
{
//what type of update are we sending?
if ( m_opSubject == Group && m_opType == Change )
changeGroupOnServer();
//add an item to the ssi list
if ( m_opType == Add )
{
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Adding an item to the SSI list" << endl;
sendEditStart();
//add the item
FLAP f1 = { 0x02, 0, 0 };
m_id = client()->snacSequence();
SNAC s1 = { 0x0013, 0x0008, 0x0000, m_id };
Buffer* ssiBuffer = new Buffer;
ssiBuffer->addString( m_newItem );
Transfer* t2 = createTransfer( f1, s1, ssiBuffer );
send( t2 );
sendEditEnd();
}
//remove an item
if ( m_opType == Remove )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing " << m_oldItem.name() << " from SSI" << endl;
sendEditStart();
//remove the item
FLAP f1 = { 0x02, 0, 0 };
m_id = client()->snacSequence();
SNAC s1 = { 0x0013, 0x000A, 0x0000, m_id };
Buffer* ssiBuffer = new Buffer;
ssiBuffer->addString( m_oldItem );
Transfer* t2 = createTransfer( f1, s1, ssiBuffer );
send( t2 );
sendEditEnd();
}
//modify an item
//we use rename for group and change for other items
if ( m_opType == Rename || ( m_opType == Change && m_opSubject != Group ) )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Modifying the item: " << m_oldItem.toString() << endl;
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "changing it to: " << m_newItem.toString() << endl;
sendEditStart();
//change the group name
FLAP f1 = { 0x02, 0, 0 };
m_id = client()->snacSequence();
SNAC s1 = { 0x0013, 0x0009, 0x0000, m_id };
Buffer* ssiBuffer = new Buffer;
ssiBuffer->addString( m_newItem );
Transfer* t2 = createTransfer( f1, s1, ssiBuffer );
send( t2 );
sendEditEnd();
}
}
void SSIModifyTask::changeGroupOnServer()
{
kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Moving a contact from one group to another" << endl;
sendEditStart();
//remove the old buddy from the list
FLAP f1 = { 0x02, 0, 0 };
SNAC s1 = { 0x0013, 0x000A, 0x0000, client()->snacSequence() };
Buffer* b1 = new Buffer;
b1->addBSTR( m_oldItem.name().latin1() );
b1->addWord( m_oldItem.gid() );
b1->addWord( m_oldItem.bid() );
b1->addWord( m_oldItem.type() );
b1->addWord( 0 );
Transfer* t2 = createTransfer( f1, s1, b1 );
send( t2 );
//add the buddy to the list with a different group
FLAP f2 = { 0x02, 0, 0 };
m_id = client()->snacSequence(); //we don't care about the first ack
SNAC s2 = { 0x0013, 0x0008, 0x0000, m_id };
Buffer* b2 = new Buffer;
addItemToBuffer( m_newItem, b2 );
Transfer* t3 = createTransfer( f2, s2, b2 );
send( t3 );
//find the old group so we can change it's list of buddy ids
//what a kludge
Oscar::SSI oldGroupItem = m_ssiManager->findGroup( m_oldItem.gid() );
/* not checking the existance of oldGroupItem because if we got here
it has to exist */
//Change the 0x00C8 TLV in the old group item to remove the bid we're
//moving to a different group
TQValueList<TLV> list = oldGroupItem.tlvList();
TLV oldIds = Oscar::findTLV( list, 0x00C8 );
if ( oldIds.type == 0x00C8 )
{
Buffer newTLVData;
Buffer tlvBuffer( oldIds.data, oldIds.length );
while ( tlvBuffer.length() != 0 )
{
WORD id = tlvBuffer.getWord();
if ( id != m_oldItem.bid() )
newTLVData.addWord( id );
}
TLV newGroupTLV( 0x00C8, newTLVData.length(), newTLVData.buffer() );
list.remove( oldIds );
list.append( newGroupTLV );
oldGroupItem.setTLVList( list );
}
//Change the 0x00C8 TLV in the new group item to add the bid we're
//adding to this group
TQValueList<TLV> list2 = m_groupItem.tlvList();
TLV oldIds2 = Oscar::findTLV( list2, 0x00C8 );
TLV newGroupTLV;
if ( oldIds2.type == 0x00C8 )
{
Buffer tlvBuffer( oldIds2.data, oldIds2.length );
tlvBuffer.addWord( m_newItem.bid() );
TLV newGroupTLV( 0x00C8, tlvBuffer.length(), tlvBuffer.buffer() );
list2.remove( oldIds );
list2.append( newGroupTLV );
m_groupItem.setTLVList( list2 );
}
//change the group properties
FLAP f3 = { 0x02, 0, 0 };
SNAC s3 = { 0x0013, 0x0009, 0x0000, client()->snacSequence() };
Buffer* b3 = new Buffer;
addItemToBuffer( oldGroupItem, b3 );
addItemToBuffer( m_groupItem, b3 );
Transfer* t4 = createTransfer( f3, s3, b3 ); //we get no ack from this packet
send( t4 );
sendEditEnd();
}
void SSIModifyTask::updateSSIManager()
{
if ( m_oldItem.isValid() && m_newItem.isValid() )
{
if ( m_opSubject == Contact )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing " << m_oldItem.name() << endl;
m_ssiManager->removeContact( m_oldItem.name() );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "and adding " << m_newItem.name() << " to SSI manager" << endl;
m_ssiManager->newContact( m_newItem );
}
else if ( m_opSubject == Group )
{
if ( m_opType == Rename )
m_ssiManager->updateGroup( m_newItem );
else if ( m_opType == Change )
m_ssiManager->updateContact( m_newItem );
}
else if ( m_opSubject == NoSubject )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing " << m_oldItem.name() << endl;
m_ssiManager->removeItem( m_oldItem );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "and adding " << m_newItem.name() << " to SSI manager" << endl;
m_ssiManager->newItem( m_newItem );
}
setSuccess( 0, TQString() );
return;
}
if ( m_oldItem.isValid() && !m_newItem )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing " << m_oldItem.name() << " from SSI manager" << endl;
if ( m_opSubject == Group )
m_ssiManager->removeGroup( m_oldItem.name() );
else if ( m_opSubject == Contact )
m_ssiManager->removeContact( m_oldItem.name() );
else if ( m_opSubject == NoSubject )
m_ssiManager->removeItem( m_oldItem );
setSuccess( 0, TQString() );
return;
}
if ( m_newItem.isValid() && !m_oldItem )
{
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding " << m_newItem.name() << " to SSI manager" << endl;
if ( m_opSubject == Group )
m_ssiManager->newGroup( m_newItem );
else if ( m_opSubject == Contact )
m_ssiManager->newContact( m_newItem );
else if ( m_opSubject == NoSubject )
m_ssiManager->newItem( m_newItem );
setSuccess( 0, TQString() );
return;
}
setSuccess( 0, TQString() );
}
void SSIModifyTask::freeIdOnError()
{
if ( m_oldItem.isValid() && m_newItem.isValid() )
{
if ( m_opSubject == Contact || m_opSubject == NoSubject )
{
if ( m_oldItem.bid() != m_newItem.bid() )
m_ssiManager->removeID( m_newItem );
}
else if ( m_opSubject == Group )
{
if ( m_oldItem.gid() != m_newItem.gid() )
m_ssiManager->removeID( m_newItem );
}
}
else if ( m_newItem.isValid() && !m_oldItem )
{
if ( m_opSubject == Group || m_opSubject == Contact ||
m_opSubject == NoSubject )
{
m_ssiManager->removeID( m_newItem );
}
}
}
void SSIModifyTask::sendEditStart()
{
SNAC editStartSnac = { 0x0013, 0x0011, 0x0000, client()->snacSequence() };
FLAP editStart = { 0x02, 0, 10 };
Buffer* emptyBuffer = new Buffer;
Transfer* t1 = createTransfer( editStart, editStartSnac, emptyBuffer );
send( t1 );
}
void SSIModifyTask::sendEditEnd()
{
SNAC editEndSnac = { 0x0013, 0x0012, 0x0000, client()->snacSequence() };
FLAP editEnd = { 0x02, 0, 10 } ;
Buffer* emptyBuffer = new Buffer;
Transfer *t5 = createTransfer( editEnd, editEndSnac, emptyBuffer );
send( t5 );
}
void SSIModifyTask::addItemToBuffer( Oscar::SSI item, Buffer* buffer )
{
buffer->addBSTR( item.name().latin1() );
buffer->addWord( item.gid() );
buffer->addWord( item.bid() );
buffer->addWord( item.type() );
buffer->addWord( item.tlvListLength() );
TQValueList<TLV>::const_iterator it = item.tlvList().begin();
TQValueList<TLV>::const_iterator listEnd = item.tlvList().end();
for( ; it != listEnd; ++it )
buffer->addTLV( ( *it ) );
}
Oscar::SSI SSIModifyTask::getItemFromBuffer( Buffer* buffer ) const
{
TQValueList<TLV> tlvList;
WORD strlength = buffer->getWord();
TQString itemName = TQString::fromUtf8( buffer->getBlock( strlength ), strlength );
WORD groupId = buffer->getWord();
WORD itemId = buffer->getWord();
WORD itemType = buffer->getWord();
WORD tlvLength = buffer->getWord();
for ( int i = 0; i < tlvLength; )
{
TLV t = buffer->getTLV();
i += 4;
i += t.length;
tlvList.append( t );
}
if ( itemType == ROSTER_CONTACT )
itemName = Oscar::normalize( itemName );
return Oscar::SSI( itemName, groupId, itemId, itemType, tlvList );
}
void SSIModifyTask::handleSSIAdd()
{
Buffer* b = transfer()->buffer();
while ( b->length() > 0 )
{
Oscar::SSI item = getItemFromBuffer( b );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding " << item.name() << " to SSI manager" << endl;
if ( item.type() == ROSTER_GROUP )
m_ssiManager->newGroup( item );
else if ( item.type() == ROSTER_CONTACT )
m_ssiManager->newContact( item );
else
m_ssiManager->newItem( item );
}
}
void SSIModifyTask::handleSSIUpdate()
{
Buffer* b = transfer()->buffer();
while ( b->length() > 0 )
{
Oscar::SSI item = getItemFromBuffer( b );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Updating " << item.name() << " in SSI manager" << endl;
if ( item.type() == ROSTER_GROUP )
m_ssiManager->updateGroup( item );
else if ( item.type() == ROSTER_CONTACT )
m_ssiManager->updateContact( item );
else
m_ssiManager->updateItem( item );
}
}
void SSIModifyTask::handleSSIRemove()
{
Buffer* b = transfer()->buffer();
while ( b->length() > 0 )
{
Oscar::SSI item = getItemFromBuffer( b );
kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing " << item.name() << " from SSI manager" << endl;
if ( item.type() == ROSTER_GROUP )
m_ssiManager->removeGroup( item );
else if ( item.type() == ROSTER_CONTACT )
m_ssiManager->removeContact( item );
else
m_ssiManager->removeItem( item );
}
}
//kate: tab-width 4; indent-mode csands;
|