sic

personal fork with prompts, tmux splits
git clone git://git.garbash.com/alex/sic
Log | Files | Refs | README | LICENSE

sic.c (4503B)


      1  /* See LICENSE file for license details. */
      2 #include <sys/select.h>
      3 
      4 #include <ctype.h>
      5 #include <errno.h>
      6 #include <stdarg.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <time.h>
     11 #include <unistd.h>
     12 
     13 #include "arg.h"
     14 #include "config.h"
     15 
     16 char *argv0;
     17 static char *host = DEFAULT_HOST;
     18 static char *port = DEFAULT_PORT;
     19 static char *password;
     20 static int prompt = 0;
     21 static char nick[32];
     22 static char bufin[4096];
     23 static char bufout[4096];
     24 static char channel[256];
     25 static time_t trespond;
     26 static FILE *srv;
     27 
     28 #undef strlcpy
     29 #include "strlcpy.c"
     30 #include "util.c"
     31 
     32 static void
     33 pout(char *channel, char *fmt, ...) {
     34 	static char timestr[80];
     35 	time_t t;
     36 	va_list ap;
     37 
     38 	va_start(ap, fmt);
     39 	vsnprintf(bufout, sizeof bufout, fmt, ap);
     40 	va_end(ap);
     41 	t = time(NULL);
     42 	strftime(timestr, sizeof timestr, TIMESTAMP_FORMAT, localtime(&t));
     43 	fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
     44 }
     45 
     46 static void
     47 sout(char *fmt, ...) {
     48 	va_list ap;
     49 
     50 	va_start(ap, fmt);
     51 	vsnprintf(bufout, sizeof bufout, fmt, ap);
     52 	va_end(ap);
     53 	fprintf(srv, "%s\r\n", bufout);
     54 }
     55 
     56 static void
     57 privmsg(char *channel, char *msg) {
     58 	if(channel[0] == '\0') {
     59 		pout("", "No channel to send to");
     60 		return;
     61 	}
     62 	pout(channel, "<%s> %s", nick, msg);
     63 	sout("PRIVMSG %s :%s", channel, msg);
     64 }
     65 
     66 static void
     67 parsein(char *s) {
     68 	char c, *p;
     69 
     70 	if(s[0] == '\0')
     71 		return;
     72 	skip(s, '\n');
     73 	if(s[0] != COMMAND_PREFIX_CHARACTER) {
     74 		privmsg(channel, s);
     75 		return;
     76 	}
     77 	c = *++s;
     78 	if(c != '\0' && isspace((unsigned char)s[1])) {
     79 		p = s + 2;
     80 		switch(c) {
     81 		case 'j':
     82 			sout("JOIN %s", p);
     83 			if(channel[0] == '\0')
     84 				strlcpy(channel, p, sizeof channel);
     85 			return;
     86 		case 'l':
     87 			s = eat(p, isspace, 1);
     88 			p = eat(s, isspace, 0);
     89 			if(!*s)
     90 				s = channel;
     91 			if(*p)
     92 				*p++ = '\0';
     93 			if(!*p)
     94 				p = DEFAULT_PARTING_MESSAGE;
     95 			sout("PART %s :%s", s, p);
     96 			return;
     97 		case 'm':
     98 			s = eat(p, isspace, 1);
     99 			p = eat(s, isspace, 0);
    100 			if(*p)
    101 				*p++ = '\0';
    102 			privmsg(s, p);
    103 			return;
    104 		case 's':
    105 			strlcpy(channel, p, sizeof channel);
    106 			return;
    107 		}
    108 	}
    109 	sout("%s", s);
    110 }
    111 
    112 static void
    113 parsesrv(char *cmd) {
    114 	char *usr, *par, *txt;
    115 
    116 	usr = host;
    117 	if(!cmd || !*cmd)
    118 		return;
    119 	if(cmd[0] == ':') {
    120 		usr = cmd + 1;
    121 		cmd = skip(usr, ' ');
    122 		if(cmd[0] == '\0')
    123 			return;
    124 		skip(usr, '!');
    125 	}
    126 	skip(cmd, '\r');
    127 	par = skip(cmd, ' ');
    128 	txt = skip(par, ':');
    129 	trim(par);
    130 	if(!strcmp("PONG", cmd))
    131 		return;
    132 	if(!strcmp("PRIVMSG", cmd))
    133 		pout(par, "<%s> %s", usr, txt);
    134 	else if(!strcmp("PING", cmd))
    135 		sout("PONG %s", txt);
    136 	else {
    137 		pout(usr, ">< %s (%s): %s", cmd, par, txt);
    138 		if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
    139 			strlcpy(nick, txt, sizeof nick);
    140 	}
    141 }
    142 
    143 
    144 static void
    145 usage(void) {
    146 	eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
    147 }
    148 
    149 int
    150 main(int argc, char *argv[]) {
    151 	struct timeval tv;
    152 	const char *user = getenv("USER");
    153 	int n;
    154 	fd_set rd;
    155 
    156 	strlcpy(nick, user ? user : "unknown", sizeof nick);
    157 	ARGBEGIN {
    158 	case 'h':
    159 		host = EARGF(usage());
    160 		break;
    161 	case 'p':
    162 		port = EARGF(usage());
    163 		break;
    164 	case 'P':
    165 		prompt = 1;
    166 		break;
    167 	case 'n':
    168 		strlcpy(nick, EARGF(usage()), sizeof nick);
    169 		break;
    170 	case 'k':
    171 		password = EARGF(usage());
    172 		break;
    173 	case 'v':
    174 		eprint("sic-"VERSION", © 2005-2014 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
    175 		break;
    176 	default:
    177 		usage();
    178 	} ARGEND;
    179 
    180 	/* init */
    181 	srv = fdopen(dial(host, port), "r+");
    182 	if (!srv)
    183 		eprint("fdopen:");
    184 	/* login */
    185 	if(password)
    186 		sout("PASS %s", password);
    187 	sout("NICK %s", nick);
    188 	sout("USER %s localhost %s :%s", nick, host, nick);
    189 	fflush(srv);
    190 	setbuf(stdout, NULL);
    191 	setbuf(srv, NULL);
    192 	setbuf(stdin, NULL);
    193 #ifdef __OpenBSD__
    194 	if (pledge("stdio", NULL) == -1)
    195 		eprint("error: pledge:");
    196 #endif
    197 	for(;;) { /* main loop */
    198 		FD_ZERO(&rd);
    199 		FD_SET(0, &rd);
    200 		FD_SET(fileno(srv), &rd);
    201 		tv.tv_sec = 120;
    202 		tv.tv_usec = 0;
    203 		n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
    204 		if(n < 0) {
    205 			if(errno == EINTR)
    206 				continue;
    207 			eprint("sic: error on select():");
    208 		}
    209 		else if(n == 0) {
    210 			if(time(NULL) - trespond >= 300)
    211 				eprint("sic shutting down: parse timeout\n");
    212 			sout("PING %s", host);
    213 			continue;
    214 		}
    215 		if(FD_ISSET(fileno(srv), &rd)) {
    216 			if(fgets(bufin, sizeof bufin, srv) == NULL)
    217 				eprint("sic: remote host closed connection\n");
    218 			parsesrv(bufin);
    219 			trespond = time(NULL);
    220 		}
    221 		if(FD_ISSET(0, &rd)) {
    222 			if(fgets(bufin, sizeof bufin, stdin) == NULL)
    223 				eprint("sic: broken pipe\n");
    224 			parsein(bufin);
    225 			if (prompt)
    226 				fprintf(stderr, "%s> ", channel);
    227 		}
    228 	}
    229 	return 0;
    230 }