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