sic

simple irc client
git clone git://git.suckless.org/sic
Log | Files | Refs | README | LICENSE

sic.c (4353B)


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