-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisabot.cpp
More file actions
402 lines (351 loc) · 12.7 KB
/
Copy pathisabot.cpp
File metadata and controls
402 lines (351 loc) · 12.7 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
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
#include <arpa/inet.h>
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <getopt.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <regex>
#include <vector>
#include <ctime>
#include <netinet/in.h>
/* IMPORTANT
Parts of program marked with "MARK" were inspired and modified from StackOverflow post:
https://stackoverflow.com/questions/41229601/openssl-in-c-socket-connection-https-client
They were posted by user "O.logN".
These parts of program mainly concern setuping SSL Library.
*/
int RecvPacket(int sw);
int SendPacket(const char *req);
using namespace std;
/* Class containing information about one message in discord channel */
class Message {
public:
string username;
string content;
string timestamp;
string msgID;
Message(string u,string c,string t, string id){
username = u;
content = c;
timestamp = t;
msgID = id;
}
/* Function that compares if two messages are identical */
bool are_Identical(Message b){
if((username == b.username) && (content == b.content) && (timestamp == b.timestamp) && (msgID == b.msgID)){
return true;
}
return false;
}
};
/* Global variables */
SSL *ssl;
int sock;
vector<Message> msg_vect;
vector<string> str_vect;
bool loaded = false;
string tok;
string channel = "";
string channel_name = "";
bool verbose = false;
bool too_many_requests = false;
/* Segments answer recieved from GET request to array of messages */
vector<string> retrieveMessages(string s){
vector<string> s_array;
s = s.substr(s.find("{\"id"));
string delimiter = "}, {";
size_t last = 0;
size_t next = 0;
while ((next = s.find(delimiter, last)) != string::npos) {
s_array.push_back(s.substr(last, next-last));
last = next + 1;
}
s_array.push_back(s.substr(last, next-last));
reverse(s_array.begin(),s_array.end());
return s_array;
}
/* Parses messages and sends those, which were written in the channel after the bot was started */
void parseMessages(vector<string> vs){
for (int i=0; i<vs.size();i++){
/* Gets a message out of the array */
string message = vs[i];
smatch match;
/* Getting username with regex */
string start = "username\": \"";
string end = "\", \"avatar\"";
regex usrR ( start + "(.*)" + end);
regex_search(message,match,usrR);
string username = match[1].str();
/* Getting content with regex */
start = "\"content\": \"";
end = "\", \"channel";
regex cntntR ( start + "(.*)" + end);
regex_search(message,match,cntntR);
string content = match[1].str();
/* Getting timestamp with regex */
start = "timestamp\": \"";
end = "\", \"edited_timestamp";
regex tmstmpR ( start + "(.*)" + end);
regex_search(message,match,tmstmpR);
string timestamp = match[1].str();
/* Getting msgID with regex */
start = "\"id\": \"";
end = "\", \"type\"";
regex msgidR ( start + "(.*)" + end);
regex_search(message,match,msgidR);
string messageID = match[1].str();
/* If substring "bot" is contained in username - skip */
if(!strstr(username.c_str(), "bot")){
/* "loaded" - After first GET request is sent, this function only loads messages
* from chat history straight to the vector "msg_vect" for comparasion purposes ("loaded" - false)
* then "flips" "loaded" to true, so from then, it can compare new messages to chat history
*/
if (loaded == true){
/* Creating message object */
Message msg(username,content,timestamp, messageID);
bool is_Contained = false;
/* Comparing new message with those in the "msg_vect" */
for (int i=0; i<msg_vect.size(); i++){
if(msg.are_Identical(msg_vect[i])){
is_Contained = true;
}
}
/* If the message is not contained in vector, create POST request and send it to discord server */
if (!is_Contained){
/* Push message to the vector, so program knows it has been processed */
msg_vect.push_back(msg);
string request = "POST /api/v6/channels/"+channel+"/messages HTTP/1.1\r\nHost: discord.com\r\nAuthorization: Bot "+tok+"\r\nContent-Type: application/json\r\nContent-Length:";
request += to_string(msg.username.length() + msg.content.length()+25);
request += "\r\n\r\n{\"content\": \"";
request += "echo: " + msg.username + " - " + msg.content + "\"}\r\n\r\n";
if(msg.content.length() != 0){
/* Sending POST request with message to the discord server */
sleep(1);
SendPacket(request.c_str());
/* If verbose flag has been set, print message to STDOUT */
if (verbose){
cout << channel_name + " - " + msg.username + ": " + msg.content + "\n";
}
}
}
/* "loaded - false", loading chat history into vector */
} else {
Message msg(username,content,timestamp, messageID);
msg_vect.push_back(msg);
}
}
}
/* Flipping loaded "switch" variable to true so program knows chat history has been loaded */
loaded = true;
}
/* Processes a packet, "sw" - variable that distinguishes between GET - chat, GET - guilds, GET - channels*/
int RecvPacket(int sw)
{
int change = 0;
int len=100;
char buf[1000000];
int HTMLH = 1369;
string s;
/* Loading SSL_read into string */
/* MARK */
do {
len=SSL_read(ssl, buf, HTMLH);
buf[len]=0;
s += buf;
if(len < 10){
break;
}
} while (len > 0);
/* Splitting the answer into messages GET - chat */
if(sw == 0){
str_vect = retrieveMessages(s);
/* Getting guild_id and further processing it to get channel_ID */
} else if(sw == 1) {
smatch match;
string start = "\"id\": \"";
string end = "\", \"name\"";
regex guild_idR ( start + "(.*)" + end);
regex_search(s,match,guild_idR);
string guild_id = match[1].str();
string request = "GET /api/v6/guilds/"+guild_id+"/channels HTTP/1.1\r\nHost: discord.com\r\nAuthorization: Bot "+tok+"\r\n\r\n";
SendPacket(request.c_str());
RecvPacket(2);
/* Getting channel_ID of channel with name "isa-bot", assigning channel name and channel_ID to global variables */
} else if(sw == 2) {
/* Segmenting individual channels to find the one with name "isa-bot" */
vector<string> v = retrieveMessages(s);
smatch match;
/* Searching for desired channel "isa-bot" */
for (int i = 0; i < v.size(); i++){
/* Getting whole part that needs to be processed from message about channel */
string start = "\"id\": \"";
string end = "\"name\": \"isa-bot\"";
regex stgR ( start + "(.*)" + end);
regex_search(v[i],match,stgR);
string tmp = match.str();
/* Getting channel name */
start = "\"name\": \"";
end = "\", \"position";
regex channel_nameR ( start + "(.*)" + end);
regex_search(v[i],match,channel_nameR);
channel_name = match[1].str();
/* Getting channel_ID */
start = "\"id\": \"";
end = "\", \"last_message";
regex channel_idR ( start + "(.*)" + end);
regex_search(tmp,match,channel_idR);
channel = match[1].str();
/* Regex hit! Break out of the loop */
if (channel != ""){
break;
}
}
}
/* Checking for reading errors */
/* MARK */
if (len < 0) {
int err = SSL_get_error(ssl, len);
if (err == SSL_ERROR_WANT_READ)
return 0;
if (err == SSL_ERROR_WANT_WRITE)
return 0;
if (err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL)
return -1;
}
return 0;
}
/* Sending a request */
/* MARK */
int SendPacket(const char *req)
{
int len = SSL_write(ssl, req, strlen(req));
if (len < 0) {
int err = SSL_get_error(ssl, len);
switch (err) {
case SSL_ERROR_WANT_WRITE:
return 0;
case SSL_ERROR_WANT_READ:
return 0;
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
case SSL_ERROR_SSL:
default:
return -1;
}
}
return 0;
}
/* Printing help */
void printhelp(){
printf("This program loads recieved message from isa-bot discord channel and echoes them back.\n");
printf("Usage: ./isabot [-v|--verbose] for printing messages sent also to STDOUT, -t <bot_access_token> token of your bot\n");
exit(0);
}
int main(int argc, char *argv[])
{
/**************************/
/* Resolving arguments */
/**************************/
if(argc < 2){
printhelp();
}
/* Struct for longargs */
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 't'}
};
char ch;
char* token = NULL;
/* Loading and setting arguments */
while ((ch = getopt_long(argc, argv, "vht:", long_options, NULL)) != -1){
switch (ch){
case 't':
token = optarg;
tok = token;
break;
case 'h':
printhelp();
case 'v':
verbose = true;
break;
case '?':
cout << "UKNOWN ARGUMENT!";
exit(1);
default:
cout << "UKNOWN ARGUMENT!";
exit(1);
}
}
/*************************/
/* Setting up connection */
/* MARK */
/*************************/
/* Setting up socket with Discord IP */
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
printf("Error creating socket.\n");
return -1;
}
struct sockaddr_in sa;
memset (&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("162.159.128.233");
sa.sin_port = htons (443);
socklen_t socklen = sizeof(sa);
if (connect(s, (struct sockaddr *)&sa, socklen)) {
printf("Error connecting to server.\n");
return -1;
}
/* Initializing SSL_library */
SSL_library_init();
SSLeay_add_ssl_algorithms();
SSL_load_error_strings();
/* Defining function for different systems */
#if defined(LWS_HAVE_TLS_CLIENT_METHOD)
const SSL_METHOD *meth = (SSL_METHOD *)TLS_client_method();
#elif defined(LWS_HAVE_TLSV1_2_CLIENT_METHOD)
const SSL_METHOD *meth = (SSL_METHOD *)TLSv1_2_client_method();
#else
const SSL_METHOD *meth = (SSL_METHOD *)SSLv23_client_method();
#endif
/* Creating SSL */
SSL_CTX *ctx = SSL_CTX_new (meth);
ssl = SSL_new (ctx);
if (!ssl) {
printf("Error creating SSL.\n");
return -1;
}
/* Creating SSL connection */
SSL_set_fd(ssl, s);
int err = SSL_connect(ssl);
if (err <= 0) {
printf("Error creating SSL connection. err=%x\n", err);
fflush(stdout);
return -1;
}
/*****************************************/
/* Communicating with Discord REST API */
/****************************************/
/* Assigns channel_ID to global variable "channel" and channel name to global variable "channel_name". */
string request = "GET /api/v6/users/@me/guilds HTTP/1.1\r\nHost: discord.com\r\nAuthorization: Bot "+tok+"\r\n\r\n";
SendPacket(request.c_str());
RecvPacket(1);
/* Core while that infinitely loops, gets new messages from chat and echoes them back. */
while(true){
string request = "GET /api/v6/channels/"+channel+"/messages HTTP/1.1\r\nHost: discord.com\r\nAuthorization: Bot "+tok+"\r\n\r\n";
SendPacket(request.c_str());
RecvPacket(0);
parseMessages(str_vect);
sleep(1);
}
return 0;
}