-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_socket.c
More file actions
639 lines (535 loc) · 16.1 KB
/
t_socket.c
File metadata and controls
639 lines (535 loc) · 16.1 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include "utils.h"
#include "config.h"
#include "irc_message.h"
#include "user.h"
#include "message.h"
#include "channel.h"
#include "tag.h"
#define MAX_RECV_SIZE 512 // 512B
#define FULL_BUFFER_SIZE 2 * 50 // 50KB
static int sock = 0, running = 0, connected = 0, motd = 0;
static struct sockaddr_in serv_addr;
static size_t bytes_recv = 0, full_buffer_size = 0;
static char recv_buffer[MAX_RECV_SIZE] = {0};
static char full_buffer[FULL_BUFFER_SIZE * MAX_RECV_SIZE];
/**
* @brief Send raw string through socket
*
* @param raw
* @return int
*/
int send_raw(char* raw) {
if(!sock) return -1;
printf("%s>%s Sending %s", GRN, RESET, raw);
return send(sock, raw, strlen(raw), 0);
}
/**
* @brief Convert IRC string to IRC type
*
* @param raw
* @return char*
*/
char* irc_2_type(char* raw) {
// We must make sure PRIVMSG is not part of any other type
// since it allows for "code injection".
if(!strstr(raw, "PRIVMSG") == 0) return "PRIVMSG";
if(!strstr(raw, "JOIN") == 0 && strstr(raw, "PRIVMSG") == 0) return "JOIN";
if(!strstr(raw, "PART") == 0 && strstr(raw, "PRIVMSG") == 0) return "PART";
if(!strstr(raw, "CAP") == 0 && strstr(raw, "PRIVMSG") == 0) return "CAP";
if(!strstr(raw, "USERNOTICE") == 0 && strstr(raw, "PRIVMSG") == 0) return "USERNOTICE";
if(!strstr(raw, "ROOMSTATE") == 0 && strstr(raw, "PRIVMSG") == 0) return "ROOMSTATE";
if(!strstr(raw, "USERSTATE") == 0 && strstr(raw, "PRIVMSG") == 0) return "USERSTATE";
if(!strstr(raw, "CLEARCHAT") == 0 && strstr(raw, "PRIVMSG") == 0) return "CLEARCHAT";
if(!strstr(raw, "CLEARMSG") == 0 && strstr(raw, "PRIVMSG") == 0) return "CLEARMSG";
if(!strstr(raw, "HOSTTARGET") == 0 && strstr(raw, "PRIVMSG") == 0) return "HOSTTARGET";
if(!strstr(raw, "CTCP") == 0 && strstr(raw, "PRIVMSG") == 0) return "CTCP";
if(!strstr(raw, "RECONNECT") == 0 && strstr(raw, "PRIVMSG") == 0) return "RECONNECT";
if(!strstr(raw, "NOTICE") == 0 && strstr(raw, "PRIVMSG") == 0) return "NOTICE";
return "NA";
}
/**
* @brief Join channel
*
* @param channel
*/
void join_channel(char* channel) {
char buffer[1024];
sprintf(buffer, "JOIN %s\r\n", channel);
add_channel(channel);
send_raw(buffer);
}
/**
* @brief Part channel
*
* @param name
*/
void part_channel(char* name) {
char buffer[1024];
struct channel_t* channel = get_channel(name);
sprintf(buffer, "PART %s\r\n", name);
destroy_channel(channel);
send_raw(buffer);
}
/**
* @brief Send channel message
*
* @param channel
* @param message
*/
void send_channel_message(struct channel_t* channel, char message[500]) {
char buffer[1024];
sprintf(buffer, "PRIVMSG %s : %s\t\n", channel->name, message);
send_raw(buffer);
}
/**
* @brief Connect to chat server
*
* @param host
* @param port
* @return int
*/
int conn(char* host, int port) {
// Initalize socket
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
printf("Created socket\n");
// Convert IP to binary form
if(inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) {
printf("Failed to convert IP to binary form\n");
return -1;
}
// Connect socket to host
if(connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("Connection failed\n");
return -1;
}
printf("Connected!\n");
return 1;
}
/**
* @brief Check if ID is location in LINE
*
* @param line
* @param id
* @return int
*/
int received_id(char* line, int id) {
char* copy = strdup(line);
char* token;
char* result;
int count = 0;
int received_id = 0;
for(token = strtok_r(copy, " ", &result); token != 0; token = strtok_r(0, " ", &result)) {
if(count != 1) {
count++;
continue;
}
received_id = atoi(token);
break;
}
free(copy);
return received_id == id;
}
/**
* @brief Get the username from line
*
* @param line
* @return char*
*/
char* get_username_from_line(char* line) {
char* username_result;
char* username = strtok_r(line, "!", &username_result);
memmove(username, username + 1, strlen(username));
return username;
}
/**
* @brief Handle CAP
*
*/
void handle_cap() {
// printf("Received good ACK\n");
}
/**
* @brief Handle JOIN
*
* @param raw
*/
void handle_join(char* raw) {
char* token;
char* result;
char username_str[30];
char channel_str[30];
int count = 0;
for(token = strtok_r(raw, " ", &result); token != 0; token = strtok_r(0, " ", &result)) {
switch(count) {
// Username and Host
case 0:
sprintf(username_str, "%s", get_username_from_line(token));
break;
// Do nothing
case 1:
break;
// Channel
case 2:
sprintf(channel_str, "%s", token);
break;
}
count++;
}
if(!strcmp(username_str, get_config_value("username"))) return;
struct channel_t* channel = get_channel(channel_str);
struct user_t* user = create_user(channel, username_str);
printf(RESET "[" GRN "%s" RESET "/" GRN "%s" RESET "] - " GRN "Joined\n" RESET, channel_str, username_str);
free(user);
}
/**
* @brief Handle PART
*
* @param raw
*/
void handle_part(char* raw) {
char* token;
char* result;
char username_str[30];
char channel_str[30];
int count = 0;
for(token = strtok_r(raw, " ", &result); token != 0; token = strtok_r(0, " ", &result)) {
switch(count) {
// Username and Host
case 0:
sprintf(username_str, "%s", get_username_from_line(token));
break;
// Do nothing
case 1:
break;
// Channel
case 2:
sprintf(channel_str, "%s", token);
break;
}
count++;
}
if(!strcmp(username_str, get_config_value("username"))) return;
struct channel_t* channel = get_channel(channel_str);
struct user_t* user = create_user(channel, username_str);
printf(RESET "[" GRN "%s" RESET "/" GRN "%s" RESET "] - " RED "Parted\n" RESET, channel_str, username_str);
free(user);
}
/**
* @brief Handle PRIVMSG
*
* @param raw
*/
void handle_privmsg(char* raw) {
int building = 0;
char message_buffer[3 * 1024] = {0}; // 3KB
size_t message_buffer_size = 0;
char tag_str[500];
char username_str[30];
char channel_str[30];
char* token;
char* result;
int count = 0;
struct tag_header_t* tag_header = 0;
// Grab information for private message
for(token = strtok_r(raw, " ", &result); token != 0; token = strtok_r(0, " ", &result)) {
switch(count) {
// Tag
case 0:
sprintf(tag_str, "%s", token);
// tag_header = create_tag(tag_str);
break;
// Sender
case 1:
sprintf(username_str, "%s", get_username_from_line(token));
break;
// Do nothing (irc_type)
case 2:
break;
// Channel
case 3:
sprintf(channel_str, "%s", token);
break;
default:
if(!building) {
building = 1;
// Create a copy and add a space
char token_copy[strlen(token) + 1]; // + 1 for additional space
strcpy(token_copy, token);
strcat(token_copy, " ");
// Copy over first word into message buffer
memcpy(message_buffer, token_copy, sizeof(token_copy));
message_buffer_size += sizeof(token_copy);
} else {
// Create a copy and add a space
char token_copy[strlen(token) + 1];
strcpy(token_copy, token);
strcat(token_copy, " ");
// Copy over any additional words into message buffer
memcpy(message_buffer + message_buffer_size, token_copy, sizeof(token_copy));
message_buffer_size += sizeof(token_copy);
}
break;
}
count++;
}
memmove(message_buffer, message_buffer + 1, strlen(message_buffer)); // Remove ":"
message_buffer[message_buffer_size - 1] = '\0';
struct channel_t* channel = get_channel(channel_str);
struct user_t* sender = create_user(channel, username_str);
// If we do not free these users eventually, we will fill up our heap!!!
struct message_t message_block = create_message(channel, sender, message_buffer);
print_message_block(&message_block);
// printf("============================\n");
// printf("The tag:\n");
// print_tag_header(tag_header);
// printf("============================\n");
// destroy_tag_header(tag_header);
// Clear buffers
memset(message_buffer, 0, sizeof(message_buffer));
free(sender);
}
/**
* @brief Handle USERNOTICE
*
* @param raw
*/
void handle_usernotice(char* raw) {
printf("Usernotice: %s\n", raw);
}
/**
* @brief Handle CLEARCHAT
*
* @param raw
*/
void handle_clearchat(char* raw) {
char tag_str[500];
char username_str[30];
char channel_str[30];
char* token;
char* result;
int count = 0;
// Grab information for private message
for(token = strtok_r(raw, " ", &result); token != 0; token = strtok_r(0, " ", &result)) {
switch(count) {
// Tag
case 0:
sprintf(tag_str, "%s", token);
// struct tag_header_t* tag = create_tag(tag_str);
break;
// Sender
case 1:
break;
// Do nothing (irc_type)
case 2:
break;
// Channel
case 3:
sprintf(channel_str, "%s", token);
break;
case 4:
sprintf(username_str, "%s", token);
memmove(username_str, username_str + 1, strlen(username_str));
break;
}
count++;
}
printf("CLEARCHAT [%s:%s]\n", channel_str, username_str);
}
/**
* @brief Handle CLEARMSG
*
* @param raw
*/
void handle_clearmsg(char* raw) {
printf("Clearmsg: %s\n", raw);
}
/**
* @brief Handle HOSTTARGET
*
* @param raw
*/
void handle_hosttarget(char* raw) {
printf("Host Target: %s\n", raw);
}
/**
* @brief Handle PING
*
*/
void handle_ping() {
mark(20);
send_raw("PONG :tmi.twitch.tv\r\n");
}
/**
* @brief Handle connection
*
* @param message
*/
void handle(struct irc_message_t message) {
// Checks
if(!connected) {
connected = received_id(message.data, 1);
join_channel("#xqcow");
join_channel("#illojuan");
join_channel("#elweroking");
join_channel("#casimito");
join_channel("#loltyler1");
join_channel("#montanablack88");
}
if(!motd) {
motd = received_id(message.data, 376);
return;
}
char* irc_type = irc_2_type(message.data);
// printf("Received: %s\n", irc_type);
// printf("< %s\n", raw);
if(!strcmp("PRIVMSG", irc_type)) {
handle_privmsg(message.data);
} else if(!strcmp("JOIN", irc_type)) {
handle_join(message.data);
// printf("< %s\n\n\n", raw);
} else if(!strcmp("PART", irc_type)) {
handle_part(message.data);
} else if(!strcmp("PING", irc_type)) {
handle_ping();
} else if(!strcmp("CAP", irc_type)) {
handle_cap();
} else if(!strcmp("USERNOTICE", irc_type)) {
handle_usernotice(message.data);
} else if(!strcmp("NOTICE", irc_type)) {
// handle_privmsg(raw);
} else if(!strcmp("CLEARCHAT", irc_type)) {
handle_clearchat(message.data);
} else if(!strcmp("CLEARMSG", irc_type)) {
handle_clearmsg(message.data);
} else if(!strcmp("HOSTTARGET", irc_type)) {
handle_hosttarget(message.data);
} else if(!strcmp("CTCP", irc_type)) {
// handle_privmsg(raw);
} else if(!strcmp("RECONNECT", irc_type)) {
// handle_privmsg(raw);
}
}
/**
* @brief Receive full application message
*
* @param more
*/
void receive_full_chunk(int* more) {
// Clear the recv_buffer before reading in more
memset(recv_buffer, 0, sizeof(recv_buffer));
if((bytes_recv = recv(sock, recv_buffer, sizeof(recv_buffer), 0)) > 0) {
int newline_flag = recv_buffer[bytes_recv - 1] == '\n';
int carriage_flag = recv_buffer[bytes_recv - 2] == '\r';
memmove(full_buffer + full_buffer_size, recv_buffer, bytes_recv * sizeof(char));
full_buffer_size += bytes_recv;
if(newline_flag && carriage_flag) {
*more = 0;
}
}
}
/**
* @brief Thread body for starting connection
*
* @param vargs
* @return void*
*/
void* thread_start(void *vargs) {
int connection = conn("44.226.36.141", 6667);
char* password;
char* username;
char* nickname;
asprintf(&password, "PASS %s\r\n", get_config_value("oauth_key"));
asprintf(&nickname, "NICK %s\r\n", get_config_value("username"));
asprintf(&username, "USER %s\r\n", get_config_value("username"));
// Auth
send_raw(password);
send_raw(nickname);
send_raw(username);
free(password);
free(username);
free(nickname);
// Caps
send_raw("CAP REQ :twitch.tv/commands\r\n");
send_raw("CAP REQ :twitch.tv/tags\r\n");
send_raw("CAP REQ :twitch.tv/membership\r\n");
if(connection < 0) {
printf("Error calling conn()\n");
}
int more_flag = 0;
// Clear recv_buffer
memset(recv_buffer, 0, sizeof(recv_buffer));
// Sleep before reading data to allow server to process authorization and CAPS
sleep(1);
while(running) {
// Read in a stream of up to 512B
if((bytes_recv = recv(sock, recv_buffer, sizeof(recv_buffer), 0)) > 0) {
if(bytes_recv <= 0) {
printf("Connection aborted\n");
break;
}
// Check if our buffer ends with /r/n
int newline_flag = recv_buffer[bytes_recv - 1] == '\n';
int carriage_flag = recv_buffer[bytes_recv - 2] == '\r';
// If it does not, dump the recv buffer into the full_buffer
// and continue loading more data until /r/n
if(!(newline_flag && carriage_flag)) {
memmove(full_buffer, recv_buffer, bytes_recv * sizeof(char));
full_buffer_size += bytes_recv;
more_flag = 1;
while(more_flag) {
receive_full_chunk(&more_flag);
}
} else {
// We have /r/n, copy the recv_buffer to the full_buffer
memmove(full_buffer, recv_buffer, bytes_recv * sizeof(char));
}
// Add null termination
full_buffer[full_buffer_size - 1] = '\0';
full_buffer_size = 0;
char* token;
char* result;
for(token = strtok_r(full_buffer, "\r\n", &result); token != 0; token = strtok_r(0, "\r\n", &result)) {
struct irc_message_t irc_message = create_irc_message(token);
handle(irc_message);
}
// Clear memory when we are done
memset(full_buffer, 0, sizeof(full_buffer));
memset(recv_buffer, 0, sizeof(recv_buffer));
}
}
printf("Closing socket\n");
if(!close(sock)) {
printf("Socket closed\n");
} else {
printf("Error closing socket");
}
}
/**
* @brief Connect to Twitch's chat server
*
* @return int
*/
int connect_to_twitch() {
// Socket thread identifier
pthread_t socket_thread;
running = 1;
printf("Creating socket thread\n");
if(pthread_create(&socket_thread, 0, thread_start, 0) != 0) {
printf("Could not create socket thread!!!\n");
return 0;
}
pthread_join(socket_thread, 0);
return 1;
}