summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/ssilisttask.cpp
blob: a62e6721098d4deee99d7e023b27aeef48636fc2 (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
169
170
171
172
173
174
/*
  Kopete Oscar Protocol
  ssilisttask.cpp - handles all operations dealing with the whole SSI list

  Copyright (c) 2004 Matt Rogers <mattr@kde.org>

  Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>

  *************************************************************************
  *                                                                       *
  * 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 "ssilisttask.h"

#include <kdebug.h>
#include "connection.h"
#include "oscarutils.h"
#include "ssimanager.h"
#include "transfer.h"

SSIListTask::SSIListTask( Task* parent ) : Task( parent )
{
	m_ssiManager = client()->ssiManager();
	TQObject::connect( this, TQT_SIGNAL( newContact( const Oscar::SSI& ) ), m_ssiManager, TQT_SLOT( newContact( const Oscar::SSI& ) ) );
	TQObject::connect( this, TQT_SIGNAL( newGroup( const Oscar::SSI& ) ), m_ssiManager, TQT_SLOT( newGroup( const Oscar::SSI& ) ) );
	TQObject::connect( this, TQT_SIGNAL( newItem( const Oscar::SSI& ) ), m_ssiManager, TQT_SLOT( newItem( const Oscar::SSI& ) ) );
}


SSIListTask::~SSIListTask()
{}

bool SSIListTask::forMe( const Transfer* transfer ) const
{
	const SnacTransfer * st = dynamic_cast<const SnacTransfer*>( transfer );
	if ( !st )
		return false;

	if ( st->snacService() == 0x0013 )
	{
		switch ( st->snacSubtype() )
		{
		case 0x0006:
		case 0x000F:
			return true;
		default:
			return false;
		};
	}

	return false;
}

bool SSIListTask::take( Transfer* transfer )
{
	if ( forMe( transfer ) )
	{
		SnacTransfer * st = dynamic_cast<SnacTransfer*>( transfer );
		if ( st->snacSubtype() == 0x0006 )
		{
			setTransfer( transfer );
			handleSSIListReply();
			setTransfer( 0 );
			return true;
		}
		else if ( st->snacSubtype() == 0x000F )
		{
			setTransfer( transfer );
			handleSSIUpToDate();
			setTransfer( 0 );
			return true;
		}
	}

	return false;
}

void SSIListTask::onGo()
{
	checkSSITimestamp();
}

void SSIListTask::handleSSIListReply()
{
	TQValueList<TLV> tlvList;

	Buffer* buffer = transfer()->buffer();
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "SSI Protocol version: " << buffer->getByte() << endl;
	WORD ssiItems = buffer->getWord();
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Number of items in this SSI packet: " << ssiItems << endl;
	WORD parsedItems;
	for ( parsedItems = 1; parsedItems <= ssiItems; ++parsedItems )
	{
		tlvList.clear();
		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 );
		
		Oscar::SSI s( itemName, groupId, itemId, itemType, tlvList );
		
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Got SSI Item: " << s.toString() << endl;
		if ( s.type() == ROSTER_GROUP )
			emit newGroup( s );
		
		if ( s.type() == ROSTER_CONTACT )
			emit newContact( s );
		
		if ( s.type() != ROSTER_CONTACT && s.type() != ROSTER_GROUP )
			emit newItem( s );
	}
	
	if ( buffer->length() > 0 )
	{
		client()->ssiManager()->setLastModificationTime( buffer->getDWord() );
		//check the snac flags for another packet
		SnacTransfer* st = dynamic_cast<SnacTransfer*>( transfer() );
		if ( st && st->snacFlags() == 0  )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "SSI List complete" << endl;
			client()->ssiManager()->setListComplete( true );
			setSuccess( 0, TQString() );
		}
		else
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Awaiting another SSI packet" << endl;
	}

}

void SSIListTask::handleSSIUpToDate()
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Our SSI List is up to date" << endl;
	Buffer* buffer = transfer()->buffer();

	client()->ssiManager()->setLastModificationTime( buffer->getDWord() );
	WORD ssiItems = buffer->getWord();
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Number of items in SSI list: " << ssiItems << endl;

	client()->ssiManager()->setListComplete( true );
	setSuccess( 0, TQString() );
}

void SSIListTask::checkSSITimestamp()
{
	kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Checking the timestamp of the SSI list" << endl;
	FLAP f = { 0x02, 0, 0 };
	SNAC s = { 0x0013, 0x0005, 0x0000, client()->snacSequence() };
	Buffer* buffer = new Buffer();
	buffer->addDWord( client()->ssiManager()->lastModificationTime() );
	buffer->addDWord( client()->ssiManager()->numberOfItems() );
	Transfer* t = createTransfer( f, s, buffer );
	send( t );
}

#include "ssilisttask.moc"

// kate: tab-width 4; indent-mode csands;