forked from wipedlifepotato/RusNetBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.c
431 lines (395 loc) · 11.6 KB
/
irc.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
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
#include"irc.h"
#include"curl.h"
SSL_CTX*
InitCTX(void)
{
SSL_METHOD *method;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSL_load_error_strings(); /* Bring in and register error messages */
method = TLSv1_2_client_method(); /* Create new client-method instance */
ctx = SSL_CTX_new(method); /* Create new context */
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
return ctx;
}
void
ShowCerts(SSL* ssl)
{
X509 *cert;
char *line;
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( cert != NULL )
{
printf("Server certificates:\n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %s\n", line);
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %s\n", line);
free(line); /* free the malloc'ed string */
X509_free(cert); /* free the malloc'ed certificate copy */
}
else
printf("Info: No client certificates configured.\n");
}
struct IRCConnection
OpenConnection(const char *hostname, int port, bool useSSL)
{
struct IRCConnection ret;
if(useSSL)
if(!ssl_inited){
SSL_library_init();
ssl_inited=true;
}
struct hostent *host;
struct sockaddr_in addr;
if ( (host = gethostbyname(hostname)) == NULL )
{
perror(hostname);
abort();
}
ret.socket = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *(long*)(host->h_addr);
struct timeval timeout;
timeout.tv_sec=320;
timeout.tv_usec=0;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(ret.socket, &rfds);
if( select(ret.socket+1,&rfds,NULL,NULL,&timeout) == -1 ){
perror("select() timeout");
}
if ( connect(ret.socket, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
{
close(ret.socket);
perror(hostname);
//abort();
}
ret.socket=ret.socket;
if(useSSL){
ret.ctx = InitCTX();
ret.ssl_socket = SSL_new(ret.ctx); /* create new SSL connection state */
SSL_set_fd(ret.ssl_socket, ret.socket); /* attach the socket descriptor */
if ( SSL_connect(ret.ssl_socket) == FAIL ){ /* perform the connection */
ERR_print_errors_fp(stderr);
perror(hostname);
freeConnect(&ret);
struct IRCConnection noinfo;;
return noinfo;
}
ShowCerts(ret.ssl_socket); /* get any certs */
ret.isSSLConnection=true;
}//
ret.host=malloc( strlen(hostname)+1 * sizeof(char) );
memcpy(ret.host, hostname, strlen(hostname));
ret.host[strlen(hostname)]='\0';
ret.port = port;
return ret;
}
void irc_sendMsg(ircc c, const char * msg){
if(c.isSSLConnection)
SSL_write(c.ssl_socket,msg, strlen(msg));
else
write(c.socket, msg, strlen(msg));
}
size_t irc_recvMsg(ircc c, char * buf, size_t n){
if(c.isSSLConnection){
return SSL_read(c.ssl_socket, buf, n);
}
//puts("Read");
return read(c.socket, buf, n);
}
void
pingIfNeed(ircc c, const char * last_buf){
char * pPing;
if( (pPing=strstr(last_buf, "PING :")) != 0 ){
char buf[BUF_SIZE];
//bzero(buf,sizeof(BUF_SIZE));
size_t bufc=0;
void *sBuf = buf;
//size_t size_pong;
//puts("PING - PONG");
//PING :ZASDFDFSDF\n
pPing += 6;
//printf("%s\n",pPing);
//char * tmp = pPing;
while(*(pPing) != '\n' && *(pPing) != '\0'){
buf[bufc++]=*(pPing++);
}
buf[bufc++]='\0';
if(*pPing !='\n') return;
printf("buf: %s\n",buf);
char pongLine[sizeof("PONG :")+bufc+sizeof("\r\n")+1];
sprintf(pongLine, "PONG :%s\r\n", buf);
printf("buf: %s\n",pongLine);
irc_sendMsg(c, pongLine);
}//if found PING
}
void
regOnServ(ircc c, const char * nick, const char * username, const char * realname){
char buf[BUF_SIZE];
sprintf(buf, "NICK %s\r\nUSER %s * * %s\r\n", nick, username, realname);
irc_sendMsg(c, buf);
if (irc_recvMsg(c,buf,BUF_SIZE)){
pingIfNeed(c, buf);
puts("Connected to server");
}// if recvMsg
//puts(buf);
}
void
joinChn(ircc c, const char * channel, ...){
va_list ap;
char buf[BUF_SIZE];
va_start(ap, channel);
while(*channel && *channel =='#'){
sprintf(buf,"JOIN %s\r\n",channel);
irc_sendMsg(c,buf);
printf("JOIN TO %s\n", channel);
channel = va_arg(ap, char*);
}
va_end(ap);
}
char **
split_msg(char * buf, const char schar, size_t * splitted_size){
/*
*/
const unsigned long long max_splitted = 120;
size_t arr_size=0;
char **splitted;
splitted = (char**)malloc( sizeof(char*) * arr_size );
if(!splitted) abort();
char * str;
char str2[BUF_SIZE];
do{
bzero(str2, BUF_SIZE);
str = strchr(buf, schar);
if(str != NULL || ( str = strchr(buf, '\r') )!=NULL ){
arr_size++;
splitted = (char**)realloc( splitted,sizeof(char*) * arr_size );
if(!splitted) abort();
memcpy(str2, buf, (str-buf));
//printf("str2 = %s\n", str2);
splitted[arr_size-1] = (char*)malloc( sizeof(char) * strlen(str2) + 1);
if( !splitted[arr_size-1] ) abort();
strcpy(splitted[arr_size-1], str2);
splitted[arr_size-1][strlen(str2)]=0;
}
buf=str+1;
}while(str != NULL && arr_size < max_splitted);
*splitted_size=arr_size;
return splitted;
}
void
free_splitted(char ** what, size_t n){
for(n;n--;){
if(n == 0) break;
if( what[n][0] == 0 ) continue;
free((void*) (what[n-1]) );
}
free((void*)what);
}
void
PRIVMSG(ircc c, const char * chnusr, const char * msg, const unsigned short color){
char buf[BUF_SIZE];
bzero(buf, BUF_SIZE);
#define COLOR_CHAR ""
char color_buf[4];
bzero(color_buf, sizeof(color_buf));
if(color != 0)
sprintf(color_buf, COLOR_CHAR"%d", color);
else
sprintf(color_buf, COLOR_CHAR"");
sprintf(buf,"PRIVMSG %s :%s%s\r\n",chnusr, color_buf, msg);
printf("buf: %s\n", buf);
irc_sendMsg(c,buf);
}
unsigned long long _strlen_without_space(void * buf){
unsigned long long s=0;
char * b=buf;
while(*(b++)){
if(*b == ' ') continue;
s++;
}
return s;
}
void
welcome_handler(ircc c, const char ** splitted, size_t splitted_size, bool isJoin){
const char *sender, *channel;
sender=splitted[0];
// sender=sender+1;
channel=splitted[2];
// channel=channel+1;
size_t sChannel = strlen(channel);
char channel_[sChannel+1];
if(isJoin)
strcpy(channel_,channel+1);
else
strcpy(channel_, "#ru");
for(unsigned int i = sChannel-1;i--;){
if(i == sChannel-1) channel_[i] = '\0';
if( channel_[i]== '\n' || channel_[i] == '\r' ) {
channel_[i]='\0';
break;
}
}
char nickSender[BUF_SIZE];
char buf[BUF_SIZE];
bzero(nickSender, sizeof(nickSender));
memcpy(nickSender, (sender+1), (strstr(sender,"!") -(sender+1)));
if(isJoin)
sprintf(buf,"%s, добро пожаловать на канал :)\n", nickSender);
else
sprintf(buf,"%s, прощай...Это всё из-за темных...\n", nickSender);
printf("Sender: %s\n channel:%s\n",nickSender,channel_);
printf("buf:%s \n",buf);
PRIVMSG(c, channel_, buf, 2);
}
void
msg_handler(ircc c, const char ** splitted, size_t splitted_size){
/*if(!regex_inited){
if(regcomp(®ex_url, URL_REGEX, 0) != 0) abort();
}*/
const char *sender, *channel;
sender=splitted[0];
channel=splitted[2];
splitted[3] = splitted[3]+1;
char nickSender[BUF_SIZE];
bzero(nickSender, sizeof(nickSender));
memcpy(nickSender, (sender+1), (strstr(sender,"!") -(sender+1)));
//printf("%s send msg in %s -> ", sender, channel);
#define UNALLOWED {return;}
size_t i = 0;
for(i=0; i < splitted_size;i++){
for(unsigned un = 0; un < UNALLOWED_DOMAIN_COUNT;un++){
//printf("i: %d;un:%d\n",i,un);
if( strstr(splitted[i], unallowed_hosts[un]) != NULL){
splitted[3] = splitted[3] - 1;
UNALLOWED;
}
}
}
i = 3;
char buf[BUF_SIZE];
while( i < splitted_size ){
//printf("%s %d\n", splitted[i], i);
//if( regexec(®ex_url, splitted[i], 0, NULL, 0) == 0 ){
// printf("splitted_size: %d\n", splitted_size);
if( splitted[i] == 0 || splitted[i][0] == 0) {
printf("Warning splitted[%d] = 0!\n", i);
i++;
continue;
}
if(strstr(splitted[i], "http://") != NULL || strstr(splitted[i], "https://") != NULL ){
// if( strstr(splitted[i], "127.0.0.1") != NULL) UNALLOWED;
// if( strstr(splitted[i], "192.168.") != NULL) UNALLOWED;
//printf("Url: %s\n", splitted[i]);
char about_page[BUF_SIZE];
char buf[BUF_SIZE];
bzero(about_page,sizeof(about_page));
get_info_about_url(splitted[i], about_page);
if(about_page[0] == 0){
i++;
continue;
}
printf("About_Page: %s\n", about_page);
PRIVMSG(c, channel, about_page, 3);
}else if( (strstr(splitted[i], "!ac") != NULL) && i == 3){
i++;
bzero(buf, sizeof(buf));
while(i < splitted_size){
sprintf(buf, "%s %s", buf,splitted[i++]);
}
printf("%s\n", buf);
if(_strlen_without_space(buf) < 15) {
sprintf(buf, "%s, КоРоТкАя цитата какая-то, 15 символов над:(: !ac <цитата>", nickSender);
PRIVMSG(c, channel, buf, 4);
}
else if(addQuote(channel, nickSender, buf)){
sprintf(buf, "%s, цитата добавлена,у неё должен быть номер %d\n", nickSender, getQuotesLength());
PRIVMSG(c, channel, buf, 0);
}
}else if( (strstr(splitted[i], "!c") != NULL) && i == 3){
char buf[BUF_SIZE];
i++;
long long quote_count=getQuotesLength();
long long quote_id= ((i) < splitted_size) ? atoll(splitted[i]) : (rand() % quote_count)+1;
printf("!c %ld %ld\n", quote_count, quote_id);
if(quote_id == 0 || quote_id > quote_count || quote_count < 0){
sprintf(buf, "%s, номер цитаты дан не правильным должен быть от 1-%d",nickSender, quote_count);
PRIVMSG(c, channel, buf, 0);
break;
}
puts("Цитата найдена");
char cID[100];
sprintf(cID,"%ld",quote_id);
getQuote(cID, buf);
PRIVMSG(c, channel, buf, 0);
break;
}else if( (strstr(splitted[i], "!kubic") != NULL) && i == 3){
sprintf(buf, "%s, Кубик выдал число: %d", nickSender, (rand() % 7)+1);
PRIVMSG(c, channel, buf, 2);
break;
}else if( (strstr(splitted[i], "!source") != NULL) && i == 3){
sprintf(buf, "%s", "https://github.com/wipedlifepotato/RusNetBot");
PRIVMSG(c, channel, buf, 0);
break;
}else if( (strstr(splitted[i], "!help") != NULL) && i == 3){
sprintf(buf, "%s", "!ac <quote> !c <num-quote> !kubic !source ...");
PRIVMSG(c, channel, buf, 0);
break;
}
i++;
}
#undef UNALLOWED
splitted[3] = splitted[3]-1;
puts("");
}
void
recvHandler(ircc c){
srand(time(NULL));
char buf[BUF_SIZE];
size_t rcvBytes;
do{
bzero(buf, BUF_SIZE);
rcvBytes=irc_recvMsg(c, buf, BUF_SIZE);
if(buf[0] == 0){
break;
}
if(buf[0] == 0) break;
printf("Recv: %s\n", buf);
pingIfNeed(c,buf);
size_t size;
char ** splitted = split_msg(buf, ' ', &size);
//puts("Splitted:");
for(unsigned int i =0; i<size;i++){
//strcasecmp ignore case
if(strcmp(splitted[i], "PRIVMSG") == 0){
msg_handler(c, (const char**)splitted, size);
}//else if( i == 1 && (strcmp(splitted[i], "JOIN") == 0 || strcmp(splitted[i],"PART") == 0
// || strcmp(splitted[i], "QUIT") == 0 ) ){
// bool isJoin=false;
// if( splitted[i][0]=='J' ) isJoin=true;
//puts("Welcome Handler");
//welcome_handler(c, (const char**)splitted, size, isJoin);
//}
else if( strcmp(splitted[i], "ERROR") == 0 && i == 0) break;
//puts(splitted[i]);
}
free_splitted(splitted,size);
}while(rcvBytes);
}
void
freeConnect(struct IRCConnection * con){
if (con->isSSLConnection){
SSL_free(con->ssl_socket);
SSL_CTX_free(con->ctx);
}
close(con->socket);
}