--- ii.c | 264 +++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 138 insertions(+), 126 deletions(-) diff --git a/ii.c b/ii.c index b290292..b1ba89e 100644 --- a/ii.c +++ b/ii.c _AT_@ -58,8 +58,9 @@ static void * ecalloc(size_t nmemb, size_t size) { void *p; - if(!(p = calloc(nmemb, size))) + if (!(p = calloc(nmemb, size))) eprintf("ii: cannot allocate memory:"); + return p; } _AT_@ -70,12 +71,12 @@ ewritestr(int fd, const char *s) int w = -1; len = strlen(s); - for(off = 0; off < len; off += w) { - if((w = write(fd, s + off, len - off)) == -1) + for (off = 0; off < len; off += w) { + if ((w = write(fd, s + off, len - off)) == -1) break; off += w; } - if(w == -1) + if (w == -1) eprintf("write error:"); } _AT_@ -89,14 +90,14 @@ create_dirtree(const char *dir) strlcpy(tmp, dir, sizeof(tmp)); len = strlen(tmp); - if(len > 0 && tmp[len - 1] == '/') + if (len > 0 && tmp[len - 1] == '/') tmp[len - 1] = '\0'; - if((stat(tmp, &st) != -1) && S_ISDIR(st.st_mode)) + if ((stat(tmp, &st) != -1) && S_ISDIR(st.st_mode)) return; /* dir exists */ - for(p = tmp + 1; *p; p++) { - if(*p != '/') + for (p = tmp + 1; *p; p++) { + if (*p != '/') continue; *p = '\0'; mkdir(tmp, S_IRWXU); _AT_@ -108,10 +109,10 @@ create_dirtree(const char *dir) static void channel_normalize_path(char *s) { - for(; *s; s++) { - if(isalpha(*s)) + for (; *s; s++) { + if (isalpha(*s)) *s = tolower(*s); - else if(!isdigit(*s) && !strchr(".#&", *s)) + else if (!isdigit(*s) && !strchr(".#&", *s)) *s = '_'; } } _AT_@ -121,10 +122,10 @@ channel_normalize_name(char *s) { char *p; - if(*s == '&' || *s == '#') + if (*s == '&' || *s == '#') s++; - for(p = s; *s; s++) { - if(!strchr(" ,&#\x07", *s)) { + for (p = s; *s; s++) { + if (!strchr(" ,&#\x07", *s)) { *p = *s; p++; } _AT_@ -139,17 +140,17 @@ create_filepath(char *filepath, size_t len, const char *path, const char *e = "ii: path to irc directory too long\n"; int r; - if(channel[0]) { + if (channel[0]) { r = snprintf(filepath, len, "%s/%s", path, channel); - if(r < 0 || (size_t)r >= len) + if (r < 0 || (size_t)r >= len) eprintf(e); create_dirtree(filepath); r = snprintf(filepath, len, "%s/%s/%s", path, channel, suffix); - if(r < 0 || (size_t)r >= len) + if (r < 0 || (size_t)r >= len) eprintf(e); } else { r = snprintf(filepath, len, "%s/%s", path, suffix); - if(r < 0 || (size_t)r >= len) + if (r < 0 || (size_t)r >= len) eprintf(e); } } _AT_@ -166,11 +167,11 @@ channel_open(Channel *c) int fd; /* make "in" fifo if it doesn't exist already. */ - if(access(c->inpath, F_OK) == -1) + if (access(c->inpath, F_OK) == -1) mkfifo(c->inpath, S_IRWXU); c->fdin = -1; fd = open(c->inpath, O_RDONLY | O_NONBLOCK, 0); - if(fd == -1) + if (fd == -1) return -1; c->fdin = fd; _AT_@ -184,6 +185,7 @@ channel_reopen(Channel *c) close(c->fdin); c->fdin = -1; } + return channel_open(c); } _AT_@ -205,6 +207,7 @@ channel_new(const char *name) channelpath, "in"); create_filepath(c->outpath, sizeof(c->outpath), ircpath, channelpath, "out"); + return c; } _AT_@ -216,10 +219,11 @@ channel_find(const char *name) strlcpy(chan, name, sizeof(chan)); channel_normalize_name(chan); - for(c = channels; c; c = c->next) { - if(!strcmp(chan, c->name)) + for (c = channels; c; c = c->next) { + if (!strcmp(chan, c->name)) return c; /* already handled */ } + return NULL; } _AT_@ -229,17 +233,18 @@ channel_add(const char *name) Channel *c; c = channel_new(name); - if(channel_open(c) == -1) { + if (channel_open(c) == -1) { weprintf("ii: cannot create channel: %s:", name); channel_free(c); return NULL; } - if(!channels) { + if (!channels) { channels = c; } else { c->next = channels; channels = c; } + return c; } _AT_@ -248,8 +253,9 @@ channel_join(const char *name) { Channel *c; - if(!(c = channel_find(name))) + if (!(c = channel_find(name))) c = channel_add(name); + return c; } _AT_@ -258,11 +264,11 @@ channel_rm(Channel *c) { Channel *p; - if(channels == c) { + if (channels == c) { channels = channels->next; } else { - for(p = channels; p && p->next != c; p = p->next); - if(p->next == c) + for (p = channels; p && p->next != c; p = p->next); + if (p->next == c) p->next = c->next; } channel_free(c); _AT_@ -271,7 +277,7 @@ channel_rm(Channel *c) static void channel_leave(Channel *c) { - if(c->fdin > 2) { + if (c->fdin > 2) { close(c->fdin); c->fdin = -1; } _AT_@ -304,15 +310,16 @@ udsopen(const char *uds) int fd; fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(fd == -1) + if (fd == -1) eprintf("ii: socket:"); sun.sun_family = AF_UNIX; - if(strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) + if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) eprintf("ii: unix domain socket path truncation\n"); len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family); - if(connect(fd, (struct sockaddr *)&sun, len) == -1) + if (connect(fd, (struct sockaddr *)&sun, len) == -1) eprintf("ii: connect:"); + return fd; } _AT_@ -327,20 +334,21 @@ tcpopen(const char *host, const char *service) hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */ hints.ai_socktype = SOCK_STREAM; - if((e = getaddrinfo(host, service, &hints, &res))) + if ((e = getaddrinfo(host, service, &hints, &res))) eprintf("ii: getaddrinfo() failed: %s\n", gai_strerror(e)); for (rp = res; rp; rp = rp->ai_next) { fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if(fd == -1) + if (fd == -1) continue; /* retry */ - if(connect(fd, res->ai_addr, res->ai_addrlen) != 1) + if (connect(fd, res->ai_addr, res->ai_addrlen) != 1) break; /* success */ } - if(!rp) + if (!rp) eprintf("ii: could not connect:"); freeaddrinfo(res); + return fd; } _AT_@ -349,7 +357,8 @@ isnumeric(const char *s) { errno = 0; strtol(s, NULL, 10); - return errno == 0; + + return (errno == 0); } static size_t _AT_@ -358,14 +367,14 @@ tokenize(char **result, size_t reslen, char *str, int delim) char *p = NULL, *n = NULL; size_t i = 0; - for(n = str; *n == ' '; n++); + for (n = str; *n == ' '; n++); p = n; - while(*n != '\0') { - if(i >= reslen) + while (*n != '\0') { + if (i >= reslen) return 0; - if(i > TOK_CHAN - TOK_CMD && result[0] && isnumeric(result[0])) + if (i > TOK_CHAN - TOK_CMD && result[0] && isnumeric(result[0])) delim = ':'; /* workaround non-RFC compliant messages */ - if(*n == delim) { + if (*n == delim) { *n = '\0'; result[i++] = p; p = ++n; _AT_@ -374,8 +383,9 @@ tokenize(char **result, size_t reslen, char *str, int delim) } } /* add last entry */ - if(i < reslen && p < n && p && *p) + if (i < reslen && p < n && p && *p) result[i++] = p; + return i; /* number of tokens */ } _AT_@ -385,7 +395,7 @@ channel_print(Channel *c, const char *buf) FILE *fp = NULL; time_t t = time(NULL); - if(!(fp = fopen(c->outpath, "a"))) + if (!(fp = fopen(c->outpath, "a"))) return; fprintf(fp, "%lu %s\n", (unsigned long)t, buf); fclose(fp); _AT_@ -406,56 +416,56 @@ proc_channels_input(int ircfd, Channel *c, char *buf) char *p = NULL; size_t buflen; - if(buf[0] != '/' && buf[0] != '\0') { + if (buf[0] != '/' && buf[0] != '\0') { proc_channels_privmsg(ircfd, c, buf); return; } msg[0] = '\0'; - if(buf[2] == ' ' || buf[2] == '\0') { + if (buf[2] == ' ' || buf[2] == '\0') { buflen = strlen(buf); switch(buf[1]) { case 'j': /* join */ - if((p = strchr(&buf[3], ' '))) /* password parameter */ + if ((p = strchr(&buf[3], ' '))) /* password parameter */ *p = '\0'; - if((buf[3] == '#') || (buf[3] == '&') || (buf[3] == '+') || + if ((buf[3] == '#') || (buf[3] == '&') || (buf[3] == '+') || (buf[3] == '!')) { /* password protected channel */ - if(p) + if (p) snprintf(msg, sizeof(msg), "JOIN %s %s\r\n", &buf[3], p + 1); else snprintf(msg, sizeof(msg), "JOIN %s\r\n", &buf[3]); c = channel_join(&buf[3]); - } else if(p) { - if((c = channel_join(&buf[3]))) + } else if (p) { + if ((c = channel_join(&buf[3]))) proc_channels_privmsg(ircfd, c, p + 1); return; } break; case 't': /* topic */ - if(buflen >= 3) + if (buflen >= 3) snprintf(msg, sizeof(msg), "TOPIC %s :%s\r\n", c->name, &buf[3]); break; case 'a': /* away */ - if(buflen >= 3) { + if (buflen >= 3) { snprintf(msg, sizeof(msg), "-!- %s is away \"%s\"", nick, &buf[3]); channel_print(c, msg); } - if(buflen >= 3) + if (buflen >= 3) snprintf(msg, sizeof(msg), "AWAY :%s\r\n", &buf[3]); else snprintf(msg, sizeof(msg), "AWAY\r\n"); break; case 'n': /* change nick */ - if(buflen >= 3) { + if (buflen >= 3) { strlcpy(_nick, &buf[3], sizeof(nick)); snprintf(msg, sizeof(msg), "NICK %s\r\n", &buf[3]); } break; case 'l': /* leave */ - if(c == channelmaster) + if (c == channelmaster) return; - if(buflen >= 3) + if (buflen >= 3) snprintf(msg, sizeof(msg), "PART %s :%s\r\n", c->name, &buf[3]); else snprintf(msg, sizeof(msg), _AT_@ -465,7 +475,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) return; break; case 'q': /* quit */ - if(buflen >= 3) + if (buflen >= 3) snprintf(msg, sizeof(msg), "QUIT %s\r\n", &buf[3]); else snprintf(msg, sizeof(msg), _AT_@ -482,7 +492,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) /* raw IRC command */ snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]); } - if(msg[0] != '\0') + if (msg[0] != '\0') ewritestr(ircfd, msg); } _AT_@ -494,22 +504,22 @@ proc_server_cmd(int fd, char *buf) char *argv[TOK_LAST], *cmd = NULL, *p = NULL; unsigned int i; - if(!buf || buf[0] == '\0') + if (!buf || buf[0] == '\0') return; /* clear tokens */ - for(i = 0; i < TOK_LAST; i++) + for (i = 0; i < TOK_LAST; i++) argv[i] = NULL; /* check prefix */ - if(buf[0] == ':') { + if (buf[0] == ':') { if (!(p = strchr(buf, ' '))) return; *p = '\0'; - for(++p; *p == ' '; p++); + for (++p; *p == ' '; p++); cmd = p; argv[TOK_NICKSRV] = &buf[1]; - if((p = strchr(buf, '!'))) { + if ((p = strchr(buf, '!'))) { *p = '\0'; argv[TOK_USER] = ++p; } _AT_@ -518,87 +528,87 @@ proc_server_cmd(int fd, char *buf) } /* remove CRLFs */ - for(p = cmd; p && *p != '\0'; p++) { - if(*p == '\r' || *p == '\n') + for (p = cmd; p && *p != '\0'; p++) { + if (*p == '\r' || *p == '\n') *p = '\0'; } - if((p = strchr(cmd, ':'))) { + if ((p = strchr(cmd, ':'))) { *p = '\0'; argv[TOK_TEXT] = ++p; } tokenize(&argv[TOK_CMD], TOK_LAST - TOK_CMD, cmd, ' '); - if(!argv[TOK_CMD] || !strcmp("PONG", argv[TOK_CMD])) { + if (!argv[TOK_CMD] || !strcmp("PONG", argv[TOK_CMD])) { return; - } else if(!strcmp("PING", argv[TOK_CMD])) { + } else if (!strcmp("PING", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "PONG %s\r\n", argv[TOK_TEXT]); ewritestr(fd, msg); return; - } else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { + } else if (!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */ snprintf(msg, sizeof(msg), "%s%s", - argv[TOK_ARG] ? argv[TOK_ARG] : "", - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + argv[TOK_ARG] ? argv[TOK_ARG] : "", + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); channel_print(channelmaster, msg); return; /* don't process further */ - } else if(!strcmp("ERROR", argv[TOK_CMD])) + } else if (!strcmp("ERROR", argv[TOK_CMD])) snprintf(msg, sizeof(msg), "-!- error %s", - argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown"); - else if(!strcmp("JOIN", argv[TOK_CMD]) && (argv[TOK_CHAN] || argv[TOK_TEXT])) { + argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown"); + else if (!strcmp("JOIN", argv[TOK_CMD]) && (argv[TOK_CHAN] || argv[TOK_TEXT])) { if (argv[TOK_TEXT] != NULL) argv[TOK_CHAN] = argv[TOK_TEXT]; snprintf(msg, sizeof(msg), "-!- %s(%s) has joined %s", - argv[TOK_NICKSRV],argv[TOK_USER], argv[TOK_CHAN]); - } else if(!strcmp("PART", argv[TOK_CMD]) && argv[TOK_CHAN]) { + argv[TOK_NICKSRV],argv[TOK_USER], argv[TOK_CHAN]); + } else if (!strcmp("PART", argv[TOK_CMD]) && argv[TOK_CHAN]) { snprintf(msg, sizeof(msg), "-!- %s(%s) has left %s", - argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]); + argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]); /* if user itself leaves, don't write to channel (don't reopen channel). */ - if(!strcmp(argv[TOK_NICKSRV], nick)) + if (!strcmp(argv[TOK_NICKSRV], nick)) return; - } else if(!strcmp("MODE", argv[TOK_CMD])) { + } else if (!strcmp("MODE", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "-!- %s changed mode/%s -> %s %s", - argv[TOK_NICKSRV], - argv[TOK_CHAN] ? argv[TOK_CHAN] : "", - argv[TOK_ARG] ? argv[TOK_ARG] : "", - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - } else if(!strcmp("QUIT", argv[TOK_CMD])) { + argv[TOK_NICKSRV], + argv[TOK_CHAN] ? argv[TOK_CHAN] : "", + argv[TOK_ARG] ? argv[TOK_ARG] : "", + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + } else if (!strcmp("QUIT", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "-!- %s(%s) has quit \"%s\"", - argv[TOK_NICKSRV], argv[TOK_USER], - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - } else if(!strcmp("NICK", argv[TOK_CMD]) && argv[TOK_TEXT] && + argv[TOK_NICKSRV], argv[TOK_USER], + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + } else if (!strcmp("NICK", argv[TOK_CMD]) && argv[TOK_TEXT] && !strcmp(_nick, argv[TOK_TEXT])) { strlcpy(nick, _nick, sizeof(nick)); snprintf(msg, sizeof(msg), "-!- %s changed nick to %s", - argv[TOK_NICKSRV], argv[TOK_TEXT]); - } else if(!strcmp("TOPIC", argv[TOK_CMD])) { + argv[TOK_NICKSRV], argv[TOK_TEXT]); + } else if (!strcmp("TOPIC", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "-!- %s changed topic to \"%s\"", - argv[TOK_NICKSRV], - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - } else if(!strcmp("KICK", argv[TOK_CMD]) && argv[TOK_ARG]) { + argv[TOK_NICKSRV], + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + } else if (!strcmp("KICK", argv[TOK_CMD]) && argv[TOK_ARG]) { snprintf(msg, sizeof(msg), "-!- %s kicked %s (\"%s\")", - argv[TOK_NICKSRV], argv[TOK_ARG], - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - } else if(!strcmp("NOTICE", argv[TOK_CMD])) { + argv[TOK_NICKSRV], argv[TOK_ARG], + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + } else if (!strcmp("NOTICE", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "-!- \"%s\")", - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - } else if(!strcmp("PRIVMSG", argv[TOK_CMD])) { + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + } else if (!strcmp("PRIVMSG", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "<%s> %s", argv[TOK_NICKSRV], - argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); + argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); } else { return; /* can't read this message */ } - if(!argv[TOK_CHAN] || !strcmp(argv[TOK_CHAN], nick)) + if (!argv[TOK_CHAN] || !strcmp(argv[TOK_CHAN], nick)) channel = argv[TOK_NICKSRV]; else channel = argv[TOK_CHAN]; - if(!channel || channel[0] == '\0') - c = channelmaster; + if (!channel || channel[0] == '\0') + c = channelmaster; else c = channel_join(channel); - if(c) + if (c) channel_print(c, msg); } _AT_@ -609,11 +619,12 @@ read_line(int fd, char *buf, size_t bufsiz) char c = '\0'; do { - if(read(fd, &c, sizeof(char)) != sizeof(char)) + if (read(fd, &c, sizeof(char)) != sizeof(char)) return -1; buf[i++] = c; - } while(c != '\n' && i < bufsiz); + } while (c != '\n' && i < bufsiz); buf[i - 1] = '\0'; /* eliminates '\n' */ + return 0; } _AT_@ -622,8 +633,8 @@ handle_channels_input(int ircfd, Channel *c) { char buf[BUFSIZ]; - if(read_line(c->fdin, buf, sizeof(buf)) == -1) { - if(channel_reopen(c) == -1) + if (read_line(c->fdin, buf, sizeof(buf)) == -1) { + if (channel_reopen(c) == -1) channel_rm(c); return; } _AT_@ -635,7 +646,7 @@ handle_server_output(int ircfd) { char buf[BUFSIZ]; - if(read_line(ircfd, buf, sizeof(buf)) == -1) + if (read_line(ircfd, buf, sizeof(buf)) == -1) eprintf("ii: remote host closed connection\n"); fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf); fflush(stdout); _AT_@ -645,7 +656,7 @@ handle_server_output(int ircfd) static void sighandler(int sig) { - if(sig == SIGTERM || sig == SIGINT) + if (sig == SIGTERM || sig == SIGINT) isrunning = 0; } _AT_@ -670,36 +681,37 @@ run(int ircfd, const char *host) int r, maxfd; snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host); - while(isrunning) { + while (isrunning) { maxfd = ircfd; FD_ZERO(&rdset); FD_SET(ircfd, &rdset); - for(c = channels; c; c = c->next) { + for (c = channels; c; c = c->next) { maxfd = MAX(c->fdin, maxfd); FD_SET(c->fdin, &rdset); } memset(&tv, 0, sizeof(tv)); tv.tv_sec = 120; r = select(maxfd + 1, &rdset, 0, 0, &tv); - if(r < 0) { - if(errno == EINTR) + if (r < 0) { + if (errno == EINTR) continue; eprintf("ii: error on select():"); - } else if(r == 0) { - if(time(NULL) - last_response >= PING_TIMEOUT) { - channel_print(channelmaster, "-!- ii shutting down: ping timeout"); + } else if (r == 0) { + if (time(NULL) - last_response >= PING_TIMEOUT) { + channel_print(channelmaster, + "-!- ii shutting down: ping timeout"); exit(1); } ewritestr(ircfd, ping_msg); continue; } - if(FD_ISSET(ircfd, &rdset)) { + if (FD_ISSET(ircfd, &rdset)) { handle_server_output(ircfd); last_response = time(NULL); } - for(c = channels; c; c = n) { + for (c = channels; c; c = n) { n = c->next; - if(FD_ISSET(c->fdin, &rdset)) + if (FD_ISSET(c->fdin, &rdset)) handle_channels_input(ircfd, c); } } _AT_@ -716,7 +728,7 @@ main(int argc, char *argv[]) int ircfd; /* use nickname and home dir of user by default */ - if(!(spw = getpwuid(getuid()))) + if (!(spw = getpwuid(getuid()))) eprintf("ii: getpwuid() failed:"); strlcpy(nick, spw->pw_name, sizeof(nick)); snprintf(prefix, sizeof(prefix), "%s/irc", spw->pw_dir); _AT_@ -749,26 +761,26 @@ main(int argc, char *argv[]) break; } ARGEND; - if(!*host) + if (!*host) usage(); - if(uds) + if (uds) ircfd = udsopen(uds); else ircfd = tcpopen(host, service); - if(snprintf(ircpath, sizeof(ircpath), "%s/%s", prefix, host) <= 0) + if (snprintf(ircpath, sizeof(ircpath), "%s/%s", prefix, host) <= 0) eprintf("ii: path to irc directory too long\n"); create_dirtree(ircpath); channelmaster = channel_add(""); /* master channel */ - if(key) + if (key) loginkey(ircfd, key); loginuser(ircfd, host, fullname && *fullname ? fullname : nick); setup(); run(ircfd, host); - if(channelmaster) + if (channelmaster) channel_leave(channelmaster); - for(c = channels; c; c = c->next) + for (c = channels; c; c = c->next) channel_leave(c); return 0; -- 2.4.10 --Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE--Received on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST