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
|
#include "dropBox_proto.h"
HANDLE g_hNetlibUser;
ULONG g_fileId = 1;
bool HasAccessToken()
{
return db_get_sa(NULL, MODULE, "TokenSecret") != NULL;
}
int OnModulesLoaded(WPARAM wParam, LPARAM lParam)
{
NETLIBUSER nlu = { sizeof(nlu) };
nlu.flags = NUF_INCOMING | NUF_OUTGOING | NUF_HTTPCONNS | NUF_TCHAR;
nlu.szSettingsModule = MODULE;
nlu.szSettingsModule = MODULE;
nlu.ptszDescriptiveName = L"DropBox";
g_hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
DropBoxLogIn();
return 0;
}
int OnOptionsInit(WPARAM wParam, LPARAM lParam)
{
OPTIONSDIALOGPAGE odp = { sizeof(odp) };
odp.position = 100000000;
odp.hInstance = g_hInstance;
odp.flags = ODPF_BOLDGROUPS;
odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPTIONS_MAIN);
odp.pszGroup = LPGEN("Network");
odp.pszTitle = LPGEN("DropBox");
odp.pfnDlgProc = MainOptionsProc;
//Options_AddPage(wParam, &odp);
return 0;
}
INT_PTR DropBoxGetCaps(WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case PFLAGNUM_1:
return PF1_IM | PF1_FILESEND;
case PFLAGNUM_2:
return PF2_ONLINE;
case PFLAG_UNIQUEIDTEXT:
return (INT_PTR) "Dropbox ID";
case PFLAG_UNIQUEIDSETTING:
return (DWORD_PTR)"uid";
}
return 0;
}
HttpRequest *DropBoxCreateFileChunkedRequest(const char *data, int length)
{
HttpRequest *request = new HttpRequest(g_hNetlibUser, REQUEST_PUT, DROPBOX_APICONTENT_URL "/chunked_upload");
request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret"));
if (length > 0)
{
request->AddHeader("Content-Type", "application/octet-stream");
request->dataLength = length;
request->pData = (char*)mir_alloc(sizeof(char) * (length + 1));
memcpy(request->pData, data, length);
request->pData[length] = 0;
}
return request;
}
bool DropBoxSendFileChunkedStart(const char *data, int length, char *uploadId, int &offset)
{
HttpRequest *request = DropBoxCreateFileChunkedRequest(data, length);
NETLIBHTTPREQUEST *response = request->Send();
delete request;
if (response && response->resultCode == 200)
{
JSONNODE *root = json_parse(response->pData);
if (root != NULL)
{
JSONNODE *node = json_get(root, "upload_id");
strcpy(uploadId, mir_u2a(json_as_string(node)));
node = json_get(root, "offset");
offset = json_as_int(node);
return true;
}
}
return 0;
}
bool DropBoxSendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset)
{
HttpRequest *request = DropBoxCreateFileChunkedRequest(data, length);
request->AddParameter("upload_id", uploadId);
request->AddParameter("offset", offset);
NETLIBHTTPREQUEST *response = request->Send();
delete request;
if (response && response->resultCode == 200)
{
JSONNODE *root = json_parse(response->pData);
if (root != NULL)
{
JSONNODE *node = json_get(root, "offset");
offset = json_as_int(node);
return true;
}
}
return false;
}
bool DropBoxSendFileChunkedEnd(const char *fileName, const char *uploadId, MCONTACT hContact)
{
char url[MAX_PATH];
mir_snprintf(
url,
SIZEOF(url),
"%s/commit_chunked_upload/%s/%s",
DROPBOX_APICONTENT_URL,
DROPBOX_API_ROOT,
fileName);
HttpRequest *request = new HttpRequest(g_hNetlibUser, REQUEST_POST, url);
request->AddParameter("upload_id", uploadId);
request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret"));
NETLIBHTTPREQUEST *response = request->Send();
delete request;
if (response && response->resultCode == 200)
{
mir_snprintf(
url,
SIZEOF(url),
"%s/shares/%s/%s",
DROPBOX_API_URL,
DROPBOX_API_ROOT,
fileName);
request = new HttpRequest(g_hNetlibUser, REQUEST_POST, url);
request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret"));
response = request->Send();
if (response && response->resultCode == 200)
{
JSONNODE *root = json_parse(response->pData);
if (root != NULL)
{
JSONNODE *node = json_get(root, "url");
char *message = mir_utf8encodeW(json_as_string(node));
DBEVENTINFO dbei = { sizeof(dbei) };
dbei.szModule = MODULE;
dbei.timestamp = time(NULL);
dbei.eventType = EVENTTYPE_MESSAGE;
dbei.cbBlob = strlen(message);
dbei.pBlob = (PBYTE)mir_strdup(message);
dbei.flags = DBEF_UTF;
::db_event_add(hContact, &dbei);
}
}
return true;
}
return false;
}
void DropBoxAsyncFileSend(void *arg)
{
FileTransferParam *ftp = (FileTransferParam *)arg;
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, ftp->hProcess, 0);
for (int i = 0; ftp->pfts.pszFiles[i]; i++)
{
FILE *file = fopen(ftp->pfts.pszFiles[i], "rb");
if (file != NULL)
{
int offset = 0;
char *uploadId = new char[32];
const char *fileName = strrchr(ftp->pfts.pszFiles[i], '\\') + 1;
fseek(file, 0, SEEK_END);
DWORD fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
ftp->pfts.currentFileNumber = i;
ftp->pfts.currentFileSize = fileSize;
ftp->pfts.currentFileProgress = 0;
ftp->pfts.szCurrentFile = strrchr(ftp->pfts.pszFiles[i], '\\') + 1;
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);
while (!feof(file) && !ferror(file))
{
int chunkSize = DROPBOX_FILE_CHUNK_SIZE;
if (fileSize < 1024*1024)
chunkSize = DROPBOX_FILE_CHUNK_SIZE / 5;
else if (fileSize > 20*1024*1024)
chunkSize = DROPBOX_FILE_CHUNK_SIZE * 4;
char *data = new char[chunkSize + 1];
size_t count = fread(data, sizeof(char), chunkSize, file);
if (!offset)
DropBoxSendFileChunkedStart(data, count, uploadId, offset);
else
DropBoxSendFileChunkedNext(data, count, uploadId, offset);
ftp->pfts.currentFileProgress += count;
ftp->pfts.totalProgress += count;
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);
}
fclose(file);
DropBoxSendFileChunkedEnd(fileName, uploadId, ftp->pfts.hContact);
ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize;
if (i < ftp->pfts.totalFiles - 1)
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftp->hProcess, 0);
}
}
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0);
delete ftp;
}
INT_PTR DropBoxSendFile(WPARAM wParam, LPARAM lParam)
{
CCSDATA *pccsd = (CCSDATA*)lParam;
FileTransferParam *ftp = new FileTransferParam();
ftp->pfts.flags = PFTS_SENDING | PFTS_UTF;
ftp->pfts.hContact = pccsd->hContact;
char **files = (char**)pccsd->lParam;
for (ftp->pfts.totalFiles = 0; files[ftp->pfts.totalFiles]; ftp->pfts.totalFiles++);
ftp->pfts.pszFiles = new char*[ftp->pfts.totalFiles + 1];
ftp->pfts.pszFiles[ftp->pfts.totalFiles] = NULL;
for (int i = 0; i < ftp->pfts.totalFiles; i++)
{
ftp->pfts.pszFiles[i] = mir_strdup(files[i]);
FILE *file = fopen(files[i], "rb");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
ftp->pfts.totalBytes += ftell(file);
fseek(file, 0, SEEK_SET);
fclose(file);
}
}
ULONG fileId = ::InterlockedIncrement(&g_fileId);
ftp->hProcess = (HANDLE)fileId;
mir_forkthread(DropBoxAsyncFileSend, ftp);
return fileId;
}
INT_PTR DropBoxSendMessage(WPARAM wParam, LPARAM lParam)
{
return 0;
}
void SetContactStatus(MCONTACT hContact, int newStatus)
{
if (db_get_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE) != newStatus)
db_set_w(hContact, MODULE, "Status", newStatus);
}
bool DropBoxLogIn()
{
char *access_token = db_get_sa(NULL, MODULE, "TokenSecret");
if (!access_token)
{
ShellExecuteA(NULL, "open", "https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id="DROPBOX_API_KEY, NULL, NULL, SW_SHOWDEFAULT);
char request_token[128];
request_token[0] = 0;
if (DialogBoxParam(
g_hInstance,
MAKEINTRESOURCE(IDD_TOKEN_REQUEST),
NULL,
TokenRequestProc,
(LPARAM)&request_token) == IDOK)
{
HttpRequest *request = new HttpRequest(g_hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token");
request->AddParameter("grant_type", "authorization_code");
request->AddParameter("code", request_token);
request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
char data[64];
mir_snprintf(
data,
SIZEOF(data),
"client_id=%s&client_secret=%s",
DROPBOX_API_KEY,
DROPBOX_API_SECRET);
request->pData = mir_strdup(data);
request->dataLength = strlen(data);
NETLIBHTTPREQUEST *response = request->Send();
if (response && response->resultCode == 200)
{
JSONNODE *root = json_parse(response->pData);
if (root != NULL)
{
JSONNODE *node = json_get(root, "access_token");
access_token = mir_u2a(json_as_string(node));
db_set_s(NULL, MODULE, "TokenSecret", access_token);
MCONTACT hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
if (!CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)MODULE))
{
node = json_get(root, "uid");
wchar_t *uid = json_as_string(node);
db_set_ws(hContact, MODULE, "uid", uid);
db_set_s(hContact, MODULE, "Nick", MODULE);
db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE);
//SetContactStatus(hContact, ID_STATUS_ONLINE);
}
delete node;
delete root;
delete request;
//delete access_token;
return true;
}
}
delete request;
}
}
/*else
{
for (MCONTACT hContact = db_find_first(MODULE); hContact; hContact = db_find_next(hContact, MODULE))
SetContactStatus(hContact, ID_STATUS_ONLINE);
}*/
return false;
}
//void LogOut()
//{
// HttpRequest<CDropBoxProto> *request = new HttpRequest<CDropBoxProto>(this, REQUEST_POST, DROPBOX_API_URL "/disable_access_token");
// //request->SendAsync(&CDropBoxProto::AsyncFunc);
// request->Send();
//
// delete request;
//}
INT_PTR CALLBACK TokenRequestProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
char *token = reinterpret_cast<char*>(::GetWindowLongPtr(hwndDlg, GWLP_USERDATA));
switch (msg)
{
case WM_INITDIALOG:
TranslateDialogDefault(hwndDlg);
token = (char*)lParam;
SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
{
//::SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)CSkypeProto::IconList[0].Handle);
//::SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)CSkypeProto::IconList[0].Handle);
/*wchar_t title[MAX_PATH];
::mir_sntprintf(
title,
MAX_PATH,
::TranslateT("Enter a password for %s:"),
param->login);*/
//::SetDlgItemText(hwndDlg, IDC_INSTRUCTION, title);
SendDlgItemMessage(hwndDlg, IDC_TOKEN, EM_LIMITTEXT, 128 - 1, 0);
}
break;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
{
char data[128];
GetDlgItemTextA(hwndDlg, IDC_TOKEN, data, SIZEOF(data));
strcpy(token, data);
EndDialog(hwndDlg, IDOK);
}
break;
case IDCANCEL:
EndDialog(hwndDlg, IDCANCEL);
break;
}
}
break;
}
return FALSE;
}
INT_PTR CALLBACK MainOptionsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
return FALSE;
}
|