author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:38:49 +0100 | |
changeset 8 | 3152de03758e |
parent 6 | 7eb12be31bb5 |
child 9 | aff6726b8b87 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: irc.c 51 2008-01-10 00:19:39Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/irc.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <unistd.h> |
|
10 |
#include <string.h> |
|
11 |
||
12 |
#include <sys/types.h> |
|
13 |
#include <sys/socket.h> |
|
14 |
#include <netinet/in.h> |
|
15 |
#include <arpa/inet.h> |
|
16 |
#include <netdb.h> |
|
17 |
||
18 |
#include <pwd.h> |
|
19 |
||
20 |
#include <irc.h> |
|
21 |
||
22 |
#define VERSION_STRING "MCBOT on GNU/LINUX" |
|
23 |
||
24 |
const char *IRC_Commands[] = { |
|
25 |
"NOTICE", "MODE", "JOIN", "PART", |
|
26 |
"TOPIC", "PING", "ENOMEM", "ERROR", |
|
27 |
"VERSION", "PRIVMSG", "QUIT", "NICK", |
|
28 |
NULL, |
|
29 |
}; |
|
30 |
||
31 |
FILE *irc_connect (char *server, unsigned int port) |
|
32 |
{ |
|
33 |
struct hostent *he; |
|
34 |
char *ip; |
|
35 |
struct sockaddr_in ca; |
|
36 |
int csocket; |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
37 |
FILE *stream = NULL; |
0 | 38 |
|
39 |
he = gethostbyname (server); |
|
40 |
if (he == NULL) { |
|
41 |
perror ("GETHOSTBYNAME"); |
|
42 |
return NULL; |
|
43 |
} |
|
44 |
||
45 |
if ((ip = inet_ntoa (*((struct in_addr *)he->h_addr_list[0]))) == NULL) { |
|
46 |
perror ("GETHOSTBYNAME"); |
|
47 |
return NULL; |
|
48 |
} else |
|
49 |
printf ("IP: %s\n", ip); |
|
50 |
||
51 |
ca.sin_family = AF_INET; |
|
52 |
ca.sin_addr.s_addr = inet_addr (ip); |
|
53 |
ca.sin_port = htons (port); |
|
54 |
||
55 |
csocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
56 |
if (connect (csocket, (struct sockaddr *)&ca, (socklen_t) sizeof (ca)) == -1) { |
|
57 |
perror ("CONNECT"); |
|
58 |
return NULL; |
|
59 |
} |
|
60 |
||
61 |
/* |
|
62 |
* rw mode,but many seek errors ... |
|
63 |
*/ |
|
5
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
64 |
#ifdef NETBSD |
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
65 |
/* |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
66 |
* BEGIN OF STREAM |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
67 |
*/ |
5
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
68 |
stream = fdopen (csocket, "r+"); |
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
69 |
#else |
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
70 |
/* |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
71 |
* END OF STREAM |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
72 |
*/ |
0 | 73 |
stream = fdopen (csocket, "a+"); |
5
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
74 |
#endif |
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
75 |
csocket = fileno (stream); |
5
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
76 |
|
7c7fc8906920
FreeBSD Stream Handling added and debian control files improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
77 |
printf ("Using filedescriptor %d for stream operations\n", csocket); |
0 | 78 |
return stream; |
79 |
} |
|
80 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
81 |
int irc_login (FILE * stream, char *server, char *nick, char *password) |
0 | 82 |
{ |
83 |
MSG message; |
|
84 |
char msg[513]; |
|
85 |
char *user; |
|
86 |
struct passwd *pwd = NULL; |
|
87 |
||
88 |
if ((user = getenv ("USER")) != NULL) |
|
89 |
if ((pwd = getpwnam (user)) == NULL) |
|
90 |
return IRC_GENERAL_ERROR; |
|
91 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
92 |
if (password == NULL) |
0 | 93 |
return IRC_LOGIN_ERROR; |
94 |
||
95 |
if (stream == NULL) |
|
96 |
return IRC_GENERAL_ERROR; |
|
97 |
||
98 |
fprintf (stream, "NICK %s\r\n", nick); |
|
99 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
100 |
fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos); |
0 | 101 |
|
102 |
fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password); |
|
103 |
||
104 |
for (;;) { |
|
105 |
fgets (msg, 512, stream); |
|
106 |
if ((user = irc_parsemessage (msg, &message)) != NULL) |
|
107 |
printf ("%10s %s\n", user, message.line); |
|
108 |
||
109 |
if (strstr (msg, "VERSION") != NULL) { |
|
110 |
printf ("%10s %s", "VERSION", msg); |
|
111 |
printf ("%10s %s %s :%s\n", "WRITE", message.command, message.user, VERSION_STRING); |
|
112 |
fprintf (stream, "VERSION :%s\r\n", VERSION_STRING); |
|
113 |
} |
|
114 |
||
115 |
if (strstr (msg, ":Password accepted") != NULL) { |
|
116 |
break; |
|
117 |
} |
|
118 |
||
1
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
119 |
if (strstr (msg, ":You are now logged in.") != NULL) { |
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
120 |
break; |
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
121 |
} |
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
122 |
|
0 | 123 |
if (strstr (msg, ":Password Incorrect") != NULL) { |
124 |
return IRC_LOGIN_ERROR; |
|
125 |
} |
|
126 |
||
127 |
if (strstr (msg, "ERROR :Closing Link") != NULL) { |
|
128 |
return IRC_GENERAL_ERROR; |
|
129 |
} |
|
130 |
||
131 |
if (strstr (msg, "is not registered") != NULL) { |
|
132 |
return IRC_LOGIN_ERROR; |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
sleep (2); |
|
137 |
return 0; |
|
138 |
} |
|
139 |
||
140 |
static char *irc_getmessage (const char *line, MSG * message) |
|
141 |
{ |
|
142 |
char *theLine; |
|
143 |
char *token; |
|
144 |
char *ptr; |
|
145 |
||
146 |
if ((theLine = strdup (line)) == NULL) |
|
147 |
return "ENOMEM"; |
|
148 |
||
149 |
message->user = message->email = NULL; |
|
150 |
message->command = NULL; |
|
151 |
message->channel = message->line = NULL; |
|
152 |
||
153 |
token = strtok (theLine, " "); |
|
154 |
if (*token != ':') { |
|
155 |
/* |
|
156 |
* SERVER MESSAGES |
|
157 |
*/ |
|
158 |
if ((ptr = strtok (NULL, "\r\n")) != NULL) |
|
159 |
++ptr; |
|
160 |
message->line = ptr; |
|
161 |
return token; |
|
162 |
} |
|
163 |
||
164 |
message->user = ++token; |
|
165 |
message->command = strtok (NULL, " "); |
|
166 |
||
167 |
message->line = strtok (NULL, "\r\n"); |
|
168 |
||
169 |
return message->command; |
|
170 |
} |
|
171 |
||
172 |
/* |
|
173 |
* Main prints ("%10s %s %s\n", MSG->command MSG->channel MSG->line). |
|
174 |
* This functions makes sure, that there will be someting to print... |
|
175 |
*/ |
|
176 |
||
177 |
char *irc_parsemessage (const char *line, MSG * message) |
|
178 |
{ |
|
179 |
int i; |
|
180 |
char *command; |
|
181 |
char *ptr; |
|
182 |
int value; |
|
183 |
||
184 |
i = 0; |
|
185 |
if ((command = irc_getmessage (line, message)) != NULL) { |
|
186 |
while (IRC_Commands[i] != NULL) { |
|
187 |
if (strcmp (IRC_Commands[i], command) == 0) { |
|
188 |
switch (i) { |
|
189 |
||
190 |
case 0: /* NOTICE */ |
|
191 |
if (message->line == NULL) { |
|
192 |
message->channel = "*"; |
|
193 |
message->line = "*"; |
|
194 |
} else { |
|
195 |
message->channel = strtok (message->line, " "); |
|
196 |
if ((message->line = strtok (NULL, "\r\n")) != NULL) |
|
197 |
++message->line; |
|
198 |
} |
|
199 |
return command; |
|
200 |
case 1: /* MODE */ |
|
201 |
message->channel = strtok (message->line, " "); |
|
202 |
message->line = strtok (NULL, "\r\n"); |
|
203 |
return command; |
|
204 |
case 2: /* JOIN */ |
|
205 |
case 3: /* PART */ |
|
206 |
if ((message->channel = strchr (message->line, ':'))) |
|
207 |
++message->channel; |
|
208 |
message->line = message->user; |
|
209 |
return command; |
|
210 |
case 4: /* TOPIC */ |
|
211 |
message->channel = strtok (message->line, " "); |
|
212 |
if ((message->line = strtok (NULL, "\r\n"))) |
|
213 |
++message->line; |
|
214 |
return command; |
|
215 |
case 5: /* PING */ |
|
216 |
#ifdef DEBUG |
|
217 |
printf ("%10s %s localhost\n", "PING", message->line); |
|
218 |
#endif |
|
219 |
fprintf (message->stream, "PONG :%s\r\n", message->line); |
|
220 |
message->channel = "localhost"; |
|
221 |
#ifdef DEBUG |
|
222 |
return "PONG"; |
|
223 |
#else |
|
224 |
return NULL; |
|
225 |
#endif |
|
226 |
case 6: /* ENOMEM */ |
|
227 |
case 7: /* ERROR */ |
|
228 |
exit (-1); |
|
229 |
break; |
|
230 |
case 8: /* VERSION */ |
|
231 |
if ((ptr = strchr (message->user, ' '))) |
|
232 |
*ptr = 0; |
|
233 |
return command; |
|
234 |
case 9: /* PRIVMSG */ |
|
235 |
if ((message->email = strchr (message->user, '='))) |
|
236 |
++message->email; |
|
237 |
if ((ptr = strchr (message->user, '!'))) |
|
238 |
*ptr = 0; |
|
239 |
||
240 |
message->channel = strtok (message->line, " "); |
|
241 |
message->line = strtok (NULL, "\r\n"); |
|
242 |
message->line++; |
|
243 |
printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line); |
|
244 |
return NULL; |
|
245 |
case 10: /* QUIT */ |
|
246 |
message->channel = message->user; |
|
247 |
return command; |
|
248 |
case 11: /* NICK */ |
|
249 |
message->channel = message->user; |
|
250 |
return command; |
|
251 |
} |
|
252 |
} |
|
253 |
i++; |
|
254 |
} |
|
255 |
||
256 |
if ((value = atoi (command)) != 0) { |
|
257 |
switch (value) { |
|
258 |
case 1: /* CONNECTION INFOS */ |
|
259 |
case 2: |
|
260 |
case 3: |
|
261 |
case 4: |
|
262 |
case 5: |
|
263 |
case 250: |
|
264 |
case 251: |
|
265 |
case 252: |
|
266 |
case 254: |
|
267 |
case 255: |
|
268 |
case 265: |
|
269 |
case 266: |
|
270 |
/* |
|
271 |
* prints as is in irc_login |
|
272 |
*/ |
|
273 |
return command; |
|
274 |
break; |
|
275 |
case 311: |
|
276 |
case 312: |
|
277 |
case 315: /* END OF WHO */ |
|
278 |
case 318: |
|
279 |
message->channel = strtok (message->line, " "); |
|
280 |
message->line = strtok (NULL, "\r\n"); |
|
281 |
return command; |
|
282 |
case 319: |
|
283 |
message->channel = strtok (message->line, " "); |
|
284 |
message->line = strtok (NULL, "\r\n"); |
|
285 |
fprintf (message->stream, "PRIVMSG %s :%s\r\n", "#mcbot", message->line); |
|
286 |
return command; |
|
287 |
case 320: |
|
288 |
case 332: /* TOPIC OF CHANNEL */ |
|
289 |
case 333: /* NAMES IN CHANNEL */ |
|
290 |
message->channel = strtok (message->line, " "); |
|
291 |
message->line = strtok (NULL, "\r\n"); |
|
292 |
return command; |
|
293 |
case 351: /* SVERSION */ |
|
294 |
message->channel = strtok (message->line, " "); |
|
295 |
message->line = strtok (NULL, "\r\n"); |
|
296 |
return command; |
|
297 |
case 352: /* WHO LIST */ |
|
298 |
message->channel = strtok (message->line, " "); |
|
299 |
message->line = strtok (NULL, "\r\n"); |
|
300 |
/* |
|
301 |
* MORE THAN 3 LINES AND YOU WILL be KICKED |
|
302 |
*/ |
|
303 |
return command; |
|
304 |
break; |
|
305 |
case 353: |
|
306 |
case 365: |
|
307 |
case 366: /* END OF NAMES */ |
|
308 |
message->channel = strtok (message->line, " "); |
|
309 |
message->line = strtok (NULL, "\r\n"); |
|
310 |
return command; |
|
311 |
break; |
|
312 |
case 372: /* MOTD MESSAGES */ |
|
313 |
case 375: |
|
314 |
case 376: /* END OF MOTD */ |
|
315 |
return command; |
|
316 |
break; |
|
317 |
case 401: /* NO SUCH NICK/CHANNEL */ |
|
318 |
case 403: /* That CHANNEL doesnt exist */ |
|
319 |
return command; |
|
320 |
break; |
|
321 |
case 474: |
|
322 |
case 475: |
|
323 |
case 476: |
|
324 |
case 477: |
|
325 |
message->channel = strtok (message->line, " "); |
|
326 |
message->line = strtok (NULL, "\r\n"); |
|
327 |
return command; |
|
328 |
break; |
|
2
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
329 |
case 901: /* notify or some crap */ |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
330 |
message->channel = strtok (message->line, " "); |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
331 |
message->line = strtok (NULL, "\r\n"); |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
332 |
return command; |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
333 |
break; |
0 | 334 |
default: |
335 |
printf ("DEBUG %s", line); |
|
336 |
printf ("Unknown Value: %d\n", value); |
|
337 |
} |
|
338 |
} |
|
339 |
printf ("DEBUG %s", line); |
|
340 |
printf ("Unknown Command: %s\n", command); |
|
341 |
return command; |
|
342 |
} |
|
343 |
printf ("DEBUG %s", line); |
|
344 |
return NULL; |
|
345 |
} |