-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.C
92 lines (68 loc) · 2.21 KB
/
test.C
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
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <termios.h>
#include <unistd.h>
#include "test.h"
/**
* Simple proof of concept - currently just gets user email/password and
* returns their token (as provided by Discord) which will be needed later
* when making any other request of the Discord API
*/
int main(int argc, char * argv[]) {
CURL *curl;
CURLcode err;
// get uname and pw
std::cout << "Please enter your email: ";
std::string email = "";
std::string pass = "";
getline(std::cin, email);
std::cout << "Please enter your password: ";
struct termios save, modify;
tcgetattr (STDIN_FILENO, &save);
modify = save;
modify.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSANOW, &modify) != 0)
return -1;
getline(std::cin, pass);
if (tcsetattr(STDIN_FILENO, TCSANOW, &save) != 0)
return -1;
// create CURL handle
curl = curl_easy_init();
if (curl) {
std::string JSONresult;
std::string post = "email=" + email + "&password=" + pass;
// Discord auth URL
curl_easy_setopt(curl, CURLOPT_URL, "https://discordapp.com/api/auth/login");
// uname and pw
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &JSONresult);
Json::Reader reader;
Json::Value result;
err = curl_easy_perform(curl);
bool successfulParse = reader.parse(JSONresult, result);
if (!successfulParse) {
std::cout << "Parsing error: " << reader.getFormattedErrorMessages();
exit(0);
}
if (err != CURLE_OK) {
std::cout << "Error: " << curl_easy_strerror(err);
exit(0);
}
std::string token;
if (result.isMember("token")) {
token = result.get("token", result).asString();
std::cout << "\nResult: " << token << "\n";
} else {
std::cout << "\nError: No token received, check your email and password!\n";
return 0;
}
curl_easy_cleanup(curl);
}
return 0;
}
size_t writeString(void * contents, size_t size, size_t nmemb, void * userp) {
((std::string *)userp)->append((char *) contents, size * nmemb);
return size * nmemb;
}