author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:40:11 +0100 | |
changeset 9 | aff6726b8b87 |
parent 6 | 7eb12be31bb5 |
child 10 | 311ea5fa60dd |
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); |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
5
diff
changeset
|
99 |
fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos); |
0 | 100 |
fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password); |
101 |
||
102 |
for (;;) { |
|
103 |
fgets (msg, 512, stream); |
|
104 |
if ((user = irc_parsemessage (msg, &message)) != NULL) |
|
105 |
printf ("%10s %s\n", user, message.line); |
|
106 |
||
107 |
if (strstr (msg, "VERSION") != NULL) { |
|
108 |
printf ("%10s %s", "VERSION", msg); |
|
109 |
printf ("%10s %s %s :%s\n", "WRITE", message.command, message.user, VERSION_STRING); |
|
110 |
fprintf (stream, "VERSION :%s\r\n", VERSION_STRING); |
|
111 |
} |
|
112 |
||
113 |
if (strstr (msg, ":Password accepted") != NULL) { |
|
114 |
break; |
|
115 |
} |
|
116 |
||
1
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
117 |
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
|
118 |
break; |
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
119 |
} |
5d249c79842d
quick and dirty freenode connect fix
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
120 |
|
0 | 121 |
if (strstr (msg, ":Password Incorrect") != NULL) { |
122 |
return IRC_LOGIN_ERROR; |
|
123 |
} |
|
124 |
||
125 |
if (strstr (msg, "ERROR :Closing Link") != NULL) { |
|
126 |
return IRC_GENERAL_ERROR; |
|
127 |
} |
|
128 |
||
129 |
if (strstr (msg, "is not registered") != NULL) { |
|
130 |
return IRC_LOGIN_ERROR; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
sleep (2); |
|
135 |
return 0; |
|
136 |
} |
|
137 |
||
138 |
static char *irc_getmessage (const char *line, MSG * message) |
|
139 |
{ |
|
140 |
char *theLine; |
|
141 |
char *token; |
|
142 |
char *ptr; |
|
143 |
||
144 |
if ((theLine = strdup (line)) == NULL) |
|
145 |
return "ENOMEM"; |
|
146 |
||
147 |
message->user = message->email = NULL; |
|
148 |
message->command = NULL; |
|
149 |
message->channel = message->line = NULL; |
|
150 |
||
151 |
token = strtok (theLine, " "); |
|
152 |
if (*token != ':') { |
|
153 |
/* |
|
154 |
* SERVER MESSAGES |
|
155 |
*/ |
|
156 |
if ((ptr = strtok (NULL, "\r\n")) != NULL) |
|
157 |
++ptr; |
|
158 |
message->line = ptr; |
|
159 |
return token; |
|
160 |
} |
|
161 |
||
162 |
message->user = ++token; |
|
163 |
message->command = strtok (NULL, " "); |
|
164 |
||
165 |
message->line = strtok (NULL, "\r\n"); |
|
166 |
||
167 |
return message->command; |
|
168 |
} |
|
169 |
||
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
170 |
/** |
0 | 171 |
* Main prints ("%10s %s %s\n", MSG->command MSG->channel MSG->line). |
172 |
* This functions makes sure, that there will be someting to print... |
|
173 |
*/ |
|
174 |
char *irc_parsemessage (const char *line, MSG * message) |
|
175 |
{ |
|
176 |
int i; |
|
177 |
char *command; |
|
178 |
char *ptr; |
|
179 |
int value; |
|
180 |
||
181 |
i = 0; |
|
182 |
if ((command = irc_getmessage (line, message)) != NULL) { |
|
183 |
while (IRC_Commands[i] != NULL) { |
|
184 |
if (strcmp (IRC_Commands[i], command) == 0) { |
|
185 |
switch (i) { |
|
186 |
case 0: /* NOTICE */ |
|
187 |
if (message->line == NULL) { |
|
188 |
message->channel = "*"; |
|
189 |
message->line = "*"; |
|
190 |
} else { |
|
191 |
message->channel = strtok (message->line, " "); |
|
192 |
if ((message->line = strtok (NULL, "\r\n")) != NULL) |
|
193 |
++message->line; |
|
194 |
} |
|
195 |
return command; |
|
196 |
case 1: /* MODE */ |
|
197 |
message->channel = strtok (message->line, " "); |
|
198 |
message->line = strtok (NULL, "\r\n"); |
|
199 |
return command; |
|
200 |
case 2: /* JOIN */ |
|
201 |
case 3: /* PART */ |
|
202 |
if ((message->channel = strchr (message->line, ':'))) |
|
203 |
++message->channel; |
|
204 |
message->line = message->user; |
|
205 |
return command; |
|
206 |
case 4: /* TOPIC */ |
|
207 |
message->channel = strtok (message->line, " "); |
|
208 |
if ((message->line = strtok (NULL, "\r\n"))) |
|
209 |
++message->line; |
|
210 |
return command; |
|
211 |
case 5: /* PING */ |
|
212 |
#ifdef DEBUG |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
213 |
/* |
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
214 |
* DONT NERVE WITH PING PONG MESSAGES |
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
215 |
*/ |
0 | 216 |
printf ("%10s %s localhost\n", "PING", message->line); |
217 |
#endif |
|
218 |
fprintf (message->stream, "PONG :%s\r\n", message->line); |
|
219 |
message->channel = "localhost"; |
|
220 |
#ifdef DEBUG |
|
221 |
return "PONG"; |
|
222 |
#else |
|
223 |
return NULL; |
|
224 |
#endif |
|
225 |
case 6: /* ENOMEM */ |
|
226 |
case 7: /* ERROR */ |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
227 |
return command; |
0 | 228 |
case 8: /* VERSION */ |
229 |
if ((ptr = strchr (message->user, ' '))) |
|
230 |
*ptr = 0; |
|
231 |
return command; |
|
232 |
case 9: /* PRIVMSG */ |
|
233 |
if ((message->email = strchr (message->user, '='))) |
|
234 |
++message->email; |
|
235 |
if ((ptr = strchr (message->user, '!'))) |
|
236 |
*ptr = 0; |
|
237 |
||
238 |
message->channel = strtok (message->line, " "); |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
239 |
message->current_channel = message->channel; |
0 | 240 |
message->line = strtok (NULL, "\r\n"); |
241 |
message->line++; |
|
242 |
printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line); |
|
243 |
return NULL; |
|
244 |
case 10: /* QUIT */ |
|
245 |
message->channel = message->user; |
|
246 |
return command; |
|
247 |
case 11: /* NICK */ |
|
248 |
message->channel = message->user; |
|
249 |
return command; |
|
250 |
} |
|
251 |
} |
|
252 |
i++; |
|
253 |
} |
|
254 |
||
255 |
if ((value = atoi (command)) != 0) { |
|
256 |
switch (value) { |
|
257 |
case 1: /* CONNECTION INFOS */ |
|
258 |
case 2: |
|
259 |
case 3: |
|
260 |
case 4: |
|
261 |
case 5: |
|
262 |
case 250: |
|
263 |
case 251: |
|
264 |
case 252: |
|
265 |
case 254: |
|
266 |
case 255: |
|
267 |
case 265: |
|
268 |
case 266: |
|
269 |
/* |
|
270 |
* prints as is in irc_login |
|
271 |
*/ |
|
272 |
return command; |
|
273 |
case 311: |
|
274 |
case 312: |
|
275 |
case 315: /* END OF WHO */ |
|
276 |
case 318: |
|
277 |
message->channel = strtok (message->line, " "); |
|
278 |
message->line = strtok (NULL, "\r\n"); |
|
279 |
return command; |
|
280 |
case 319: |
|
281 |
message->channel = strtok (message->line, " "); |
|
282 |
message->line = strtok (NULL, "\r\n"); |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
283 |
fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line); |
0 | 284 |
return command; |
285 |
case 320: |
|
286 |
case 332: /* TOPIC OF CHANNEL */ |
|
287 |
case 333: /* NAMES IN CHANNEL */ |
|
288 |
message->channel = strtok (message->line, " "); |
|
289 |
message->line = strtok (NULL, "\r\n"); |
|
290 |
return command; |
|
291 |
case 351: /* SVERSION */ |
|
292 |
message->channel = strtok (message->line, " "); |
|
293 |
message->line = strtok (NULL, "\r\n"); |
|
294 |
return command; |
|
295 |
case 352: /* WHO LIST */ |
|
296 |
message->channel = strtok (message->line, " "); |
|
297 |
message->line = strtok (NULL, "\r\n"); |
|
298 |
/* |
|
299 |
* MORE THAN 3 LINES AND YOU WILL be KICKED |
|
300 |
*/ |
|
301 |
return command; |
|
302 |
case 353: |
|
303 |
case 365: |
|
304 |
case 366: /* END OF NAMES */ |
|
305 |
message->channel = strtok (message->line, " "); |
|
306 |
message->line = strtok (NULL, "\r\n"); |
|
307 |
return command; |
|
308 |
case 372: /* MOTD MESSAGES */ |
|
309 |
case 375: |
|
310 |
case 376: /* END OF MOTD */ |
|
311 |
return command; |
|
312 |
break; |
|
313 |
case 401: /* NO SUCH NICK/CHANNEL */ |
|
314 |
case 403: /* That CHANNEL doesnt exist */ |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
315 |
case 441: /* They aren't on this channel */ |
0 | 316 |
return command; |
317 |
case 474: |
|
318 |
case 475: |
|
319 |
case 476: |
|
320 |
case 477: |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
321 |
case 482: |
0 | 322 |
message->channel = strtok (message->line, " "); |
323 |
message->line = strtok (NULL, "\r\n"); |
|
324 |
return command; |
|
2
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
325 |
case 901: /* notify or some crap */ |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
326 |
message->channel = strtok (message->line, " "); |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
327 |
message->line = strtok (NULL, "\r\n"); |
6a2e88214b80
freenode fix indented
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
1
diff
changeset
|
328 |
return command; |
0 | 329 |
default: |
330 |
printf ("DEBUG %s", line); |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
331 |
printf ("Unknown Command Value: %d\n", value); |
0 | 332 |
} |
333 |
} |
|
334 |
printf ("DEBUG %s", line); |
|
335 |
printf ("Unknown Command: %s\n", command); |
|
336 |
return command; |
|
337 |
} |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
338 |
printf ("NOT PARSEABLE %s", line); |
0 | 339 |
return NULL; |
340 |
} |