Prevent wrapper.c from flooding the IRC server.
[iii.git] / ii.c
blob2c6d735857437977ac54344531df3d0b3e01d131
1 /* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMV-MMXI Nico Golde <nico at ngolde dot de>
3 * See LICENSE file for license details. */
4 #include <errno.h>
5 #include <netdb.h>
6 #include <netinet/in.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include <unistd.h>
18 #ifdef HAVE_SSL
19 #include <openssl/rand.h>
20 #include <openssl/ssl.h>
21 #include <openssl/err.h>
22 #endif
24 #ifndef PIPE_BUF /* FreeBSD don't know PIPE_BUF */
25 #define PIPE_BUF 4096
26 #endif
27 #define PING_TIMEOUT 300
28 #define SERVER_PORT 6667
29 #define IS_CHANNEL(s) (((s)[0]=='#')||((s)[0]=='&')||((s)[0]=='+')||((s)[0]=='!'))
30 #ifdef HAVE_SSL
31 #define SSL_SERVER_PORT 6697
32 #define WRITE(conn, msg) (use_ssl ? SSL_write(irc->sslHandle, msg, strlen(msg)) : write(conn->irc, msg, strlen(msg)))
33 #define READ(fd, buf, s) (from_srv && use_ssl ? SSL_read(irc->sslHandle, buf, s) : read(fd, buf, s))
34 #else
35 #define WRITE(irc, msg) (write(irc, msg, strlen(msg)))
36 #define READ(fd, buf, s) (read(fd, buf, s))
37 #endif
38 enum { TOK_NICKSRV = 0, TOK_USER, TOK_CMD, TOK_CHAN, TOK_ARG, TOK_TEXT, TOK_LAST };
40 typedef struct Channel Channel;
41 struct Channel {
42 int fd;
43 char *name;
44 Channel *next;
46 #ifdef HAVE_SSL
47 typedef struct {
48 int irc;
49 SSL *sslHandle;
50 SSL_CTX *sslContext;
51 } conn;
52 #endif
54 #ifdef HAVE_SSL
55 static size_t use_ssl = 0;
56 static conn *irc;
57 static unsigned char fp[EVP_MAX_MD_SIZE];
58 static int fp_len;
59 #else
60 static int irc;
61 #endif
62 static time_t last_response;
63 static Channel *channels = NULL;
64 static char *host = "irc.freenode.net";
65 static char nick[32] = "anonymous"; /* might change while running */
66 static char path[_POSIX_PATH_MAX];
67 static char message[PIPE_BUF]; /* message buf used for communication */
69 static void usage() {
70 fprintf(stderr, "%s",
71 "ii - irc it - " VERSION "\n"
72 "(C)opyright MMV-MMVI Anselm R. Garbe\n"
73 "(C)opyright MMV-MMXI Nico Golde\n"
74 "usage: ii [-i <irc dir>] [-s <host>] [-p <port>]\n"
75 " [-n <nick>] [-k <password>] [-f <fullname>]\n"
76 #ifdef HAVE_SSL
77 " [-e] [-d <directory>]\n");
78 #else
79 " [-d <directory>]\n");
80 #endif
81 exit(EXIT_SUCCESS);
84 static char *striplower(char *s) {
85 char *p = NULL;
86 for(p = s; p && *p; p++) {
87 if(*p == '/') *p = '_';
88 *p = tolower(*p);
90 return s;
93 /* creates directories top-down, if necessary */
94 static void create_dirtree(const char *dir) {
95 char tmp[256];
96 char *p = NULL;
97 size_t len;
99 snprintf(tmp, sizeof(tmp),"%s",dir);
100 len = strlen(tmp);
101 if(tmp[len - 1] == '/')
102 tmp[len - 1] = 0;
103 for(p = tmp + 1; *p; p++)
104 if(*p == '/') {
105 *p = 0;
106 mkdir(tmp, S_IRWXU);
107 *p = '/';
109 mkdir(tmp, S_IRWXU);
112 static int get_filepath(char *filepath, size_t len, char *channel, char *file) {
113 if(channel) {
114 if(!snprintf(filepath, len, "%s/%s", path, channel))
115 return 0;
116 create_dirtree(filepath);
117 return snprintf(filepath, len, "%s/%s/%s", path, channel, file);
119 return snprintf(filepath, len, "%s/%s", path, file);
122 static void create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
123 if(!get_filepath(filepath, len, striplower(channel), suffix)) {
124 fprintf(stderr, "%s", "ii: path to irc directory too long\n");
125 exit(EXIT_FAILURE);
129 static int open_channel(char *name) {
130 static char infile[256];
131 create_filepath(infile, sizeof(infile), name, "in");
132 if(access(infile, F_OK) == -1)
133 mkfifo(infile, S_IRWXU);
134 return open(infile, O_RDONLY | O_NONBLOCK, 0);
137 static void add_channel(char *cname) {
138 Channel *c;
139 int fd;
140 char *name = striplower(cname);
142 for(c = channels; c; c = c->next)
143 if(!strcmp(name, c->name))
144 return; /* already handled */
146 fd = open_channel(name);
147 if(fd == -1) {
148 printf("ii: exiting, cannot create in channel: %s\n", name);
149 exit(EXIT_FAILURE);
151 c = calloc(1, sizeof(Channel));
152 if(!c) {
153 perror("ii: cannot allocate memory");
154 exit(EXIT_FAILURE);
156 if(!channels) channels = c;
157 else {
158 c->next = channels;
159 channels = c;
161 c->fd = fd;
162 c->name = strdup(name);
165 static void rm_channel(Channel *c) {
166 Channel *p;
167 if(channels == c) channels = channels->next;
168 else {
169 for(p = channels; p && p->next != c; p = p->next);
170 if(p->next == c)
171 p->next = c->next;
173 free(c->name);
174 free(c);
177 static void login(char *key, char *fullname) {
178 if(key) snprintf(message, PIPE_BUF,
179 "PASS %s\r\nNICK %s\r\nUSER %s localhost * :%s\r\n", key,
180 nick, nick, fullname ? fullname : nick);
181 else snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost * :%s\r\n",
182 nick, nick, fullname ? fullname : nick);
183 WRITE(irc, message); /* login */
186 #ifdef HAVE_SSL
187 static conn *ssl_connect(int fd) {
188 conn *c = NULL;
190 c = malloc(sizeof(conn));
191 if(!c) {
192 perror("ii: cannot allocate memory");
193 exit(EXIT_FAILURE);
195 c->irc = fd;
197 if (use_ssl) {
198 c->sslHandle = NULL;
199 c->sslContext = NULL;
200 SSL_load_error_strings();
201 SSL_library_init();
202 c->sslContext = SSL_CTX_new(SSLv23_client_method());
203 if(c->sslContext == NULL)
204 ERR_print_errors_fp(stderr);
205 c->sslHandle = SSL_new(c->sslContext);
206 if(!SSL_set_fd(c->sslHandle, c->irc) || SSL_connect(c->sslHandle) != 1)
207 ERR_print_errors_fp(stderr);
208 if(!X509_digest(SSL_get_peer_certificate(c->sslHandle), EVP_md5(), fp, &fp_len))
209 ERR_print_errors_fp(stderr);
212 return c;
215 static conn *tcpopen(unsigned short port) {
216 #else
217 static int tcpopen(unsigned short port) {
218 #endif
219 int fd;
220 struct sockaddr_in sin;
221 struct hostent *hp = gethostbyname(host);
223 memset(&sin, 0, sizeof(struct sockaddr_in));
224 if(!hp) {
225 perror("ii: cannot retrieve host information");
226 exit(EXIT_FAILURE);
228 sin.sin_family = AF_INET;
229 memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
230 sin.sin_port = htons(port);
231 if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
232 perror("ii: cannot create socket");
233 exit(EXIT_FAILURE);
235 if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
236 perror("ii: cannot connect to host");
237 exit(EXIT_FAILURE);
240 #ifdef HAVE_SSL
241 return ssl_connect(fd);
242 #else
243 return fd;
244 #endif
247 static size_t tokenize(char **result, size_t reslen, char *str, char delim) {
248 char *p = NULL, *n = NULL;
249 size_t i;
251 if(!str)
252 return 0;
253 for(n = str; *n == ' '; n++);
254 p = n;
255 for(i = 0; *n != 0;) {
256 if(i == reslen)
257 return 0;
258 if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0) delim=':'; /* workaround non-RFC compliant messages */
259 if(*n == delim) {
260 *n = 0;
261 result[i++] = p;
262 p = ++n;
263 } else
264 n++;
266 if(i<reslen && p < n && strlen(p))
267 result[i++] = p;
268 return i; /* number of tokens */
271 static void print_out(char *channel, char *buf) {
272 static char outfile[256], server[256], buft[18];
273 FILE *out = NULL;
274 time_t t = time(0);
276 if(channel) snprintf(server, sizeof(server), "-!- %s", channel);
277 if(strstr(buf, server)) channel="";
278 create_filepath(outfile, sizeof(outfile), channel, "out");
279 if(!(out = fopen(outfile, "a"))) return;
280 if(channel && channel[0]) add_channel(channel);
282 strftime(buft, sizeof(buft), "%F %R", localtime(&t));
283 fprintf(out, "%s %s\n", buft, buf);
284 fclose(out);
287 static void proc_channels_privmsg(char *channel, char *buf) {
288 snprintf(message, PIPE_BUF, "<%s> %s", nick, buf);
289 print_out(channel, message);
290 snprintf(message, PIPE_BUF, "PRIVMSG %s :%s\r\n", channel, buf);
291 WRITE(irc, message);
294 static void proc_channels_input(Channel *c, char *buf) {
295 char *p = NULL;
297 if(buf[0] && buf[0] != '/') {
298 proc_channels_privmsg(c->name, buf);
299 return;
301 message[0] = '\0';
302 if(buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) {
303 case 'j':
304 if(buf[3] == ' ' || buf[3] == '\0') return;
305 p = strchr(&buf[3], ' ');
306 if(p) *p = 0;
307 if(IS_CHANNEL(&buf[3])){
308 if(p && strlen(p + 1)) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */
309 else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]);
310 add_channel(&buf[3]);
312 else if(p && strlen(p + 1)) {
313 add_channel(&buf[3]);
314 proc_channels_privmsg(&buf[3], p + 1);
315 return;
317 break;
318 case 't':
319 if(IS_CHANNEL(&buf[3])){
320 p = strchr(&buf[3], ' ');
321 if(p) *p = 0;
322 if(p && strlen(p + 1)) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", &buf[3], p + 1);
323 else snprintf(message, PIPE_BUF, "TOPIC %s\r\n", &buf[3]);
325 else {
326 if(c->name[0] == 0) return;
327 if(strlen(buf)>3) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]);
328 else snprintf(message, PIPE_BUF, "TOPIC %s\r\n", c->name);
330 break;
331 case 'a':
332 if(strlen(buf)>3){
333 snprintf(message, PIPE_BUF, "-!- %s is away \"%s\"", nick, &buf[3]);
334 print_out(c->name, message);
335 snprintf(message, PIPE_BUF, "AWAY :%s\r\n", &buf[3]);
336 } else snprintf(message, PIPE_BUF, "AWAY\r\n");
337 break;
338 case 'n':
339 if(strlen(buf)>3){
340 snprintf(nick, sizeof(nick),"%s", &buf[3]);
341 snprintf(message, PIPE_BUF, "NICK %s\r\n", &buf[3]);
343 break;
344 case 'l':
345 if(c->name[0] == 0) return;
346 if(strlen(buf)>3) snprintf(message, PIPE_BUF, "PART %s :%s\r\n", c->name, &buf[3]);
347 else snprintf(message, PIPE_BUF, "PART %s\r\n", c->name);
348 if(IS_CHANNEL(c->name)) WRITE(irc, message);
349 close(c->fd);
350 rm_channel(c);
351 return;
352 break;
353 default:
354 snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
355 break;
356 } else
357 snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
359 if (message[0] != '\0')
360 WRITE(irc, message);
363 static void proc_server_cmd(char *buf) {
364 char *argv[TOK_LAST], *cmd = NULL, *p = NULL;
365 int i;
367 if(!buf || *buf=='\0')
368 return;
370 for(i = 0; i < TOK_LAST; i++)
371 argv[i] = NULL;
372 /* <message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
373 <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
374 <command> ::= <letter> { <letter> } | <number> <number> <number>
375 <SPACE> ::= ' ' { ' ' }
376 <params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
377 <middle> ::= <Any *non-empty* sequence of octets not including SPACE
378 or NUL or CR or LF, the first of which may not be ':'>
379 <trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
380 <crlf> ::= CR LF */
382 if(buf[0] == ':') { /* check prefix */
383 if (!(p = strchr(buf, ' '))) return;
384 *p = 0;
385 for(++p; *p == ' '; p++);
386 cmd = p;
387 argv[TOK_NICKSRV] = &buf[1];
388 if((p = strchr(buf, '!'))) {
389 *p = 0;
390 argv[TOK_USER] = ++p;
392 } else
393 cmd = buf;
395 /* remove CRLFs */
396 for(p = cmd; p && *p != 0; p++)
397 if(*p == '\r' || *p == '\n')
398 *p = 0;
400 if((p = strchr(cmd, ':'))) {
401 *p = 0;
402 argv[TOK_TEXT] = ++p;
405 tokenize(&argv[TOK_CMD], TOK_LAST - TOK_CMD, cmd, ' ');
407 if(!argv[TOK_CMD] || !strncmp("PONG", argv[TOK_CMD], 5)) {
408 return;
409 } else if(!strncmp("PING", argv[TOK_CMD], 5)) {
410 snprintf(message, PIPE_BUF, "PONG %s\r\n", argv[TOK_TEXT]);
411 WRITE(irc, message);
412 return;
413 } else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */
414 snprintf(message, PIPE_BUF, "%s%s", argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
415 print_out(0, message);
416 return;
417 } else if(!strncmp("ERROR", argv[TOK_CMD], 6))
418 snprintf(message, PIPE_BUF, "-!- error %s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown");
419 else if(!strncmp("JOIN", argv[TOK_CMD], 5)) {
420 if(argv[TOK_TEXT] != NULL){
421 p = strchr(argv[TOK_TEXT], ' ');
422 if(p)
423 *p = 0;
425 argv[TOK_CHAN] = argv[TOK_TEXT];
426 snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT]);
427 } else if(!strncmp("PART", argv[TOK_CMD], 5)) {
428 snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
429 } else if(!strncmp("MODE", argv[TOK_CMD], 5))
430 snprintf(message, PIPE_BUF, "-!- %s changed mode/%s -> %s %s", argv[TOK_NICKSRV], argv[TOK_CMD + 1] ? argv[TOK_CMD + 1] : "" , argv[TOK_CMD + 2]? argv[TOK_CMD + 2] : "", argv[TOK_CMD + 3] ? argv[TOK_CMD + 3] : "");
431 else if(!strncmp("QUIT", argv[TOK_CMD], 5))
432 snprintf(message, PIPE_BUF, "-!- %s(%s) has quit \"%s\"", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
433 else if(!strncmp("NICK", argv[TOK_CMD], 5))
434 snprintf(message, PIPE_BUF, "-!- %s changed nick to %s", argv[TOK_NICKSRV], argv[TOK_TEXT]);
435 else if(!strncmp("TOPIC", argv[TOK_CMD], 6))
436 snprintf(message, PIPE_BUF, "-!- %s changed topic to \"%s\"", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
437 else if(!strncmp("KICK", argv[TOK_CMD], 5))
438 snprintf(message, PIPE_BUF, "-!- %s kicked %s (\"%s\")", argv[TOK_NICKSRV], argv[TOK_ARG], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
439 else if(!strncmp("NOTICE", argv[TOK_CMD], 7))
440 snprintf(message, PIPE_BUF, "-!- \"%s\")", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
441 else if(!strncmp("PRIVMSG", argv[TOK_CMD], 8))
442 snprintf(message, PIPE_BUF, "<%s> %s", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
443 if(!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick)))
444 print_out(argv[TOK_NICKSRV], message);
445 else
446 print_out(argv[TOK_CHAN], message);
449 #ifdef HAVE_SSL
450 static int read_line(int fd, size_t res_len, char *buf, size_t from_srv) {
451 #else
452 static int read_line(int fd, size_t res_len, char *buf) {
453 #endif
454 size_t i = 0;
455 char c = 0;
456 do {
457 if(READ(fd, &c, sizeof(char)) != sizeof(char))
458 return -1;
459 buf[i++] = c;
461 while(c != '\n' && i < res_len);
462 buf[i - 1] = 0; /* eliminates '\n' */
463 return 0;
466 static void handle_channels_input(Channel *c) {
467 static char buf[PIPE_BUF];
468 #ifdef HAVE_SSL
469 if(read_line(c->fd, PIPE_BUF, buf, 0) == -1) {
470 #else
471 if(read_line(c->fd, PIPE_BUF, buf) == -1) {
472 #endif
473 close(c->fd);
474 int fd = open_channel(c->name);
475 if(fd != -1)
476 c->fd = fd;
477 else
478 rm_channel(c);
479 return;
481 proc_channels_input(c, buf);
484 static void handle_server_output() {
485 static char buf[PIPE_BUF];
486 #ifdef HAVE_SSL
487 if(read_line(irc->irc, PIPE_BUF, buf, 1) == -1) {
488 #else
489 if(read_line(irc, PIPE_BUF, buf) == -1) {
490 #endif
491 perror("ii: remote host closed connection");
492 exit(EXIT_FAILURE);
494 proc_server_cmd(buf);
497 static void run() {
498 Channel *c;
499 int r, maxfd;
500 fd_set rd;
501 struct timeval tv;
502 char ping_msg[17] = "PING localhost\r\n";
504 for(;;) {
505 FD_ZERO(&rd);
506 #ifdef HAVE_SSL
507 maxfd = irc->irc;
508 FD_SET(irc->irc, &rd);
509 #else
510 maxfd = irc;
511 FD_SET(irc, &rd);
512 #endif
513 for(c = channels; c; c = c->next) {
514 if(maxfd < c->fd)
515 maxfd = c->fd;
516 FD_SET(c->fd, &rd);
519 tv.tv_sec = 120;
520 tv.tv_usec = 0;
521 r = select(maxfd + 1, &rd, 0, 0, &tv);
522 if(r < 0) {
523 if(errno == EINTR)
524 continue;
525 perror("ii: error on select()");
526 exit(EXIT_FAILURE);
527 } else if(r == 0) {
528 if(time(NULL) - last_response >= PING_TIMEOUT) {
529 print_out(NULL, "-!- ii shutting down: ping timeout");
530 exit(EXIT_FAILURE);
532 WRITE(irc, ping_msg);
533 continue;
535 #ifdef HAVE_SSL
536 if(FD_ISSET(irc->irc, &rd)) {
537 #else
538 if(FD_ISSET(irc, &rd)) {
539 #endif
540 handle_server_output();
541 last_response = time(NULL);
543 for(c = channels; c; c = c->next)
544 if(FD_ISSET(c->fd, &rd))
545 handle_channels_input(c);
549 int main(int argc, char *argv[]) {
550 int i;
551 unsigned short port = SERVER_PORT;
552 char *key = NULL, *fullname = NULL, *dir = NULL;
553 #ifdef HAVE_SSL
554 char prefix[_POSIX_PATH_MAX] = "irc", *pmsg = message + 17;
555 #else
556 char prefix[_POSIX_PATH_MAX] = "irc";
557 #endif
559 for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
560 switch (argv[i][1]) {
561 #ifdef HAVE_SSL
562 case 'e': use_ssl = 1; break;
563 #endif
564 case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break;
565 case 's': host = argv[++i]; break;
566 case 'p': port = strtol(argv[++i], NULL, 10); break;
567 case 'n': snprintf(nick,sizeof(nick),"%s", argv[++i]); break;
568 case 'k': key = argv[++i]; break;
569 case 'f': fullname = argv[++i]; break;
570 case 'd': dir = argv[++i]; break;
571 default: usage(); break;
574 if(i != argc) usage();
575 #ifdef HAVE_SSL
576 if(use_ssl) port = port == SERVER_PORT ? SSL_SERVER_PORT : port;
577 #endif
578 irc = tcpopen(port);
579 if(!snprintf(path, sizeof(path), "%s/%s", prefix, dir ? dir : host)) {
580 fprintf(stderr, "%s", "ii: path to irc directory too long\n");
581 exit(EXIT_FAILURE);
583 create_dirtree(path);
585 add_channel(""); /* master channel */
586 login(key, fullname);
587 #ifdef HAVE_SSL
588 if (use_ssl) {
589 snprintf(message, PIPE_BUF, "MD5 Fingerprint: ");
590 for(i = 0; strlen(message) < PIPE_BUF && i < fp_len; i++) {
591 snprintf(pmsg, PIPE_BUF, i > 0 ? ":%02X" : "%02X", fp[i]);
592 if(i > 0) pmsg += 3;
593 else pmsg += 2;
595 print_out(NULL, message);
597 #endif
598 run();
599 return 0;