Mercurial > pidgin
annotate src/protocols/irc/parse.c @ 6718:37af5dea14d1
[gaim-migrate @ 7245]
IRC diety schmiety
committer: Tailor Script <tailor@pidgin.im>
author | Ethan Blanton <elb@pidgin.im> |
---|---|
date | Wed, 03 Sep 2003 03:21:27 +0000 |
parents | 0c260c4e753e |
children | 47e49e3c00f4 |
rev | line source |
---|---|
6333 | 1 /** |
2 * @file parse.c | |
3 * | |
4 * gaim | |
5 * | |
6 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 | |
23 #include "internal.h" | |
24 | |
25 #include "accountopt.h" | |
26 #include "conversation.h" | |
27 #include "notify.h" | |
28 #include "debug.h" | |
29 #include "irc.h" | |
30 | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <ctype.h> | |
34 | |
35 static char *irc_send_convert(struct irc_conn *irc, const char *string); | |
36 static char *irc_recv_convert(struct irc_conn *irc, const char *string); | |
37 | |
38 char *irc_mirc2html(const char *string); | |
39 | |
40 static void irc_parse_error_cb(struct irc_conn *irc, char *input); | |
41 | |
42 static char *irc_mirc_colors[16] = { | |
43 "white", "black", "blue", "dark green", "red", "brown", "purple", | |
44 "orange", "yellow", "green", "teal", "cyan", "light blue", | |
45 "pink", "grey", "light grey" }; | |
46 | |
47 /*typedef void (*IRCMsgCallback)(struct irc_conn *irc, char *from, char *name, char **args);*/ | |
48 static struct _irc_msg { | |
49 char *name; | |
50 char *format; | |
51 void (*cb)(struct irc_conn *irc, const char *name, const char *from, char **args); | |
52 } _irc_msgs[] = { | |
53 { "301", "nn:", irc_msg_away }, /* User is away */ | |
54 { "303", "n:", irc_msg_ison }, /* ISON reply */ | |
55 { "311", "nnvvv:", irc_msg_whois }, /* Whois user */ | |
56 { "312", "nnv:", irc_msg_whois }, /* Whois server */ | |
57 { "313", "nn:", irc_msg_whois }, /* Whois ircop */ | |
58 { "317", "nnvv", irc_msg_whois }, /* Whois idle */ | |
59 { "318", "nt:", irc_msg_endwhois }, /* End of WHOIS */ | |
60 { "319", "nn:", irc_msg_whois }, /* Whois channels */ | |
61 { "320", "nn:", irc_msg_whois }, /* Whois (fn ident) */ | |
62 { "324", "ncv:", irc_msg_chanmode }, /* Channel modes */ | |
63 { "331", "nc:", irc_msg_topic }, /* No channel topic */ | |
64 { "332", "nc:", irc_msg_topic }, /* Channel topic */ | |
65 { "333", "*", irc_msg_ignore }, /* Topic setter stuff */ | |
66 { "353", "nvc:", irc_msg_names }, /* Names list */ | |
67 { "366", "nc:", irc_msg_names }, /* End of names */ | |
68 { "372", "n:", irc_msg_motd }, /* MOTD */ | |
69 { "375", "n:", irc_msg_motd }, /* Start MOTD */ | |
70 { "376", "n:", irc_msg_endmotd }, /* End of MOTD */ | |
71 { "401", "nt:", irc_msg_nonick }, /* No such nick/chan */ | |
72 { "404", "nt:", irc_msg_nosend }, /* Cannot send to chan */ | |
73 { "421", "nv:", irc_msg_unknown }, /* Unknown command */ | |
6350 | 74 { "422", "nv:", irc_msg_endmotd }, /* No MOTD available */ |
6333 | 75 { "433", "vn:", irc_msg_nickused }, /* Nickname already in use */ |
6718 | 76 { "438", "nn:", irc_msg_nochangenick }, /* Nick may not change */ |
6333 | 77 { "442", "nc:", irc_msg_notinchan }, /* Not in channel */ |
78 { "473", "nc:", irc_msg_inviteonly }, /* Tried to join invite-only */ | |
79 { "474", "nc:", irc_msg_banned }, /* Banned from channel */ | |
80 { "482", "nc:", irc_msg_notop }, /* Need to be op to do that */ | |
81 { "501", "n:", irc_msg_badmode }, /* Unknown mode flag */ | |
6714 | 82 { "515", "nc:", irc_msg_regonly }, /* Registration required */ |
6333 | 83 { "invite", "n:", irc_msg_invite }, /* Invited */ |
84 { "join", ":", irc_msg_join }, /* Joined a channel */ | |
85 { "kick", "cn:", irc_msg_kick }, /* KICK */ | |
86 { "mode", "tv:", irc_msg_mode }, /* MODE for channel */ | |
87 { "nick", ":", irc_msg_nick }, /* Nick change */ | |
88 { "notice", "t:", irc_msg_notice }, /* NOTICE recv */ | |
89 { "part", "c:", irc_msg_part }, /* Parted a channel */ | |
90 { "ping", ":", irc_msg_ping }, /* Received PING from server */ | |
91 { "pong", "v:", irc_msg_pong }, /* Received PONG from server */ | |
92 { "privmsg", "t:", irc_msg_privmsg }, /* Received private message */ | |
93 { "topic", "c:", irc_msg_topic }, /* TOPIC command */ | |
94 { "quit", ":", irc_msg_quit }, /* QUIT notice */ | |
95 { "wallops", ":", irc_msg_wallops }, /* WALLOPS command */ | |
96 { NULL, NULL, NULL } | |
97 }; | |
98 | |
99 static struct _irc_user_cmd { | |
100 char *name; | |
101 char *format; | |
102 IRCCmdCallback cb; | |
103 } _irc_cmds[] = { | |
104 { "away", ":", irc_cmd_away }, | |
105 { "deop", ":", irc_cmd_op }, | |
106 { "devoice", ":", irc_cmd_op }, | |
6415
e3be6b9744b7
[gaim-migrate @ 6922]
Christian Hammond <chipx86@chipx86.com>
parents:
6350
diff
changeset
|
107 { "help", "v", irc_cmd_help }, |
6333 | 108 { "invite", ":", irc_cmd_invite }, |
109 { "j", "cv", irc_cmd_join }, | |
110 { "join", "cv", irc_cmd_join }, | |
111 { "kick", "n:", irc_cmd_kick }, | |
112 { "me", ":", irc_cmd_ctcp_action }, | |
113 { "mode", ":", irc_cmd_mode }, | |
114 { "msg", "t:", irc_cmd_privmsg }, | |
115 { "names", "c", irc_cmd_names }, | |
116 { "nick", "n", irc_cmd_nick }, | |
117 { "op", ":", irc_cmd_op }, | |
118 { "operwall", ":", irc_cmd_wallops }, | |
119 { "part", "c:", irc_cmd_part }, | |
120 { "ping", "n", irc_cmd_ping }, | |
121 { "query", "n:", irc_cmd_query }, | |
122 { "quit", ":", irc_cmd_quit }, | |
123 { "quote", "*", irc_cmd_quote }, | |
124 { "remove", "n:", irc_cmd_remove }, | |
125 { "topic", ":", irc_cmd_topic }, | |
126 { "umode", ":", irc_cmd_mode }, | |
127 { "voice", ":", irc_cmd_op }, | |
128 { "wallops", ":", irc_cmd_wallops }, | |
129 { "whois", "n", irc_cmd_whois }, | |
130 { NULL, NULL } | |
131 }; | |
132 | |
133 static char *irc_send_convert(struct irc_conn *irc, const char *string) | |
134 { | |
135 char *utf8; | |
136 GError *err = NULL; | |
137 | |
138 utf8 = g_convert(string, strlen(string), | |
139 gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET), | |
140 "UTF-8", NULL, NULL, &err); | |
141 if (err) { | |
142 gaim_debug(GAIM_DEBUG_ERROR, "irc", "send conversion error: %s\n", err->message); | |
143 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Sending raw, which probably isn't right\n"); | |
144 utf8 = g_strdup(string); | |
145 } | |
146 | |
147 return utf8; | |
148 } | |
149 | |
150 static char *irc_recv_convert(struct irc_conn *irc, const char *string) | |
151 { | |
152 char *utf8; | |
153 GError *err = NULL; | |
154 | |
155 utf8 = g_convert(string, strlen(string), "UTF-8", | |
156 gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET), | |
157 NULL, NULL, &err); | |
158 if (err) { | |
159 gaim_debug(GAIM_DEBUG_ERROR, "irc", "recv conversion error: %s\n", err->message); | |
160 utf8 = g_strdup(_("(There was an error converting this message. Check the 'Encoding' option in the Account Editor)")); | |
161 } | |
162 | |
163 return utf8; | |
164 } | |
165 | |
166 /* XXX tag closings are not necessarily correctly nested here! If we | |
167 * get a ^O or reach the end of the string and there are open | |
168 * tags, they are closed in a fixed order ... this means, for | |
169 * example, you might see <FONT COLOR="blue">some text <B>with | |
170 * various attributes</FONT></B> (notice that B and FONT overlap | |
171 * and are not cleanly nested). This is imminently fixable but | |
172 * I am not fixing it right now. | |
173 */ | |
174 char *irc_mirc2html(const char *string) | |
175 { | |
176 const char *cur, *end; | |
177 char fg[3] = "\0\0", bg[3] = "\0\0"; | |
178 int fgnum, bgnum; | |
179 int font = 0, bold = 0; | |
180 GString *decoded = g_string_sized_new(strlen(string)); | |
181 | |
182 cur = string; | |
183 do { | |
184 end = strpbrk(cur, "\002\003\007\017\026"); | |
185 | |
186 decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur)); | |
187 cur = end ? end : cur + strlen(cur); | |
188 | |
189 switch (*cur) { | |
190 case '\002': | |
191 cur++; | |
192 if (!bold) { | |
193 decoded = g_string_append(decoded, "<B>"); | |
194 bold = TRUE; | |
195 } else { | |
196 decoded = g_string_append(decoded, "</B>"); | |
197 bold = FALSE; | |
198 } | |
199 break; | |
200 case '\003': | |
201 cur++; | |
202 fg[0] = fg[1] = bg[0] = bg[1] = '\0'; | |
203 if (isdigit(*cur)) | |
204 fg[0] = *cur++; | |
205 if (isdigit(*cur)) | |
206 fg[1] = *cur++; | |
207 if (*cur == ',') { | |
208 cur++; | |
209 if (isdigit(*cur)) | |
210 bg[0] = *cur++; | |
211 if (isdigit(*cur)) | |
212 bg[1] = *cur++; | |
213 } | |
214 if (font) { | |
215 decoded = g_string_append(decoded, "</FONT>"); | |
216 font = FALSE; | |
217 } | |
218 | |
219 if (fg[0]) { | |
220 fgnum = atoi(fg); | |
221 if (fgnum < 0 || fgnum > 15) | |
222 continue; | |
223 font = TRUE; | |
224 g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]); | |
225 if (bg[0]) { | |
226 bgnum = atoi(bg); | |
227 if (bgnum >= 0 && bgnum < 16) | |
228 g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]); | |
229 } | |
230 decoded = g_string_append_c(decoded, '>'); | |
231 } | |
232 break; | |
233 case '\007': | |
234 case '\026': | |
235 cur++; | |
236 break; | |
237 case '\017': | |
238 cur++; | |
239 /* fallthrough */ | |
240 case '\000': | |
241 if (bold) | |
242 decoded = g_string_append(decoded, "</BOLD>"); | |
243 if (font) | |
244 decoded = g_string_append(decoded, "</FONT>"); | |
245 break; | |
246 default: | |
247 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur); | |
248 } | |
249 } while (*cur); | |
250 | |
251 return g_string_free(decoded, FALSE); | |
252 } | |
253 | |
254 char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice) | |
255 { | |
256 GaimConnection *gc; | |
257 const char *cur = msg + 1; | |
258 char *buf, *ctcp; | |
259 time_t timestamp; | |
260 | |
261 if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001') | |
262 return g_strdup(msg); | |
263 | |
264 if (!strncmp(cur, "ACTION ", 7)) { | |
265 cur += 7; | |
266 buf = g_strdup_printf("/me %s", cur); | |
267 buf[strlen(buf) - 1] = '\0'; | |
268 return buf; | |
269 } else if (!strncmp(cur, "PING ", 5)) { | |
270 if (notice) { /* reply */ | |
271 sscanf(cur, "PING %lu", ×tamp); | |
272 gc = gaim_account_get_connection(irc->account); | |
273 if (!gc) | |
274 return NULL; | |
6350 | 275 buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp); |
6333 | 276 gaim_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf); |
277 g_free(buf); | |
278 return NULL; | |
279 } else { | |
280 buf = irc_format(irc, "vt:", "NOTICE", from, msg); | |
281 irc_send(irc, buf); | |
282 g_free(buf); | |
283 gc = gaim_account_get_connection(irc->account); | |
284 } | |
285 } else if (!strncmp(cur, "VERSION", 7) && !notice) { | |
286 buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Gaim IRC\001"); | |
287 irc_send(irc, buf); | |
288 g_free(buf); | |
289 } | |
290 | |
291 ctcp = g_strdup(msg + 1); | |
292 ctcp[strlen(ctcp) - 1] = '\0'; | |
293 buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from); | |
294 g_free(ctcp); | |
295 return buf; | |
296 } | |
297 | |
298 void irc_msg_table_build(struct irc_conn *irc) | |
299 { | |
300 int i; | |
301 | |
302 if (!irc || !irc->msgs) { | |
303 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n"); | |
304 return; | |
305 } | |
306 | |
307 for (i = 0; _irc_msgs[i].name; i++) { | |
308 g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]); | |
309 } | |
310 } | |
311 | |
312 void irc_cmd_table_build(struct irc_conn *irc) | |
313 { | |
314 int i; | |
315 | |
316 if (!irc || !irc->cmds) { | |
317 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n"); | |
318 return; | |
319 } | |
320 | |
321 for (i = 0; _irc_cmds[i].name ; i++) { | |
322 g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]); | |
323 } | |
324 } | |
325 | |
326 char *irc_format(struct irc_conn *irc, const char *format, ...) | |
327 { | |
328 GString *string = g_string_new(""); | |
329 char *tok, *tmp; | |
330 const char *cur; | |
331 va_list ap; | |
332 | |
333 va_start(ap, format); | |
334 for (cur = format; *cur; cur++) { | |
335 if (cur != format) | |
336 g_string_append_c(string, ' '); | |
337 | |
338 tok = va_arg(ap, char *); | |
339 switch (*cur) { | |
340 case 'v': | |
341 g_string_append(string, tok); | |
342 break; | |
343 case ':': | |
344 g_string_append_c(string, ':'); | |
345 /* no break! */ | |
346 case 't': | |
347 case 'n': | |
348 case 'c': | |
349 tmp = irc_send_convert(irc, tok); | |
350 g_string_append(string, tmp); | |
351 g_free(tmp); | |
352 break; | |
353 default: | |
354 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur); | |
355 break; | |
356 } | |
357 } | |
358 va_end(ap); | |
359 g_string_append(string, "\r\n"); | |
360 return (g_string_free(string, FALSE)); | |
361 } | |
362 | |
363 void irc_parse_msg(struct irc_conn *irc, char *input) | |
364 { | |
365 struct _irc_msg *msgent; | |
366 char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg; | |
367 int i; | |
368 | |
369 if (!strncmp(input, "PING ", 5)) { | |
370 msg = irc_format(irc, "vv", "PONG", input + 5); | |
371 irc_send(irc, msg); | |
372 g_free(msg); | |
373 return; | |
374 } else if (!strncmp(input, "ERROR ", 6)) { | |
375 gaim_connection_error(gaim_account_get_connection(irc->account), _("Disconnected")); | |
376 return; | |
377 } | |
378 | |
379 if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) { | |
380 irc_parse_error_cb(irc, input); | |
381 return; | |
382 } | |
383 | |
384 from = g_strndup(&input[1], cur - &input[1]); | |
385 cur++; | |
386 end = strchr(cur, ' '); | |
387 if (!end) | |
388 end = cur + strlen(cur); | |
389 | |
390 tmp = g_strndup(cur, end - cur); | |
391 msgname = g_ascii_strdown(tmp, -1); | |
392 g_free(tmp); | |
393 | |
394 if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) { | |
395 irc_msg_default(irc, "", from, &input); | |
396 g_free(msgname); | |
397 g_free(from); | |
398 return; | |
399 } | |
400 g_free(msgname); | |
401 | |
402 args = g_new0(char *, strlen(msgent->format)); | |
403 for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) { | |
404 switch (fmt[i]) { | |
405 case 'v': | |
406 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
407 args[i] = g_strndup(cur, end - cur); | |
408 cur += end - cur; | |
409 break; | |
410 case 't': | |
411 case 'n': | |
412 case 'c': | |
413 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
414 tmp = g_strndup(cur, end - cur); | |
415 args[i] = irc_recv_convert(irc, tmp); | |
416 g_free(tmp); | |
417 cur += end - cur; | |
418 break; | |
419 case ':': | |
420 if (*cur == ':') cur++; | |
421 args[i] = irc_recv_convert(irc, cur); | |
422 cur = cur + strlen(cur); | |
423 break; | |
424 case '*': | |
425 args[i] = g_strdup(cur); | |
426 cur = cur + strlen(cur); | |
427 break; | |
428 default: | |
429 gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]); | |
430 break; | |
431 } | |
432 } | |
433 (msgent->cb)(irc, msgent->name, from, args); | |
434 for (i = 0; i < strlen(msgent->format); i++) { | |
435 g_free(args[i]); | |
436 } | |
437 g_free(args); | |
438 g_free(from); | |
439 } | |
440 | |
441 int irc_parse_cmd(struct irc_conn *irc, const char *target, const char *cmdstr) | |
442 { | |
443 const char *cur, *end, *fmt; | |
444 char *tmp, *cmd, **args; | |
445 struct _irc_user_cmd *cmdent; | |
446 int i, ret; | |
447 | |
448 cur = cmdstr; | |
449 end = strchr(cmdstr, ' '); | |
450 if (!end) | |
451 end = cur + strlen(cur); | |
452 | |
453 tmp = g_strndup(cur, end - cur); | |
454 cmd = g_utf8_strdown(tmp, -1); | |
455 g_free(tmp); | |
456 | |
457 if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL) { | |
458 ret = irc_cmd_default(irc, cmd, target, &cmdstr); | |
459 g_free(cmd); | |
460 return ret; | |
461 } | |
462 | |
463 args = g_new0(char *, strlen(cmdent->format)); | |
464 for (cur = end, fmt = cmdent->format, i = 0; fmt[i] && *cur++; i++) { | |
465 switch (fmt[i]) { | |
466 case 'v': | |
467 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
468 args[i] = g_strndup(cur, end - cur); | |
469 cur += end - cur; | |
470 break; | |
471 case 't': | |
472 case 'n': | |
473 case 'c': | |
474 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
475 args[i] = g_strndup(cur, end - cur); | |
476 cur += end - cur; | |
477 break; | |
478 case ':': | |
479 case '*': | |
480 args[i] = g_strdup(cur); | |
481 cur = cur + strlen(cur); | |
482 break; | |
483 default: | |
484 gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid command format character '%c'\n", fmt[i]); | |
485 break; | |
486 } | |
487 } | |
488 ret = (cmdent->cb)(irc, cmd, target, (const char **)args); | |
489 for (i = 0; i < strlen(cmdent->format); i++) | |
490 g_free(args[i]); | |
491 g_free(args); | |
492 | |
493 g_free(cmd); | |
494 return ret; | |
495 } | |
496 | |
497 static void irc_parse_error_cb(struct irc_conn *irc, char *input) | |
498 { | |
499 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", input); | |
500 } |