Update ii.in.1 to address the new feature.
[iii.git] / ii.c
blob20aa10a16912e23c96aaa0b6cfd2f9468ce8ff0b
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 SSL_SERVER_PORT 6697
30 #define IS_CHANNEL(s) (((s)[0]=='#')||((s)[0]=='&')||((s)[0]=='+')||((s)[0]=='!'))
31 #ifdef HAVE_SSL
32 #define SSL_SERVER_PORT 6697
33 #define WRITE(conn, msg) (use_ssl ? SSL_write(irc->sslHandle, msg, strlen(msg)) : write(conn->irc, msg, strlen(msg)))
34 #define READ(fd, buf, s) (from_srv && use_ssl ? SSL_read(irc->sslHandle, buf, s) : read(fd, buf, s))
35 #else
36 #define WRITE(irc, msg) (write(irc, msg, strlen(msg)))
37 #define READ(fd, buf, s) (read(fd, buf, s))
38 #endif
39 enum { TOK_NICKSRV = 0, TOK_USER, TOK_CMD, TOK_CHAN, TOK_ARG, TOK_TEXT, TOK_LAST };
41 typedef struct Channel Channel;
42 struct Channel {
43 int fd;
44 char *name;
45 Channel *next;
47 #ifdef HAVE_SSL
48 typedef struct {
49 int irc;
50 SSL *sslHandle;
51 SSL_CTX *sslContext;
52 } conn;
53 #endif
55 #ifdef HAVE_SSL
56 static size_t use_ssl = 0;
57 static conn *irc;
58 static unsigned char fp[EVP_MAX_MD_SIZE];
59 static int fp_len;
60 #else
61 static int irc;
62 #endif
63 static time_t last_response;
64 static Channel *channels = NULL;
65 static char *host = "irc.freenode.net";
66 static char nick[32] = "anonymous"; /* might change while running */
67 static char path[_POSIX_PATH_MAX];
68 static char message[PIPE_BUF]; /* message buf used for communication */
70 static void usage() {
71 fprintf(stderr, "%s",
72 "ii - irc it - " VERSION "\n"
73 "(C)opyright MMV-MMVI Anselm R. Garbe\n"
74 "(C)opyright MMV-MMXI Nico Golde\n"
75 "usage: ii [-i <irc dir>] [-s <host>] [-p <port>]\n"
76 " [-n <nick>] [-k <password>] [-f <fullname>]\n"
77 #ifdef HAVE_SSL
78 " [-e] [-d <directory>]\n");
79 #else
80 " [-d <directory>]\n");
81 #endif
82 exit(EXIT_SUCCESS);
85 static char *striplower(char *s) {
86 char *p = NULL;
87 for(p = s; p && *p; p++) {
88 if(*p == '/') *p = '_';
89 *p = tolower(*p);
91 return s;
94 /* creates directories top-down, if necessary */
95 static void create_dirtree(const char *dir) {
96 char tmp[256];
97 char *p = NULL;
98 size_t len;
100 snprintf(tmp, sizeof(tmp),"%s",dir);
101 len = strlen(tmp);
102 if(tmp[len - 1] == '/')
103 tmp[len - 1] = 0;
104 for(p = tmp + 1; *p; p++)
105 if(*p == '/') {
106 *p = 0;
107 mkdir(tmp, S_IRWXU);
108 *p = '/';
110 mkdir(tmp, S_IRWXU);
113 static int get_filepath(char *filepath, size_t len, char *channel, char *file) {
114 if(channel) {
115 if(!snprintf(filepath, len, "%s/%s", path, channel))
116 return 0;
117 create_dirtree(filepath);
118 return snprintf(filepath, len, "%s/%s/%s", path, channel, file);
120 return snprintf(filepath, len, "%s/%s", path, file);
123 static void create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
124 if(!get_filepath(filepath, len, striplower(channel), suffix)) {
125 fprintf(stderr, "%s", "ii: path to irc directory too long\n");
126 exit(EXIT_FAILURE);
130 static int open_channel(char *name) {
131 static char infile[256];
132 create_filepath(infile, sizeof(infile), name, "in");
133 if(access(infile, F_OK) == -1)
134 mkfifo(infile, S_IRWXU);
135 return open(infile, O_RDONLY | O_NONBLOCK, 0);
138 static void add_channel(char *cname) {
139 Channel *c;
140 int fd;
141 char *name = striplower(cname);
143 for(c = channels; c; c = c->next)
144 if(!strcmp(name, c->name))
145 return; /* already handled */
147 fd = open_channel(name);
148 if(fd == -1) {
149 printf("ii: exiting, cannot create in channel: %s\n", name);
150 exit(EXIT_FAILURE);
152 c = calloc(1, sizeof(Channel));
153 if(!c) {
154 perror("ii: cannot allocate memory");
155 exit(EXIT_FAILURE);
157 if(!channels) channels = c;
158 else {
159 c->next = channels;
160 channels = c;
162 c->fd = fd;
163 c->name = strdup(name);
166 static void rm_channel(Channel *c) {
167 Channel *p;
168 if(channels == c) channels = channels->next;
169 else {
170 for(p = channels; p && p->next != c; p = p->next);
171 if(p->next == c)
172 p->next = c->next;
174 free(c->name);
175 free(c);
178 static void login(char *key, char *fullname) {
179 if(key) snprintf(message, PIPE_BUF,
180 "PASS %s\r\nNICK %s\r\nUSER %s localhost * :%s\r\n", key,
181 nick, nick, fullname ? fullname : nick);
182 else snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost * :%s\r\n",
183 nick, nick, fullname ? fullname : nick);
184 WRITE(irc, message); /* login */
187 #ifdef HAVE_SSL
188 static conn *ssl_connect(int fd) {
189 conn *c = NULL;
191 c = malloc(sizeof(conn));
192 if(!c) {
193 perror("ii: cannot allocate memory");
194 exit(EXIT_FAILURE);
196 c->irc = fd;
198 if (use_ssl) {
199 c->sslHandle = NULL;
200 c->sslContext = NULL;
201 SSL_load_error_strings();
202 SSL_library_init();
203 c->sslContext = SSL_CTX_new(SSLv23_client_method());
204 if(c->sslContext == NULL)
205 ERR_print_errors_fp(stderr);
206 c->sslHandle = SSL_new(c->sslContext);
207 if(!SSL_set_fd(c->sslHandle, c->irc) || SSL_connect(c->sslHandle) != 1)
208 ERR_print_errors_fp(stderr);
209 if(!X509_digest(SSL_get_peer_certificate(c->sslHandle), EVP_md5(), fp, &fp_len))
210 ERR_print_errors_fp(stderr);
213 return c;
216 static conn *tcpopen(unsigned short port) {
217 #else
218 static int tcpopen(unsigned short port) {
219 #endif
220 int fd;
221 struct sockaddr_in sin;
222 struct hostent *hp = gethostbyname(host);
224 memset(&sin, 0, sizeof(struct sockaddr_in));
225 if(!hp) {
226 perror("ii: cannot retrieve host information");
227 exit(EXIT_FAILURE);
229 sin.sin_family = AF_INET;
230 memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
231 sin.sin_port = htons(port);
232 if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
233 perror("ii: cannot create socket");
234 exit(EXIT_FAILURE);
236 if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
237 perror("ii: cannot connect to host");
238 exit(EXIT_FAILURE);
241 #ifdef HAVE_SSL
242 return ssl_connect(fd);
243 #else
244 return fd;
245 #endif
248 static size_t tokenize(char **result, size_t reslen, char *str, char delim) {
249 char *p = NULL, *n = NULL;
250 size_t i;
252 if(!str)
253 return 0;
254 for(n = str; *n == ' '; n++);
255 p = n;
256 for(i = 0; *n != 0;) {
257 if(i == reslen)
258 return 0;
259 if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0) delim=':'; /* workaround non-RFC compliant messages */
260 if(*n == delim) {
261 *n = 0;
262 result[i++] = p;
263 p = ++n;
264 } else
265 n++;
267 if(i<reslen && p < n && strlen(p))
268 result[i++] = p;
269 return i; /* number of tokens */
272 static void print_out(char *channel, char *buf) {
273 static char outfile[256], server[256], buft[18];
274 FILE *out = NULL;
275 time_t t = time(0);
277 if(channel) snprintf(server, sizeof(server), "-!- %s", channel);
278 if(strstr(buf, server)) channel="";
279 create_filepath(outfile, sizeof(outfile), channel, "out");
280 if(!(out = fopen(outfile, "a"))) return;
281 if(channel && channel[0]) add_channel(channel);
283 strftime(buft, sizeof(buft), "%F %R", localtime(&t));
284 fprintf(out, "%s %s\n", buft, buf);
285 fclose(out);
288 static void proc_channels_privmsg(char *channel, char *buf) {
289 snprintf(message, PIPE_BUF, "<%s> %s", nick, buf);
290 print_out(channel, message);
291 snprintf(message, PIPE_BUF, "PRIVMSG %s :%s\r\n", channel, buf);
292 WRITE(irc, message);
295 static void proc_channels_input(Channel *c, char *buf) {
296 char *p = NULL;
298 if(buf[0] && buf[0] != '/') {
299 proc_channels_privmsg(c->name, buf);
300 return;
302 message[0] = '\0';
303 if(buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) {
304 case 'j':
305 if(buf[3] == ' ' || buf[3] == '\0') return;
306 p = strchr(&buf[3], ' ');
307 if(p) *p = 0;
308 if(IS_CHANNEL(&buf[3])){
309 if(p && strlen(p + 1)) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */
310 else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]);
311 add_channel(&buf[3]);
313 else if(p && strlen(p + 1)) {
314 add_channel(&buf[3]);
315 proc_channels_privmsg(&buf[3], p + 1);
316 return;
318 break;
319 case 't':
320 if(IS_CHANNEL(&buf[3])){
321 p = strchr(&buf[3], ' ');
322 if(p) *p = 0;
323 if(p && strlen(p + 1)) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", &buf[3], p + 1);
324 else snprintf(message, PIPE_BUF, "TOPIC %s\r\n", &buf[3]);
326 else {
327 if(c->name[0] == 0) return;
328 if(strlen(buf)>3) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]);
329 else snprintf(message, PIPE_BUF, "TOPIC %s\r\n", c->name);
331 break;
332 case 'a':
333 if(strlen(buf)>3){
334 snprintf(message, PIPE_BUF, "-!- %s is away \"%s\"", nick, &buf[3]);
335 print_out(c->name, message);
336 snprintf(message, PIPE_BUF, "AWAY :%s\r\n", &buf[3]);
337 } else snprintf(message, PIPE_BUF, "AWAY\r\n");
338 break;
339 case 'n':
340 if(strlen(buf)>3){
341 snprintf(nick, sizeof(nick),"%s", &buf[3]);
342 snprintf(message, PIPE_BUF, "NICK %s\r\n", &buf[3]);
344 break;
345 case 'l':
346 if(c->name[0] == 0) return;
347 if(strlen(buf)>3) snprintf(message, PIPE_BUF, "PART %s :%s\r\n", c->name, &buf[3]);
348 else snprintf(message, PIPE_BUF, "PART %s\r\n", c->name);
349 if(IS_CHANNEL(c->name)) WRITE(irc, message);
350 close(c->fd);
351 rm_channel(c);
352 return;
353 break;
354 default:
355 snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
356 break;
357 } else
358 snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
360 if (message[0] != '\0')
361 WRITE(irc, message);
364 static void proc_server_cmd(char *buf) {
365 char *argv[TOK_LAST], *cmd = NULL, *p = NULL;
366 int i;
368 if(!buf || *buf=='\0')
369 return;
371 for(i = 0; i < TOK_LAST; i++)
372 argv[i] = NULL;
373 /* <message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
374 <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
375 <command> ::= <letter> { <letter> } | <number> <number> <number>
376 <SPACE> ::= ' ' { ' ' }
377 <params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
378 <middle> ::= <Any *non-empty* sequence of octets not including SPACE
379 or NUL or CR or LF, the first of which may not be ':'>
380 <trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
381 <crlf> ::= CR LF */
383 if(buf[0] == ':') { /* check prefix */
384 if (!(p = strchr(buf, ' '))) return;
385 *p = 0;
386 for(++p; *p == ' '; p++);
387 cmd = p;
388 argv[TOK_NICKSRV] = &buf[1];
389 if((p = strchr(buf, '!'))) {
390 *p = 0;
391 argv[TOK_USER] = ++p;
393 } else
394 cmd = buf;
396 /* remove CRLFs */
397 for(p = cmd; p && *p != 0; p++)
398 if(*p == '\r' || *p == '\n')
399 *p = 0;
401 if((p = strchr(cmd, ':'))) {
402 *p = 0;
403 argv[TOK_TEXT] = ++p;
406 tokenize(&argv[TOK_CMD], TOK_LAST - TOK_CMD, cmd, ' ');
408 if(!argv[TOK_CMD] || !strncmp("PONG", argv[TOK_CMD], 5)) {
409 return;
410 } else if(!strncmp("PING", argv[TOK_CMD], 5)) {
411 snprintf(message, PIPE_BUF, "PONG %s\r\n", argv[TOK_TEXT]);
412 WRITE(irc, message);
413 return;
414 } else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */
415 snprintf(message, PIPE_BUF, "%s%s", argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
416 print_out(0, message);
417 return;
418 } else if(!strncmp("ERROR", argv[TOK_CMD], 6))
419 snprintf(message, PIPE_BUF, "-!- error %s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown");
420 else if(!strncmp("JOIN", argv[TOK_CMD], 5)) {
421 if(argv[TOK_TEXT] != NULL){
422 p = strchr(argv[TOK_TEXT], ' ');
423 if(p)
424 *p = 0;
426 argv[TOK_CHAN] = argv[TOK_TEXT];
427 snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT]);
428 } else if(!strncmp("PART", argv[TOK_CMD], 5)) {
429 snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
430 } else if(!strncmp("MODE", argv[TOK_CMD], 5))
431 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] : "");
432 else if(!strncmp("QUIT", argv[TOK_CMD], 5))
433 snprintf(message, PIPE_BUF, "-!- %s(%s) has quit \"%s\"", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
434 else if(!strncmp("NICK", argv[TOK_CMD], 5))
435 snprintf(message, PIPE_BUF, "-!- %s changed nick to %s", argv[TOK_NICKSRV], argv[TOK_TEXT]);
436 else if(!strncmp("TOPIC", argv[TOK_CMD], 6))
437 snprintf(message, PIPE_BUF, "-!- %s changed topic to \"%s\"", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
438 else if(!strncmp("KICK", argv[TOK_CMD], 5))
439 snprintf(message, PIPE_BUF, "-!- %s kicked %s (\"%s\")", argv[TOK_NICKSRV], argv[TOK_ARG], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
440 else if(!strncmp("NOTICE", argv[TOK_CMD], 7))
441 snprintf(message, PIPE_BUF, "-!- \"%s\")", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
442 else if(!strncmp("PRIVMSG", argv[TOK_CMD], 8))
443 snprintf(message, PIPE_BUF, "<%s> %s", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
444 if(!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick)))
445 print_out(argv[TOK_NICKSRV], message);
446 else
447 print_out(argv[TOK_CHAN], message);
450 #ifdef HAVE_SSL
451 static int read_line(int fd, size_t res_len, char *buf, size_t from_srv) {
452 #else
453 static int read_line(int fd, size_t res_len, char *buf) {
454 #endif
455 size_t i = 0;
456 char c = 0;
457 do {
458 if(READ(fd, &c, sizeof(char)) != sizeof(char))
459 return -1;
460 buf[i++] = c;
462 while(c != '\n' && i < res_len);
463 buf[i - 1] = 0; /* eliminates '\n' */
464 return 0;
467 static void handle_channels_input(Channel *c) {
468 static char buf[PIPE_BUF];
469 #ifdef HAVE_SSL
470 if(read_line(c->fd, PIPE_BUF, buf, 0) == -1) {
471 #else
472 if(read_line(c->fd, PIPE_BUF, buf) == -1) {
473 #endif
474 close(c->fd);
475 int fd = open_channel(c->name);
476 if(fd != -1)
477 c->fd = fd;
478 else
479 rm_channel(c);
480 return;
482 proc_channels_input(c, buf);
485 static void handle_server_output() {
486 static char buf[PIPE_BUF];
487 #ifdef HAVE_SSL
488 if(read_line(irc->irc, PIPE_BUF, buf, 1) == -1) {
489 #else
490 if(read_line(irc, PIPE_BUF, buf) == -1) {
491 #endif
492 perror("ii: remote host closed connection");
493 exit(EXIT_FAILURE);
495 proc_server_cmd(buf);
498 static void run() {
499 Channel *c;
500 int r, maxfd;
501 fd_set rd;
502 struct timeval tv;
503 char ping_msg[17] = "PING localhost\r\n";
505 for(;;) {
506 FD_ZERO(&rd);
507 #ifdef HAVE_SSL
508 maxfd = irc->irc;
509 FD_SET(irc->irc, &rd);
510 #else
511 maxfd = irc;
512 FD_SET(irc, &rd);
513 #endif
514 for(c = channels; c; c = c->next) {
515 if(maxfd < c->fd)
516 maxfd = c->fd;
517 FD_SET(c->fd, &rd);
520 tv.tv_sec = 120;
521 tv.tv_usec = 0;
522 r = select(maxfd + 1, &rd, 0, 0, &tv);
523 if(r < 0) {
524 if(errno == EINTR)
525 continue;
526 perror("ii: error on select()");
527 exit(EXIT_FAILURE);
528 } else if(r == 0) {
529 if(time(NULL) - last_response >= PING_TIMEOUT) {
530 print_out(NULL, "-!- ii shutting down: ping timeout");
531 exit(EXIT_FAILURE);
533 WRITE(irc, ping_msg);
534 continue;
536 #ifdef HAVE_SSL
537 if(FD_ISSET(irc->irc, &rd)) {
538 #else
539 if(FD_ISSET(irc, &rd)) {
540 #endif
541 handle_server_output();
542 last_response = time(NULL);
544 for(c = channels; c; c = c->next)
545 if(FD_ISSET(c->fd, &rd))
546 handle_channels_input(c);
550 int main(int argc, char *argv[]) {
551 int i;
552 unsigned short port = SERVER_PORT;
553 char *key = NULL, *fullname = NULL, *dir = NULL;
554 #ifdef HAVE_SSL
555 char prefix[_POSIX_PATH_MAX] = "irc", *pmsg = message + 17;
556 #else
557 char prefix[_POSIX_PATH_MAX] = "irc";
558 #endif
560 for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
561 switch (argv[i][1]) {
562 #ifdef HAVE_SSL
563 case 'e': use_ssl = 1; break;
564 #endif
565 case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break;
566 case 's': host = argv[++i]; break;
567 case 'p': port = strtol(argv[++i], NULL, 10); break;
568 case 'n': snprintf(nick,sizeof(nick),"%s", argv[++i]); break;
569 case 'k': key = argv[++i]; break;
570 case 'f': fullname = argv[++i]; break;
571 case 'd': dir = argv[++i]; break;
572 default: usage(); break;
575 if(i != argc) usage();
576 #ifdef HAVE_SSL
577 if(use_ssl) port = port == SERVER_PORT ? SSL_SERVER_PORT : port;
578 #endif
579 irc = tcpopen(port);
580 if(!snprintf(path, sizeof(path), "%s/%s", prefix, dir ? dir : host)) {
581 fprintf(stderr, "%s", "ii: path to irc directory too long\n");
582 exit(EXIT_FAILURE);
584 create_dirtree(path);
586 add_channel(""); /* master channel */
587 login(key, fullname);
588 #ifdef HAVE_SSL
589 if (use_ssl) {
590 snprintf(message, PIPE_BUF, "MD5 Fingerprint: ");
591 for(i = 0; strlen(message) < PIPE_BUF && i < fp_len; i++) {
592 snprintf(pmsg, PIPE_BUF, i > 0 ? ":%02X" : "%02X", fp[i]);
593 if(i > 0) pmsg += 3;
594 else pmsg += 2;
596 print_out(NULL, message);
598 #endif
599 run();
601 return 0;