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
|
// ---------------------------------------------------------------------------80
// ICQ plugin for Miranda Instant Messenger
// ________________________________________
//
// Copyright © 2000-2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2009 Joe Kucera
//
// 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.
//
// -----------------------------------------------------------------------------
// DESCRIPTION:
//
// Describe me here please...
//
// -----------------------------------------------------------------------------
#include "icqoscar.h"
static void file_buildProtoFileTransferStatus(filetransfer* ft, PROTOFILETRANSFERSTATUS* pfts)
{
ZeroMemory(pfts, sizeof(PROTOFILETRANSFERSTATUS));
pfts->cbSize = sizeof(PROTOFILETRANSFERSTATUS);
pfts->hContact = ft->hContact;
pfts->flags = PFTS_UTF | (ft->sending ? PFTS_SENDING : PFTS_RECEIVING); /* Standard FT is Ansi only */
if (ft->sending)
pfts->pszFiles = ft->pszFiles;
else
pfts->pszFiles = NULL; /* FIXME */
pfts->totalFiles = ft->dwFileCount;
pfts->currentFileNumber = ft->iCurrentFile;
pfts->totalBytes = ft->dwTotalSize;
pfts->totalProgress = ft->dwBytesDone;
pfts->szWorkingDir = ft->szSavePath;
pfts->szCurrentFile = ft->szThisFile;
pfts->currentFileSize = ft->dwThisFileSize;
pfts->currentFileTime = ft->dwThisFileDate;
pfts->currentFileProgress = ft->dwFileBytesDone;
}
static void file_sendTransferSpeed(CIcqProto* ppro, directconnect* dc)
{
icq_packet packet;
directPacketInit(&packet, 5);
packByte(&packet, PEER_FILE_SPEED); /* Ident */
packLEDWord(&packet, dc->ft->dwTransferSpeed);
ppro->sendDirectPacket(dc, &packet);
}
static void file_sendNick(CIcqProto* ppro, directconnect* dc)
{
icq_packet packet;
char* szNick;
WORD wNickLen;
DBVARIANT dbv = {DBVT_DELETED};
if (ppro->getSettingString(NULL, "Nick", &dbv))
szNick = "";
else
szNick = dbv.pszVal;
wNickLen = strlennull(szNick);
directPacketInit(&packet, (WORD)(8 + wNickLen));
packByte(&packet, PEER_FILE_INIT_ACK); /* Ident */
packLEDWord(&packet, dc->ft->dwTransferSpeed);
packLEWord(&packet, (WORD)(wNickLen + 1));
packBuffer(&packet, (LPBYTE)szNick, (WORD)(wNickLen + 1));
ppro->sendDirectPacket(dc, &packet);
db_free(&dbv);
}
static void file_sendNextFile(CIcqProto* ppro, directconnect* dc)
{
icq_packet packet;
struct _stati64 statbuf;
char szThisSubDir[MAX_PATH];
if (dc->ft->iCurrentFile >= (int)dc->ft->dwFileCount)
{
ppro->BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, dc->ft, 0);
ppro->CloseDirectConnection(dc);
dc->ft->hConnection = NULL;
return;
}
dc->ft->szThisFile = dc->ft->pszFiles[dc->ft->iCurrentFile];
if (FileStatUtf(dc->ft->szThisFile, &statbuf))
{
ppro->icq_LogMessage(LOG_ERROR, LPGEN("Your file transfer has been aborted because one of the files that you selected to send is no longer readable from the disk. You may have deleted or moved it."));
ppro->CloseDirectConnection(dc);
dc->ft->hConnection = NULL;
return;
}
char *pszThisFileName = FindFilePathContainer((LPCSTR*)dc->ft->pszFiles, dc->ft->iCurrentFile, szThisSubDir);
if (statbuf.st_mode&_S_IFDIR)
{
dc->ft->currentIsDir = 1;
}
else
{
dc->ft->currentIsDir = 0;
dc->ft->fileId = OpenFileUtf(dc->ft->szThisFile, _O_BINARY | _O_RDONLY, _S_IREAD);
if (dc->ft->fileId == -1)
{
ppro->icq_LogMessage(LOG_ERROR, LPGEN("Your file transfer has been aborted because one of the files that you selected to send is no longer readable from the disk. You may have deleted or moved it."));
ppro->CloseDirectConnection(dc);
dc->ft->hConnection = NULL;
return;
}
}
dc->ft->dwThisFileSize = statbuf.st_size;
dc->ft->dwThisFileDate = statbuf.st_mtime;
dc->ft->dwFileBytesDone = 0;
char *szThisFileNameAnsi = NULL, *szThisSubDirAnsi = NULL;
if (!utf8_decode(pszThisFileName, &szThisFileNameAnsi))
szThisFileNameAnsi = _strdup(pszThisFileName); // Legacy fix
if (!utf8_decode(szThisSubDir, &szThisSubDirAnsi))
szThisSubDirAnsi = _strdup(szThisSubDir); // Legacy fix
WORD wThisFileNameLen = strlennull(szThisFileNameAnsi);
WORD wThisSubDirLen = strlennull(szThisSubDirAnsi);
directPacketInit(&packet, (WORD)(20 + wThisFileNameLen + wThisSubDirLen));
packByte(&packet, PEER_FILE_NEXTFILE); /* Ident */
packByte(&packet, (BYTE)((statbuf.st_mode & _S_IFDIR) != 0)); // Is subdir
packLEWord(&packet, (WORD)(wThisFileNameLen + 1));
packBuffer(&packet, (LPBYTE)szThisFileNameAnsi, (WORD)(wThisFileNameLen + 1));
packLEWord(&packet, (WORD)(wThisSubDirLen + 1));
packBuffer(&packet, (LPBYTE)szThisSubDirAnsi, (WORD)(wThisSubDirLen + 1));
packLEDWord(&packet, dc->ft->dwThisFileSize);
packLEDWord(&packet, statbuf.st_mtime);
packLEDWord(&packet, dc->ft->dwTransferSpeed);
SAFE_FREE(&szThisFileNameAnsi);
SAFE_FREE(&szThisSubDirAnsi);
ppro->sendDirectPacket(dc, &packet);
ppro->BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, dc->ft, 0);
}
static void file_sendResume(CIcqProto* ppro, directconnect* dc)
{
icq_packet packet;
directPacketInit(&packet, 17);
packByte(&packet, PEER_FILE_RESUME); /* Ident */
packLEDWord(&packet, dc->ft->dwFileBytesDone); /* file resume */
packLEDWord(&packet, 0); /* unknown */
packLEDWord(&packet, dc->ft->dwTransferSpeed);
packLEDWord(&packet, dc->ft->iCurrentFile + 1); /* file number */
ppro->sendDirectPacket(dc, &packet);
}
static void file_sendData(CIcqProto* ppro, directconnect* dc)
{
BYTE buf[2048];
int bytesRead = 0;
if (!dc->ft->currentIsDir)
{
icq_packet packet;
if (dc->ft->fileId == -1)
return;
bytesRead = _read(dc->ft->fileId, buf, sizeof(buf));
if (bytesRead == -1)
return;
directPacketInit(&packet, (WORD)(1 + bytesRead));
packByte(&packet, PEER_FILE_DATA); /* Ident */
packBuffer(&packet, buf, (WORD)bytesRead);
ppro->sendDirectPacket(dc, &packet);
}
dc->ft->dwBytesDone += bytesRead;
dc->ft->dwFileBytesDone += bytesRead;
if (GetTickCount() > dc->ft->dwLastNotify + 500 || bytesRead == 0)
{
PROTOFILETRANSFERSTATUS pfts;
file_buildProtoFileTransferStatus(dc->ft, &pfts);
ppro->BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, dc->ft, (LPARAM)&pfts);
dc->ft->dwLastNotify = GetTickCount();
}
if (bytesRead == 0)
{
if (!dc->ft->currentIsDir) _close(dc->ft->fileId);
dc->ft->fileId = -1;
dc->wantIdleTime = 0;
dc->ft->iCurrentFile++;
file_sendNextFile(ppro, dc); /* this will close the socket if no more files */
}
}
void CIcqProto::handleFileTransferIdle(directconnect* dc)
{
file_sendData(this, dc);
}
void CIcqProto::icq_sendFileResume(filetransfer *ft, int action, const char *szFilename)
{
if (ft->hConnection == NULL)
return;
directconnect *dc = FindFileTransferDC(ft);
if (!dc) return; // something is broken...
int openFlags;
switch (action)
{
case FILERESUME_RESUME:
openFlags = _O_BINARY | _O_WRONLY;
break;
case FILERESUME_OVERWRITE:
openFlags = _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY;
ft->dwFileBytesDone = 0;
break;
case FILERESUME_SKIP:
openFlags = _O_BINARY | _O_WRONLY;
ft->dwFileBytesDone = ft->dwThisFileSize;
break;
case FILERESUME_RENAME:
openFlags = _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY;
SAFE_FREE(&ft->szThisFile);
ft->szThisFile = null_strdup(szFilename);
ft->dwFileBytesDone = 0;
break;
}
ft->fileId = OpenFileUtf(ft->szThisFile, openFlags, _S_IREAD | _S_IWRITE);
if (ft->fileId == -1)
{
icq_LogMessage(LOG_ERROR, LPGEN("Your file receive has been aborted because Miranda could not open the destination file in order to write to it. You may be trying to save to a read-only folder."));
NetLib_CloseConnection(&ft->hConnection, FALSE);
return;
}
if (action == FILERESUME_RESUME)
ft->dwFileBytesDone = _lseek(ft->fileId, 0, SEEK_END);
else
_lseek(ft->fileId, ft->dwFileBytesDone, SEEK_SET);
ft->dwBytesDone += ft->dwFileBytesDone;
file_sendResume(this, dc);
BroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);
}
// small utility function
void NormalizeBackslash(char* path)
{
int len = strlennull(path);
if (len && path[len-1] != '\\') strcat(path, "\\");
}
/* a file transfer looks like this:
S: 0
R: 5
R: 1
S: 2
R: 3
S: 6 * many
(for more files, send 2, 3, 6*many)
*/
void CIcqProto::handleFileTransferPacket(directconnect* dc, PBYTE buf, WORD wLen)
{
if (wLen < 1)
return;
NetLog_Direct("Handling file packet");
switch (buf[0])
{
case PEER_FILE_INIT: /* first packet of a file transfer */
if (dc->initialised)
return;
if (wLen < 19)
return;
buf += 5; /* id, and unknown 0 */
dc->type = DIRECTCONN_FILE;
{
DWORD dwFileCount;
DWORD dwTotalSize;
DWORD dwTransferSpeed;
WORD wNickLength;
int bAdded;
unpackLEDWord(&buf, &dwFileCount);
unpackLEDWord(&buf, &dwTotalSize);
unpackLEDWord(&buf, &dwTransferSpeed);
unpackLEWord(&buf, &wNickLength);
dc->ft = FindExpectedFileRecv(dc->dwRemoteUin, dwTotalSize);
if (dc->ft == NULL)
{
NetLog_Direct("Unexpected file receive");
CloseDirectConnection(dc);
return;
}
dc->ft->dwFileCount = dwFileCount;
dc->ft->dwTransferSpeed = dwTransferSpeed;
dc->ft->hContact = HContactFromUIN(dc->ft->dwUin, &bAdded);
dc->ft->dwBytesDone = 0;
dc->ft->iCurrentFile = -1;
dc->ft->fileId = -1;
dc->ft->hConnection = dc->hConnection;
dc->ft->dwLastNotify = GetTickCount();
dc->initialised = 1;
file_sendTransferSpeed(this, dc);
file_sendNick(this, dc);
}
BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, dc->ft, 0);
break;
case PEER_FILE_INIT_ACK:
if (wLen < 8)
return;
buf++;
unpackLEDWord(&buf, &dc->ft->dwTransferSpeed);
/* followed by nick */
file_sendNextFile(this, dc);
break;
case PEER_FILE_NEXTFILE:
if (wLen < 20)
return;
buf++; /* id */
{
char *szAnsi;
WORD wThisFilenameLen, wSubdirLen;
BYTE isDirectory;
unpackByte(&buf, &isDirectory);
unpackLEWord(&buf, &wThisFilenameLen);
if (wLen < 19 + wThisFilenameLen)
return;
SAFE_FREE(&dc->ft->szThisFile);
szAnsi = (char *)_alloca(wThisFilenameLen + 1);
memcpy(szAnsi, buf, wThisFilenameLen);
szAnsi[wThisFilenameLen] = '\0';
dc->ft->szThisFile = ansi_to_utf8(szAnsi);
buf += wThisFilenameLen;
unpackLEWord(&buf, &wSubdirLen);
if (wLen < 18 + wThisFilenameLen + wSubdirLen)
return;
SAFE_FREE(&dc->ft->szThisSubdir);
szAnsi = (char *)_alloca(wSubdirLen + 1);
memcpy(szAnsi, buf, wSubdirLen);
szAnsi[wSubdirLen] = '\0';
dc->ft->szThisSubdir = ansi_to_utf8(szAnsi);
buf += wSubdirLen;
unpackLEDWord(&buf, &dc->ft->dwThisFileSize);
unpackLEDWord(&buf, &dc->ft->dwThisFileDate);
unpackLEDWord(&buf, &dc->ft->dwTransferSpeed);
/* no cheating with paths */
if (!IsValidRelativePath(dc->ft->szThisFile) || !IsValidRelativePath(dc->ft->szThisSubdir))
{
NetLog_Direct("Invalid path information");
break;
}
char *szFullPath = (char*)SAFE_MALLOC(strlennull(dc->ft->szSavePath)+strlennull(dc->ft->szThisSubdir)+strlennull(dc->ft->szThisFile)+3);
strcpy(szFullPath, dc->ft->szSavePath);
NormalizeBackslash(szFullPath);
strcat(szFullPath, dc->ft->szThisSubdir);
NormalizeBackslash(szFullPath);
// _chdir(szFullPath); // set current dir - not very useful
strcat(szFullPath, dc->ft->szThisFile);
// we joined the full path to dest file
SAFE_FREE(&dc->ft->szThisFile);
dc->ft->szThisFile = szFullPath;
dc->ft->dwFileBytesDone = 0;
dc->ft->iCurrentFile++;
if (isDirectory)
{
MakeDirUtf(dc->ft->szThisFile);
dc->ft->fileId = -1;
}
else
{
/* file resume */
PROTOFILETRANSFERSTATUS pfts = {0};
file_buildProtoFileTransferStatus(dc->ft, &pfts);
if (BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, dc->ft, (LPARAM)&pfts))
break; /* UI supports resume: it will call PS_FILERESUME */
dc->ft->fileId = OpenFileUtf(dc->ft->szThisFile, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);
if (dc->ft->fileId == -1)
{
icq_LogMessage(LOG_ERROR, LPGEN("Your file receive has been aborted because Miranda could not open the destination file in order to write to it. You may be trying to save to a read-only folder."));
CloseDirectConnection(dc);
dc->ft->hConnection = NULL;
break;
}
}
}
file_sendResume(this, dc);
BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, dc->ft, 0);
break;
case PEER_FILE_RESUME:
if (dc->ft->fileId == -1 && !dc->ft->currentIsDir)
return;
if (wLen < 13)
return;
if (wLen < 17)
NetLog_Direct("Warning: Received short PEER_FILE_RESUME");
buf++;
{
DWORD dwRestartFrom;
unpackLEDWord(&buf, &dwRestartFrom);
if (dwRestartFrom > dc->ft->dwThisFileSize)
return;
buf += 4; /* unknown. 0 */
unpackLEDWord(&buf, &dc->ft->dwTransferSpeed);
buf += 4; /* unknown. 1 */
if (!dc->ft->currentIsDir)
_lseek(dc->ft->fileId, dwRestartFrom, 0);
dc->wantIdleTime = 1;
dc->ft->dwBytesDone += dwRestartFrom;
dc->ft->dwFileBytesDone += dwRestartFrom;
}
break;
case PEER_FILE_SPEED:
if (wLen < 5)
return;
buf++;
unpackLEDWord(&buf, &dc->ft->dwTransferSpeed);
dc->ft->dwLastNotify = GetTickCount();
break;
case PEER_FILE_DATA:
if (!dc->ft->currentIsDir)
{
if (dc->ft->fileId == -1)
break;
buf++; wLen--;
_write(dc->ft->fileId, buf, wLen);
}
else
wLen = 0;
dc->ft->dwBytesDone += wLen;
dc->ft->dwFileBytesDone += wLen;
if (GetTickCount() > dc->ft->dwLastNotify + 500 || wLen < 2048)
{
PROTOFILETRANSFERSTATUS pfts;
file_buildProtoFileTransferStatus(dc->ft, &pfts);
BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, dc->ft, (LPARAM)&pfts);
dc->ft->dwLastNotify = GetTickCount();
}
if (wLen < 2048)
{
/* EOF */
if (!dc->ft->currentIsDir)
_close(dc->ft->fileId);
dc->ft->fileId = -1;
if ((DWORD)dc->ft->iCurrentFile == dc->ft->dwFileCount - 1)
{
dc->type = DIRECTCONN_CLOSING; /* this guarantees that we won't accept any more data but that the sender is still free to closesocket() neatly */
BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, dc->ft, 0);
}
}
break;
default:
NetLog_Direct("Unknown file transfer packet ignored.");
break;
}
}
|