2009-11-28 15:16:35 +03:00
|
|
|
/* See LICENSE file for license details. */
|
|
|
|
#include <ctype.h>
|
2006-07-17 20:17:31 +04:00
|
|
|
#include <errno.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-17 20:17:31 +04:00
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
static char *host = "irc.oftc.net";
|
2011-03-06 10:37:21 +03:00
|
|
|
static char *port = "6667";
|
2009-11-28 15:16:35 +03:00
|
|
|
static char *password;
|
2007-02-09 18:16:06 +03:00
|
|
|
static char nick[32];
|
2009-11-28 15:16:35 +03:00
|
|
|
static char bufin[4096];
|
|
|
|
static char bufout[4096];
|
2006-07-19 10:29:40 +04:00
|
|
|
static char channel[256];
|
2006-07-18 10:07:07 +04:00
|
|
|
static time_t trespond;
|
2009-11-28 15:16:35 +03:00
|
|
|
static FILE *srv;
|
|
|
|
|
|
|
|
#include "util.c"
|
2006-07-17 20:17:31 +04:00
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
static void
|
|
|
|
pout(char *channel, char *fmt, ...) {
|
|
|
|
static char timestr[18];
|
|
|
|
time_t t;
|
2007-02-09 18:04:49 +03:00
|
|
|
va_list ap;
|
2009-11-28 15:16:35 +03:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(bufout, sizeof bufout, fmt, ap);
|
2007-02-09 18:04:49 +03:00
|
|
|
va_end(ap);
|
2009-11-28 15:16:35 +03:00
|
|
|
t = time(NULL);
|
|
|
|
strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
|
|
|
|
fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
|
2007-02-09 18:04:49 +03:00
|
|
|
}
|
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
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);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
static void
|
2006-10-09 09:20:07 +04:00
|
|
|
privmsg(char *channel, char *msg) {
|
2009-11-28 15:16:35 +03:00
|
|
|
if(channel[0] == '\0') {
|
|
|
|
pout("", "No channel to send to");
|
2007-02-08 17:03:42 +03:00
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
}
|
|
|
|
pout(channel, "<%s> %s", nick, msg);
|
|
|
|
sout("PRIVMSG %s :%s", channel, msg);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
static void
|
2010-04-17 14:12:54 +04:00
|
|
|
parsein(char *s) {
|
|
|
|
char c, *p;
|
2009-11-28 15:16:35 +03:00
|
|
|
|
2010-04-17 14:12:54 +04:00
|
|
|
if(s[0] == '\0')
|
2006-07-21 12:05:51 +04:00
|
|
|
return;
|
2010-04-17 14:12:54 +04:00
|
|
|
skip(s, '\n');
|
|
|
|
if(s[0] != ':') {
|
|
|
|
privmsg(channel, s);
|
2006-07-17 20:17:31 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-04-17 14:12:54 +04:00
|
|
|
c = *++s;
|
|
|
|
if(c != '\0' && isspace(s[1])) {
|
|
|
|
p = s + 2;
|
2009-11-28 15:16:35 +03:00
|
|
|
switch(c) {
|
|
|
|
case 'j':
|
2010-04-17 14:12:54 +04:00
|
|
|
sout("JOIN %s", p);
|
2009-11-28 15:16:35 +03:00
|
|
|
if(channel[0] == '\0')
|
2010-04-17 14:12:54 +04:00
|
|
|
strlcpy(channel, p, sizeof channel);
|
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
case 'l':
|
2010-04-17 14:12:54 +04:00
|
|
|
s = eat(p, isspace, 1);
|
|
|
|
p = eat(s, isspace, 0);
|
|
|
|
if(!*s)
|
|
|
|
s = channel;
|
|
|
|
if(*p)
|
|
|
|
*p++ = '\0';
|
2009-11-28 15:16:35 +03:00
|
|
|
if(!*p)
|
2010-04-17 14:12:54 +04:00
|
|
|
p = "sic - 250 LOC are too much!";
|
|
|
|
sout("PART %s :%s", s, p);
|
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
case 'm':
|
2010-04-17 14:12:54 +04:00
|
|
|
s = eat(p, isspace, 1);
|
|
|
|
p = eat(s, isspace, 0);
|
|
|
|
if(*p)
|
|
|
|
*p++ = '\0';
|
|
|
|
privmsg(s, p);
|
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
case 's':
|
2010-04-17 14:12:54 +04:00
|
|
|
strlcpy(channel, p, sizeof channel);
|
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
}
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
2010-04-17 14:12:54 +04:00
|
|
|
sout("%s", s);
|
2006-07-18 10:07:07 +04:00
|
|
|
}
|
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
static void
|
2010-04-17 14:12:54 +04:00
|
|
|
parsesrv(char *cmd) {
|
|
|
|
char *usr, *par, *txt;
|
2006-10-09 09:20:07 +04:00
|
|
|
|
2007-02-13 18:06:28 +03:00
|
|
|
usr = host;
|
2010-04-17 14:12:54 +04:00
|
|
|
if(!cmd || !*cmd)
|
2006-07-17 20:17:31 +04:00
|
|
|
return;
|
2010-04-17 14:12:54 +04:00
|
|
|
if(cmd[0] == ':') {
|
|
|
|
usr = cmd + 1;
|
|
|
|
cmd = skip(usr, ' ');
|
|
|
|
if(cmd[0] == '\0')
|
2007-02-13 18:06:28 +03:00
|
|
|
return;
|
2010-04-17 14:12:54 +04:00
|
|
|
skip(usr, '!');
|
2007-02-13 18:06:28 +03:00
|
|
|
}
|
2010-04-17 14:12:54 +04:00
|
|
|
skip(cmd, '\r');
|
|
|
|
par = skip(cmd, ' ');
|
|
|
|
txt = skip(par, ':');
|
|
|
|
trim(par);
|
2009-11-28 15:16:35 +03:00
|
|
|
if(!strcmp("PONG", cmd))
|
2007-02-08 16:51:04 +03:00
|
|
|
return;
|
2009-11-28 15:16:35 +03:00
|
|
|
if(!strcmp("PRIVMSG", cmd))
|
2010-04-17 14:12:54 +04:00
|
|
|
pout(par, "<%s> %s", usr, txt);
|
2009-11-28 15:16:35 +03:00
|
|
|
else if(!strcmp("PING", cmd))
|
|
|
|
sout("PONG %s", txt);
|
2007-02-09 16:40:11 +03:00
|
|
|
else {
|
2010-04-17 14:12:54 +04:00
|
|
|
pout(usr, ">< %s (%s): %s", cmd, par, txt);
|
2009-11-28 15:16:35 +03:00
|
|
|
if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
|
|
|
|
strlcpy(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
|
2009-11-28 15:16:35 +03:00
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
int i, c;
|
2006-07-17 20:17:31 +04:00
|
|
|
struct timeval tv;
|
2010-03-22 16:29:33 +03:00
|
|
|
const char *user = getenv("USER");
|
2006-07-17 20:17:31 +04:00
|
|
|
fd_set rd;
|
|
|
|
|
2010-03-22 16:29:33 +03:00
|
|
|
strlcpy(nick, user ? user : "unknown", sizeof nick);
|
2009-11-28 15:16:35 +03:00
|
|
|
for(i = 1; i < argc; i++) {
|
|
|
|
c = argv[i][1];
|
|
|
|
if(argv[i][0] != '-' || argv[i][2])
|
|
|
|
c = -1;
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
2007-02-08 22:42:29 +03:00
|
|
|
if(++i < argc) host = argv[i];
|
2009-11-28 15:16:35 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
2009-02-14 14:53:47 +03:00
|
|
|
if(++i < argc) port = argv[i];
|
2009-11-28 15:16:35 +03:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if(++i < argc) strlcpy(nick, argv[i], sizeof nick);
|
|
|
|
break;
|
|
|
|
case 'k':
|
2006-10-23 12:56:06 +04:00
|
|
|
if(++i < argc) password = argv[i];
|
2009-02-14 14:53:47 +03:00
|
|
|
break;
|
2009-11-28 15:16:35 +03:00
|
|
|
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");
|
|
|
|
}
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
2009-11-28 15:16:35 +03:00
|
|
|
/* init */
|
|
|
|
i = dial(host, port);
|
|
|
|
srv = fdopen(i, "r+");
|
2006-07-17 20:17:31 +04:00
|
|
|
/* login */
|
2009-11-28 15:16:35 +03:00
|
|
|
if(password)
|
|
|
|
sout("PASS %s", password);
|
|
|
|
sout("NICK %s", nick);
|
|
|
|
sout("USER %s localhost %s :%s", nick, host, nick);
|
|
|
|
fflush(srv);
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
setbuf(srv, NULL);
|
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);
|
2009-11-28 15:16:35 +03:00
|
|
|
FD_SET(fileno(srv), &rd);
|
2006-07-17 20:17:31 +04:00
|
|
|
tv.tv_sec = 120;
|
|
|
|
tv.tv_usec = 0;
|
2009-11-28 15:16:35 +03:00
|
|
|
i = select(fileno(srv) + 1, &rd, 0, 0, &tv);
|
2006-07-18 10:07:07 +04:00
|
|
|
if(i < 0) {
|
2006-07-17 20:17:31 +04:00
|
|
|
if(errno == EINTR)
|
|
|
|
continue;
|
2009-11-28 15:16:35 +03:00
|
|
|
eprint("sic: error on select():");
|
2007-02-09 18:04:49 +03:00
|
|
|
}
|
|
|
|
else if(i == 0) {
|
2009-11-28 15:16:35 +03:00
|
|
|
if(time(NULL) - trespond >= 300)
|
|
|
|
eprint("sic shutting down: parse timeout\n");
|
|
|
|
sout("PING %s", host);
|
2006-07-17 20:17:31 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-28 15:16:35 +03:00
|
|
|
if(FD_ISSET(fileno(srv), &rd)) {
|
|
|
|
if(fgets(bufin, sizeof bufin, srv) == NULL)
|
|
|
|
eprint("sic: remote host closed connection\n");
|
2006-07-18 10:07:07 +04:00
|
|
|
parsesrv(bufin);
|
|
|
|
trespond = time(NULL);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
2009-11-28 15:16:35 +03:00
|
|
|
if(FD_ISSET(0, &rd)) {
|
|
|
|
if(fgets(bufin, sizeof bufin, stdin) == NULL)
|
|
|
|
eprint("sic: broken pipe\n");
|
2006-07-18 10:07:07 +04:00
|
|
|
parsein(bufin);
|
2006-07-17 20:17:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|