void Callback(JSONNode & test, void * ide){ assertEquals(ide, (void*)0xDEADBEEF); ++counter; switch(counter){ case 1: assertEquals(test.type(), JSON_NODE); assertTrue(test.empty()); break; case 2: assertEquals(test.type(), JSON_ARRAY); assertTrue(test.empty()); break; case 3: assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 1); assertEquals(test[0].name(), JSON_TEXT("hello")); assertEquals(test[0].as_int(), 1); break; case 4: assertEquals(test.type(), JSON_ARRAY); assertEquals(test.size(), 3); break; case 5: assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 1); assertEquals(test[0].name(), JSON_TEXT("hi")); assertEquals(test[0].size(), 1) assertEquals(test[0][0].type(), JSON_NUMBER); assertEquals(test[0][0].name(), JSON_TEXT("one")); assertEquals(test[0][0].as_int(), 1); break; } }
void CDropbox::RequestAccountInfo() { MCONTACT hContact = CDropbox::GetDefaultContact(); ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); GetAccountInfoRequest request(token); NLHR_PTR response(request.Send(hNetlibConnection)); HandleHttpResponseError(response); JSONNode root = JSONNode::parse(response->pData); if (root.empty()) return; JSONNode referral_link = root.at("referral_link"); if (!referral_link.empty()) db_set_s(hContact, MODULE, "Homepage", referral_link.as_string().c_str()); JSONNode display_name = root.at("display_name"); if (!display_name.empty()) { ptrT display_name(mir_utf8decodeT(display_name.as_string().c_str())); TCHAR *sep = _tcsrchr(display_name, _T(' ')); if (sep) { db_set_ts(hContact, MODULE, "LastName", sep + 1); display_name[mir_tstrlen(display_name) - mir_tstrlen(sep)] = '\0'; db_set_ts(hContact, MODULE, "FirstName", display_name); } else { db_set_ts(hContact, MODULE, "FirstName", display_name); db_unset(hContact, MODULE, "LastName"); } } JSONNode country = root.at("country"); if (!country.empty()) { std::string isocode = country.as_string(); if (isocode.empty()) db_unset(hContact, MODULE, "Country"); else { char *country = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode.c_str(), 0); db_set_s(hContact, MODULE, "Country", country); } } JSONNode quota_info = root.at("quota_info"); if (!quota_info.empty()) { db_set_dw(hContact, MODULE, "SharedQuota", quota_info.at("shared").as_int()); db_set_dw(hContact, MODULE, "NormalQuota", quota_info.at("normal").as_int()); db_set_dw(hContact, MODULE, "TotalQuota", quota_info.at("quota").as_int()); } }
void CDropbox::RequestAccountInfo() { MCONTACT hContact = CDropbox::GetDefaultContact(); ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); GetAccountInfoRequest request(token); NLHR_PTR response(request.Send(hNetlibConnection)); HandleHttpResponseError(response); JSONNode root = JSONNode::parse(response->pData); if (root.empty()) return; JSONNode referral_link = root.at("referral_link"); if (!referral_link.empty()) db_set_s(hContact, MODULE, "Homepage", referral_link.as_string().c_str()); JSONNode display_name = root.at("display_name"); if (!display_name.empty()) { CMString tszDisplayName(display_name.as_mstring()); int pos = tszDisplayName.ReverseFind(' '); if (pos != -1) { db_set_ts(hContact, MODULE, "LastName", tszDisplayName.Mid(pos+1)); db_set_ts(hContact, MODULE, "FirstName", tszDisplayName.Left(pos)); } else { db_set_ts(hContact, MODULE, "FirstName", tszDisplayName); db_unset(hContact, MODULE, "LastName"); } } JSONNode country = root.at("country"); if (!country.empty()) { std::string isocode = country.as_string(); if (isocode.empty()) db_unset(hContact, MODULE, "Country"); else { char *szCountry = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode.c_str(), 0); db_set_s(hContact, MODULE, "Country", szCountry); } } JSONNode quota_info = root.at("quota_info"); if (!quota_info.empty()) { db_set_dw(hContact, MODULE, "SharedQuota", quota_info.at("shared").as_int()); db_set_dw(hContact, MODULE, "NormalQuota", quota_info.at("normal").as_int()); db_set_dw(hContact, MODULE, "TotalQuota", quota_info.at("quota").as_int()); } }
void CDropbox::RequestAccountInfo(void *p) { CDropbox *self = (CDropbox*)p; MCONTACT hContact = self->GetDefaultContact(); ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); GetCurrentAccountRequest request(token); NLHR_PTR response(request.Send(self->hNetlibConnection)); HandleJsonResponseError(response); JSONNode root = JSONNode::parse(response->pData); if (root.empty()) return; JSONNode referral_link = root.at("referral_link"); if (!referral_link.empty()) db_set_s(hContact, MODULE, "Homepage", referral_link.as_string().c_str()); JSONNode email = root.at("email"); if (!email.empty()) db_set_s(hContact, MODULE, "e-mail", email.as_string().c_str()); JSONNode name = root.at("name"); if (!name.empty()) { db_set_utf(hContact, MODULE, "FirstName", name.at("given_name").as_string().c_str()); db_set_utf(hContact, MODULE, "LastName", name.at("surname").as_string().c_str()); } JSONNode country = root.at("country"); if (!country.empty()) { std::string isocode = country.as_string(); if (isocode.empty()) db_unset(hContact, MODULE, "Country"); else { char *szCountry = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode.c_str(), 0); db_set_s(hContact, MODULE, "Country", szCountry); } } /*JSONNode quota_info = root.at("quota_info"); if (!quota_info.empty()) { ULONG lTotalQuota = quota_info.at("quota").as_int(); ULONG lNormalQuota = quota_info.at("normal").as_int(); ULONG lSharedQuota = quota_info.at("shared").as_int(); db_set_dw(hContact, MODULE, "SharedQuota", lSharedQuota); db_set_dw(hContact, MODULE, "NormalQuota", lNormalQuota); db_set_dw(hContact, MODULE, "TotalQuota", lTotalQuota); db_set_s(hContact, "CList", "StatusMsg", CMStringA(FORMAT, Translate("Free %ld of %ld MB"), (lTotalQuota - lNormalQuota) / (1024 * 1024), lTotalQuota / (1024 * 1024))); }*/ }
void CDropbox::CommandShare(void *arg) { CommandParam *param = (CommandParam*)arg; char *path = (char*)param->data; if (path == NULL) { CMStringA error(FORMAT, T2Utf(TranslateT("\"%s\" command has invalid parameter.\nUse \"/help\" for more info.")), "/share"); ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)error.GetBuffer()); return; } ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); ptrA encodedPath(mir_utf8encode(path)); bool useShortUrl = db_get_b(NULL, MODULE, "UseSortLinks", 1) > 0; ShareRequest request(token, encodedPath, useShortUrl); NLHR_PTR response(request.Send(param->instance->hNetlibConnection)); if (response == NULL || response->resultCode != HTTP_STATUS_OK) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } CMStringA link = root.at("url").as_string().c_str(); ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)link.GetBuffer()); }
void CDropbox::CommandDelete(void *arg) { CommandParam *param = (CommandParam*)arg; char *path = (char*)param->data; if (path == NULL) { CMStringA error(FORMAT, T2Utf(TranslateT("\"%s\" command has invalid parameter.\nUse \"/help\" for more info.")), "/delete"); ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)error.GetBuffer()); return; } ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); ptrA encodedPath(mir_utf8encode(path)); DeleteRequest request(token, encodedPath); NLHR_PTR response(request.Send(param->instance->hNetlibConnection)); if (response == NULL || response->resultCode != HTTP_STATUS_OK) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } bool isDeleted = root.at("is_deleted").as_bool(); CMStringA message(FORMAT, "%s %s", path, !isDeleted ? T2Utf(TranslateT("is not deleted")) : T2Utf(TranslateT("is deleted"))); ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer()); }
void CDropbox::CommandContent(void *arg) { CommandParam *param = (CommandParam*)arg; char *path = (char*)param->data; if (path == NULL) path = ""; ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); ptrA encodedPath(mir_utf8encode(path)); GetMetadataRequest request(token, encodedPath); NLHR_PTR response(request.Send(param->instance->hNetlibConnection)); if (response == NULL || response->resultCode != HTTP_STATUS_OK) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) { ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0); return; } CMStringA message; bool isDir = root.at("is_dir").as_bool(); if (!isDir) message.AppendFormat("\"%s\" %s", encodedPath, T2Utf(TranslateT("is file"))); else { JSONNode content = root.at("contents").as_array(); for (size_t i = 0; i < content.size(); i++) { JSONNode item = content[i]; if (item.empty()) { if (i == 0) message.AppendFormat("\"%s\" %s", encodedPath, T2Utf(TranslateT("is empty"))); break; } CMStringA subName(item.at("path").as_string().c_str()); message.AppendFormat("%s\n", (subName[0] == '/') ? subName.Mid(1) : subName); } } ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer()); }
void CDropbox::HandleJsonResponseError(NETLIBHTTPREQUEST *response) { if (response == NULL) throw DropboxException(HttpStatusToText(HTTP_STATUS_ERROR)); if (response->resultCode == HTTP_STATUS_OK) return; if (response->resultCode != HTTP_STATUS_CONFLICT) { if (response->dataLength) throw DropboxException(response->pData); throw DropboxException(HttpStatusToText((HTTP_STATUS)response->resultCode)); } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) throw DropboxException(HttpStatusToText(HTTP_STATUS_ERROR)); JSONNode error = root.at("error_summary"); if (error.empty()) return; throw DropboxException(error.as_string().c_str()); }
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, _countof(requestToken)); GetAccessTokenRequest request(requestToken); NLHR_PTR response(request.Send(instance->hNetlibConnection)); if (response == NULL || response->resultCode != HTTP_STATUS_OK) { 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; } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) { Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, HttpStatusToText((HTTP_STATUS)response->resultCode)); if (hwndDlg) SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("server does not respond")); /*else ShowNotification((TCHAR*)error_description, MB_ICONERROR);*/ return 0; } JSONNode node = root.at("error_description"); if (node != JSONNULL) { ptrT error_description(mir_a2t_cp(node.as_string().c_str(), CP_UTF8)); 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; } node = root.at("access_token"); db_set_s(NULL, MODULE, "TokenSecret", node.as_string().c_str()); 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_ONLINE) db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE); } try { RequestAccountInfo(instance); } catch (DropboxException &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; }