1 /* See LICENSE file for license details. */
11 static char *host = "irc.oftc.net";
12 static char *port = "ircd";
13 static char *password;
15 static char bufin[4096];
16 static char bufout[4096];
17 static char channel[256];
18 static time_t trespond;
24 pout(char *channel, char *fmt, ...) {
25 static char timestr[18];
30 vsnprintf(bufout, sizeof bufout, fmt, ap);
33 strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
34 fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
38 sout(char *fmt, ...) {
42 vsnprintf(bufout, sizeof bufout, fmt, ap);
44 fprintf(srv, "%s\r\n", bufout);
48 privmsg(char *channel, char *msg) {
49 if(channel[0] == '\0') {
50 pout("", "No channel to send to");
53 pout(channel, "<%s> %s", nick, msg);
54 sout("PRIVMSG %s :%s", channel, msg);
64 msg = ctok(&msg, '\n');
66 privmsg(channel, msg);
70 if(!c || !isspace(msg[1]))
78 if(channel[0] == '\0')
79 strlcpy(channel, msg, sizeof channel);
86 msg = "sic - 250 LOC are too much!";
87 sout("PART %s :%s", p, msg);
94 strlcpy(channel, msg, sizeof channel);
97 sout("%c %s", c, msg);
104 parsesrv(char *msg) {
105 char *cmd, *p, *usr, *txt;
117 txt = ctok(&msg, '\r');
118 msg = ctok(&txt, ':');
120 if(!strcmp("PONG", cmd))
122 if(!strcmp("PRIVMSG", cmd))
123 pout(msg, "<%s> %s", usr, txt);
124 else if(!strcmp("PING", cmd))
125 sout("PONG %s", txt);
127 pout(usr, ">< %s: %s", cmd, txt);
128 if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
129 strlcpy(nick, txt, sizeof nick);
134 main(int argc, char *argv[]) {
137 const char *user = getenv("USER");
140 strlcpy(nick, user ? user : "unknown", sizeof nick);
141 for(i = 1; i < argc; i++) {
143 if(argv[i][0] != '-' || argv[i][2])
147 if(++i < argc) host = argv[i];
150 if(++i < argc) port = argv[i];
153 if(++i < argc) strlcpy(nick, argv[i], sizeof nick);
156 if(++i < argc) password = argv[i];
159 eprint("sic-"VERSION", © 2005-2009 Kris Maglione, Anselm R. Garbe, Nico Golde\n");
161 eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
165 i = dial(host, port);
166 srv = fdopen(i, "r+");
169 sout("PASS %s", password);
170 sout("NICK %s", nick);
171 sout("USER %s localhost %s :%s", nick, host, nick);
173 setbuf(stdout, NULL);
175 for(;;) { /* main loop */
178 FD_SET(fileno(srv), &rd);
181 i = select(fileno(srv) + 1, &rd, 0, 0, &tv);
185 eprint("sic: error on select():");
188 if(time(NULL) - trespond >= 300)
189 eprint("sic shutting down: parse timeout\n");
190 sout("PING %s", host);
193 if(FD_ISSET(fileno(srv), &rd)) {
194 if(fgets(bufin, sizeof bufin, srv) == NULL)
195 eprint("sic: remote host closed connection\n");
197 trespond = time(NULL);
199 if(FD_ISSET(0, &rd)) {
200 if(fgets(bufin, sizeof bufin, stdin) == NULL)
201 eprint("sic: broken pipe\n");