-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPastebinThread.cpp
More file actions
123 lines (101 loc) · 4.93 KB
/
Copy pathPastebinThread.cpp
File metadata and controls
123 lines (101 loc) · 4.93 KB
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
#include "PastebinThread.h"
#include "PastebinApi.h"
#include "CredentialManager.h"
#include <thread>
#include <memory>
#include <windows.h>
void pastebinUploadWorker(HWND hNppWnd,
std::string api_dev_key,
std::string paste_code,
std::string paste_name,
std::string paste_format,
std::string paste_private,
std::string paste_expire_date,
std::string api_user_key) {
auto result = std::make_unique<PasteResult>();
std::string response = postToPastebin(api_dev_key, paste_code, paste_name,
paste_format, paste_private, paste_expire_date, api_user_key);
if (response.rfind("http", 0) == 0) {
result->success = true;
result->message = response;
} else {
result->success = false;
result->message = response;
}
PostMessage(hNppWnd, WM_PASTEBIN_THREAD_FINISHED, (WPARAM)result.release(), 0);
}
void startPastebinUploadThread(HWND hNppWnd,
const std::string& api_dev_key,
const std::string& paste_code,
const std::string& paste_name,
const std::string& paste_format,
const std::string& paste_private,
const std::string& paste_expire_date,
const std::string& api_user_key) {
std::thread(pastebinUploadWorker, hNppWnd, api_dev_key, paste_code, paste_name,
paste_format, paste_private, paste_expire_date, api_user_key).detach();
}
void pastebinListWorker(HWND hNppWnd, std::string api_dev_key, std::string api_user_key) {
auto result = std::make_unique<ListResult>();
if (api_user_key.empty()) {
result->success = false;
result->error = "Not logged in. Please open Settings and click OK to login.";
PostMessage(hNppWnd, WM_PASTEBIN_LIST_FINISHED, (WPARAM)result.release(), 0);
return;
}
try {
result->pastes = listUserPastes(api_dev_key, api_user_key);
result->success = true;
} catch (...) {
result->success = false;
result->error = "Unknown error occurred while listing pastes.";
}
PostMessage(hNppWnd, WM_PASTEBIN_LIST_FINISHED, (WPARAM)result.release(), 0);
}
void startPastebinListThread(HWND hNppWnd, const std::string& api_dev_key, const std::string& api_user_key) {
std::thread(pastebinListWorker, hNppWnd, api_dev_key, api_user_key).detach();
}
void pastebinGetRawWorker(HWND hWnd, std::string devKey, std::string userKey, std::string pasteKey, PasteInfo meta) {
auto result = std::make_unique<RawResult>();
result->pasteKey = pasteKey;
result->meta = meta;
std::string response = getRawPaste(devKey, userKey, pasteKey);
if (!response.empty() &&
response.find("Bad API") == std::string::npos &&
response.find("Post limit") == std::string::npos) {
result->success = true;
result->content = response;
} else {
result->success = false;
result->error = response.empty()
? "Empty response from server. Check your connection and credentials."
: response;
}
PostMessage(hWnd, WM_PASTEBIN_GET_RAW_FINISHED, (WPARAM)result.release(), 0);
}
void startPastebinGetRawThread(HWND hWnd, const std::string& devKey, const std::string& userKey, const std::string& pasteKey, const PasteInfo& meta) {
std::thread(pastebinGetRawWorker, hWnd, devKey, userKey, pasteKey, meta).detach();
}
void pastebinDeleteWorker(HWND hWnd, std::string devKey, std::string userKey, std::string pasteKey) {
auto result = std::make_unique<DeleteResult>();
result->pasteKey = pasteKey;
std::string response = deletePaste(devKey, userKey, pasteKey);
if (response == "Paste Removed") {
result->success = true;
} else {
result->success = false;
result->error = response;
}
PostMessage(hWnd, WM_PASTEBIN_DELETE_FINISHED, (WPARAM)result.release(), 0);
}
void startPastebinDeleteThread(HWND hWnd, const std::string& devKey, const std::string& userKey, const std::string& pasteKey) {
std::thread(pastebinDeleteWorker, hWnd, devKey, userKey, pasteKey).detach();
}
static void silentLoginWorker(std::string devKey, std::string username, std::string password) {
std::string key = loginPastebin(devKey, username, password);
if (key.length() > 20 && key.find("Bad API") == std::string::npos)
storeCredential(PASTEBIN_CRED_USER_KEY, key);
}
void startSilentLoginThread(const std::string& devKey, const std::string& username, const std::string& password) {
std::thread(silentLoginWorker, devKey, username, password).detach();
}