summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_ft.cpp
blob: e68ce31eac9c3ebe74a7500dd90f1fd5aea9dafc (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2002-04  Santithorn Bunchua
Copyright (c) 2005-12  George Hazan
Copyright (c) 2007     Maxim Mluhov
Copyright (ñ) 2012-16 Miranda NG project

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"
#include "jabber_iq.h"
#include "jabber_byte.h"
#include "jabber_ibb.h"
#include "jabber_caps.h"

void CJabberProto::FtCancel(filetransfer *ft)
{
	debugLogA("Invoking JabberFtCancel()");

	// For file sending session that is still in si negotiation phase
	if (m_iqManager.ExpireByUserData(ft))
		return;

	// For file receiving session that is still in si negotiation phase
	LISTFOREACH(i, this, LIST_FTRECV)
	{
		JABBER_LIST_ITEM *item = ListGetItemPtrFromIndex(i);
		if (item->ft == ft) {
			debugLogA("Canceling file receiving session while in si negotiation");
			ListRemoveByIndex(i);
			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
			delete ft;
			return;
		}
	}
	// For file transfer through bytestream
	JABBER_BYTE_TRANSFER *jbt = ft->jbt;
	if (jbt != NULL) {
		debugLogA("Canceling bytestream session");
		jbt->state = JBT_ERROR;
		if (jbt->hConn) {
			debugLogA("Force closing bytestream session");
			Netlib_CloseHandle(jbt->hConn);
			jbt->hConn = NULL;
		}
		if (jbt->hSendEvent) SetEvent(jbt->hSendEvent);
		if (jbt->hEvent) SetEvent(jbt->hEvent);
		if (jbt->hProxyEvent) SetEvent(jbt->hProxyEvent);
	}

	// For file transfer through IBB
	JABBER_IBB_TRANSFER *jibb = ft->jibb;
	if (jibb != NULL) {
		debugLogA("Canceling IBB session");
		jibb->state = JIBB_ERROR;
		m_iqManager.ExpireByUserData(jibb);
	}
}

///////////////// File sending using stream initiation /////////////////////////

void CJabberProto::FtInitiate(wchar_t* jid, filetransfer *ft)
{
	wchar_t *rs;
	wchar_t *filename, *p;
	int i;
	wchar_t sid[9];

	if (jid == NULL || ft == NULL || !m_bJabberOnline || (rs = ListGetBestClientResourceNamePtr(jid)) == NULL) {
		if (ft) {
			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
			delete ft;
		}
		return;
	}
	ft->type = FT_SI;
	for (i = 0; i < 8; i++)
		sid[i] = (rand() % 10) + '0';
	sid[8] = '\0';
	if (ft->sid != NULL) mir_free(ft->sid);
	ft->sid = mir_wstrdup(sid);
	filename = ft->std.ptszFiles[ft->std.currentFileNumber];
	if ((p = wcsrchr(filename, '\\')) != NULL)
		filename = p + 1;

	wchar_t tszJid[512];
	mir_snwprintf(tszJid, L"%s/%s", jid, rs);

	XmlNodeIq iq(AddIQ(&CJabberProto::OnFtSiResult, JABBER_IQ_TYPE_SET, tszJid, JABBER_IQ_PARSE_FROM | JABBER_IQ_PARSE_TO, -1, ft));
	HXML si = iq << XCHILDNS(L"si", JABBER_FEAT_SI) << XATTR(L"id", sid)
		<< XATTR(L"mime-type", L"binary/octet-stream") << XATTR(L"profile", JABBER_FEAT_SI_FT);
	si << XCHILDNS(L"file", JABBER_FEAT_SI_FT) << XATTR(L"name", filename)
		<< XATTRI64(L"size", ft->fileSize[ft->std.currentFileNumber]) << XCHILD(L"desc", ft->szDescription);

	HXML field = si << XCHILDNS(L"feature", JABBER_FEAT_FEATURE_NEG)
		<< XCHILDNS(L"x", JABBER_FEAT_DATA_FORMS) << XATTR(L"type", L"form")
		<< XCHILD(L"field") << XATTR(L"var", L"stream-method") << XATTR(L"type", L"list-single");

	BOOL bDirect = m_options.BsDirect;
	BOOL bProxy = m_options.BsProxyManual;

	// bytestreams support?
	if (bDirect || bProxy)
		field << XCHILD(L"option") << XCHILD(L"value", JABBER_FEAT_BYTESTREAMS);

	field << XCHILD(L"option") << XCHILD(L"value", JABBER_FEAT_IBB);
	m_ThreadInfo->send(iq);
}

void CJabberProto::OnFtSiResult(HXML iqNode, CJabberIqInfo *pInfo)
{
	HXML siNode, featureNode, xNode, fieldNode, valueNode;
	filetransfer *ft = (filetransfer *)pInfo->GetUserData();
	if (!ft) return;

	if ((pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT) && pInfo->m_szFrom && pInfo->m_szTo) {
		if ((siNode = XmlGetChild(iqNode, "si")) != NULL) {

			// fix for very smart clients, like gajim
			BOOL bDirect = m_options.BsDirect;
			BOOL bProxy = m_options.BsProxyManual;

			if ((featureNode = XmlGetChild(siNode, "feature")) != NULL) {
				if ((xNode = XmlGetChildByTag(featureNode, "x", "xmlns", JABBER_FEAT_DATA_FORMS)) != NULL) {
					if ((fieldNode = XmlGetChildByTag(xNode, "field", "var", L"stream-method")) != NULL) {
						if ((valueNode = XmlGetChild(fieldNode, "value")) != NULL && XmlGetText(valueNode) != NULL) {
							if ((bDirect || bProxy) && !mir_wstrcmp(XmlGetText(valueNode), JABBER_FEAT_BYTESTREAMS)) {
								// Start Bytestream session
								JABBER_BYTE_TRANSFER *jbt = new JABBER_BYTE_TRANSFER;
								memset(jbt, 0, sizeof(JABBER_BYTE_TRANSFER));
								jbt->srcJID = mir_wstrdup(pInfo->m_szTo);
								jbt->dstJID = mir_wstrdup(pInfo->m_szFrom);
								jbt->sid = mir_wstrdup(ft->sid);
								jbt->pfnSend = &CJabberProto::FtSend;
								jbt->pfnFinal = &CJabberProto::FtSendFinal;
								jbt->ft = ft;
								ft->type = FT_BYTESTREAM;
								ft->jbt = jbt;
								ForkThread((MyThreadFunc)&CJabberProto::ByteSendThread, jbt);
							}
							else if (!mir_wstrcmp(XmlGetText(valueNode), JABBER_FEAT_IBB)) {
								JABBER_IBB_TRANSFER *jibb = (JABBER_IBB_TRANSFER *)mir_alloc(sizeof(JABBER_IBB_TRANSFER));
								memset(jibb, 0, sizeof(JABBER_IBB_TRANSFER));
								jibb->srcJID = mir_wstrdup(pInfo->m_szTo);
								jibb->dstJID = mir_wstrdup(pInfo->m_szFrom);
								jibb->sid = mir_wstrdup(ft->sid);
								jibb->pfnSend = &CJabberProto::FtIbbSend;
								jibb->pfnFinal = &CJabberProto::FtSendFinal;
								jibb->ft = ft;
								ft->type = FT_IBB;
								ft->jibb = jibb;
								ForkThread((MyThreadFunc)&CJabberProto::IbbSendThread, jibb);
							}
						}
					}
				}
			}
		}
	}
	else {
		debugLogA("File transfer stream initiation request denied or failed");
		ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, pInfo->GetIqType() == JABBER_IQ_TYPE_ERROR ? ACKRESULT_DENIED : ACKRESULT_FAILED, ft, 0);
		delete ft;
	}
}

BOOL CJabberProto::FtSend(HANDLE hConn, filetransfer *ft)
{
	struct _stati64 statbuf;
	int fd;
	char* buffer;
	int numRead;

	debugLogW(L"Sending [%s]", ft->std.ptszFiles[ft->std.currentFileNumber]);
	_wstat64(ft->std.ptszFiles[ft->std.currentFileNumber], &statbuf);	// file size in statbuf.st_size
	if ((fd = _wopen(ft->std.ptszFiles[ft->std.currentFileNumber], _O_BINARY | _O_RDONLY)) < 0) {
		debugLogW(L"File cannot be opened");
		return FALSE;
	}

	ft->std.flags |= PFTS_SENDING;
	ft->std.currentFileSize = statbuf.st_size;
	ft->std.currentFileProgress = 0;

	if ((buffer = (char*)mir_alloc(2048)) != NULL) {
		while ((numRead = _read(fd, buffer, 2048)) > 0) {
			if (Netlib_Send(hConn, buffer, numRead, 0) != numRead) {
				mir_free(buffer);
				_close(fd);
				return FALSE;
			}
			ft->std.currentFileProgress += numRead;
			ft->std.totalProgress += numRead;
			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->std);
		}
		mir_free(buffer);
	}
	_close(fd);
	return TRUE;
}

BOOL CJabberProto::FtIbbSend(int blocksize, filetransfer *ft)
{
	debugLogW(L"Sending [%s]", ft->std.ptszFiles[ft->std.currentFileNumber]);

	struct _stati64 statbuf;
	_wstat64(ft->std.ptszFiles[ft->std.currentFileNumber], &statbuf);	// file size in statbuf.st_size

	int fd = _wopen(ft->std.ptszFiles[ft->std.currentFileNumber], _O_BINARY | _O_RDONLY);
	if (fd < 0) {
		debugLogA("File cannot be opened");
		return FALSE;
	}

	ft->std.flags |= PFTS_SENDING;
	ft->std.currentFileSize = statbuf.st_size;
	ft->std.currentFileProgress = 0;

	ptrA buffer((char*)mir_alloc(blocksize));
	if (buffer != NULL) {
		int numRead;
		while ((numRead = _read(fd, buffer, blocksize)) > 0) {
			int iqId = SerialNext();
			XmlNode msg(L"message");
			XmlAddAttr(msg, L"to", ft->jibb->dstJID);
			msg << XATTRID(iqId);

			// let others send data too
			Sleep(2);

			char *encoded = mir_base64_encode((PBYTE)(char*)buffer, numRead);

			msg << XCHILD(L"data", _A2T(encoded)) << XATTR(L"xmlns", JABBER_FEAT_IBB)
				<< XATTR(L"sid", ft->jibb->sid) << XATTRI(L"seq", ft->jibb->wPacketId);

			HXML ampNode = msg << XCHILDNS(L"amp", JABBER_FEAT_AMP);
			ampNode << XCHILD(L"rule") << XATTR(L"condition", L"deliver-at")
				<< XATTR(L"value", L"stored") << XATTR(L"action", L"error");
			ampNode << XCHILD(L"rule") << XATTR(L"condition", L"match-resource")
				<< XATTR(L"value", L"exact") << XATTR(L"action", L"error");
			ft->jibb->wPacketId++;

			mir_free(encoded);

			if (ft->jibb->state == JIBB_ERROR || ft->jibb->bStreamClosed || m_ThreadInfo->send(msg) == SOCKET_ERROR) {
				debugLogA("JabberFtIbbSend unsuccessful exit");
				_close(fd);
				return FALSE;
			}

			ft->jibb->dwTransferredSize += (DWORD)numRead;

			ft->std.currentFileProgress += numRead;
			ft->std.totalProgress += numRead;
			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->std);
		}
	}
	_close(fd);
	return TRUE;
}

void CJabberProto::FtSendFinal(BOOL success, filetransfer *ft)
{
	if (!success) {
		debugLogA("File transfer complete with error");
		ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ft->state == FT_DENIED ? ACKRESULT_DENIED : ACKRESULT_FAILED, ft, 0);
	}
	else {
		if (ft->std.currentFileNumber < ft->std.totalFiles - 1) {
			ft->std.currentFileNumber++;
			replaceStrW(ft->std.tszCurrentFile, ft->std.ptszFiles[ft->std.currentFileNumber]);
			ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);
			FtInitiate(ft->jid, ft);
			return;
		}

		ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
	}

	delete ft;
}

///////////////// File receiving through stream initiation /////////////////////////

void CJabberProto::FtHandleSiRequest(HXML iqNode)
{
	const wchar_t *from, *sid, *str, *szId, *filename;
	HXML siNode, fileNode, featureNode, xNode, fieldNode, n;
	int i;
	unsigned __int64 filesize;

	if (!iqNode ||
		(from = XmlGetAttrValue(iqNode, L"from")) == NULL ||
		(str = XmlGetAttrValue(iqNode, L"type")) == NULL || mir_wstrcmp(str, L"set") ||
		(siNode = XmlGetChildByTag(iqNode, "si", "xmlns", JABBER_FEAT_SI)) == NULL)
		return;

	szId = XmlGetAttrValue(iqNode, L"id");
	if ((sid = XmlGetAttrValue(siNode, L"id")) != NULL &&
		(fileNode = XmlGetChildByTag(siNode, "file", "xmlns", JABBER_FEAT_SI_FT)) != NULL &&
		(filename = XmlGetAttrValue(fileNode, L"name")) != NULL &&
		(str = XmlGetAttrValue(fileNode, L"size")) != NULL) {

		filesize = _wtoi64(str);
		if ((featureNode = XmlGetChildByTag(siNode, "feature", "xmlns", JABBER_FEAT_FEATURE_NEG)) != NULL &&
			(xNode = XmlGetChildByTag(featureNode, "x", "xmlns", JABBER_FEAT_DATA_FORMS)) != NULL &&
			(fieldNode = XmlGetChildByTag(xNode, "field", "var", L"stream-method")) != NULL) {

			BOOL bIbbOnly = m_options.BsOnlyIBB;
			HXML optionNode = NULL;
			JABBER_FT_TYPE ftType = FT_OOB;

			if (!bIbbOnly) {
				for (i = 0; ; i++) {
					optionNode = XmlGetChild(fieldNode, i);
					if (!optionNode)
						break;

					if (!mir_wstrcmp(XmlGetName(optionNode), L"option")) {
						if ((n = XmlGetChild(optionNode, "value")) != NULL && XmlGetText(n)) {
							if (!mir_wstrcmp(XmlGetText(n), JABBER_FEAT_BYTESTREAMS)) {
								ftType = FT_BYTESTREAM;
								break;
							}
						}
					}
				}
			}

			// try IBB only if bytestreams support not found or BsOnlyIBB flag exists
			if (bIbbOnly || !optionNode) {
				for (i = 0; ; i++) {
					optionNode = XmlGetChild(fieldNode, i);
					if (!optionNode)
						break;

					if (!mir_wstrcmp(XmlGetName(optionNode), L"option")) {
						if ((n = XmlGetChild(optionNode, "value")) != NULL && XmlGetText(n)) {
							if (!mir_wstrcmp(XmlGetText(n), JABBER_FEAT_IBB)) {
								ftType = FT_IBB;
								break;
							}
						}
					}
				}
			}

			if (optionNode != NULL) {
				// Found known stream mechanism
				filetransfer *ft = new filetransfer(this);
				ft->dwExpectedRecvFileSize = filesize;
				ft->jid = mir_wstrdup(from);
				ft->std.hContact = HContactFromJID(from);
				ft->sid = mir_wstrdup(sid);
				ft->szId = mir_wstrdup(szId);
				ft->type = ftType;
				ft->std.totalFiles = 1;
				ft->std.tszCurrentFile = mir_wstrdup(filename);
				ft->std.totalBytes = ft->std.currentFileSize = filesize;

				PROTORECVFILET pre = { 0 };
				pre.dwFlags = PRFF_UNICODE;
				pre.fileCount = 1;
				pre.timestamp = time(NULL);
				pre.files.w = (wchar_t**)&filename;
				pre.lParam = (LPARAM)ft;
				if ((n = XmlGetChild(fileNode, "desc")) != NULL)
					pre.descr.w = (wchar_t*)XmlGetText(n);

				ProtoChainRecvFile(ft->std.hContact, &pre);
				return;
			}
			else {
				// Unknown stream mechanism
				XmlNodeIq iq(L"error", szId, from);
				HXML e = iq << XCHILD(L"error") << XATTRI(L"code", 400) << XATTR(L"type", L"cancel");
				e << XCHILDNS(L"bad-request", L"urn:ietf:params:xml:ns:xmpp-stanzas");
				e << XCHILDNS(L"no-valid-streams", JABBER_FEAT_SI);
				m_ThreadInfo->send(iq);
				return;
			}
		}
	}

	// Bad stream initiation, reply with bad-profile
	XmlNodeIq iq(L"error", szId, from);
	HXML e = iq << XCHILD(L"error") << XATTRI(L"code", 400) << XATTR(L"type", L"cancel");
	e << XCHILDNS(L"bad-request", L"urn:ietf:params:xml:ns:xmpp-stanzas");
	e << XCHILDNS(L"bad-profile", JABBER_FEAT_SI);
	m_ThreadInfo->send(iq);
}

void CJabberProto::FtAcceptSiRequest(filetransfer *ft)
{
	if (!m_bJabberOnline || ft == NULL || ft->jid == NULL || ft->sid == NULL) return;

	JABBER_LIST_ITEM *item;
	if ((item = ListAdd(LIST_FTRECV, ft->sid)) != NULL) {
		item->ft = ft;

		m_ThreadInfo->send(
			XmlNodeIq(L"result", ft->szId, ft->jid)
			<< XCHILDNS(L"si", JABBER_FEAT_SI)
			<< XCHILDNS(L"feature", JABBER_FEAT_FEATURE_NEG)
			<< XCHILDNS(L"x", JABBER_FEAT_DATA_FORMS) << XATTR(L"type", L"submit")
			<< XCHILD(L"field") << XATTR(L"var", L"stream-method")
			<< XCHILD(L"value", JABBER_FEAT_BYTESTREAMS));
	}
}

void CJabberProto::FtAcceptIbbRequest(filetransfer *ft)
{
	if (!m_bJabberOnline || ft == NULL || ft->jid == NULL || ft->sid == NULL) return;

	JABBER_LIST_ITEM *item;
	if ((item = ListAdd(LIST_FTRECV, ft->sid)) != NULL) {
		item->ft = ft;

		m_ThreadInfo->send(
			XmlNodeIq(L"result", ft->szId, ft->jid)
			<< XCHILDNS(L"si", JABBER_FEAT_SI)
			<< XCHILDNS(L"feature", JABBER_FEAT_FEATURE_NEG)
			<< XCHILDNS(L"x", JABBER_FEAT_DATA_FORMS) << XATTR(L"type", L"submit")
			<< XCHILD(L"field") << XATTR(L"var", L"stream-method")
			<< XCHILD(L"value", JABBER_FEAT_IBB));
	}
}

BOOL CJabberProto::FtHandleBytestreamRequest(HXML iqNode, CJabberIqInfo *pInfo)
{
	HXML queryNode = pInfo->GetChildNode();

	const wchar_t *sid;
	JABBER_LIST_ITEM *item;

	if ((sid = XmlGetAttrValue(queryNode, L"sid")) != NULL && (item = ListGetItemPtr(LIST_FTRECV, sid)) != NULL) {
		// Start Bytestream session
		JABBER_BYTE_TRANSFER *jbt = new JABBER_BYTE_TRANSFER;
		memset(jbt, 0, sizeof(JABBER_BYTE_TRANSFER));
		jbt->iqNode = xmlCopyNode(iqNode);
		jbt->pfnRecv = &CJabberProto::FtReceive;
		jbt->pfnFinal = &CJabberProto::FtReceiveFinal;
		jbt->ft = item->ft;
		item->ft->jbt = jbt;
		ForkThread((MyThreadFunc)&CJabberProto::ByteReceiveThread, jbt);
		ListRemove(LIST_FTRECV, sid);
		return TRUE;
	}

	debugLogA("File transfer invalid bytestream initiation request received");
	return TRUE;
}

BOOL CJabberProto::FtHandleIbbRequest(HXML iqNode, BOOL bOpen)
{
	if (iqNode == NULL) return FALSE;

	const wchar_t *id = XmlGetAttrValue(iqNode, L"id");
	const wchar_t *from = XmlGetAttrValue(iqNode, L"from");
	const wchar_t *to = XmlGetAttrValue(iqNode, L"to");
	if (!id || !from || !to) return FALSE;

	HXML ibbNode = XmlGetChildByTag(iqNode, bOpen ? "open" : "close", "xmlns", JABBER_FEAT_IBB);
	if (!ibbNode) return FALSE;

	const wchar_t *sid = XmlGetAttrValue(ibbNode, L"sid");
	if (!sid) return FALSE;

	// already closed?
	JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_FTRECV, sid);
	if (item == NULL) {
		m_ThreadInfo->send(
			XmlNodeIq(L"error", id, from)
			<< XCHILD(L"error") << XATTRI(L"code", 404) << XATTR(L"type", L"cancel")
			<< XCHILDNS(L"item-not-found", L"urn:ietf:params:xml:ns:xmpp-stanzas"));
		return FALSE;
	}

	// open event
	if (bOpen) {
		if (!item->jibb) {
			JABBER_IBB_TRANSFER *jibb = (JABBER_IBB_TRANSFER *)mir_alloc(sizeof(JABBER_IBB_TRANSFER));
			memset(jibb, 0, sizeof(JABBER_IBB_TRANSFER));
			jibb->srcJID = mir_wstrdup(from);
			jibb->dstJID = mir_wstrdup(to);
			jibb->sid = mir_wstrdup(sid);
			jibb->pfnRecv = &CJabberProto::FtReceive;
			jibb->pfnFinal = &CJabberProto::FtReceiveFinal;
			jibb->ft = item->ft;
			item->ft->jibb = jibb;
			item->jibb = jibb;
			ForkThread((MyThreadFunc)&CJabberProto::IbbReceiveThread, jibb);

			m_ThreadInfo->send(XmlNodeIq(L"result", id, from));
			return TRUE;
		}
		// stream already open
		m_ThreadInfo->send(
			XmlNodeIq(L"error", id, from)
			<< XCHILD(L"error") << XATTRI(L"code", 404) << XATTR(L"type", L"cancel")
			<< XCHILDNS(L"item-not-found", L"urn:ietf:params:xml:ns:xmpp-stanzas"));
		return FALSE;
	}

	// close event && stream already open
	if (item->jibb && item->jibb->hEvent) {
		item->jibb->bStreamClosed = TRUE;
		SetEvent(item->jibb->hEvent);

		m_ThreadInfo->send(XmlNodeIq(L"result", id, from));
		return TRUE;
	}

	ListRemove(LIST_FTRECV, sid);

	return FALSE;
}

int CJabberProto::FtReceive(HANDLE, filetransfer *ft, char* buffer, int datalen)
{
	if (ft->create() == -1)
		return -1;

	__int64 remainingBytes = ft->std.currentFileSize - ft->std.currentFileProgress;
	if (remainingBytes > 0) {
		int writeSize = (remainingBytes < datalen) ? remainingBytes : datalen;
		if (_write(ft->fileId, buffer, writeSize) != writeSize) {
			debugLogA("_write() error");
			return -1;
		}

		ft->std.currentFileProgress += writeSize;
		ft->std.totalProgress += writeSize;
		ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->std);
		return (ft->std.currentFileSize == ft->std.currentFileProgress) ? 0 : writeSize;
	}

	return 0;
}

void CJabberProto::FtReceiveFinal(BOOL success, filetransfer *ft)
{
	if (success) {
		debugLogA("File transfer complete successfully");
		ft->complete();
	}
	else debugLogA("File transfer complete with error");

	delete ft;
}