While going through ii's code I changed a couple of things in order to improve its readability as well as consistency.
Regards, kl3 PS: [email protected] is mentioned in the README but will actually bounce every message returning "Relay access denied" (554 5.7.1), is there something wrong with modprobe.de's mail setup?
From ff3af20464045740687424a9a94bd50e5b75a8e7 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sun, 3 Apr 2016 00:10:54 +0200 Subject: [PATCH 1/7] Use whitespaces These are keywords not function calls. --- ii.c | 173 ++++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/ii.c b/ii.c index 745e29a..b96f63b 100644 --- a/ii.c +++ b/ii.c @@ -53,8 +53,8 @@ static void usage() { static char *striplower(char *s) { char *p = NULL; - for(p = s; p && *p; p++) { - if(*p == '/') *p = ','; + for (p = s; p && *p; p++) { + if (*p == '/') *p = ','; *p = tolower(*p); } return s; @@ -65,12 +65,12 @@ static void create_dirtree(const char *dir) { char tmp[256]; char *p = NULL; size_t len; - snprintf(tmp, sizeof(tmp),"%s",dir); + snprintf(tmp, sizeof(tmp), "%s", dir); len = strlen(tmp); - if(tmp[len - 1] == '/') + if (tmp[len - 1] == '/') tmp[len - 1] = 0; - for(p = tmp + 1; *p; p++) - if(*p == '/') { + for (p = tmp + 1; *p; p++) + if (*p == '/') { *p = 0; mkdir(tmp, S_IRWXU); *p = '/'; @@ -79,8 +79,8 @@ static void create_dirtree(const char *dir) { } static int get_filepath(char *filepath, size_t len, char *channel, char *file) { - if(channel) { - if(!snprintf(filepath, len, "%s/%s", path, channel)) + if (channel) { + if (!snprintf(filepath, len, "%s/%s", path, channel)) return 0; create_dirtree(filepath); return snprintf(filepath, len, "%s/%s/%s", path, channel, file); @@ -89,7 +89,7 @@ static int get_filepath(char *filepath, size_t len, char *channel, char *file) { } static void create_filepath(char *filepath, size_t len, char *channel, char *suffix) { - if(!get_filepath(filepath, len, striplower(channel), suffix)) { + if (!get_filepath(filepath, len, striplower(channel), suffix)) { fputs("ii: path to irc directory too long\n", stderr); exit(EXIT_FAILURE); } @@ -98,7 +98,7 @@ static void create_filepath(char *filepath, size_t len, char *channel, char *suf static int open_channel(char *name) { static char infile[256]; create_filepath(infile, sizeof(infile), name, "in"); - if(access(infile, F_OK) == -1) + if (access(infile, F_OK) == -1) mkfifo(infile, S_IRWXU); return open(infile, O_RDONLY | O_NONBLOCK, 0); } @@ -108,21 +108,21 @@ static void add_channel(char *cname) { int fd; char *name = striplower(cname); - for(c = channels; c; c = c->next) - if(!strcmp(name, c->name)) + for (c = channels; c; c = c->next) + if (!strcmp(name, c->name)) return; /* already handled */ fd = open_channel(name); - if(fd == -1) { + if (fd == -1) { printf("ii: exiting, cannot create in channel: %s\n", name); exit(EXIT_FAILURE); } c = calloc(1, sizeof(Channel)); - if(!c) { + if (!c) { perror("ii: cannot allocate memory"); exit(EXIT_FAILURE); } - if(!channels) channels = c; + if (!channels) channels = c; else { c->next = channels; channels = c; @@ -133,10 +133,10 @@ static void add_channel(char *cname) { static void rm_channel(Channel *c) { Channel *p; - if(channels == c) channels = channels->next; + 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; } free(c->name); @@ -144,7 +144,7 @@ static void rm_channel(Channel *c) { } static void login(char *key, char *fullname) { - if(key) snprintf(message, PIPE_BUF, + if (key) snprintf(message, PIPE_BUF, "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", key, nick, nick, host, fullname ? fullname : nick); else snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost %s :%s\r\n", @@ -158,18 +158,18 @@ static int tcpopen(unsigned short port) { struct hostent *hp = gethostbyname(host); memset(&sin, 0, sizeof(struct sockaddr_in)); - if(!hp) { + if (!hp) { perror("ii: cannot retrieve host information"); exit(EXIT_FAILURE); } sin.sin_family = AF_INET; memcpy(&sin.sin_addr, hp->h_addr, hp->h_length); sin.sin_port = htons(port); - if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("ii: cannot create socket"); exit(EXIT_FAILURE); } - if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) { + if (connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) { perror("ii: cannot connect to host"); exit(EXIT_FAILURE); } @@ -180,22 +180,22 @@ static size_t tokenize(char **result, size_t reslen, char *str, char delim) { char *p = NULL, *n = NULL; size_t i; - if(!str) + if (!str) return 0; - for(n = str; *n == ' '; n++); + for (n = str; *n == ' '; n++); p = n; - for(i = 0; *n != 0;) { - if(i == reslen) + for (i = 0; *n != 0;) { + if (i == reslen) return 0; - if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0) delim=':'; /* workaround non-RFC compliant messages */ - if(*n == delim) { + if (i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0) delim=':'; /* workaround non-RFC compliant messages */ + if (*n == delim) { *n = 0; result[i++] = p; p = ++n; } else n++; } - if(i<reslen && p < n && strlen(p)) + if (i < reslen && p < n && strlen(p)) result[i++] = p; return i; /* number of tokens */ } @@ -205,11 +205,11 @@ static void print_out(char *channel, char *buf) { FILE *out = NULL; time_t t = time(0); - if(channel) snprintf(server, sizeof(server), "-!- %s", channel); - if(strstr(buf, server)) channel=""; + if (channel) snprintf(server, sizeof(server), "-!- %s", channel); + if (strstr(buf, server)) channel=""; create_filepath(outfile, sizeof(outfile), channel, "out"); - if(!(out = fopen(outfile, "a"))) return; - if(channel && channel[0]) add_channel(channel); + if (!(out = fopen(outfile, "a"))) return; + if (channel && channel[0]) add_channel(channel); strftime(buft, sizeof(buft), "%F %R", localtime(&t)); fprintf(out, "%s %s\n", buft, buf); @@ -227,49 +227,50 @@ static void proc_channels_input(Channel *c, char *buf) { /* static char infile[256]; */ char *p = NULL; - if(buf[0] != '/' && buf[0] != 0) { + if (buf[0] != '/' && buf[0] != 0) { proc_channels_privmsg(c->name, buf); return; } message[0] = '\0'; - if(buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) { + if (buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) { case 'j': p = strchr(&buf[3], ' '); - if(p) *p = 0; - if((buf[3]=='#')||(buf[3]=='&')||(buf[3]=='+')||(buf[3]=='!')){ - if(p) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */ + if (p) *p = 0; + if ((buf[3]=='#')||(buf[3]=='&')||(buf[3]=='+')||(buf[3]=='!')){ + if (p) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */ else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]); add_channel(&buf[3]); } - else if(p){ + else if (p) { add_channel(&buf[3]); proc_channels_privmsg(&buf[3], p + 1); return; } break; case 't': - if(strlen(buf)>=3) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]); + if (strlen(buf) >= 3) + snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]); break; case 'a': - if(strlen(buf)>=3){ + if (strlen(buf) >= 3){ snprintf(message, PIPE_BUF, "-!- %s is away \"%s\"", nick, &buf[3]); print_out(c->name, message); } - if(buf[2] == 0 || strlen(buf)<3) /* or used to make else part safe */ + if (buf[2] == 0 || strlen(buf)<3) /* or used to make else part safe */ snprintf(message, PIPE_BUF, "AWAY\r\n"); else snprintf(message, PIPE_BUF, "AWAY :%s\r\n", &buf[3]); break; case 'n': - if(strlen(buf)>=3){ + if (strlen(buf) >= 3){ snprintf(_nick, sizeof(nick),"%s", &buf[3]); snprintf(message, PIPE_BUF, "NICK %s\r\n", &buf[3]); } break; case 'l': - if(c->name[0] == 0) + if (c->name[0] == 0) return; - if(buf[2] == ' ' && strlen(buf)>=3) + if (buf[2] == ' ' && strlen(buf)>=3) snprintf(message, PIPE_BUF, "PART %s :%s\r\n", c->name, &buf[3]); else snprintf(message, PIPE_BUF, @@ -296,10 +297,10 @@ static void proc_server_cmd(char *buf) { char *argv[TOK_LAST], *cmd = NULL, *p = NULL; int i; - if(!buf || *buf=='\0') + if (!buf || *buf == '\0') return; - for(i = 0; i < TOK_LAST; i++) + for (i = 0; i < TOK_LAST; i++) argv[i] = NULL; /* <message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf> <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ] @@ -311,13 +312,13 @@ static void proc_server_cmd(char *buf) { <trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF> <crlf> ::= CR LF */ - if(buf[0] == ':') { /* check prefix */ + if (buf[0] == ':') { /* check prefix */ 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; } @@ -325,57 +326,57 @@ static void proc_server_cmd(char *buf) { cmd = 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] || !strncmp("PONG", argv[TOK_CMD], 5)) { + if (!argv[TOK_CMD] || !strncmp("PONG", argv[TOK_CMD], 5)) { return; - } else if(!strncmp("PING", argv[TOK_CMD], 5)) { + } else if (!strncmp("PING", argv[TOK_CMD], 5)) { snprintf(message, PIPE_BUF, "PONG %s\r\n", argv[TOK_TEXT]); write(irc, message, strlen(message)); return; - } else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */ + } else if (!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */ snprintf(message, PIPE_BUF, "%s%s", argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); print_out(0, message); return; - } else if(!strncmp("ERROR", argv[TOK_CMD], 6)) + } else if (!strncmp("ERROR", argv[TOK_CMD], 6)) snprintf(message, PIPE_BUF, "-!- error %s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown"); - else if(!strncmp("JOIN", argv[TOK_CMD], 5) && (argv[TOK_CHAN] || argv[TOK_TEXT])) { + else if (!strncmp("JOIN", argv[TOK_CMD], 5) && (argv[TOK_CHAN] || argv[TOK_TEXT])) { if (argv[TOK_TEXT] != NULL) argv[TOK_CHAN] = argv[TOK_TEXT]; snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]); - } else if(!strncmp("PART", argv[TOK_CMD], 5) && argv[TOK_CHAN]) { + } else if (!strncmp("PART", argv[TOK_CMD], 5) && argv[TOK_CHAN]) { snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]); - } else if(!strncmp("MODE", argv[TOK_CMD], 5)) + } else if (!strncmp("MODE", argv[TOK_CMD], 5)) 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] : ""); - else if(!strncmp("QUIT", argv[TOK_CMD], 5)) + else if (!strncmp("QUIT", argv[TOK_CMD], 5)) snprintf(message, PIPE_BUF, "-!- %s(%s) has quit \"%s\"", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - else if(!strncmp("NICK", argv[TOK_CMD], 5) && argv[TOK_TEXT] && !strcmp(_nick, argv[TOK_TEXT])) { + else if (!strncmp("NICK", argv[TOK_CMD], 5) && argv[TOK_TEXT] && !strcmp(_nick, argv[TOK_TEXT])) { snprintf(nick, sizeof(nick), "%s", _nick); snprintf(message, PIPE_BUF, "-!- changed nick to \"%s\"", nick); print_out(NULL, message); - } else if(!strncmp("NICK", argv[TOK_CMD], 5) && argv[TOK_TEXT]) + } else if (!strncmp("NICK", argv[TOK_CMD], 5) && argv[TOK_TEXT]) snprintf(message, PIPE_BUF, "-!- %s changed nick to %s", argv[TOK_NICKSRV], argv[TOK_TEXT]); - else if(!strncmp("TOPIC", argv[TOK_CMD], 6)) + else if (!strncmp("TOPIC", argv[TOK_CMD], 6)) snprintf(message, PIPE_BUF, "-!- %s changed topic to \"%s\"", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - else if(!strncmp("KICK", argv[TOK_CMD], 5) && argv[TOK_ARG]) + else if (!strncmp("KICK", argv[TOK_CMD], 5) && argv[TOK_ARG]) snprintf(message, PIPE_BUF, "-!- %s kicked %s (\"%s\")", argv[TOK_NICKSRV], argv[TOK_ARG], argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - else if(!strncmp("NOTICE", argv[TOK_CMD], 7)) + else if (!strncmp("NOTICE", argv[TOK_CMD], 7)) snprintf(message, PIPE_BUF, "-!- \"%s\")", argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); - else if(!strncmp("PRIVMSG", argv[TOK_CMD], 8)) + else if (!strncmp("PRIVMSG", argv[TOK_CMD], 8)) snprintf(message, PIPE_BUF, "<%s> %s", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : ""); else return; /* can't read this message */ - if(!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick))) + if (!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick))) print_out(argv[TOK_NICKSRV], message); else print_out(argv[TOK_CHAN], message); @@ -385,21 +386,21 @@ static int read_line(int fd, size_t res_len, char *buf) { size_t i = 0; 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 < res_len); + while (c != '\n' && i < res_len); buf[i - 1] = 0; /* eliminates '\n' */ return 0; } static void handle_channels_input(Channel *c) { static char buf[PIPE_BUF]; - if(read_line(c->fd, PIPE_BUF, buf) == -1) { + if (read_line(c->fd, PIPE_BUF, buf) == -1) { close(c->fd); int fd = open_channel(c->name); - if(fd != -1) + if (fd != -1) c->fd = fd; else rm_channel(c); @@ -410,7 +411,7 @@ static void handle_channels_input(Channel *c) { static void handle_server_output() { static char buf[PIPE_BUF]; - if(read_line(irc, PIPE_BUF, buf) == -1) { + if (read_line(irc, PIPE_BUF, buf) == -1) { perror("ii: remote host closed connection"); exit(EXIT_FAILURE); } @@ -425,12 +426,12 @@ static void run() { char ping_msg[512]; snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host); - for(;;) { + for (;;) { FD_ZERO(&rd); maxfd = irc; FD_SET(irc, &rd); - for(c = channels; c; c = c->next) { - if(maxfd < c->fd) + for (c = channels; c; c = c->next) { + if (maxfd < c->fd) maxfd = c->fd; FD_SET(c->fd, &rd); } @@ -438,26 +439,26 @@ static void run() { tv.tv_sec = 120; tv.tv_usec = 0; r = select(maxfd + 1, &rd, 0, 0, &tv); - if(r < 0) { - if(errno == EINTR) + if (r < 0) { + if (errno == EINTR) continue; perror("ii: error on select()"); exit(EXIT_FAILURE); - } else if(r == 0) { - if(time(NULL) - last_response >= PING_TIMEOUT) { + } else if (r == 0) { + if (time(NULL) - last_response >= PING_TIMEOUT) { print_out(NULL, "-!- ii shutting down: ping timeout"); exit(EXIT_FAILURE); } write(irc, ping_msg, strlen(ping_msg)); continue; } - if(FD_ISSET(irc, &rd)) { + if (FD_ISSET(irc, &rd)) { handle_server_output(); last_response = time(NULL); } - for(c = channels; c; c = n) { + for (c = channels; c; c = n) { n = c->next; - if(FD_ISSET(c->fd, &rd)) + if (FD_ISSET(c->fd, &rd)) handle_channels_input(c); } } @@ -470,7 +471,7 @@ int main(int argc, char *argv[]) { char *key = NULL, *fullname = NULL; char prefix[_POSIX_PATH_MAX]; - if(!spw) { + if (!spw) { fputs("ii: getpwuid() failed\n", stderr); exit(EXIT_FAILURE); } @@ -478,7 +479,7 @@ int main(int argc, char *argv[]) { snprintf(prefix, sizeof(prefix),"%s/irc", spw->pw_dir); if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) usage(); - for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) { + for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) { switch (argv[i][1]) { case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break; case 's': host = argv[++i]; break; @@ -490,7 +491,7 @@ int main(int argc, char *argv[]) { } } irc = tcpopen(port); - if(!snprintf(path, sizeof(path), "%s/%s", prefix, host)) { + if (!snprintf(path, sizeof(path), "%s/%s", prefix, host)) { fputs("ii: path to irc directory too long\n", stderr); exit(EXIT_FAILURE); } -- 2.8.0
From 210b722f843b88d500ee7a0c124804e955b82e7d Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sun, 3 Apr 2016 00:17:06 +0200 Subject: [PATCH 2/7] Use line breaks, indent properly This makes the code more readable. --- ii.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/ii.c b/ii.c index b96f63b..8b5e44e 100644 --- a/ii.c +++ b/ii.c @@ -54,7 +54,8 @@ static void usage() { static char *striplower(char *s) { char *p = NULL; for (p = s; p && *p; p++) { - if (*p == '/') *p = ','; + if (*p == '/') + *p = ','; *p = tolower(*p); } return s; @@ -133,7 +134,8 @@ static void add_channel(char *cname) { static void rm_channel(Channel *c) { Channel *p; - if (channels == c) channels = channels->next; + if (channels == c) + channels = channels->next; else { for (p = channels; p && p->next != c; p = p->next); if (p->next == c) @@ -144,10 +146,13 @@ static void rm_channel(Channel *c) { } static void login(char *key, char *fullname) { - if (key) snprintf(message, PIPE_BUF, + if (key) + snprintf(message, PIPE_BUF, "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", key, nick, nick, host, fullname ? fullname : nick); - else snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost %s :%s\r\n", + else + snprintf(message, PIPE_BUF, + "NICK %s\r\nUSER %s localhost %s :%s\r\n", nick, nick, host, fullname ? fullname : nick); write(irc, message, strlen(message)); /* login */ } @@ -183,7 +188,7 @@ static size_t tokenize(char **result, size_t reslen, char *str, char delim) { if (!str) return 0; for (n = str; *n == ' '; n++); - p = n; + p = n; for (i = 0; *n != 0;) { if (i == reslen) return 0; @@ -205,11 +210,15 @@ static void print_out(char *channel, char *buf) { FILE *out = NULL; time_t t = time(0); - if (channel) snprintf(server, sizeof(server), "-!- %s", channel); - if (strstr(buf, server)) channel=""; + if (channel) + snprintf(server, sizeof(server), "-!- %s", channel); + if (strstr(buf, server)) + channel=""; create_filepath(outfile, sizeof(outfile), channel, "out"); - if (!(out = fopen(outfile, "a"))) return; - if (channel && channel[0]) add_channel(channel); + if (!(out = fopen(outfile, "a"))) + return; + if (channel && channel[0]) + add_channel(channel); strftime(buft, sizeof(buft), "%F %R", localtime(&t)); fprintf(out, "%s %s\n", buft, buf); @@ -235,10 +244,13 @@ static void proc_channels_input(Channel *c, char *buf) { if (buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) { case 'j': p = strchr(&buf[3], ' '); - if (p) *p = 0; + if (p) + *p = 0; if ((buf[3]=='#')||(buf[3]=='&')||(buf[3]=='+')||(buf[3]=='!')){ - if (p) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */ - else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]); + if (p) + snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */ + else + snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]); add_channel(&buf[3]); } else if (p) { @@ -313,10 +325,11 @@ static void proc_server_cmd(char *buf) { <crlf> ::= CR LF */ if (buf[0] == ':') { /* check prefix */ - if (!(p = strchr(buf, ' '))) return; + if (!(p = strchr(buf, ' '))) + return; *p = 0; for (++p; *p == ' '; p++); - cmd = p; + cmd = p; argv[TOK_NICKSRV] = &buf[1]; if ((p = strchr(buf, '!'))) { *p = 0; @@ -391,7 +404,7 @@ static int read_line(int fd, size_t res_len, char *buf) { buf[i++] = c; } while (c != '\n' && i < res_len); - buf[i - 1] = 0; /* eliminates '\n' */ + buf[i - 1] = 0; /* eliminates '\n' */ return 0; } @@ -477,17 +490,32 @@ int main(int argc, char *argv[]) { } snprintf(nick, sizeof(nick), "%s", spw->pw_name); snprintf(prefix, sizeof(prefix),"%s/irc", spw->pw_dir); - if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) usage(); + if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) + usage(); for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) { switch (argv[i][1]) { - case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break; - case 's': host = argv[++i]; break; - case 'p': port = strtol(argv[++i], NULL, 10); break; - case 'n': snprintf(nick,sizeof(nick),"%s", argv[++i]); break; - case 'k': key = getenv(argv[++i]); break; - case 'f': fullname = argv[++i]; break; - default: usage(); break; + case 'i': + snprintf(prefix,sizeof(prefix),"%s", argv[++i]); + break; + case 's': + host = argv[++i]; + break; + case 'p': + port = strtol(argv[++i], NULL, 10); + break; + case 'n': + snprintf(nick,sizeof(nick),"%s", argv[++i]); + break; + case 'k': + key = getenv(argv[++i]); + break; + case 'f': + fullname = argv[++i]; + break; + default: + usage(); + break; } } irc = tcpopen(port); -- 2.8.0
From e8f777470bcfc98987ea7f84762885409b5517d2 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sun, 3 Apr 2016 00:45:02 +0200 Subject: [PATCH 3/7] query.sh: Backticks are not POSIX compliant --- query.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query.sh b/query.sh index 87a9525..0ffef19 100755 --- a/query.sh +++ b/query.sh @@ -13,7 +13,7 @@ if [ ! -f $TMPFILE ]; then fi echo "searching new query data" -for i in `find $IRCPATH -newer $TMPFILE -name 'out'` +for i in $(find $IRCPATH -newer $TMPFILE -name 'out') do grep -v '\-!\-' $i > /dev/null 2>&1 # if file doesnt just contain server stuff if [ $? -ne 1 ]; then -- 2.8.0
From 099a006ef38084d7c52fa16889c385288ccc3268 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sun, 3 Apr 2016 00:48:14 +0200 Subject: [PATCH 4/7] query.sh: Simplify if statement --- query.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/query.sh b/query.sh index 0ffef19..a97aaec 100755 --- a/query.sh +++ b/query.sh @@ -8,9 +8,7 @@ IRCPATH=$HOME/irc TMPFILE=$IRCPATH/queries.tmp -if [ ! -f $TMPFILE ]; then - touch $TMPFILE -fi +[ -f $TMPFILE ] || touch $TMPFILE echo "searching new query data" for i in $(find $IRCPATH -newer $TMPFILE -name 'out') -- 2.8.0
From 444c18bbe3c3c297a602861f70101221ffde46d1 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sun, 3 Apr 2016 00:49:52 +0200 Subject: [PATCH 5/7] query.sh: Use grep properly --- query.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/query.sh b/query.sh index a97aaec..83c5b51 100755 --- a/query.sh +++ b/query.sh @@ -13,10 +13,11 @@ TMPFILE=$IRCPATH/queries.tmp echo "searching new query data" for i in $(find $IRCPATH -newer $TMPFILE -name 'out') do - grep -v '\-!\-' $i > /dev/null 2>&1 # if file doesnt just contain server stuff + # if file doesnt just contain server stuff + grep -q -v '\-!\-' $i if [ $? -ne 1 ]; then # strip server, nickserv and channel out files - echo $i | egrep -v -i "nickserv|#|$IRCPATH/(irc\.freenode\.net|irc\.oftc\.net)/out" > /dev/null 2>&1 + echo "$i" | grep -q -v -i -E "nickserv|#|$IRCPATH/(irc\.freenode\.net|irc\.oftc\.net)/out" if [ $? -ne 1 ]; then printf "new data in: %s\n========================================================\n" "$i" tail -5 $i -- 2.8.0
From 74e2a58d4664e830cd2cd383d5e1e91216fb3545 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sat, 16 Apr 2016 03:50:16 +0200 Subject: [PATCH 6/7] query.sh: 'tail -5' is deprecated, use 'tail -n5' --- query.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query.sh b/query.sh index 83c5b51..9e2337d 100755 --- a/query.sh +++ b/query.sh @@ -20,7 +20,7 @@ do echo "$i" | grep -q -v -i -E "nickserv|#|$IRCPATH/(irc\.freenode\.net|irc\.oftc\.net)/out" if [ $? -ne 1 ]; then printf "new data in: %s\n========================================================\n" "$i" - tail -5 $i + tail -n5 $i fi fi done -- 2.8.0
From 85af69fc0f7941bea940cb8e98e4ee869c4f11f1 Mon Sep 17 00:00:00 2001 From: Klemens Nanni <[email protected]> Date: Sat, 16 Apr 2016 04:00:35 +0200 Subject: [PATCH 7/7] query.sh: Quote properly, use ${var} not $var --- query.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/query.sh b/query.sh index 9e2337d..72657b3 100755 --- a/query.sh +++ b/query.sh @@ -5,24 +5,24 @@ # Purpose: locate new queries for the ii irc client # ---------------------------------------------------- -IRCPATH=$HOME/irc -TMPFILE=$IRCPATH/queries.tmp +IRCPATH="${HOME}"/irc +TMPFILE="${IRCPATH}"/queries.tmp -[ -f $TMPFILE ] || touch $TMPFILE +[ -f "${TMPFILE}" ] || touch "${TMPFILE}" echo "searching new query data" -for i in $(find $IRCPATH -newer $TMPFILE -name 'out') +for i in $(find "${IRCPATH}" -newer $TMPFILE -name out) do # if file doesnt just contain server stuff grep -q -v '\-!\-' $i if [ $? -ne 1 ]; then # strip server, nickserv and channel out files - echo "$i" | grep -q -v -i -E "nickserv|#|$IRCPATH/(irc\.freenode\.net|irc\.oftc\.net)/out" + echo "${i}" | grep -q -v -i -E "nickserv|#|${IRCPATH}/(irc\.freenode\.net|irc\.oftc\.net)/out" if [ $? -ne 1 ]; then - printf "new data in: %s\n========================================================\n" "$i" - tail -n5 $i + printf "new data in: %s\n========================================================\n" "${i}" + tail -n5 "${i}" fi fi done -touch $TMPFILE +touch "${TMPFILE}" -- 2.8.0
signature.asc
Description: PGP signature
