updated sic

This commit is contained in:
anselm@aab 2008-07-06 13:29:19 +01:00
parent 589cce35c5
commit 8a58b25cb7
2 changed files with 52 additions and 45 deletions

View File

@ -12,10 +12,9 @@ INCS = -I. -I/usr/include
LIBS = -L/usr/lib -lc LIBS = -L/usr/lib -lc
# flags # flags
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\" CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
LDFLAGS = ${LIBS} CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" LDFLAGS = -s ${LIBS}
#LDFLAGS = -g ${LIBS}
# compiler and linker # compiler and linker
CC = cc CC = cc

82
sic.c
View File

@ -1,4 +1,4 @@
/* © 2005-2007 Anselm R. Garbe <garbeam at gmail dot com> /* © 2005-2008 Anselm R Garbe <garbeam at gmail dot com>
* © 2005 Nico Golde <nico at ngolde dot de> * © 2005 Nico Golde <nico at ngolde dot de>
* See LICENSE file for license details. */ * See LICENSE file for license details. */
#include <errno.h> #include <errno.h>
@ -16,6 +16,13 @@
#define PINGTIMEOUT 300 #define PINGTIMEOUT 300
#define MAXMSG 4096 #define MAXMSG 4096
static void die(const char *errstr, ...);
static void printl(char *channel, char *msg);
static void privmsg(char *channel, char *msg);
static void parsein(char *msg);
static void parsesrv(char *msg);
static int readl(int fd, unsigned int len, char *buf);
static char *host = "irc.oftc.net"; static char *host = "irc.oftc.net";
static unsigned short port = 6667; static unsigned short port = 6667;
static char *password = NULL; static char *password = NULL;
@ -26,8 +33,8 @@ static char channel[256];
static int srv; static int srv;
static time_t trespond; static time_t trespond;
static void void
eprint(const char *errstr, ...) { die(const char *errstr, ...) {
va_list ap; va_list ap;
va_start(ap, errstr); va_start(ap, errstr);
@ -36,23 +43,8 @@ eprint(const char *errstr, ...) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static int void
getline(int fd, unsigned int len, char *buf) { printl(char *channel, char *msg) {
unsigned int i = 0;
char c;
do {
if(read(fd, &c, sizeof(char)) != sizeof(char))
return -1;
buf[i++] = c;
}
while(c != '\n' && i < len);
buf[i - 1] = 0;
return 0;
}
static void
pout(char *channel, char *msg) {
static char timestr[18]; static char timestr[18];
time_t t = time(0); time_t t = time(0);
@ -60,17 +52,17 @@ pout(char *channel, char *msg) {
fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg); fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg);
} }
static 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); snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
pout(channel, bufout); printl(channel, bufout);
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg); snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
write(srv, bufout, strlen(bufout)); write(srv, bufout, strlen(bufout));
} }
static void void
parsein(char *msg) { parsein(char *msg) {
char *p; char *p;
@ -99,7 +91,7 @@ parsein(char *msg) {
write(srv, bufout, strlen(bufout)); write(srv, bufout, strlen(bufout));
} }
static void void
parsesrv(char *msg) { parsesrv(char *msg) {
char *chan, *cmd, *p, *txt, *usr; char *chan, *cmd, *p, *txt, *usr;
@ -135,7 +127,7 @@ parsesrv(char *msg) {
for(; *p && *p != ' '; p++); for(; *p && *p != ' '; p++);
*p = 0; *p = 0;
snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt); snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt);
pout(chan, bufout); printl(chan, bufout);
} }
else if(!strncmp("PING", cmd, 4) && txt) { else if(!strncmp("PING", cmd, 4) && txt) {
snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt); snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt);
@ -143,12 +135,28 @@ parsesrv(char *msg) {
} }
else { else {
snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : ""); snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : "");
pout(usr, bufout); printl(usr, bufout);
if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt) if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt)
strncpy(nick, txt, sizeof nick); strncpy(nick, txt, sizeof nick);
} }
} }
int
readl(int fd, unsigned int len, char *buf) {
unsigned int i = 0;
char c;
do {
if(read(fd, &c, sizeof(char)) != sizeof(char))
return -1;
buf[i++] = c;
}
while(c != '\n' && i < len);
buf[i - 1] = 0;
return 0;
}
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
int i; int i;
@ -173,21 +181,21 @@ main(int argc, char *argv[]) {
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))
eprint("sic-"VERSION", © 2005-2007 Anselm R. Garbe, Nico Golde\n"); die("sic-"VERSION", © 2005-2008 Anselm R Garbe, Nico Golde\n");
else else
eprint("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");
/* init */ /* init */
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
eprint("sic: cannot connect host '%s'\n", host); die("error: cannot connect host '%s'\n", host);
if(NULL == (hp = gethostbyname(host))) if(NULL == (hp = gethostbyname(host)))
eprint("sic: cannot resolve hostname '%s'\n", host); die("error: cannot resolve hostname '%s'\n", host);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) { if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
close(srv); close(srv);
eprint("sic: cannot connect host '%s'\n", host); die("error: cannot connect host '%s'\n", host);
} }
/* login */ /* login */
if(password) if(password)
@ -212,23 +220,23 @@ main(int argc, char *argv[]) {
if(i < 0) { if(i < 0) {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
eprint("sic: error on select()"); die("error: error on select()");
} }
else if(i == 0) { else if(i == 0) {
if(time(NULL) - trespond >= PINGTIMEOUT) if(time(NULL) - trespond >= PINGTIMEOUT)
eprint("sic shutting down: parse timeout"); die("error: sic shutting down: parse timeout");
write(srv, ping, strlen(ping)); write(srv, ping, strlen(ping));
continue; continue;
} }
if(FD_ISSET(srv, &rd)) { if(FD_ISSET(srv, &rd)) {
if(getline(srv, sizeof bufin, bufin) == -1) if(readl(srv, sizeof bufin, bufin) == -1)
eprint("sic: remote host closed connection"); die("error: remote host closed connection");
parsesrv(bufin); parsesrv(bufin);
trespond = time(NULL); trespond = time(NULL);
} }
if(FD_ISSET(0, &rd)) { if(FD_ISSET(0, &rd)) {
if(getline(0, sizeof bufin, bufin) == -1) if(readl(0, sizeof bufin, bufin) == -1)
eprint("sic: broken pipe"); die("error: broken pipe");
parsein(bufin); parsein(bufin);
} }
} }