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;
30 pout(char *channel, char *fmt, ...) {
31 static char timestr[80];
36 vsnprintf(bufout, sizeof bufout, fmt, ap);
39 strftime(timestr, sizeof timestr, TIMESTAMP_FORMAT, localtime(&t));
40 fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
44 sout(char *fmt, ...) {
48 vsnprintf(bufout, sizeof bufout, fmt, ap);
50 fprintf(srv, "%s\r\n", bufout);
54 privmsg(char *channel, char *msg) {
55 if(channel[0] == '\0') {
56 pout("", "No channel to send to");
59 pout(channel, "<%s> %s", nick, msg);
60 sout("PRIVMSG %s :%s", channel, msg);
70 if(s[0] != COMMAND_PREFIX_CHARACTER) {
75 if(c != '\0' && isspace(s[1])) {
80 if(channel[0] == '\0')
81 strlcpy(channel, p, sizeof channel);
84 s = eat(p, isspace, 1);
85 p = eat(s, isspace, 0);
91 p = DEFAULT_PARTING_MESSAGE;
92 sout("PART %s :%s", s, p);
95 s = eat(p, isspace, 1);
96 p = eat(s, isspace, 0);
102 strlcpy(channel, p, sizeof channel);
110 parsesrv(char *cmd) {
111 char *usr, *par, *txt;
118 cmd = skip(usr, ' ');
124 par = skip(cmd, ' ');
125 txt = skip(par, ':');
127 if(!strcmp("PONG", cmd))
129 if(!strcmp("PRIVMSG", cmd))
130 pout(par, "<%s> %s", usr, txt);
131 else if(!strcmp("PING", cmd))
132 sout("PONG %s", txt);
134 pout(usr, ">< %s (%s): %s", cmd, par, txt);
135 if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
136 strlcpy(nick, txt, sizeof nick);
143 eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
147 main(int argc, char *argv[]) {
149 const char *user = getenv("USER");
153 strlcpy(nick, user ? user : "unknown", sizeof nick);
156 host = EARGF(usage());
159 port = EARGF(usage());
162 strlcpy(nick, EARGF(usage()), sizeof nick);
165 password = EARGF(usage());
168 eprint("sic-"VERSION", © 2005-2014 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
175 srv = fdopen(dial(host, port), "r+");
180 sout("PASS %s", password);
181 sout("NICK %s", nick);
182 sout("USER %s localhost %s :%s", nick, host, nick);
184 setbuf(stdout, NULL);
188 if (pledge("stdio", NULL) == -1)
189 eprint("error: pledge:");
191 for(;;) { /* main loop */
194 FD_SET(fileno(srv), &rd);
197 n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
201 eprint("sic: error on select():");
204 if(time(NULL) - trespond >= 300)
205 eprint("sic shutting down: parse timeout\n");
206 sout("PING %s", host);
209 if(FD_ISSET(fileno(srv), &rd)) {
210 if(fgets(bufin, sizeof bufin, srv) == NULL)
211 eprint("sic: remote host closed connection\n");
213 trespond = time(NULL);
215 if(FD_ISSET(0, &rd)) {
216 if(fgets(bufin, sizeof bufin, stdin) == NULL)
217 eprint("sic: broken pipe\n");