summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/groupwise/libgroupwise/responseprotocol.cpp
blob: 50788178b4132974b4d828801ea4b448f91b9330 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
    Kopete Groupwise Protocol
    responseprotocol.cpp - Protocol used for reading incoming GroupWise Responses

    Copyright (c) 2004      SUSE Linux AG	 	 http://www.suse.com
    
    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 <tqbuffer.h>

#include "response.h"

#include "responseprotocol.h"

ResponseProtocol::ResponseProtocol(TQObject* parent, const char* name): InputProtocolBase(parent, name)
{
}


ResponseProtocol::~ResponseProtocol()
{
}

Transfer * ResponseProtocol::parse( const TQByteArray & wire, uint & bytes )
{
	m_bytes = 0;
	m_collatingFields.clear();
	//m_din = new TQDataStream( wire, IO_ReadOnly );
	TQBuffer inBuf( wire );
	inBuf.open( IO_ReadOnly); 
	m_din.setDevice( &inBuf );
	m_din.setByteOrder( TQDataStream::LittleEndian );
	
	// check that this begins with a HTTP (is a response)
	TQ_UINT32 val;
	m_din >> val;
	m_bytes += sizeof( TQ_UINT32 );
	
	Q_ASSERT( tqstrncmp( (const char *)&val, "HTTP", strlen( "HTTP" ) ) == 0 );
	
	// read rest of HTTP header and look for a 301 redirect. 
	TQCString headerFirst;
	if ( !readGroupWiseLine( headerFirst ) )
		return 0;
	// pull out the HTTP return code
	int firstSpace = headerFirst.find( ' ' );
	TQString rtnField = headerFirst.mid( firstSpace, headerFirst.find( ' ', firstSpace + 1 ) );
	bool ok = true;
	int rtnCode;
	int packetState = -1;
	rtnCode = rtnField.toInt( &ok );
	debug( "CoreProtocol::readResponse() got HTTP return code " );
	// read rest of header
	TQStringList headerRest;
	TQCString line;
	while ( line != "\r\n" )
	{
		if ( !readGroupWiseLine( line ) )
		{
			m_din.unsetDevice();
			return 0;
		}
		headerRest.append( line );
		debug( TQString( "- read header line - (%1) : %2" ).tqarg( line.length() ).tqarg( line.data() ) );
	}
	debug( "ResponseProtocol::readResponse() header finished" );
	// if it's a redirect, set flag
	if ( ok && rtnCode == 301 )
	{	
		debug( "- server redirect " );
		packetState = ServerRedirect;
		m_din.unsetDevice();
		return 0;
	}
	// other header processing ( 500! )
	if ( ok && rtnCode == 500 )
	{
		debug( TQString( "- server error %1" ).tqarg( rtnCode ) );
		packetState = ServerError;
		m_din.unsetDevice();
		return 0;
	}
	if ( ok && rtnCode == 404 )
	{
		debug( TQString( "- server error %1" ).tqarg( rtnCode ) );
		packetState = ServerError;
		m_din.unsetDevice();
		return 0;
	}
	if ( m_din.atEnd() )
	{
		debug( "- no fields" );
		packetState = ProtocolError;
		m_din.unsetDevice();
		return 0;
	}
	
	// read fields
	if ( !readFields( -1 ) )
	{
		m_din.unsetDevice();
		return 0;
	}
	// find transaction id field and create Response object if nonzero
	int tId = 0;
	int resultCode = 0;
	Field::FieldListIterator it;
	Field::FieldListIterator end = m_collatingFields.end();
	it = m_collatingFields.find( NM_A_SZ_TRANSACTION_ID );
	if ( it != end )
	{
		Field::SingleField * sf = dynamic_cast<Field::SingleField*>( *it );
		if ( sf )
		{
			tId = sf->value().toInt();
			debug( TQString( "ResponseProtocol::readResponse() - transaction ID is %1" ).tqarg( tId ) );
			m_collatingFields.remove( it );
			delete sf;
		}
	}
	it = m_collatingFields.find( NM_A_SZ_RESULT_CODE );
	if ( it != end )
	{
		Field::SingleField * sf = dynamic_cast<Field::SingleField*>( *it );
		if ( sf )
		{
			resultCode = sf->value().toInt();
			debug( TQString( "ResponseProtocol::readResponse() - result code is %1" ).tqarg( resultCode ) );
			m_collatingFields.remove( it );
			delete sf;
		}
	}
	// append to inQueue
	if ( tId )
	{
		debug( TQString( "ResponseProtocol::readResponse() - setting state Available, got %1 fields in base array" ).tqarg(m_collatingFields.count() ) );
		packetState = Available;
		bytes = m_bytes;
		m_din.unsetDevice();
		return new Response( tId, resultCode, m_collatingFields );
	}
	else
	{
		debug( "- WARNING - NO TRANSACTION ID FOUND!" );
		m_state = ProtocolError;
		m_din.unsetDevice();
		m_collatingFields.purge();
		return 0;
	}
}

bool ResponseProtocol::readFields( int fieldCount, Field::FieldList * list )
{
	// build a list of fields.  
	// If there is already a list of fields stored in m_collatingFields, 
	// the list we're reading on this iteration must be a nested list
	// so when we're done reading it, add it to the MultiList element
	// that is the last element in the top list in m_collatingFields.
	// if we find the beginning of a new nested list, push the current list onto m_collatingFields
	debug( "ResponseProtocol::readFields()" );
	if ( fieldCount > 0 )
		debug( TQString( "reading %1 fields" ).tqarg( fieldCount ) );
	Field::FieldList currentList;
	while ( fieldCount != 0 )  // prevents bad input data from ruining our day
	{
		// the field being read
		// read field
		TQ_UINT8 type, method;
		TQ_UINT32 val;
		TQCString tag;
		// read uint8 type
		if ( !okToProceed() )
		{
			currentList.purge();
			return false;
		}
		m_din >> type;
		m_bytes += sizeof( TQ_UINT8 );
		// if type is 0 SOMETHING_INVALID, we're at the end of the fields
		if ( type == 0 ) /*&& m_din->atEnd() )*/
		{
			debug( "- end of field list" );
			m_packetState = FieldsRead;
			// do something to indicate we're done
			break;
		}
		// read uint8 method
		if ( !okToProceed() )
		{
			currentList.purge();
			return false;
		}
		m_din >> method;
		m_bytes += sizeof( TQ_UINT8 );
		// read tag and length
		if ( !safeReadBytes( tag, val ) )
		{
			currentList.purge();
			return false;
		}

		debug( TQString( "- type: %1, method: %2, tag: %3," ).tqarg( type ).tqarg( method ).tqarg( tag.data() ) );
		// if multivalue or array
		if ( type == NMFIELD_TYPE_MV || type == NMFIELD_TYPE_ARRAY )
		{
			// read length uint32
			if ( !okToProceed() )
			{
				currentList.purge();
				return false;
			}
			m_din >> val;
			m_bytes += sizeof( TQ_UINT32 );

			// create multifield
			debug( TQString( " multi field containing: %1" ).tqarg( val ) );
			Field::MultiField* m = new Field::MultiField( tag, method, 0, type );
			currentList.append( m );
			if ( !readFields( val, &currentList) )
			{
				currentList.purge();
				return false;
			}
		}
		else 
		{
		
			if ( type == NMFIELD_TYPE_UTF8 || type == NMFIELD_TYPE_DN )
			{
				TQCString rawData;
				if( !safeReadBytes( rawData, val ) )
				{
					currentList.purge();
					return false;
				}
				if ( val > NMFIELD_MAX_STR_LENGTH )
				{
					m_packetState = ProtocolError;
					break;
				}
				// convert to tqunicode - ignore the terminating NUL, because TQt<3.3.2 doesn't sanity check val.
				TQString fieldValue = TQString::fromUtf8( rawData.data(), val - 1 );
				debug( TQString( "- utf/dn single field: %1" ).tqarg( fieldValue ) );
				// create singlefield
				Field::SingleField* s = new Field::SingleField( tag, method, 0, type, fieldValue );
				currentList.append( s );
			}
			else
			{
				// otherwise ( numeric )
				// read value uint32
				if ( !okToProceed() )
				{
					currentList.purge();
					return false;
				}
				m_din >> val;
				m_bytes += sizeof( TQ_UINT32 );
				debug( TQString( "- numeric field: %1" ).tqarg( val ) );
				Field::SingleField* s = new Field::SingleField( tag, method, 0, type, val );
				currentList.append( s );
			}
		}
		// decrease the fieldCount if we're using it
		if ( fieldCount > 0 )
			fieldCount--;
	}
	// got a whole list!
	// if fieldCount == 0, we've just read a whole nested list, so add this list to the last element in 'list'
	if ( fieldCount == 0 && list )
	{
		debug( "- finished reading nested list" );
		Field::MultiField * m = dynamic_cast<Field::MultiField*>( list->last() );
		m->setFields( currentList );
	}

	// if fieldCount == -1; we're done reading the top level fieldlist, so store it.
	if ( fieldCount == -1 )
	{
		debug( "- finished reading ALL FIELDS!" );
		m_collatingFields = currentList;
	}
	return true;
}

bool ResponseProtocol::readGroupWiseLine( TQCString & line )
{
	line = TQCString();
	while ( true )
	{
		TQ_UINT8 c;

		if (! okToProceed() )
			return false;
		m_din >> c;
		m_bytes++;
		line += TQChar(c).latin1();
		if ( c == '\n' )
			break;
	}
	return true;
}

#include "responseprotocol.moc"