]> git.armaanb.net Git - sic.git/blob - sic.c
changing the special command operator
[sic.git] / sic.c
1 /* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2  * (C)opyright MMV-MMVI Nico Golde <nico at ngolde dot de>
3  * See LICENSE file for license details.
4  */
5 #include <errno.h>
6 #include <netdb.h>
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15
16 #define PINGTIMEOUT 300
17 #define MAXMSG 4096
18
19 static char *host = "irc.oftc.net";
20 static unsigned short port = 6667;
21 static char *nick = NULL;
22 static char *fullname = NULL;
23 static char *password = NULL;
24
25 static char bufin[MAXMSG], bufout[MAXMSG];
26 static char channel[256];
27 static int srv;
28 static time_t trespond;
29
30 static int
31 getline(int fd, unsigned int len, char *buf) {
32         unsigned int i = 0;
33         char c;
34
35         do {
36                 if(read(fd, &c, sizeof(char)) != sizeof(char))
37                         return -1;
38                 buf[i++] = c;
39         }
40         while(c != '\n' && i < len);
41         buf[i - 1] = 0;
42         return 0;
43 }
44
45 static void
46 pout(char *channel, char *msg) {
47         static char timestr[18];
48         time_t t = time(0);
49
50         strftime(timestr, sizeof timestr, "%D %R", localtime(&t));
51         fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg);
52 }
53
54 static void
55 privmsg(char *channel, char *msg) {
56         if(channel[0] == 0)
57                 return;
58         snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
59         pout(channel, bufout);
60         snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
61         write(srv, bufout, strlen(bufout));
62 }
63
64 static void
65 parsein(char *msg) {
66         char *p;
67
68         if(msg[0] == 0)
69                 return;
70         if(msg[0] != ':') {
71                 privmsg(channel, msg);
72                 return;
73         }
74         if(!strncmp(msg + 1, "j ", 2) && (msg[3] == '#'))
75                 snprintf(bufout, sizeof bufout, "JOIN %s\r\n", &msg[3]);
76         else if(!strncmp(msg + 1, "l ", 2))
77                 snprintf(bufout, sizeof bufout, "PART %s :sic - 250 LOC are too much!\r\n", &msg[3]);
78         else if(!strncmp(msg + 1, "m ", 2)) {
79                 if((p = strchr(&msg[3], ' ')))
80                         *(p++) = 0;
81                 privmsg(&msg[3], p);
82                 return;
83         }
84         else if(!strncmp(msg + 1, "s ", 2)) {
85                 strncpy(channel, &msg[3], sizeof channel);
86                 return;
87         }
88         else if(!strncmp(msg + 1, "t ", 2)) {
89                 if((p = strchr(&msg[3], ' ')))
90                         *(p++) = 0;
91                 snprintf(bufout, sizeof bufout, "TOPIC %s :%s\r\n", &msg[3], p);
92         }
93         else
94                 snprintf(bufout, sizeof bufout, "%s\r\n", &msg[1]);
95         write(srv, bufout, strlen(bufout));
96 }
97
98 static void
99 parsesrv(char *msg) {
100         char *chan, *cmd, *p, *txt, *usr; 
101
102         txt = NULL;
103         if(!msg || !(*msg))
104                 return;
105         if(msg[0] != ':')
106                 return; /* don't handle prefix-less server commands */
107         if(!(p = strchr(msg, ' ')))
108                 return;
109         usr = &msg[1];
110         *p = 0;
111         cmd = ++p;
112         if((p = strchr(usr, '!')))
113                 *p = 0;
114         for(p = cmd; *p; p++) /* remove CRLFs */
115                 if(*p == '\r' || *p == '\n')
116                         *p = 0;
117         if((p = strchr(cmd, ':'))) {
118                 *p = 0;
119                 txt = ++p;
120         }
121         if(!strncmp("PONG", cmd, 4))
122                 return;
123         if(!strncmp("PRIVMSG", cmd, 7) && txt) {
124                 if(!(p = strchr(cmd, ' ')))
125                         return;
126                 *p = 0;
127                 chan = ++p;
128                 for(; *p && *p != ' '; p++);
129                 *p = 0;
130                 snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt);
131                 pout(chan, bufout);
132         }
133         else if(!strncmp("PING", cmd, 4) && txt) {
134                 snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt);
135                 write(srv, bufout, strlen(bufout));
136         }
137         else {
138                 snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : "");
139                 pout(usr, bufout);
140         }
141 }
142
143 int
144 main(int argc, char *argv[]) {
145         int i;
146         struct timeval tv;
147         struct hostent *hp;
148         static struct sockaddr_in addr;  /* initially filled with 0's */
149         char ping[256];
150         fd_set rd;
151
152         nick = fullname = getenv("USER");
153         for(i = 1; i < argc; i++)
154                 if(!strncmp(argv[i], "-h", 3)) {
155                         if(++i < argc) host = argv[i];
156                 }
157                 else if(!strncmp(argv[i], "-p", 3)) {
158                         if(++i < argc) port = (unsigned short)atoi(argv[i]);
159                 }
160                 else if(!strncmp(argv[i], "-n", 3)) {
161                         if(++i < argc) nick = argv[i];
162                 }
163                 else if(!strncmp(argv[i], "-k", 3)) {
164                         if(++i < argc) password = argv[i];
165                 }
166                 else if(!strncmp(argv[i], "-f", 3)) {
167                         if(++i < argc) fullname = argv[i];
168                 }
169                 else if(!strncmp(argv[i], "-v", 3)) {
170                         fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
171                         exit(EXIT_SUCCESS);
172                 }
173                 else {
174                         fputs("usage: sic [-h host] [-p port] [-n nick]"
175                                         " [-k keyword] [-f fullname] [-v]\n", stderr);
176                         exit(EXIT_FAILURE);
177                 }
178
179         /* init */
180         if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
181                 fprintf(stderr, "sic: cannot connect host '%s'\n", host);
182                 exit(EXIT_FAILURE);
183         }
184         if (NULL == (hp = gethostbyname(host))) {
185                 fprintf(stderr, "sic: cannot resolve hostname '%s'\n", host);
186                 exit(EXIT_FAILURE);
187         }
188         addr.sin_family = AF_INET;
189         addr.sin_port = htons(port);
190         memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
191         if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
192                 close(srv);
193                 fprintf(stderr, "sic: cannot connect host '%s'\n", host);
194                 exit(EXIT_FAILURE);
195         }
196         /* login */
197         if(password)
198                 snprintf(bufout, sizeof bufout,
199                                 "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
200                                 password, nick, nick, host, fullname);
201         else
202                 snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
203                                  nick, nick, host, fullname);
204         write(srv, bufout, strlen(bufout));
205         snprintf(ping, sizeof ping, "PING %s\r\n", host);
206         channel[0] = 0;
207         setbuf(stdout, NULL); /* unbuffered stdout */
208
209         for(;;) { /* main loop */
210                 FD_ZERO(&rd);
211                 FD_SET(0, &rd);
212                 FD_SET(srv, &rd);
213                 tv.tv_sec = 120;
214                 tv.tv_usec = 0;
215                 i = select(srv + 1, &rd, 0, 0, &tv);
216                 if(i < 0) {
217                         if(errno == EINTR)
218                                 continue;
219                         perror("sic: error on select()");
220                         exit(EXIT_FAILURE);
221                 } else if(i == 0) {
222                         if(time(NULL) - trespond >= PINGTIMEOUT) {
223                                 pout(host, "-!- sic shutting down: parse timeout");
224                                 exit(EXIT_FAILURE);
225                         }
226                         write(srv, ping, strlen(ping));
227                         continue;
228                 }
229                 if(FD_ISSET(srv, &rd)) {
230                         if(getline(srv, sizeof bufin, bufin) == -1) {
231                                 perror("sic: remote host closed connection");
232                                 exit(EXIT_FAILURE);
233                         }
234                         parsesrv(bufin);
235                         trespond = time(NULL);
236                 }
237                 if(FD_ISSET(0, &rd)) {
238                         if(getline(0, sizeof bufin, bufin) == -1) {
239                                 perror("sic: broken pipe");
240                                 exit(EXIT_FAILURE);
241                         }
242                         parsein(bufin);
243                 }
244         }
245         return 0;
246 }