sic/sic.c

225 lines
4.3 KiB
C
Raw Normal View History

2022-09-24 13:49:53 +03:00
/* See LICENSE file for license details. */
#include <sys/select.h>
#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
#include "arg.h"
2014-11-02 11:23:48 +03:00
#include "config.h"
char *argv0;
2014-11-02 11:23:48 +03:00
static char *host = DEFAULT_HOST;
static char *port = DEFAULT_PORT;
static char *password;
static char nick[32];
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;
static FILE *srv;
2017-03-18 21:24:55 +03:00
#undef strlcpy
#include "strlcpy.c"
#include "util.c"
2006-07-17 20:17:31 +04:00
static void
pout(char *channel, char *fmt, ...) {
2014-11-02 11:23:48 +03:00
static char timestr[80];
time_t t;
2007-02-09 18:04:49 +03:00
va_list ap;
va_start(ap, fmt);
vsnprintf(bufout, sizeof bufout, fmt, ap);
2007-02-09 18:04:49 +03:00
va_end(ap);
t = time(NULL);
2014-11-02 11:23:48 +03:00
strftime(timestr, sizeof timestr, TIMESTAMP_FORMAT, localtime(&t));
fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
2007-02-09 18:04:49 +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
}
static void
2006-10-09 09:20:07 +04:00
privmsg(char *channel, char *msg) {
if(channel[0] == '\0') {
pout("", "No channel to send to");
return;
}
pout(channel, "<%s> %s", nick, msg);
sout("PRIVMSG %s :%s", channel, msg);
2006-07-17 20:17:31 +04:00
}
static void
parsein(char *s) {
char c, *p;
if(s[0] == '\0')
2006-07-21 12:05:51 +04:00
return;
skip(s, '\n');
2014-11-12 13:18:50 +03:00
if(s[0] != COMMAND_PREFIX_CHARACTER) {
privmsg(channel, s);
2006-07-17 20:17:31 +04:00
return;
}
c = *++s;
if(c != '\0' && isspace((unsigned char)s[1])) {
p = s + 2;
switch(c) {
case 'j':
sout("JOIN %s", p);
if(channel[0] == '\0')
strlcpy(channel, p, sizeof channel);
return;
case 'l':
s = eat(p, isspace, 1);
p = eat(s, isspace, 0);
if(!*s)
s = channel;
if(*p)
*p++ = '\0';
if(!*p)
2014-11-12 13:18:50 +03:00
p = DEFAULT_PARTING_MESSAGE;
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;
}
2006-07-17 20:17:31 +04:00
}
sout("%s", s);
2006-07-18 10:07:07 +04:00
}
static void
parsesrv(char *cmd) {
char *usr, *par, *txt;
2006-10-09 09:20:07 +04:00
usr = host;
if(!cmd || !*cmd)
2006-07-17 20:17:31 +04:00
return;
if(cmd[0] == ':') {
usr = cmd + 1;
cmd = skip(usr, ' ');
if(cmd[0] == '\0')
return;
skip(usr, '!');
}
skip(cmd, '\r');
par = skip(cmd, ' ');
txt = skip(par, ':');
trim(par);
if(!strcmp("PONG", cmd))
2007-02-08 16:51:04 +03:00
return;
if(!strcmp("PRIVMSG", cmd))
pout(par, "<%s> %s", usr, txt);
else if(!strcmp("PING", cmd))
sout("PONG %s", txt);
2007-02-09 16:40:11 +03:00
else {
pout(usr, ">< %s (%s): %s", cmd, par, txt);
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
}
static void
usage(void) {
eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
}
2008-07-06 16:29:19 +04:00
int
main(int argc, char *argv[]) {
2006-07-17 20:17:31 +04:00
struct timeval tv;
2010-03-22 16:29:33 +03:00
const char *user = getenv("USER");
int n;
2006-07-17 20:17:31 +04:00
fd_set rd;
2012-02-05 19:47:38 +04:00
strlcpy(nick, user ? user : "unknown", sizeof nick);
ARGBEGIN {
case 'h':
host = EARGF(usage());
break;
case 'p':
port = EARGF(usage());
break;
case 'n':
strlcpy(nick, EARGF(usage()), sizeof nick);
break;
case 'k':
password = EARGF(usage());
break;
case 'v':
2022-09-24 13:49:53 +03:00
eprint("sic-"VERSION", © 2005-2017 Anselm R Garbe, Jeroen Schot, Kris Maglione, Nico Golde\n");
break;
default:
usage();
} ARGEND;
/* init */
srv = fdopen(dial(host, port), "r+");
if (!srv)
eprint("fdopen:");
2006-07-17 20:17:31 +04:00
/* login */
if(password)
sout("PASS %s", password);
sout("NICK %s", nick);
2012-02-05 19:47:38 +04:00
sout("USER %s localhost %s :%s", nick, host, nick);
fflush(srv);
setbuf(stdout, NULL);
setbuf(srv, NULL);
setbuf(stdin, NULL);
2017-03-18 21:24:55 +03:00
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1)
eprint("error: pledge:");
#endif
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);
FD_SET(fileno(srv), &rd);
2006-07-17 20:17:31 +04:00
tv.tv_sec = 120;
tv.tv_usec = 0;
n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
if(n < 0) {
2006-07-17 20:17:31 +04:00
if(errno == EINTR)
continue;
eprint("sic: error on select():");
2007-02-09 18:04:49 +03:00
}
else if(n == 0) {
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;
}
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
}
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;
}