summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/oscartypeclasses.cpp
blob: 59f3643480411a7e7fe81a57bd5e6dcb41a70ae6 (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
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
/*
    Kopete Oscar Protocol
    oscartypeclasses.cpp - Oscar Type Definitions

    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 "oscartypeclasses.h"
#include <tqdeepcopy.h>
#include <tqvaluelist.h>
#include <kdebug.h>
#include "oscarutils.h"
#include "buffer.h"


// using namespace Oscar;

Oscar::TLV::TLV()
{
	type = 0;
	length = 0;
}

Oscar::TLV::TLV( TQ_UINT16 newType, TQ_UINT16 newLength, char* newData )
{
	type = newType;
	length = newLength;
	data.truncate(0);
	data.duplicate( newData, length );
}

Oscar::TLV::TLV( TQ_UINT16 newType, TQ_UINT16 newLength, const TQByteArray& newData )
{
	type = newType;
	length = newLength;
	data.duplicate( newData );
}

Oscar::TLV::TLV( const TLV& t )
{
	type = t.type;
	length = t.length;
	data.truncate(0);
	data.duplicate( t.data );
}

Oscar::TLV::operator bool() const
{
	return type != 0;
}


Oscar::SSI::SSI()
{
	m_gid = 0;
	m_bid = 0;
	m_type = 0xFFFF;
	m_tlvLength = 0;
	m_waitingAuth = false;
}

Oscar::SSI::SSI( const TQString &name, int gid, int bid, int type, const TQValueList<TLV> &tlvlist, int tlvLength )
{
	m_name = name;
	m_gid = gid;
	m_bid = bid;
	m_type = type;
	m_tlvLength = tlvLength;

	//deepcopy the tlvs
	m_tlvList = TQDeepCopy< TQValueList<TLV> >( tlvlist );

	if ( m_tlvLength == 0 && !m_tlvList.isEmpty() )
		refreshTLVLength();

	checkTLVs();
}

Oscar::SSI::SSI( const Oscar::SSI& other )
{
	m_name = other.m_name;
	m_gid = other.m_gid;
	m_bid = other.m_bid;
	m_type = other.m_type;
	m_tlvLength = other.m_tlvLength;
	m_alias = other.m_alias;
	m_waitingAuth = other.m_waitingAuth;

	//deepcopy the tlvs
	m_tlvList = TQDeepCopy< TQValueList<TLV> >( other.m_tlvList );

	if ( m_tlvLength == 0 && !m_tlvList.isEmpty() )
		refreshTLVLength();
}

bool Oscar::SSI::isValid() const
{
	return m_type != 0xFFFF;
}

TQString Oscar::SSI::name() const
{
	return m_name;
}

TQ_UINT16 Oscar::SSI::gid() const
{
	return m_gid;
}

TQ_UINT16 Oscar::SSI::bid() const
{
	return m_bid;
}

TQ_UINT16 Oscar::SSI::type() const
{
	return m_type;
}

const TQValueList<TLV>& Oscar::SSI::tlvList() const
{
	return m_tlvList;
}

void Oscar::SSI::setTLVListLength( TQ_UINT16 newLength )
{
	m_tlvLength = newLength;
}

TQ_UINT16 Oscar::SSI::tlvListLength() const
{
	return m_tlvLength;
}

void Oscar::SSI::setTLVList( TQValueList<TLV> list )
{
	//deepcopy the tlvs
	m_tlvList = TQDeepCopy< TQValueList<TLV> >( list );
	refreshTLVLength();
	checkTLVs();
}

void Oscar::SSI::refreshTLVLength()
{
	m_tlvLength = 0;
	TQValueList<TLV>::iterator it = m_tlvList.begin();
	for( ; it != m_tlvList.end(); ++it )
	{
		m_tlvLength += 4;
		m_tlvLength += (*it).length;
	}
}

void Oscar::SSI::checkTLVs()
{
	//check for the auth TLV
	TLV authTLV = findTLV( m_tlvList, 0x0066 );
	if ( authTLV )
	{
		kdDebug(14151) << k_funcinfo << "Need auth for contact " << m_name << endl;
		m_waitingAuth = true;
	}
	else
		m_waitingAuth = false;

	//check for the alias TLV
	TLV aliasTLV = findTLV( m_tlvList, 0x0131 );
	if ( aliasTLV )
	{
		m_alias = TQString::fromUtf8( aliasTLV.data, aliasTLV.length );
		kdDebug( 14151 ) << k_funcinfo << "Got an alias '" << m_alias << "' for contact '" << m_name << "'" << endl;
	}

	TLV privacyTLV = findTLV( m_tlvList, 0x00CA );
	if ( privacyTLV )
		kdDebug(14151) << k_funcinfo << "Found privacy settings " << privacyTLV.data << endl;

	TLV infoTLV = findTLV( m_tlvList, 0x00CC );
	if ( infoTLV )
		kdDebug(14151) << k_funcinfo << "Found 'allow others to see...' options " << infoTLV.data << endl;
}

TQString Oscar::SSI::alias() const
{
	return m_alias;
}

void Oscar::SSI::setAlias( const TQString& newAlias )
{
	m_alias = newAlias;
}

bool Oscar::SSI::waitingAuth() const
{
	return m_waitingAuth;
}

void Oscar::SSI::setWaitingAuth( bool waiting )
{
	m_waitingAuth = waiting;
}

void Oscar::SSI::setIconHash( TQByteArray hash )
{
	m_hash.duplicate( hash );
}

TQByteArray Oscar::SSI::iconHash( ) const
{
	return m_hash;
}

TQString Oscar::SSI::toString() const
{
	TQString ssiString = TQString::fromLatin1( "name: " );
	ssiString += m_name;
	ssiString += " gid: ";
	ssiString += TQString::number( m_gid );
	ssiString += " bid: ";
	ssiString += TQString::number( m_bid );
	ssiString += " type: ";
	ssiString += TQString::number( m_type );
	ssiString += " tlv length: ";
	ssiString += TQString::number( m_tlvLength );
	return ssiString;
}

bool Oscar::SSI::operator==( const SSI& item ) const
{
	if ( m_name == item.name() && m_gid == item.gid() && m_bid == item.bid() && m_type == item.type() )
		return true;
	else
		return false;
}

Oscar::SSI::operator bool() const
{
	return isValid();
}

Oscar::SSI::operator TQByteArray() const
{
	Buffer b;
	TQCString name( m_name.utf8() );
	uint namelen = name.length();
	const char *namedata = name;
	b.addWord( namelen );
	// Using namedata instead of name because
	// Buffer::addString(TQByteArray, DWORD) ignores it's second argument,
	// while Buffer::addString(const char*, DWORD) does not ignore it.
	// We must provide the explicit length argument to addString() because
	// we don't need trailing null byte to be added when automatic
	// conversion from TQCString to TQByteArray is performed.
	// This hack will not be needed with TQt 4.
	b.addString( namedata, namelen );
	b.addWord( m_gid );
	b.addWord( m_bid );
	b.addWord( m_type );
	b.addWord( m_tlvLength );
	TQValueList<Oscar::TLV>::const_iterator it = m_tlvList.begin();
	for( ; it != m_tlvList.end(); ++it )
	{
		b.addWord( (*it).type );
		b.addWord( (*it).length );
		b.addString( (*it).data, (*it).data.size() );
	}

	return (TQByteArray) b;
}