15 Commits
1.0 ... 1.1

6 changed files with 215 additions and 175 deletions

View File

@ -7,3 +7,4 @@ d7923d9e717c1c6f1ed3b17ec90bfdd7e7bfcca0 0.6
643a6e8b8634b70d2459637fcfff6eca776fc919 0.7 643a6e8b8634b70d2459637fcfff6eca776fc919 0.7
07fb3efaa2e9ed18c6c16f0ddd8576cb66fec9c6 0.8 07fb3efaa2e9ed18c6c16f0ddd8576cb66fec9c6 0.8
96eb1bfede5b72fcee3f515d3113d814f7e87108 0.9 96eb1bfede5b72fcee3f515d3113d814f7e87108 0.9
b8794f3ed15e34b24ff9fb11c93a4405d0f91433 1.0

View File

@ -1,6 +1,8 @@
MIT/X Consortium License MIT/X Consortium License
© 2005-2008 Anselm R Garbe <garbeam at gmail dot com> © 2005-2009 Anselm R Garbe <anselm@garbe.us>
© 2008-2009 Jeroen Schot <schot@a-eskwadraat.nl>
© 2007-2009 Kris Maglione <maglione.k@gmail.com>
© 2005 Nico Golde <nico at ngolde dot de> © 2005 Nico Golde <nico at ngolde dot de>
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a

View File

@ -1,5 +1,5 @@
# sic version # sic version
VERSION = 1.0 VERSION = 1.1
# Customize below to fit your system # Customize below to fit your system

2
sic.1
View File

@ -17,7 +17,7 @@ different channel buffers, that's actually a feature.
.SH OPTIONS .SH OPTIONS
.TP .TP
.B \-h <host> .B \-h <host>
Overrides the default host (irc.oftc.net) Overrides the default host (irc6.oftc.net)
.TP .TP
.B \-p <port> .B \-p <port>
Overrides the default port (6667) Overrides the default port (6667)

307
sic.c
View File

@ -1,242 +1,205 @@
/* © 2005-2008 Anselm R Garbe <garbeam at gmail dot com> /* See LICENSE file for license details. */
* © 2005 Nico Golde <nico at ngolde dot de> #include <ctype.h>
* See LICENSE file for license details. */
#include <errno.h> #include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h>
#include <sys/time.h>
#define PINGTIMEOUT 300
#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 char *port = "ircd";
static char *password = NULL; static char *password;
static char nick[32]; static char nick[32];
static char bufin[4096];
static char bufin[MAXMSG], bufout[MAXMSG]; static char bufout[4096];
static char channel[256]; static char channel[256];
static int srv;
static time_t trespond; static time_t trespond;
static FILE *srv;
void #include "util.c"
die(const char *errstr, ...) {
static void
pout(char *channel, char *fmt, ...) {
static char timestr[18];
time_t t;
va_list ap; va_list ap;
va_start(ap, errstr); va_start(ap, fmt);
vfprintf(stderr, errstr, ap); vsnprintf(bufout, sizeof bufout, fmt, ap);
va_end(ap); va_end(ap);
exit(EXIT_FAILURE); t = time(NULL);
}
void
printl(char *channel, char *msg) {
static char timestr[18];
time_t t = time(0);
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, "%-12s: %s %s\n", channel, timestr, bufout);
} }
void static void
sout(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(bufout, sizeof bufout, fmt, ap);
va_end(ap);
fprintf(srv, "%s\r\n", bufout);
}
static void
privmsg(char *channel, char *msg) { privmsg(char *channel, char *msg) {
if(channel[0] == 0) if(channel[0] == '\0') {
pout("", "No channel to send to");
return; return;
snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg); }
printl(channel, bufout); pout(channel, "<%s> %s", nick, msg);
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg); sout("PRIVMSG %s :%s", channel, msg);
write(srv, bufout, strlen(bufout));
} }
void static void
parsein(char *msg) { parsein(char *s) {
char *p; char c, *p;
if(msg[0] == 0) if(s[0] == '\0')
return; return;
if(msg[0] != ':') { skip(s, '\n');
privmsg(channel, msg); if(s[0] != ':') {
privmsg(channel, s);
return; return;
} }
if(!strncmp(msg + 1, "j ", 2) && (msg[3] == '#')) c = *++s;
snprintf(bufout, sizeof bufout, "JOIN %s\r\n", msg + 3); if(c != '\0' && isspace(s[1])) {
else if(!strncmp(msg + 1, "l ", 2)) p = s + 2;
snprintf(bufout, sizeof bufout, "PART %s :sic - 250 LOC are too much!\r\n", msg + 3); switch(c) {
else if(!strncmp(msg + 1, "m ", 2)) { case 'j':
if((p = strchr(msg + 3, ' '))) sout("JOIN %s", p);
*(p++) = 0; if(channel[0] == '\0')
privmsg(msg + 3, p); strlcpy(channel, p, sizeof channel);
return; return;
case 'l':
s = eat(p, isspace, 1);
p = eat(s, isspace, 0);
if(!*s)
s = channel;
if(*p)
*p++ = '\0';
if(!*p)
p = "sic - 250 LOC are too much!";
sout("PART %s :%s", s, p);
return;
case 'm':
s = eat(p, isspace, 1);
p = eat(s, isspace, 0);
if(*p)
*p++ = '\0';
privmsg(s, p);
return;
case 's':
strlcpy(channel, p, sizeof channel);
return;
}
} }
else if(!strncmp(msg + 1, "s ", 2)) { sout("%s", s);
strncpy(channel, msg + 3, sizeof channel);
return;
}
else
snprintf(bufout, sizeof bufout, "%s\r\n", msg + 1);
write(srv, bufout, strlen(bufout));
} }
void static void
parsesrv(char *msg) { parsesrv(char *cmd) {
char *chan, *cmd, *p, *txt, *usr; char *usr, *par, *txt;
txt = NULL;
usr = host; usr = host;
if(!msg || !(*msg)) if(!cmd || !*cmd)
return; return;
if(msg[0] != ':') if(cmd[0] == ':') {
cmd = msg; usr = cmd + 1;
else { cmd = skip(usr, ' ');
if(!(p = strchr(msg, ' '))) if(cmd[0] == '\0')
return; return;
*p = 0; skip(usr, '!');
usr = msg + 1;
cmd = ++p;
if((p = strchr(usr, '!')))
*p = 0;
} }
for(p = cmd; *p; p++) /* remove CRLFs */ skip(cmd, '\r');
if(*p == '\r' || *p == '\n') par = skip(cmd, ' ');
*p = 0; txt = skip(par, ':');
if((p = strchr(cmd, ':'))) { trim(par);
*p = 0; if(!strcmp("PONG", cmd))
txt = ++p;
}
if(!strncmp("PONG", cmd, 4))
return; return;
if(!strncmp("PRIVMSG", cmd, 7) && txt) { if(!strcmp("PRIVMSG", cmd))
if(!(p = strchr(cmd, ' '))) pout(par, "<%s> %s", usr, txt);
return; else if(!strcmp("PING", cmd))
*p = 0; sout("PONG %s", txt);
chan = ++p;
for(; *p && *p != ' '; p++);
*p = 0;
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 { else {
snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : ""); pout(usr, ">< %s (%s): %s", cmd, par, txt);
printl(usr, bufout); if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt) strlcpy(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, c;
struct timeval tv; struct timeval tv;
struct hostent *hp; const char *user = getenv("USER");
static struct sockaddr_in addr; /* initially filled with 0's */
char ping[256];
fd_set rd; fd_set rd;
strncpy(nick, getenv("USER"), sizeof nick); strlcpy(nick, user ? user : "unknown", sizeof nick);
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++) {
if(!strncmp(argv[i], "-h", 3)) { c = argv[i][1];
if(argv[i][0] != '-' || argv[i][2])
c = -1;
switch(c) {
case 'h':
if(++i < argc) host = argv[i]; if(++i < argc) host = argv[i];
} break;
else if(!strncmp(argv[i], "-p", 3)) { case 'p':
if(++i < argc) port = (unsigned short)atoi(argv[i]); if(++i < argc) port = argv[i];
} break;
else if(!strncmp(argv[i], "-n", 3)) { case 'n':
if(++i < argc) strncpy(nick, argv[i], sizeof nick); if(++i < argc) strlcpy(nick, argv[i], sizeof nick);
} break;
else if(!strncmp(argv[i], "-k", 3)) { case 'k':
if(++i < argc) password = argv[i]; if(++i < argc) password = argv[i];
break;
case 'v':
eprint("sic-"VERSION", © 2005-2009 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
default:
eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
} }
else if(!strncmp(argv[i], "-v", 3))
die("sic-"VERSION", © 2005-2008 Anselm R Garbe, Nico Golde\n");
else
die("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
/* init */
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("error: cannot connect host '%s'\n", host);
if(NULL == (hp = gethostbyname(host)))
die("error: cannot resolve hostname '%s'\n", host);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
close(srv);
die("error: cannot connect host '%s'\n", host);
} }
/* init */
i = dial(host, port);
srv = fdopen(i, "r+");
/* login */ /* login */
if(password) if(password)
snprintf(bufout, sizeof bufout, sout("PASS %s", password);
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", sout("NICK %s", nick);
password, nick, nick, host, nick); sout("USER %s localhost %s :%s", nick, host, nick);
else fflush(srv);
snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n", setbuf(stdout, NULL);
nick, nick, host, nick); setbuf(srv, NULL);
write(srv, bufout, strlen(bufout));
snprintf(ping, sizeof ping, "PING %s\r\n", host);
channel[0] = 0;
setbuf(stdout, NULL); /* unbuffered stdout */
for(;;) { /* main loop */ for(;;) { /* main loop */
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(0, &rd); FD_SET(0, &rd);
FD_SET(srv, &rd); FD_SET(fileno(srv), &rd);
tv.tv_sec = 120; tv.tv_sec = 120;
tv.tv_usec = 0; tv.tv_usec = 0;
i = select(srv + 1, &rd, 0, 0, &tv); i = select(fileno(srv) + 1, &rd, 0, 0, &tv);
if(i < 0) { if(i < 0) {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
die("error: error on select()"); eprint("sic: error on select():");
} }
else if(i == 0) { else if(i == 0) {
if(time(NULL) - trespond >= PINGTIMEOUT) if(time(NULL) - trespond >= 300)
die("error: sic shutting down: parse timeout"); eprint("sic shutting down: parse timeout\n");
write(srv, ping, strlen(ping)); sout("PING %s", host);
continue; continue;
} }
if(FD_ISSET(srv, &rd)) { if(FD_ISSET(fileno(srv), &rd)) {
if(readl(srv, sizeof bufin, bufin) == -1) if(fgets(bufin, sizeof bufin, srv) == NULL)
die("error: remote host closed connection"); eprint("sic: 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)) {
if(readl(0, sizeof bufin, bufin) == -1) if(fgets(bufin, sizeof bufin, stdin) == NULL)
die("error: broken pipe"); eprint("sic: broken pipe\n");
parsein(bufin); parsein(bufin);
} }
} }

74
util.c Normal file
View File

@ -0,0 +1,74 @@
/* See LICENSE file for license details. */
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
static void
eprint(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(bufout, sizeof bufout, fmt, ap);
va_end(ap);
fprintf(stderr, "%s", bufout);
if(fmt[0] && fmt[strlen(fmt) - 1] == ':')
fprintf(stderr, " %s\n", strerror(errno));
exit(1);
}
static int
dial(char *host, char *port) {
static struct addrinfo hints;
int srv;
struct addrinfo *res, *r;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if(getaddrinfo(host, port, &hints, &res) != 0)
eprint("error: cannot resolve hostname '%s':", host);
for(r = res; r; r = r->ai_next) {
if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
continue;
if(connect(srv, r->ai_addr, r->ai_addrlen) == 0)
break;
close(srv);
}
freeaddrinfo(res);
if(!r)
eprint("error: cannot connect to host '%s'\n", host);
return srv;
}
#define strlcpy _strlcpy
static void
strlcpy(char *to, const char *from, int l) {
memccpy(to, from, '\0', l);
to[l-1] = '\0';
}
static char *
eat(char *s, int (*p)(int), int r) {
while(s != '\0' && p(*s) == r)
s++;
return s;
}
static char*
skip(char *s, char c) {
while(*s != c && *s != '\0')
s++;
if(*s != '\0')
*s++ = '\0';
return s;
}
static void
trim(char *s) {
char *e;
e = s + strlen(s) - 1;
while(isspace(*e) && e > s)
e--;
*(e + 1) = '\0';
}