2008-07-06 16:29:19 +04:00
|
|
|
/* © 2005-2008 Anselm R Garbe <garbeam at gmail dot com>
|
2007-04-13 13:50:51 +04:00
|
|
|
* © 2005 Nico Golde <nico at ngolde dot de>
|
|
|
|
* See LICENSE file for license details. */
|
2006-07-17 20:17:31 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2007-02-09 18:04:49 +03:00
|
|
|
#include <stdarg.h>
|
2006-07-17 20:17:31 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2006-09-04 10:54:14 +04:00
|
|
|
#include <unistd.h>
|
2006-07-20 19:27:30 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2006-07-18 10:07:07 +04:00
|
|
|
#define PINGTIMEOUT 300
|
2008-07-06 16:29:19 +04:00
|
|
|
#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);
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2007-02-08 22:42:29 +03:00
|
|
|
static char *host = "irc.oftc.net";
|
2006-09-04 10:54:14 +04:00
|
|
|
static unsigned short port = 6667;
|
2006-07-20 11:33:14 +04:00
|
|
|
static char *password = NULL;
|
2007-02-09 18:16:06 +03:00
|
|
|
static char nick[32];
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2006-07-18 10:07:07 +04:00
|
|
|
static char bufin[MAXMSG], bufout[MAXMSG];
|
2006-07-19 10:29:40 +04:00
|
|
|
static char channel[256];
|
2006-07-18 10:07:07 +04:00
|
|
|
static int srv;
|
|
|
|
static time_t trespond;
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
void
|
|
|
|
die(const char *errstr, ...) {
|
2007-02-09 18:04:49 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errstr);
|
|
|
|
vfprintf(stderr, errstr, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
void
|
|
|
|
printl(char *channel, char *msg) {
|
2006-07-18 10:07:07 +04:00
|
|
|
static char timestr[18];
|
2006-07-17 20:17:31 +04:00
|
|
|
time_t t = time(0);
|
|
|
|
|
2007-02-09 17:02:56 +03:00
|
|
|
strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
|
|
|
|
fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
void
|
2006-10-09 09:20:07 +04:00
|
|
|
privmsg(char *channel, char *msg) {
|
2007-02-08 17:03:42 +03:00
|
|
|
if(channel[0] == 0)
|
|
|
|
return;
|
2006-11-26 17:53:33 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
|
2008-07-06 16:29:19 +04:00
|
|
|
printl(channel, bufout);
|
2006-11-26 17:53:33 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
|
2006-07-18 10:07:07 +04:00
|
|
|
write(srv, bufout, strlen(bufout));
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
void
|
2006-10-09 09:20:07 +04:00
|
|
|
parsein(char *msg) {
|
2006-07-17 20:17:31 +04:00
|
|
|
char *p;
|
|
|
|
|
2006-07-21 12:05:51 +04:00
|
|
|
if(msg[0] == 0)
|
|
|
|
return;
|
2007-02-06 18:42:17 +03:00
|
|
|
if(msg[0] != ':') {
|
2006-07-19 10:53:38 +04:00
|
|
|
privmsg(channel, msg);
|
2006-07-17 20:17:31 +04:00
|
|
|
return;
|
|
|
|
}
|
2006-09-19 18:54:52 +04:00
|
|
|
if(!strncmp(msg + 1, "j ", 2) && (msg[3] == '#'))
|
2007-02-09 17:40:58 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "JOIN %s\r\n", msg + 3);
|
2006-09-19 18:54:52 +04:00
|
|
|
else if(!strncmp(msg + 1, "l ", 2))
|
2007-02-09 17:40:58 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "PART %s :sic - 250 LOC are too much!\r\n", msg + 3);
|
2006-09-05 11:47:15 +04:00
|
|
|
else if(!strncmp(msg + 1, "m ", 2)) {
|
2007-02-09 17:40:58 +03:00
|
|
|
if((p = strchr(msg + 3, ' ')))
|
2006-09-20 13:18:27 +04:00
|
|
|
*(p++) = 0;
|
2007-02-09 17:40:58 +03:00
|
|
|
privmsg(msg + 3, p);
|
2006-07-19 13:15:49 +04:00
|
|
|
return;
|
2006-09-05 11:47:15 +04:00
|
|
|
}
|
|
|
|
else if(!strncmp(msg + 1, "s ", 2)) {
|
2007-02-09 17:40:58 +03:00
|
|
|
strncpy(channel, msg + 3, sizeof channel);
|
2006-07-19 10:53:38 +04:00
|
|
|
return;
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
2006-09-05 11:47:15 +04:00
|
|
|
else
|
2007-02-09 17:40:58 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "%s\r\n", msg + 1);
|
2006-07-18 10:07:07 +04:00
|
|
|
write(srv, bufout, strlen(bufout));
|
|
|
|
}
|
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
void
|
2006-10-09 09:20:07 +04:00
|
|
|
parsesrv(char *msg) {
|
2007-02-08 16:51:04 +03:00
|
|
|
char *chan, *cmd, *p, *txt, *usr;
|
2006-10-09 09:20:07 +04:00
|
|
|
|
2007-02-09 17:02:56 +03:00
|
|
|
txt = NULL;
|
2007-02-13 18:06:28 +03:00
|
|
|
usr = host;
|
2006-07-18 10:07:07 +04:00
|
|
|
if(!msg || !(*msg))
|
2006-07-17 20:17:31 +04:00
|
|
|
return;
|
2007-02-09 16:40:11 +03:00
|
|
|
if(msg[0] != ':')
|
2007-02-13 18:06:28 +03:00
|
|
|
cmd = msg;
|
|
|
|
else {
|
|
|
|
if(!(p = strchr(msg, ' ')))
|
|
|
|
return;
|
2006-07-17 20:17:31 +04:00
|
|
|
*p = 0;
|
2007-02-13 18:06:28 +03:00
|
|
|
usr = msg + 1;
|
|
|
|
cmd = ++p;
|
|
|
|
if((p = strchr(usr, '!')))
|
|
|
|
*p = 0;
|
|
|
|
}
|
2007-02-09 17:02:56 +03:00
|
|
|
for(p = cmd; *p; p++) /* remove CRLFs */
|
2006-07-17 20:17:31 +04:00
|
|
|
if(*p == '\r' || *p == '\n')
|
|
|
|
*p = 0;
|
2007-02-09 17:02:56 +03:00
|
|
|
if((p = strchr(cmd, ':'))) {
|
|
|
|
*p = 0;
|
|
|
|
txt = ++p;
|
|
|
|
}
|
2007-02-08 16:51:04 +03:00
|
|
|
if(!strncmp("PONG", cmd, 4))
|
|
|
|
return;
|
2007-02-09 16:42:40 +03:00
|
|
|
if(!strncmp("PRIVMSG", cmd, 7) && txt) {
|
2007-02-08 16:51:04 +03:00
|
|
|
if(!(p = strchr(cmd, ' ')))
|
2007-02-08 16:27:39 +03:00
|
|
|
return;
|
2007-02-08 16:51:04 +03:00
|
|
|
*p = 0;
|
|
|
|
chan = ++p;
|
|
|
|
for(; *p && *p != ' '; p++);
|
|
|
|
*p = 0;
|
2007-02-09 16:40:11 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt);
|
2008-07-06 16:29:19 +04:00
|
|
|
printl(chan, bufout);
|
2007-02-09 16:40:11 +03:00
|
|
|
}
|
|
|
|
else if(!strncmp("PING", cmd, 4) && txt) {
|
|
|
|
snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt);
|
|
|
|
write(srv, bufout, strlen(bufout));
|
|
|
|
}
|
|
|
|
else {
|
2007-02-09 17:18:08 +03:00
|
|
|
snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : "");
|
2008-07-06 16:29:19 +04:00
|
|
|
printl(usr, bufout);
|
2007-02-09 18:16:06 +03:00
|
|
|
if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt)
|
|
|
|
strncpy(nick, txt, sizeof nick);
|
2006-10-09 09:20:07 +04:00
|
|
|
}
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
|
2008-07-06 16:29:19 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-17 20:17:31 +04:00
|
|
|
int
|
2006-10-09 09:20:07 +04:00
|
|
|
main(int argc, char *argv[]) {
|
2006-07-18 10:07:07 +04:00
|
|
|
int i;
|
2006-07-17 20:17:31 +04:00
|
|
|
struct timeval tv;
|
2006-07-18 11:44:41 +04:00
|
|
|
struct hostent *hp;
|
2006-09-04 10:54:14 +04:00
|
|
|
static struct sockaddr_in addr; /* initially filled with 0's */
|
2006-07-20 11:33:14 +04:00
|
|
|
char ping[256];
|
2006-07-17 20:17:31 +04:00
|
|
|
fd_set rd;
|
|
|
|
|
2007-02-09 18:16:06 +03:00
|
|
|
strncpy(nick, getenv("USER"), sizeof nick);
|
2006-10-09 09:26:24 +04:00
|
|
|
for(i = 1; i < argc; i++)
|
2007-02-08 22:42:29 +03:00
|
|
|
if(!strncmp(argv[i], "-h", 3)) {
|
|
|
|
if(++i < argc) host = argv[i];
|
2006-10-23 12:56:06 +04:00
|
|
|
}
|
|
|
|
else if(!strncmp(argv[i], "-p", 3)) {
|
|
|
|
if(++i < argc) port = (unsigned short)atoi(argv[i]);
|
|
|
|
}
|
|
|
|
else if(!strncmp(argv[i], "-n", 3)) {
|
2007-02-09 18:16:06 +03:00
|
|
|
if(++i < argc) strncpy(nick, argv[i], sizeof nick);
|
2006-10-23 12:56:06 +04:00
|
|
|
}
|
|
|
|
else if(!strncmp(argv[i], "-k", 3)) {
|
|
|
|
if(++i < argc) password = argv[i];
|
|
|
|
}
|
2007-02-09 18:04:49 +03:00
|
|
|
else if(!strncmp(argv[i], "-v", 3))
|
2008-07-06 16:29:19 +04:00
|
|
|
die("sic-"VERSION", © 2005-2008 Anselm R Garbe, Nico Golde\n");
|
2007-02-09 18:04:49 +03:00
|
|
|
else
|
2008-07-06 16:29:19 +04:00
|
|
|
die("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2006-07-18 11:44:41 +04:00
|
|
|
/* init */
|
2007-02-09 18:04:49 +03:00
|
|
|
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
2008-07-06 16:29:19 +04:00
|
|
|
die("error: cannot connect host '%s'\n", host);
|
2007-02-09 18:04:49 +03:00
|
|
|
if(NULL == (hp = gethostbyname(host)))
|
2008-07-06 16:29:19 +04:00
|
|
|
die("error: cannot resolve hostname '%s'\n", host);
|
2006-07-18 11:44:41 +04:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
2006-09-04 10:54:14 +04:00
|
|
|
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
2006-07-18 11:44:41 +04:00
|
|
|
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
|
|
|
|
close(srv);
|
2008-07-06 16:29:19 +04:00
|
|
|
die("error: cannot connect host '%s'\n", host);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
/* login */
|
|
|
|
if(password)
|
2006-11-26 17:53:33 +03:00
|
|
|
snprintf(bufout, sizeof bufout,
|
2008-07-06 16:29:19 +04:00
|
|
|
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
|
|
|
|
password, nick, nick, host, nick);
|
2006-07-17 20:17:31 +04:00
|
|
|
else
|
2006-11-26 17:53:33 +03:00
|
|
|
snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
|
2008-07-06 16:29:19 +04:00
|
|
|
nick, nick, host, nick);
|
2006-07-18 10:07:07 +04:00
|
|
|
write(srv, bufout, strlen(bufout));
|
2007-02-08 22:42:29 +03:00
|
|
|
snprintf(ping, sizeof ping, "PING %s\r\n", host);
|
2006-07-19 10:29:40 +04:00
|
|
|
channel[0] = 0;
|
2006-07-18 10:38:28 +04:00
|
|
|
setbuf(stdout, NULL); /* unbuffered stdout */
|
2006-10-09 09:20:07 +04:00
|
|
|
|
|
|
|
for(;;) { /* main loop */
|
2006-07-17 20:17:31 +04:00
|
|
|
FD_ZERO(&rd);
|
|
|
|
FD_SET(0, &rd);
|
2006-07-18 10:07:07 +04:00
|
|
|
FD_SET(srv, &rd);
|
2006-07-17 20:17:31 +04:00
|
|
|
tv.tv_sec = 120;
|
|
|
|
tv.tv_usec = 0;
|
2006-07-18 10:07:07 +04:00
|
|
|
i = select(srv + 1, &rd, 0, 0, &tv);
|
|
|
|
if(i < 0) {
|
2006-07-17 20:17:31 +04:00
|
|
|
if(errno == EINTR)
|
|
|
|
continue;
|
2008-07-06 16:29:19 +04:00
|
|
|
die("error: error on select()");
|
2007-02-09 18:04:49 +03:00
|
|
|
}
|
|
|
|
else if(i == 0) {
|
|
|
|
if(time(NULL) - trespond >= PINGTIMEOUT)
|
2008-07-06 16:29:19 +04:00
|
|
|
die("error: sic shutting down: parse timeout");
|
2006-07-18 10:07:07 +04:00
|
|
|
write(srv, ping, strlen(ping));
|
2006-07-17 20:17:31 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-18 10:07:07 +04:00
|
|
|
if(FD_ISSET(srv, &rd)) {
|
2008-07-06 16:29:19 +04:00
|
|
|
if(readl(srv, sizeof bufin, bufin) == -1)
|
|
|
|
die("error: remote host closed connection");
|
2006-07-18 10:07:07 +04:00
|
|
|
parsesrv(bufin);
|
|
|
|
trespond = time(NULL);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
if(FD_ISSET(0, &rd)) {
|
2008-07-06 16:29:19 +04:00
|
|
|
if(readl(0, sizeof bufin, bufin) == -1)
|
|
|
|
die("error: broken pipe");
|
2006-07-18 10:07:07 +04:00
|
|
|
parsein(bufin);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|