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
|
#include "StdAfx.h"
#include "sametime.h"
CSametimeProto* getProtoFromMwFileTransfer(mwFileTransfer* ft)
{
mwServiceFileTransfer* serviceFT = mwFileTransfer_getService(ft);
mwService* service = mwServiceFileTransfer_getService(serviceFT);
mwSession* session = mwService_getSession(service);
return (CSametimeProto*)mwSession_getProperty(session, "PROTO_STRUCT_PTR");
}
/** an incoming file transfer has been offered */
void mwFileTransfer_offered(mwFileTransfer* ft)
{
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
proto->debugLog(_T("mwFileTransfer_offered() start"));
const mwIdBlock* idb = mwFileTransfer_getUser(ft);
MCONTACT hContact = proto->FindContactByUserId(idb->user);
proto->debugLog(_T("Sametime mwFileTransfer_offered hContact=[%x]"), hContact);
if (!hContact) {
mwSametimeList* user_list = mwSametimeList_new();
mwSametimeGroup* stgroup = mwSametimeGroup_new(user_list, mwSametimeGroup_NORMAL, Translate("None"));
mwSametimeUser* stuser = mwSametimeUser_new(stgroup, mwSametimeUser_NORMAL, (mwIdBlock*)idb);
hContact = proto->AddContact(stuser, (proto->options.add_contacts ? false : true));
}
proto->ProtoBroadcastAck(hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, (HANDLE)ft, 0);
TCHAR* filenameT = mir_utf8decodeT(mwFileTransfer_getFileName(ft));
const char* message = mwFileTransfer_getMessage(ft);
TCHAR descriptionT[512];
if (message) {
TCHAR* messageT = mir_utf8decodeT(message);
mir_sntprintf(descriptionT, _T("%s - %s"), filenameT, messageT);
mir_free(messageT);
} else
_tcsncpy_s(descriptionT, filenameT, _TRUNCATE);
PROTORECVFILET pre = {0};
pre.dwFlags = PRFF_TCHAR;
pre.fileCount = 1;
pre.timestamp = time(NULL);
pre.descr.t = descriptionT;
pre.files.t = &filenameT;
pre.lParam = (LPARAM)ft;
ProtoChainRecvFile(hContact, &pre);
mir_free(filenameT);
}
//returns 0 if finished with current file
int SendFileChunk(CSametimeProto* proto, mwFileTransfer* ft, FileTransferClientData* ftcd) {
DWORD bytes_read;
if (!ftcd || !ftcd->buffer)
return 0;
if (!ReadFile(ftcd->hFile, ftcd->buffer, FILE_BUFF_SIZE, &bytes_read, 0)) {
proto->debugLog(_T("Sametime closing file transfer (SendFileChunk)"));
mwFileTransfer_close(ft, mwFileTransfer_SUCCESS);
return 0;
}
mwOpaque o;
o.data = (unsigned char*)ftcd->buffer;
o.len = bytes_read;
mwFileTransfer_send(ft, &o);
return bytes_read;
}
void __cdecl SendThread(LPVOID param) {
mwFileTransfer* ft = (mwFileTransfer*)param;
if (!ft) return;
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
proto->debugLog(_T("SendThread() start"));
PROTOFILETRANSFERSTATUS pfts = {0};
pfts.cbSize = sizeof(pfts);
pfts.flags = PFTS_UTF;
pfts.hContact = ftcd->hContact;
if (ftcd->sending == 1)
pfts.flags |= PFTS_SENDING;
pfts.pszFiles = NULL;
pfts.totalFiles = ftcd->first->ft_count;
pfts.totalBytes = ftcd->first->totalSize;
while(SendFileChunk(proto, ft, ftcd) && !Miranda_Terminated()) {
pfts.currentFileNumber = ftcd->ft_number;
pfts.totalProgress = ftcd->sizeToHere + mwFileTransfer_getSent(ft);
pfts.szWorkingDir = ftcd->save_path;
pfts.szCurrentFile = (char*)mwFileTransfer_getFileName(ft);
pfts.currentFileSize = mwFileTransfer_getFileSize(ft);
pfts.currentFileProgress = mwFileTransfer_getSent(ft);
pfts.currentFileTime = 0; //?
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftcd->hFt, (LPARAM)&pfts);
SleepEx(500,TRUE);
}
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftcd->hFt, 0);
mwFileTransfer_removeClientData(ft);
if (ftcd->save_path) free(ftcd->save_path);
if (ftcd->buffer) delete[] ftcd->buffer;
delete ftcd;
proto->debugLog(_T("SendThread() end"));
return;
}
/** a file transfer has been fully initiated */
void mwFileTransfer_opened(mwFileTransfer* ft)
{
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
proto->debugLog(_T("Sametime mwFileTransfer_opened start"));
if (ftcd->sending) {
// create a thread to send chunks - since it seems not all clients send acks for each of our chunks!
mir_forkthread(SendThread, ft);
}
}
/** a file transfer has been closed. Check the status of the file
transfer to determine if the transfer was complete or if it had
been interrupted */
void mwFileTransfer_closed(mwFileTransfer* ft, guint32 code)
{
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
proto->debugLog(_T("mwFileTransfer_closed() start"));
if (ftcd) {
if (ftcd->hFile != INVALID_HANDLE_VALUE)
CloseHandle(ftcd->hFile);
if (code != mwFileTransfer_SUCCESS || !mwFileTransfer_isDone(ft)) {
if (!ftcd->sending) {
char fn[MAX_PATH];
if (ftcd->save_path) mir_strcpy(fn, ftcd->save_path);
else fn[0] = 0;
mir_strcat(fn, mwFileTransfer_getFileName(ft));
DeleteFileA(fn);
}
if (code == mwFileTransfer_REJECTED)
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_DENIED, ftcd->hFt, 0);
else
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ftcd->hFt, 0);
if (ftcd->sending) {
FileTransferClientData* ftcd_next = ftcd->next;
while(ftcd_next) {
mwFileTransfer_free((mwFileTransfer*)ftcd_next->ft);
FileTransferClientData *ftcd_temp = ftcd_next->next;
if (ftcd_next->hFile != INVALID_HANDLE_VALUE)
CloseHandle(ftcd_next->hFile);
if (ftcd_next->save_path)
free(ftcd_next->save_path);
if (ftcd_next->buffer)
delete[] ftcd_next->buffer;
delete ftcd_next;
ftcd_next = ftcd_temp;
}
}
else {
mwFileTransfer_removeClientData(ft);
if (ftcd->save_path)
free(ftcd->save_path);
if (ftcd->buffer)
delete[] ftcd->buffer;
delete ftcd;
mwFileTransfer_free(ft);
}
}
else {
if (ftcd->sending) {
// check if we have more files to send...
if (ftcd->next) {
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftcd->hFt, 0);
mwFileTransfer_offer(ftcd->next->ft);
}
}
else {
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftcd->hFt, 0);
mwFileTransfer_removeClientData(ft);
if (ftcd->save_path)
free(ftcd->save_path);
if (ftcd->buffer)
delete[] ftcd->buffer;
delete ftcd;
mwFileTransfer_free(ft);
}
}
}
}
/** receive a chunk of a file from an inbound file transfer. */
void mwFileTransfer_recv(mwFileTransfer* ft, struct mwOpaque* data)
{
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
proto->debugLog(_T("mwFileTransfer_recv() start"));
DWORD bytes_written;
if (!WriteFile(ftcd->hFile, data->data, data->len, &bytes_written, 0)) {
proto->debugLog(_T("mwFileTransfer_recv() !WriteFile"));
mwFileTransfer_cancel(ft);
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ftcd->hFt, 0);
proto->debugLog(_T("mwFileTransfer_recv() ACKRESULT_FAILED"));
}
else {
mwFileTransfer_ack(ft); // acknowledge chunk
PROTOFILETRANSFERSTATUS pfts = { 0 };
pfts.cbSize = sizeof(pfts);
pfts.flags = PFTS_UTF;
pfts.hContact = ftcd->hContact;
if (ftcd->sending == 1) {
pfts.flags |= PFTS_SENDING;
}
pfts.pszFiles = NULL;
pfts.totalFiles = 1;
pfts.currentFileNumber = 0;
pfts.totalBytes = mwFileTransfer_getFileSize(ft);
pfts.totalProgress = mwFileTransfer_getSent(ft);
pfts.szWorkingDir = ftcd->save_path;
pfts.szCurrentFile = (char*)mwFileTransfer_getFileName(ft);
pfts.currentFileSize = mwFileTransfer_getFileSize(ft);
pfts.currentFileProgress = mwFileTransfer_getSent(ft);
pfts.currentFileTime = 0; //?
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftcd->hFt, (LPARAM)&pfts);
proto->debugLog(_T("mwFileTransfer_recv() ACKRESULT_DATA"));
if (mwFileTransfer_isDone(ft)) {
proto->ProtoBroadcastAck(ftcd->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftcd->hFt, 0);
proto->debugLog(_T("mwFileTransfer_recv() ACKRESULT_SUCCESS"));
}
}
}
/** received an ack for a sent chunk on an outbound file transfer.
this indicates that a previous call to mwFileTransfer_send has
reached the target and that the target has responded. */
void mwFileTransfer_handle_ack(mwFileTransfer* ft)
{
// see SendThread above - not all clients send us acks
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
//FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
proto->debugLog(_T("mwFileTransfer_handle_ack()"));
}
/** optional. called from mwService_free */
void mwFileTransfer_clear(mwServiceFileTransfer* srvc)
{
}
mwFileTransferHandler mwFileTransfer_handler = {
mwFileTransfer_offered,
mwFileTransfer_opened,
mwFileTransfer_closed,
mwFileTransfer_recv,
mwFileTransfer_handle_ack,
mwFileTransfer_clear
};
HANDLE CSametimeProto::SendFilesToUser(MCONTACT hContact, TCHAR** files, const TCHAR* ptszDesc)
{
debugLog(_T("CSametimeProto::SendFilesToUser() start"));
mwAwareIdBlock id_block;
if (GetAwareIdFromContact(hContact, &id_block)) {
mwIdBlock idb;
idb.user = id_block.user;
idb.community = id_block.community;
FileTransferClientData *ftcd, *prev_ftcd = 0, *first_ftcd = 0;
mwFileTransfer *ft, *first_ft = 0;
for (int i = 0; files[i]; i++) {
HANDLE hFile = CreateFile(files[i], GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD filesize = GetFileSize(hFile, 0);
TCHAR *fn = _tcsrchr(files[i], '\\');
if (fn)
fn++;
else
fn = files[i];
ft = mwFileTransfer_new(service_files, &idb, T2Utf(ptszDesc), T2Utf(fn), filesize);
ftcd = new FileTransferClientData;
memset(ftcd, 0, sizeof(FileTransferClientData));
ftcd->ft = ft;
ftcd->hContact = hContact;
ftcd->next = 0;
if (prev_ftcd) {
prev_ftcd->next = ftcd; // link into list
// each node contains a pointer to the first - it will contain infor linke the count etc
ftcd->first = prev_ftcd->first;
}
else ftcd->first = ftcd;
if (!first_ft) first_ft = ft;
ftcd->sending = true;
ftcd->hFile = hFile;
ftcd->hFt = (HANDLE)first_ft;
ftcd->save_path = 0;
ftcd->buffer = new char[FILE_BUFF_SIZE];
ftcd->ft_number = ftcd->first->ft_count;
ftcd->first->ft_count++;
ftcd->sizeToHere = ftcd->first->totalSize;
ftcd->first->totalSize += filesize;
mwFileTransfer_setClientData(ft, (gpointer)ftcd, 0);
prev_ftcd = ftcd;
}
}
free(id_block.user);
if (first_ft) {
mwFileTransfer_offer(first_ft);
return (HANDLE)first_ft;
}
}
return 0;
}
HANDLE CSametimeProto::AcceptFileTransfer(MCONTACT hContact, HANDLE hFt, char* save_path)
{
mwFileTransfer* ft = (mwFileTransfer*)hFt;
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
debugLog(_T("CSametimeProto::AcceptFileTransfer() start"));
FileTransferClientData* ftcd = new FileTransferClientData;
memset(ftcd, 0, sizeof(FileTransferClientData));
ftcd->ft = ft;
ftcd->sending = false;
ftcd->hFt = hFt;
if (save_path) // save path
ftcd->save_path = _strdup(save_path);
else
ftcd->save_path = 0;
mwFileTransfer_setClientData(ft, (gpointer)ftcd, 0);
char fp[MAX_PATH];
char* fn = strrchr((char*)mwFileTransfer_getFileName(ft), '\\');
if (fn) fn++;
if (ftcd->save_path)
mir_strcpy(fp, ftcd->save_path);
else
fp[0] = 0;
if (fn) mir_strcat(fp, fn);
else mir_strcat(fp, mwFileTransfer_getFileName(ft));
ftcd->hFile = CreateFileA(fp, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS, 0, 0);
if (ftcd->hFile == INVALID_HANDLE_VALUE) {
debugLog(_T("CSametimeProto::AcceptFileTransfer() INVALID_HANDLE_VALUE"));
mwFileTransfer_close(ft, mwFileTransfer_ERROR);
return 0;
}
ftcd->hContact = hContact;
mwFileTransfer_setClientData(ft, (gpointer)ftcd, 0);
mwFileTransfer_accept(ft);
return hFt;
}
void CSametimeProto::RejectFileTransfer(HANDLE hFt)
{
mwFileTransfer* ft = (mwFileTransfer*)hFt;
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
debugLog(_T("CSametimeProto::RejectFileTransfer() start"));
mwFileTransfer_reject(ft);
}
void CSametimeProto::CancelFileTransfer(HANDLE hFt)
{
mwFileTransfer* ft = (mwFileTransfer*)hFt;
CSametimeProto* proto = getProtoFromMwFileTransfer(ft);
debugLog(_T("CSametimeProto::CancelFileTransfer() start"));
FileTransferClientData* ftcd = (FileTransferClientData*)mwFileTransfer_getClientData(ft);
if (ftcd) {
while (ftcd && mwFileTransfer_isDone(ftcd->ft))
ftcd = ftcd->next;
if (ftcd) mwFileTransfer_cancel(ftcd->ft);
}
else mwFileTransfer_cancel(ft);
}
void CSametimeProto::InitFiles()
{
debugLog(_T("CSametimeProto::InitFiles()"));
mwSession_addService(session, (mwService*)(service_files = mwServiceFileTransfer_new(session, &mwFileTransfer_handler)));
}
void CSametimeProto::DeinitFiles()
{
debugLog(_T("CSametimeProto::DeinitFiles()"));
mwSession_removeService(session, mwService_FILE_TRANSFER);
mwService_free((mwService*)service_files);
service_files = 0;
}
|