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