summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/msn/p2p.cpp
blob: 11f308fb8fa3b566d7a6a755cb26cde0f7bf30e1 (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
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
/*
    p2p.cpp - msn p2p protocol

    Copyright (c) 2003-2005 by Olivier Goffart        <ogoffart@ kde.org>
    Copyright (c) 2005      by Gregg Edghill          <gregg.edghill@gmail.com>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#include "p2p.h"
#include "dispatcher.h"
using P2P::TransferContext;
using P2P::Message;
using P2P::MessageType;
using P2P::TransferType;

#include <stdlib.h>

// Kde includes
#include <kbufferedsocket.h>
#include <kdebug.h>
// TQt includes
#include <tqfile.h>

// Kopete includes
#include <kopetetransfermanager.h>

TQString P2P::Uid::createUid()
{
	return (TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16)
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16) + "-"
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16) + "-"
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16) + "-"
			+ TQString::number(rand()%0xAAFF+0x1111, 16) + "-"
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16)
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16)
			+ TQString::number((unsigned long int)rand()%0xAAFF+0x1111, 16)).upper();
}

TransferContext::TransferContext(const TQString &contact, P2P::Dispatcher *dispatcher, TQ_UINT32 sessionId)
	: TQObject(dispatcher) ,
	   m_sessionId(sessionId) ,
	   m_identifier(0) ,
	   m_file(0) ,
	   m_transactionId (0),
	   m_ackSessionIdentifier (0) ,
	   m_ackUniqueIdentifier ( 0 ),
	   m_transfer ( 0l)  ,

	   m_baseIdentifier(rand()%0x0FFFFFF0 + 4),
	   m_dispatcher (dispatcher),
	   m_isComplete (false) ,
	   m_offset(0),
	   m_totalDataSize(0),
	   m_recipient(contact),
	   m_sender(dispatcher->localContact()),
	   m_socket(0),
	   m_state ( Invitation)
{
	m_type = File ;   //uh,  why??? -Olivier
}

TransferContext::~TransferContext()
{
	m_transfer = 0l;

	if(m_file){
		delete m_file;
		m_file = 0l;
	}
}

void TransferContext::acknowledge(const Message& message)
{
	kdDebug(14140) << k_funcinfo << m_dispatcher<< endl;

	Message outbound;
	outbound.header.sessionId = message.header.sessionId;

	if(m_identifier == 0){
		m_identifier = m_baseIdentifier;
	}
// 	else if(m_state == Finished && m_direction == Incoming){
// 		m_identifier = m_baseIdentifier - 1;
// 	}
	else if(m_state == Finished && m_direction == Outgoing){
		m_identifier = m_baseIdentifier + 1;
	}
	else
		++m_identifier;

	outbound.header.identifier    = m_identifier;
	outbound.header.dataOffset    = 0l;
	outbound.header.totalDataSize = message.header.totalDataSize;
	outbound.header.dataSize      = 0;
// 	if(m_type == UserDisplayIcon && m_state == Finished){
// 		if(m_direction == Outgoing){
// 			outbound.header.flag = 0x40;
// 		}
// 	}
// 	else
		outbound.header.flag = 2;

	outbound.header.ackSessionIdentifier = message.header.identifier;
	outbound.header.ackUniqueIdentifier  = message.header.ackSessionIdentifier;
	outbound.header.ackDataSize   = message.header.totalDataSize;
	// NOTE outbound.body is null by default
	outbound.applicationIdentifier = 0l;
	outbound.destination = m_recipient;

	TQByteArray stream;
	// Write the acknowledge message to the stream.
	m_messageFormatter.writeMessage(outbound, stream, (m_socket != 0l));
	if(!m_socket)
	{
		// Send the acknowledge message.
		m_dispatcher->callbackChannel()->send(stream);
	}
	else
	{
		// Send acknowledge message directly.
		m_socket->writeBlock(stream.data(), stream.size());
	}
}

void TransferContext::error()
{
	kdDebug(14140) << k_funcinfo << endl;
	sendMessage(ERROR);
	m_dispatcher->detach(this);
}

void TransferContext::sendData(const TQByteArray& bytes)
{
	Message outbound;
	outbound.header.sessionId  = m_sessionId;
	outbound.header.identifier = m_identifier;
	outbound.header.dataOffset = m_offset;
	if(m_file){
		outbound.header.totalDataSize = m_file->size();
	}
	else
		outbound.header.totalDataSize = m_totalDataSize;

	outbound.header.dataSize = bytes.size();
	if(m_type == UserDisplayIcon){
		outbound.header.flag = 0x20;
	}
	else if(m_type == P2P::File){
		outbound.header.flag = 0x01000030;
	}
	else  outbound.header.flag = 0;

	outbound.header.ackSessionIdentifier = rand()%0x8FFFFFF0 + 4;
	outbound.header.ackUniqueIdentifier  = 0;
	outbound.header.ackDataSize = 0l;
	outbound.body = bytes;
	outbound.applicationIdentifier = (uint)m_type;

	outbound.destination = m_recipient;

	TQByteArray stream;
	m_messageFormatter.writeMessage(outbound, stream, (m_socket != 0l));
	if(!m_socket)
	{
		// Send the data message.
 		m_transactionId = m_dispatcher->callbackChannel()->send(stream);
 	}
 	else
 	{
 		// Send data directly.
 		m_socket->writeBlock(stream.data(), stream.size());
 	}
}

void TransferContext::sendDataPreparation()
{
	kdDebug(14140) << k_funcinfo << endl;

	Message outbound;
	outbound.header.sessionId  = m_sessionId;
	outbound.header.identifier = ++m_identifier;
	outbound.header.dataOffset = 0;
	outbound.header.totalDataSize = 4;
	outbound.header.dataSize = 4;
	outbound.header.flag = 0;
	outbound.header.ackSessionIdentifier = rand()%0x8FFFFFF0 + 4;
	outbound.header.ackUniqueIdentifier  = 0;
	outbound.header.ackDataSize   = 0l;
	TQByteArray bytes(4);
	bytes.fill('\0');
	outbound.body = bytes;
	outbound.applicationIdentifier = 1;
	outbound.destination = m_recipient;

	TQByteArray stream;
	m_messageFormatter.writeMessage(outbound, stream);
	// Send the receiving client the data prepartion message.
 	m_dispatcher->callbackChannel()->send(stream);
}

void TransferContext::sendMessage(MessageType type, const TQString& content, TQ_INT32 flag, TQ_INT32 appId)
{
	Message outbound;
	if(appId != 0){
		outbound.header.sessionId = m_sessionId;
	}
	else
		outbound.header.sessionId = 0;

	if(m_identifier == 0){
		m_identifier = m_baseIdentifier;
	}
	else if(m_state == Invitation && m_direction == P2P::Outgoing && m_type == UserDisplayIcon)
	{
			m_identifier -= 3;
	}
	else if(m_state == Invitation && m_direction == P2P::Incoming && m_type == File)
	{
			m_identifier -= 3;
	}
	else
		++m_identifier;

	outbound.header.identifier = m_identifier;
	outbound.header.flag = flag;
	outbound.header.ackSessionIdentifier = m_ackSessionIdentifier;
	outbound.header.ackUniqueIdentifier  = m_ackUniqueIdentifier;
	outbound.header.ackDataSize = 0l;
	outbound.applicationIdentifier = appId;
	outbound.destination = m_recipient;

	TQString contentType, cSeq, method;

	switch(m_state)
	{
		case DataTransfer:
			contentType = "application/x-msnmsgr-transreqbody";
			if(m_type == File && m_direction == Incoming)
			{
				contentType = "application/x-msnmsgr-transrespbody";
			}
			break;
		case Finished:
			contentType = "application/x-msnmsgr-sessionclosebody";
			break;
		default:
			contentType = "application/x-msnmsgr-sessionreqbody";
			if(m_type == File && m_direction == Outgoing)
			{
				if(m_state == Negotiation){
					contentType = "application/x-msnmsgr-transreqbody";
				}
			}
			if(m_type == P2P::WebcamType && type==P2P::INVITE && m_state == Negotiation)
			{
				contentType = "application/x-msnmsgr-transreqbody";
			}
			break;
	}

	switch(type)
	{
		case BYE:
			method = "BYE MSNMSGR:" + m_recipient + " MSNSLP/1.0";
			cSeq   = "0";
			break;

		case DECLINE:
			method = "MSNSLP/1.0 603 DECLINE";
			cSeq   = "1";
			break;

		case ERROR:
			contentType = "null";
			method = "MSNSLP/1.0 500 Internal Error";
			cSeq   = "1";
			break;

		case INVITE:
			method = "INVITE MSNMSGR:" + m_recipient + " MSNSLP/1.0";
			cSeq   = "0";
			break;

		case OK:
			method = "MSNSLP/1.0 200 OK";
			cSeq   = "1";
			break;
	}

	TQCString body = TQString(method + "\r\n"
		"To: <msnmsgr:" + m_recipient + ">\r\n"
		"From: <msnmsgr:" + m_sender  + ">\r\n"
		"Via: MSNSLP/1.0/TLP ;branch={" + m_branch.upper() + "}\r\n"
		"CSeq: "+ cSeq +"\r\n"
		"Call-ID: {" + m_callId.upper() + "}\r\n"
		"Max-Forwards: 0\r\n"
		"Content-Type: " + contentType + "\r\n"
		"Content-Length: "+ TQString::number(content.length() + 1) + "\r\n"
		"\r\n" +
		content).utf8();

	// NOTE The body must have a null character at the end.
	// TQCString by chance automatically adds a \0 to the
	// end of the string.

	outbound.header.totalDataSize = body.size();
	// Send the outbound message.
	sendMessage(outbound, body);
}

void TransferContext::sendMessage(Message& outbound, const TQByteArray& body)
{
	TQ_INT64 offset = 0L, bytesLeft = outbound.header.totalDataSize;
	TQ_INT16 chunkLength = 1202;

	// Split the outbound message if necessary.
	while(bytesLeft > 0L)
	{
		if(bytesLeft < chunkLength)
		{
			// Copy the last chunk of the multipart message.
			outbound.body.duplicate(body.data() + offset, bytesLeft);
			outbound.header.dataSize = bytesLeft;
			outbound.header.dataOffset = offset;
			bytesLeft = 0L;
		}
		else
		{
			// Copy the next chunk of the multipart message in the sequence.
			outbound.body.duplicate(body.data() + offset, chunkLength);
			outbound.header.dataSize = chunkLength;
			outbound.header.dataOffset = offset;
			offset += chunkLength;
			bytesLeft -= offset;
		}

		kdDebug(14140) << k_funcinfo <<
			TQCString(outbound.body.data(), outbound.body.size())
			<< endl;

		TQByteArray stream;
		// Write the outbound message to the stream.
		m_messageFormatter.writeMessage(outbound, stream, (m_socket != 0l));
		if(!m_socket)
		{
			// Send the outbound message.
			m_dispatcher->callbackChannel()->send(stream);
		}
		else
		{
			// Send outbound message directly.
			m_socket->writeBlock(stream.data(), stream.size());
		}
	}
}

void TransferContext::setType(TransferType type)
{
	m_type = type;
}

void TransferContext::abort()
{
	kdDebug(14140) << k_funcinfo << endl;
	if(m_transfer)
	{
		if(m_transfer->error() == TDEIO::ERR_ABORTED)
		{
			switch(m_direction)
			{
				case P2P::Outgoing:
					if(m_type == File)
					{
						// Do nothing.
					}
					break;

				case P2P::Incoming:
					if(m_type == File)
					{
						// Do nothing.
					}
					break;
			}
		}
		else
		{
			m_state = Finished;
			sendMessage(BYE, "\r\n");
		}
	}
}

void TransferContext::readyWrite()
{
	if(m_direction == Outgoing && m_state == DataTransfer){
		readyToSend();
	}
}

void TransferContext::readyToSend()
{}

#include "p2p.moc"