applied Martin Kopta's splint patch

This commit is contained in:
Anselm R Garbe 2009-08-07 08:37:09 +01:00
parent b3ed5f414f
commit 4a4f924fee

124
sic.c
View File

@ -13,6 +13,9 @@
#define PINGTIMEOUT 300 #define PINGTIMEOUT 300
#define MAXMSG 4096 #define MAXMSG 4096
#ifndef VERSION
#define VERSION "dev"
#endif
static void die(const char *errstr, ...); static void die(const char *errstr, ...);
static void printl(char *channel, char *msg); static void printl(char *channel, char *msg);
@ -23,7 +26,6 @@ static int readl(int fd, unsigned int len, char *buf);
static char *host = "irc6.oftc.net"; static char *host = "irc6.oftc.net";
static char *port = "6667"; static char *port = "6667";
static char *password = NULL;
static char nick[32]; static char nick[32];
static char bufin[MAXMSG], bufout[MAXMSG]; static char bufin[MAXMSG], bufout[MAXMSG];
@ -34,9 +36,8 @@ static time_t trespond;
void void
die(const char *errstr, ...) { die(const char *errstr, ...) {
va_list ap; va_list ap;
va_start(ap, errstr); va_start(ap, errstr);
vfprintf(stderr, errstr, ap); (void) vfprintf(stderr, errstr, ap);
va_end(ap); va_end(ap);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -45,48 +46,46 @@ void
printl(char *channel, char *msg) { printl(char *channel, char *msg) {
static char timestr[18]; static char timestr[18];
time_t t = time(0); time_t t = time(0);
(void) strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg); fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg);
} }
void void
privmsg(char *channel, char *msg) { privmsg(char *channel, char *msg) {
if(channel[0] == 0) if(channel[0] == '\0')
return; return;
snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg); (void) snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
printl(channel, bufout); printl(channel, bufout);
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg); (void) snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
write(srv, bufout, strlen(bufout)); (void) write(srv, bufout, strlen(bufout));
} }
void void
parsein(char *msg) { parsein(char *msg) {
char *p; char *p = NULL;
if(msg[0] == '\0')
if(msg[0] == 0)
return; return;
if(msg[0] != ':') { if(msg[0] != ':') {
privmsg(channel, msg); privmsg(channel, msg);
return; return;
} }
if(!strncmp(msg + 1, "j ", 2) && (msg[3] == '#')) if(strncmp(msg + 1, "j ", 2) == 0 && (msg[3] == '#'))
snprintf(bufout, sizeof bufout, "JOIN %s\r\n", msg + 3); (void) snprintf(bufout, sizeof bufout, "JOIN %s\r\n", msg + 3);
else if(!strncmp(msg + 1, "l ", 2)) else if(strncmp(msg + 1, "l ", 2) == 0)
snprintf(bufout, sizeof bufout, "PART %s :sic - 250 LOC are too much!\r\n", msg + 3); (void) snprintf(bufout, sizeof bufout, "PART %s :sic - 250 LOC are too much!\r\n", msg + 3);
else if(!strncmp(msg + 1, "m ", 2)) { else if(strncmp(msg + 1, "m ", 2) == 0) {
if((p = strchr(msg + 3, ' '))) if((p = strchr(msg + 3, ' ')))
*(p++) = 0; *(p++) = '\0';
privmsg(msg + 3, p); privmsg(msg + 3, p);
return; return;
} }
else if(!strncmp(msg + 1, "s ", 2)) { else if(strncmp(msg + 1, "s ", 2) == 0) {
strncpy(channel, msg + 3, sizeof channel); strncpy(channel, msg + 3, sizeof channel);
return; return;
} }
else else
snprintf(bufout, sizeof bufout, "%s\r\n", msg + 1); (void) snprintf(bufout, sizeof bufout, "%s\r\n", msg + 1);
write(srv, bufout, strlen(bufout)); (void) write(srv, bufout, strlen(bufout));
} }
void void
@ -95,90 +94,95 @@ parsesrv(char *msg) {
txt = NULL; txt = NULL;
usr = host; usr = host;
if(!msg || !(*msg)) if(msg == NULL || *msg == '\0' )
return; return;
if(msg[0] != ':') if(msg[0] != ':')
cmd = msg; cmd = msg;
else { else {
if(!(p = strchr(msg, ' '))) if(!(p = strchr(msg, ' ')))
return; return;
*p = 0; *p = '\0';
usr = msg + 1; usr = msg + 1;
cmd = ++p; cmd = ++p;
if((p = strchr(usr, '!'))) if((p = strchr(usr, '!')))
*p = 0; *p = '\0';
} }
for(p = cmd; *p; p++) /* remove CRLFs */ for(p = cmd; *p != '\0'; p++) /* remove CRLFs */
if(*p == '\r' || *p == '\n') if(*p == '\r' || *p == '\n')
*p = 0; *p = '\0';
if((p = strchr(cmd, ':'))) { if((p = strchr(cmd, ':'))) {
*p = 0; *p = '\0';
txt = ++p; txt = ++p;
} }
if(!strncmp("PONG", cmd, 4)) if(strncmp("PONG", cmd, 4) == 0)
return; return;
if(!strncmp("PRIVMSG", cmd, 7) && txt) { if(strncmp("PRIVMSG", cmd, 7) == 0 && txt != NULL) {
if(!(p = strchr(cmd, ' '))) if(!(p = strchr(cmd, ' ')))
return; return;
*p = 0; *p = '\0';
chan = ++p; chan = ++p;
for(; *p && *p != ' '; p++); for(; *p != '\0' && *p != ' '; p++);
*p = 0; *p = '\0';
snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt); (void) snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt);
printl(chan, bufout); printl(chan, bufout);
} }
else if(!strncmp("PING", cmd, 4) && txt) { else if(strncmp("PING", cmd, 4) == 0 && txt != NULL) {
snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt); (void) snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt);
write(srv, bufout, strlen(bufout)); (void) write(srv, bufout, strlen(bufout));
} }
else { else {
snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : ""); if (txt != NULL)
(void) snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt);
else
(void) snprintf(bufout, sizeof bufout, ">< %s: ", cmd);
printl(usr, bufout); printl(usr, bufout);
if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt) if(strncmp("NICK", cmd, 4) == 0 && strncmp(usr, nick, sizeof nick) == 0 &&
strncpy(nick, txt, sizeof nick); txt != NULL)
(void) strncpy(nick, txt, sizeof nick);
} }
} }
int int
readl(int fd, unsigned int len, char *buf) { readl(int fd, unsigned int len, char *buf) {
unsigned int i = 0; unsigned int i = 0;
char c; char c = '\0';
do { do {
if(read(fd, &c, sizeof(char)) != sizeof(char)) if(read(fd, &c, sizeof(char)) != (ssize_t) sizeof(char))
return -1; return -1;
buf[i++] = c; buf[i++] = c;
} }
while(c != '\n' && i < len); while(c != '\n' && i < len);
buf[i - 1] = 0; buf[i - 1] = '\0';
return 0; return 0;
} }
int int
main(int argc, char *argv[]) { main(const int argc, char *const argv[]) {
int i; int i;
struct timeval tv; struct timeval tv;
static struct addrinfo hints, *res, *r; static struct addrinfo hints, *res, *r;
char ping[256]; char ping[256];
fd_set rd; fd_set rd;
char *password = NULL;
strncpy(nick, getenv("USER"), sizeof nick); strncpy(nick, getenv("USER"), sizeof nick);
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++)
if(!strncmp(argv[i], "-h", 3)) { if(strncmp(argv[i], "-h", 3) == 0) {
if(++i < argc) host = argv[i]; if(++i < argc) host = argv[i];
} }
else if(!strncmp(argv[i], "-p", 3)) { else if(strncmp(argv[i], "-p", 3) == 0) {
if(++i < argc) port = argv[i]; if(++i < argc) port = argv[i];
} }
else if(!strncmp(argv[i], "-n", 3)) { else if(strncmp(argv[i], "-n", 3) == 0) {
if(++i < argc) strncpy(nick, argv[i], sizeof nick); if(++i < argc) strncpy(nick, argv[i], sizeof nick);
} }
else if(!strncmp(argv[i], "-k", 3)) { else if(strncmp(argv[i], "-k", 3) == 0) {
if(++i < argc) password = argv[i]; if(++i < argc) password = argv[i];
} }
else if(!strncmp(argv[i], "-v", 3)) else if(strncmp(argv[i], "-v", 3) == 0)
die("sic-"VERSION", © 2005-2009 sic engineers\n"); die("sic-%s, © 2005-2009 sic engineers\n", VERSION);
else else
die("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n"); die("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
@ -193,23 +197,23 @@ main(int argc, char *argv[]) {
continue; continue;
if(connect(srv, r->ai_addr, r->ai_addrlen) == 0) if(connect(srv, r->ai_addr, r->ai_addrlen) == 0)
break; break;
close(srv); (void) close(srv);
} }
freeaddrinfo(res); freeaddrinfo(res);
if(!r) if(!r)
die("error: cannot connect to host '%s'\n", host); die("error: cannot connect to host '%s'\n", host);
/* login */ /* login */
if(password) if (password)
snprintf(bufout, sizeof bufout, (void) snprintf(bufout, sizeof bufout,
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
password, nick, nick, host, nick); password, nick, nick, host, nick);
else else
snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n", (void) snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
nick, nick, host, nick); nick, nick, host, nick);
write(srv, bufout, strlen(bufout)); (void) write(srv, bufout, strlen(bufout));
snprintf(ping, sizeof ping, "PING %s\r\n", host); (void) snprintf(ping, sizeof ping, "PING %s\r\n", host);
channel[0] = 0; channel[0] = '\0';
setbuf(stdout, NULL); /* unbuffered stdout */ setbuf(stdout, NULL); /* unbuffered stdout */
for(;;) { /* main loop */ for(;;) { /* main loop */
@ -227,17 +231,17 @@ main(int argc, char *argv[]) {
else if(i == 0) { else if(i == 0) {
if(time(NULL) - trespond >= PINGTIMEOUT) if(time(NULL) - trespond >= PINGTIMEOUT)
die("error: sic shutting down: parse timeout\n"); die("error: sic shutting down: parse timeout\n");
write(srv, ping, strlen(ping)); (void) write(srv, ping, strlen(ping));
continue; continue;
} }
if(FD_ISSET(srv, &rd)) { if(FD_ISSET(srv, &rd) != 0) {
if(readl(srv, sizeof bufin, bufin) == -1) if(readl(srv, sizeof bufin, bufin) == -1)
die("error: remote host closed connection\n"); die("error: remote host closed connection\n");
parsesrv(bufin); parsesrv(bufin);
trespond = time(NULL); trespond = time(NULL);
} }
if(FD_ISSET(0, &rd)) { if(FD_ISSET(0, &rd) != 0) {
if(readl(0, sizeof bufin, bufin) == -1) if(readl(0, (unsigned int) sizeof bufin, bufin) == -1)
die("error: broken pipe\n"); die("error: broken pipe\n");
parsein(bufin); parsein(bufin);
} }