summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/oscarutils.cpp
blob: 93677b5996e2c0d8944c0995e7bbc60b027f0b51 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
    Kopete Oscar Protocol
    oscarutils.cpp - Oscar Utility Functions

    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 "oscarutils.h"
#include <tqhostaddress.h>
#include <tdeapplication.h>
#include <kdebug.h>


using namespace Oscar;

TQString Oscar::normalize( const TQString& contact )
{
	TQString normal = contact.lower();
	normal.remove( ' ' );
	return normal;
}

bool Oscar::operator==( TLV a, TLV b )
{
	if ( a.type == b.type && a.length == b.length )
		return true;
	else
		return false;
}

TLV Oscar::findTLV( const TQValueList<TLV>& list, int type )
{
	TLV t;
	TQValueList<TLV>::const_iterator it;
	for ( it = list.begin(); it != list.end(); ++it )
	{
		if ( ( *it ).type == type )
			return ( *it );
	}

	return t;
}

bool Oscar::uptateTLVs( SSI& item, const TQValueList<TLV>& list )
{
	bool changed = false;
	TQValueList<TLV> tList( item.tlvList() );

	TQValueList<TLV>::const_iterator it;
	for ( it = list.begin(); it != list.end(); ++it )
	{
		TLV t = Oscar::findTLV( tList, ( *it ).type );
		if ( t && t.length == ( *it ).length &&
		     memcmp( t.data.data(), ( *it ).data.data(), t.length ) == 0 )
			continue;

		if ( t )
			tList.remove( t );

		tList.append( *it );
		changed = true;
	}

	if ( changed )
		item.setTLVList( tList );

	return changed;
}

int Oscar::parseCap( char* cap )
{
	int capflag = -1;
	for (int i = 0; i < CAP_LAST; i++)
	{
		if (memcmp(&oscar_caps[i], cap, 16) == 0)
		{
			capflag = i;
			break; // should only match once...
		}
	}
	return capflag;
}

const TQString Oscar::capToString( char* cap )
{
	TQString dbg;

	dbg.sprintf( "{%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
		cap[0], cap[1], cap[2], cap[3], cap[4], cap[5], cap[6], cap[7], cap[8], cap[9],
		cap[10], cap[11], cap[12], cap[13], cap[14], cap[15] );

	return dbg;
}

DWORD Oscar::parseCapabilities( Buffer &inbuf, TQString &versionString )
{
	//
	// FIXME: port capabilities array to some qt based list class, makes usage of memcmp obsolete
	//
	DWORD capflags = 0;
	TQString dbgCaps = "CAPS: ";
	
	while(inbuf.length() >= 16)
	{
		TQByteArray cap;
		cap.duplicate( inbuf.getBlock(16) );
		
		for (int i=0; i < CAP_LAST; i++)
		{
			if (i == CAP_KOPETE)
			{
				if (memcmp(&oscar_caps[i], cap.data(), 12) == 0)
				{
					capflags |= (1 << i);
					versionString.sprintf( "%d.%d.%d%d", cap[12], cap[13], cap[14], cap[15] );
					versionString.insert( 0, "Kopete " );
					kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Kopete version - " << versionString << endl;
				}
			}
			else if (i == CAP_MICQ)
			{
				if (memcmp(&oscar_caps[i], cap.data(), 12) == 0)
				{
					kdDebug(14150) << k_funcinfo << "MICQ version : <" <<
						(int)cap[12] << ":" << (int)cap[13] << ":" <<
						(int)cap[14] << ":" << (int)cap[15] << ">" << endl;
					
					capflags |= (1 << i);
					
						// FIXME: how to decode this micq version mess? [mETz - 08.06.2004]
						/*versionString.sprintf("%d.%d.%d%d",
							cap[12], cap[13], cap[14], cap[15]);*/
					break;
				}
			}
			else if (i == CAP_SIMNEW)
			{
				if (memcmp(&oscar_caps[i], cap, 12) == 0)
				{
					kdDebug(14150) << k_funcinfo << "SIM version : <" <<
						(unsigned int)cap[12] << ":" << (unsigned int)cap[13] << ":" <<
						(unsigned int)cap[14] << ":" << (unsigned int)cap[15] << ">" << endl;
					capflags |= (1 << i);
					versionString.sprintf("%d.%d.%d%d",
					                      cap[12], cap[13], cap[14], cap[15]);
					versionString.insert( 0, "SIM " );
					break;
				}
			}
			else if (i == CAP_SIMOLD)
			{
				if (memcmp(&oscar_caps[i], cap, 15) == 0)
				{
					int hiVersion = (cap[15] >> 6) - 1;
					unsigned loVersion = cap[15] & 0x1F;
					kdDebug(14150) << k_funcinfo << "OLD SIM version : <" <<
						hiVersion << ":" << loVersion << endl;
					capflags |= (1 << i);
					versionString.sprintf("%d.%d", (unsigned int)hiVersion, loVersion);
					versionString.insert( 0, "SIM " );
					break;
				}
			}
			else if (memcmp(&oscar_caps[i], cap.data(), 16) == 0)
			{
				capflags |= (1 << i);
				dbgCaps += capName(i);
				break;
			} // END if(memcmp...
		} // END for...
	}
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << dbgCaps << endl;
	return capflags;
}

const TQString Oscar::capName( int capNumber )
{
	TQString capString;

	switch ( capNumber )
	{
	case CAP_VOICE:
		capString = "CAP_VOICE ";
		break;
	case CAP_BUDDYICON:
		capString = "CAP_BUDDYICON ";
		break;
	case CAP_IMIMAGE:
		capString = "CAP_IMIMAGE ";
		break;
	case CAP_CHAT:
		capString = "CAP_CHAT ";
		break;
	case CAP_GETFILE:
		capString = "CAP_GETFILE ";
		break;
	case CAP_SENDFILE:
		capString = "CAP_SENDFILE ";
		break;
	case CAP_GAMES2:
	case CAP_GAMES:
		capString = "CAP_GAMES ";
		break;
	case CAP_SAVESTOCKS:
		capString = "CAP_SAVESTOCKS ";
		break;
	case CAP_SENDBUDDYLIST:
		capString = "CAP_SENDBUDDYLIST ";
		break;
	case CAP_ISICQ:
		capString = "CAP_ISICQ ";
		break;
	case CAP_APINFO:
		capString = "CAP_APINFO ";
		break;
	case CAP_RTFMSGS:
		capString = "CAP_RTFMSGS ";
		break;
	case CAP_ICQSERVERRELAY:
		capString = "CAP_ICQSERVERRELAY ";
		break;
	case CAP_IS_2001:
		capString = "CAP_IS_2001 ";
		break;
	case CAP_TRILLIAN:
		capString = "CAP_TRILLIAN ";
		break;
	case CAP_TRILLIANCRYPT:
		capString = "CAP_TRILLIANCRYPT ";
		break;
	case CAP_UTF8:
		capString = "CAP_UTF8 ";
		break;
	case CAP_TYPING:
		capString = "CAP_TYPING ";
		break;
	case CAP_INTEROPERATE:
		capString = "CAP_INTEROPERATE ";
		break;
	case CAP_KOPETE:
		capString = "CAP_KOPETE ";
		break;
	case CAP_MICQ:
		capString = "CAP_MICQ ";
		break;
	case CAP_MACICQ:
		capString = "CAP_MACICQ ";
		break;
	case CAP_SIMOLD:
		capString = "CAP_SIMOLD ";
		break;
	case CAP_SIMNEW:
		capString = "CAP_SIMNEW ";
		break;
	case CAP_XTRAZ:
		capString = "CAP_XTRAZ ";
		break;
	case CAP_STR_2001:
		capString = "CAP_STR_2001 ";
		break;
	case CAP_STR_2002:
		capString = "CAP_STR_2002 ";
		break;
	default:
		capString = "UNKNOWN CAP ";
	} // END switch

	return capString;
}

DWORD Oscar::getNumericalIP(const TQString &address)
{
	TQHostAddress addr;
	if ( addr.setAddress( address ) == false )
		return 0;

	return (DWORD)addr.toIPv4Address();
}

TQString Oscar::getDottedDecimal( DWORD address )
{
	TQHostAddress addr;
	addr.setAddress((TQ_UINT32)address);
	return addr.toString();
}