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
|
#include "stdafx.h"
CDropbox::CDropbox() : transfers(1, HandleKeySortT)
{
PROTOCOLDESCRIPTOR pd = { PROTOCOLDESCRIPTOR_V3_SIZE };
pd.szName = MODULE;
pd.type = PROTOTYPE_VIRTUAL;
CallService(MS_PROTO_REGISTERMODULE, 0, (LPARAM)&pd);
HookEvent(ME_PROTO_ACK, OnProtoAck);
HookEvent(ME_SYSTEM_PRESHUTDOWN, OnPreShutdown);
HookEventObj(ME_SYSTEM_MODULESLOADED, GlobalEvent<&CDropbox::OnModulesLoaded>, this);
hFileSentEventHook = CreateHookableEvent(ME_DROPBOX_SENT);
CreateServiceFunctionObj(MS_DROPBOX_SEND_FILE, GlobalService<&CDropbox::SendFileToDropbox>, this);
CreateProtoServiceFunction(MODULE, PS_GETCAPS, ProtoGetCaps);
CreateProtoServiceFunction(MODULE, PS_GETNAME, ProtoGetName);
CreateProtoServiceFunction(MODULE, PS_LOADICON, ProtoLoadIcon);
CreateProtoServiceFunctionObj(PS_GETSTATUS, GlobalService<&CDropbox::ProtoGetStatus>, this);
CreateProtoServiceFunctionObj(PSS_FILEW, GlobalService<&CDropbox::ProtoSendFile>, this);
CreateProtoServiceFunctionObj(PSS_FILECANCEL, GlobalService<&CDropbox::ProtoCancelFile>, this);
CreateProtoServiceFunctionObj(PSS_MESSAGE, GlobalService<&CDropbox::ProtoSendMessage>, this);
CreateProtoServiceFunction(MODULE, PSR_MESSAGE, ProtoReceiveMessage);
InitializeMenus();
hFileProcess = hMessageProcess = 1;
}
CDropbox::~CDropbox()
{
DestroyHookableEvent(hFileSentEventHook);
}
MCONTACT CDropbox::GetDefaultContact()
{
if (!hDefaultContact)
hDefaultContact = db_find_first(MODULE);
if (!hDefaultContact)
{
hDefaultContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
if (!CallService(MS_PROTO_ADDTOCONTACT, hDefaultContact, (LPARAM)MODULE))
{
db_set_s(NULL, MODULE, "Nick", MODULE);
db_set_s(hDefaultContact, MODULE, "Nick", MODULE);
db_set_ws(hDefaultContact, "CList", "MyHandle", L"Dropbox");
int status = db_get_w(hDefaultContact, MODULE, "Status", ID_STATUS_OFFLINE);
if (HasAccessToken() && status == ID_STATUS_OFFLINE)
db_set_w(hDefaultContact, MODULE, "Status", ID_STATUS_ONLINE);
}
}
return hDefaultContact;
}
bool CDropbox::HasAccessToken()
{
return db_get_sa(NULL, MODULE, "TokenSecret") != NULL;
}
void CDropbox::RequestAccountInfo()
{
MCONTACT hContact = CDropbox::GetDefaultContact();
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
GetAccountInfoRequest request(token);
mir_ptr<NETLIBHTTPREQUEST> response(request.Send(hNetlibConnection));
HandleHttpResponseError(response);
JSONROOT root(response->pData);
if (root)
{
JSONNODE *node = json_get(root, "referral_link");
if (node)
{
ptrW referral_link = ptrW(json_as_string(node));
db_set_ws(hContact, MODULE, "Homepage", referral_link);
}
node = json_get(root, "display_name");
if (node)
{
ptrW display_name = ptrW(json_as_string(node));
TCHAR *sep = _tcsrchr(display_name, L' ');
if (sep)
{
db_set_ws(hContact, MODULE, "LastName", sep + 1);
display_name[mir_tstrlen(display_name) - mir_tstrlen(sep)] = '\0';
db_set_ws(hContact, MODULE, "FirstName", display_name);
}
else
{
db_set_ws(hContact, MODULE, "FirstName", display_name);
db_unset(hContact, MODULE, "LastName");
}
}
node = json_get(root, "country");
if (node)
{
ptrW isocodeW(json_as_string(node));
ptrA isocode(mir_u2a(isocodeW));
if (!strlen(isocode))
db_unset(hContact, MODULE, "Country");
else
{
char *country = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode, 0);
db_set_s(hContact, MODULE, "Country", country);
}
}
node = json_get(root, "quota_info");
JSONNODE *nroot = json_as_node(node);
if (nroot)
{
node = json_get(nroot, "shared");
if (node)
db_set_dw(hContact, MODULE, "SharedQuota", json_as_int(node));
node = json_get(nroot, "normal");
if (node)
db_set_dw(hContact, MODULE, "NormalQuota", json_as_int(node));
node = json_get(nroot, "quota");
if (node)
db_set_dw(hContact, MODULE, "TotalQuota", json_as_int(node));
}
}
}
void CDropbox::DestroyAccessToken()
{
DisableAccessTokenRequest request;
mir_ptr<NETLIBHTTPREQUEST> response(request.Send(hNetlibConnection));
db_unset(NULL, MODULE, "TokenSecret");
MCONTACT hContact = CDropbox::GetDefaultContact();
if (hContact)
if (db_get_w(hContact, MODULE, "Status", ID_STATUS_ONLINE) == ID_STATUS_ONLINE)
db_set_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE);
}
UINT CDropbox::RequestAccessTokenAsync(void *owner, void *param)
{
HWND hwndDlg = (HWND)param;
CDropbox *instance = (CDropbox*)owner;
EnableWindow(GetDlgItem(hwndDlg, IDC_AUTHORIZE), FALSE);
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("in process..."));
if (instance->HasAccessToken())
instance->DestroyAccessToken();
char requestToken[128];
GetDlgItemTextA(hwndDlg, IDC_REQUEST_CODE, requestToken, SIZEOF(requestToken));
GetAccessTokenRequest request(requestToken);
mir_ptr<NETLIBHTTPREQUEST> response(request.Send(instance->hNetlibConnection));
if (response == NULL)
{
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, HttpStatusToText(HTTP_STATUS_ERROR));
if (hwndDlg)
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("server does not respond"));
/*else
ShowNotification(TranslateT("server does not respond"), MB_ICONERROR);*/
return 0;
}
JSONROOT root(response->pData);
if (root == NULL)
{
JSONNODE *node = json_get(root, "error_description");
ptrW error_description(json_as_string(node));
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, HttpStatusToText((HTTP_STATUS)response->resultCode)));
if (hwndDlg)
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, error_description);
/*else
ShowNotification((TCHAR*)error_description, MB_ICONERROR);*/
return 0;
}
JSONNODE *node = json_get(root, "access_token");
ptrA access_token = ptrA(mir_u2a(json_as_string(node)));
db_set_s(NULL, MODULE, "TokenSecret", access_token);
ProtoBroadcastAck(MODULE, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_OFFLINE, (WPARAM)ID_STATUS_ONLINE);
MCONTACT hContact = instance->GetDefaultContact();
if (hContact)
{
if (db_get_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE)
db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE);
}
try
{
instance->RequestAccountInfo();
}
catch (TransferException &ex)
{
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, ex.what());
return 0;
}
if (hwndDlg)
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("you have been authorized"));
/*else
ShowNotification(TranslateT("you have been authorized"), MB_ICONINFORMATION);*/
SetDlgItemTextA(hwndDlg, IDC_REQUEST_CODE, "");
return 0;
}
|