1 /* See LICENSE file for license details. */
15 static char *host = DEFAULT_HOST;
16 static char *port = DEFAULT_PORT;
17 static char *password;
19 static char bufin[4096];
20 static char bufout[4096];
21 static char channel[256];
22 static time_t trespond;
28 pout(char *channel, char *fmt, ...) {
29 static char timestr[80];
34 vsnprintf(bufout, sizeof bufout, fmt, ap);
37 strftime(timestr, sizeof timestr, TIMESTAMP_FORMAT, localtime(&t));
38 fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
42 sout(char *fmt, ...) {
46 vsnprintf(bufout, sizeof bufout, fmt, ap);
48 fprintf(srv, "%s\r\n", bufout);
52 privmsg(char *channel, char *msg) {
53 if(channel[0] == '\0') {
54 pout("", "No channel to send to");
57 pout(channel, "<%s> %s", nick, msg);
58 sout("PRIVMSG %s :%s", channel, msg);
68 if(s[0] != COMMAND_PREFIX_CHARACTER) {
73 if(c != '\0' && isspace(s[1])) {
78 if(channel[0] == '\0')
79 strlcpy(channel, p, sizeof channel);
82 s = eat(p, isspace, 1);
83 p = eat(s, isspace, 0);
89 p = DEFAULT_PARTING_MESSAGE;
90 sout("PART %s :%s", s, p);
93 s = eat(p, isspace, 1);
94 p = eat(s, isspace, 0);
100 strlcpy(channel, p, sizeof channel);
108 parsesrv(char *cmd) {
109 char *usr, *par, *txt;
116 cmd = skip(usr, ' ');
122 par = skip(cmd, ' ');
123 txt = skip(par, ':');
125 if(!strcmp("PONG", cmd))
127 if(!strcmp("PRIVMSG", cmd))
128 pout(par, "<%s> %s", usr, txt);
129 else if(!strcmp("PING", cmd))
130 sout("PONG %s", txt);
132 pout(usr, ">< %s (%s): %s", cmd, par, txt);
133 if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
134 strlcpy(nick, txt, sizeof nick);
141 eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
145 main(int argc, char *argv[]) {
147 const char *user = getenv("USER");
151 strlcpy(nick, user ? user : "unknown", sizeof nick);
154 host = EARGF(usage());
157 port = EARGF(usage());
160 strlcpy(nick, EARGF(usage()), sizeof nick);
163 password = EARGF(usage());
166 eprint("sic-"VERSION", © 2005-2014 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
173 srv = fdopen(dial(host, port), "r+");
178 sout("PASS %s", password);
179 sout("NICK %s", nick);
180 sout("USER %s localhost %s :%s", nick, host, nick);
182 setbuf(stdout, NULL);
184 for(;;) { /* main loop */
187 FD_SET(fileno(srv), &rd);
190 n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
194 eprint("sic: error on select():");
197 if(time(NULL) - trespond >= 300)
198 eprint("sic shutting down: parse timeout\n");
199 sout("PING %s", host);
202 if(FD_ISSET(fileno(srv), &rd)) {
203 if(fgets(bufin, sizeof bufin, srv) == NULL)
204 eprint("sic: remote host closed connection\n");
206 trespond = time(NULL);
208 if(FD_ISSET(0, &rd)) {
209 if(fgets(bufin, sizeof bufin, stdin) == NULL)
210 eprint("sic: broken pipe\n");