Mercurial > pidgin
annotate src/protocols/irc/parse.c @ 6714:0c260c4e753e
[gaim-migrate @ 7240]
515: not identified
committer: Tailor Script <tailor@pidgin.im>
author | Ethan Blanton <elb@pidgin.im> |
---|---|
date | Tue, 02 Sep 2003 23:35:08 +0000 |
parents | e3be6b9744b7 |
children | 37af5dea14d1 |
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 */ |
76 { "442", "nc:", irc_msg_notinchan }, /* Not in channel */ | |
77 { "473", "nc:", irc_msg_inviteonly }, /* Tried to join invite-only */ | |
78 { "474", "nc:", irc_msg_banned }, /* Banned from channel */ | |
79 { "482", "nc:", irc_msg_notop }, /* Need to be op to do that */ | |
80 { "501", "n:", irc_msg_badmode }, /* Unknown mode flag */ | |
6714 | 81 { "515", "nc:", irc_msg_regonly }, /* Registration required */ |
6333 | 82 { "invite", "n:", irc_msg_invite }, /* Invited */ |
83 { "join", ":", irc_msg_join }, /* Joined a channel */ | |
84 { "kick", "cn:", irc_msg_kick }, /* KICK */ | |
85 { "mode", "tv:", irc_msg_mode }, /* MODE for channel */ | |
86 { "nick", ":", irc_msg_nick }, /* Nick change */ | |
87 { "notice", "t:", irc_msg_notice }, /* NOTICE recv */ | |
88 { "part", "c:", irc_msg_part }, /* Parted a channel */ | |
89 { "ping", ":", irc_msg_ping }, /* Received PING from server */ | |
90 { "pong", "v:", irc_msg_pong }, /* Received PONG from server */ | |
91 { "privmsg", "t:", irc_msg_privmsg }, /* Received private message */ | |
92 { "topic", "c:", irc_msg_topic }, /* TOPIC command */ | |
93 { "quit", ":", irc_msg_quit }, /* QUIT notice */ | |
94 { "wallops", ":", irc_msg_wallops }, /* WALLOPS command */ | |
95 { NULL, NULL, NULL } | |
96 }; | |
97 | |
98 static struct _irc_user_cmd { | |
99 char *name; | |
100 char *format; | |
101 IRCCmdCallback cb; | |
102 } _irc_cmds[] = { | |
103 { "away", ":", irc_cmd_away }, | |
104 { "deop", ":", irc_cmd_op }, | |
105 { "devoice", ":", irc_cmd_op }, | |
6415
e3be6b9744b7
[gaim-migrate @ 6922]
Christian Hammond <chipx86@chipx86.com>
parents:
6350
diff
changeset
|
106 { "help", "v", irc_cmd_help }, |
6333 | 107 { "invite", ":", irc_cmd_invite }, |
108 { "j", "cv", irc_cmd_join }, | |
109 { "join", "cv", irc_cmd_join }, | |
110 { "kick", "n:", irc_cmd_kick }, | |
111 { "me", ":", irc_cmd_ctcp_action }, | |
112 { "mode", ":", irc_cmd_mode }, | |
113 { "msg", "t:", irc_cmd_privmsg }, | |
114 { "names", "c", irc_cmd_names }, | |
115 { "nick", "n", irc_cmd_nick }, | |
116 { "op", ":", irc_cmd_op }, | |
117 { "operwall", ":", irc_cmd_wallops }, | |
118 { "part", "c:", irc_cmd_part }, | |
119 { "ping", "n", irc_cmd_ping }, | |
120 { "query", "n:", irc_cmd_query }, | |
121 { "quit", ":", irc_cmd_quit }, | |
122 { "quote", "*", irc_cmd_quote }, | |
123 { "remove", "n:", irc_cmd_remove }, | |
124 { "topic", ":", irc_cmd_topic }, | |
125 { "umode", ":", irc_cmd_mode }, | |
126 { "voice", ":", irc_cmd_op }, | |
127 { "wallops", ":", irc_cmd_wallops }, | |
128 { "whois", "n", irc_cmd_whois }, | |
129 { NULL, NULL } | |
130 }; | |
131 | |
132 static char *irc_send_convert(struct irc_conn *irc, const char *string) | |
133 { | |
134 char *utf8; | |
135 GError *err = NULL; | |
136 | |
137 utf8 = g_convert(string, strlen(string), | |
138 gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET), | |
139 "UTF-8", NULL, NULL, &err); | |
140 if (err) { | |
141 gaim_debug(GAIM_DEBUG_ERROR, "irc", "send conversion error: %s\n", err->message); | |
142 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Sending raw, which probably isn't right\n"); | |
143 utf8 = g_strdup(string); | |
144 } | |
145 | |
146 return utf8; | |
147 } | |
148 | |
149 static char *irc_recv_convert(struct irc_conn *irc, const char *string) | |
150 { | |
151 char *utf8; | |
152 GError *err = NULL; | |
153 | |
154 utf8 = g_convert(string, strlen(string), "UTF-8", | |
155 gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET), | |
156 NULL, NULL, &err); | |
157 if (err) { | |
158 gaim_debug(GAIM_DEBUG_ERROR, "irc", "recv conversion error: %s\n", err->message); | |
159 utf8 = g_strdup(_("(There was an error converting this message. Check the 'Encoding' option in the Account Editor)")); | |
160 } | |
161 | |
162 return utf8; | |
163 } | |
164 | |
165 /* XXX tag closings are not necessarily correctly nested here! If we | |
166 * get a ^O or reach the end of the string and there are open | |
167 * tags, they are closed in a fixed order ... this means, for | |
168 * example, you might see <FONT COLOR="blue">some text <B>with | |
169 * various attributes</FONT></B> (notice that B and FONT overlap | |
170 * and are not cleanly nested). This is imminently fixable but | |
171 * I am not fixing it right now. | |
172 */ | |
173 char *irc_mirc2html(const char *string) | |
174 { | |
175 const char *cur, *end; | |
176 char fg[3] = "\0\0", bg[3] = "\0\0"; | |
177 int fgnum, bgnum; | |
178 int font = 0, bold = 0; | |
179 GString *decoded = g_string_sized_new(strlen(string)); | |
180 | |
181 cur = string; | |
182 do { | |
183 end = strpbrk(cur, "\002\003\007\017\026"); | |
184 | |
185 decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur)); | |
186 cur = end ? end : cur + strlen(cur); | |
187 | |
188 switch (*cur) { | |
189 case '\002': | |
190 cur++; | |
191 if (!bold) { | |
192 decoded = g_string_append(decoded, "<B>"); | |
193 bold = TRUE; | |
194 } else { | |
195 decoded = g_string_append(decoded, "</B>"); | |
196 bold = FALSE; | |
197 } | |
198 break; | |
199 case '\003': | |
200 cur++; | |
201 fg[0] = fg[1] = bg[0] = bg[1] = '\0'; | |
202 if (isdigit(*cur)) | |
203 fg[0] = *cur++; | |
204 if (isdigit(*cur)) | |
205 fg[1] = *cur++; | |
206 if (*cur == ',') { | |
207 cur++; | |
208 if (isdigit(*cur)) | |
209 bg[0] = *cur++; | |
210 if (isdigit(*cur)) | |
211 bg[1] = *cur++; | |
212 } | |
213 if (font) { | |
214 decoded = g_string_append(decoded, "</FONT>"); | |
215 font = FALSE; | |
216 } | |
217 | |
218 if (fg[0]) { | |
219 fgnum = atoi(fg); | |
220 if (fgnum < 0 || fgnum > 15) | |
221 continue; | |
222 font = TRUE; | |
223 g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]); | |
224 if (bg[0]) { | |
225 bgnum = atoi(bg); | |
226 if (bgnum >= 0 && bgnum < 16) | |
227 g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]); | |
228 } | |
229 decoded = g_string_append_c(decoded, '>'); | |
230 } | |
231 break; | |
232 case '\007': | |
233 case '\026': | |
234 cur++; | |
235 break; | |
236 case '\017': | |
237 cur++; | |
238 /* fallthrough */ | |
239 case '\000': | |
240 if (bold) | |
241 decoded = g_string_append(decoded, "</BOLD>"); | |
242 if (font) | |
243 decoded = g_string_append(decoded, "</FONT>"); | |
244 break; | |
245 default: | |
246 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur); | |
247 } | |
248 } while (*cur); | |
249 | |
250 return g_string_free(decoded, FALSE); | |
251 } | |
252 | |
253 char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice) | |
254 { | |
255 GaimConnection *gc; | |
256 const char *cur = msg + 1; | |
257 char *buf, *ctcp; | |
258 time_t timestamp; | |
259 | |
260 if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001') | |
261 return g_strdup(msg); | |
262 | |
263 if (!strncmp(cur, "ACTION ", 7)) { | |
264 cur += 7; | |
265 buf = g_strdup_printf("/me %s", cur); | |
266 buf[strlen(buf) - 1] = '\0'; | |
267 return buf; | |
268 } else if (!strncmp(cur, "PING ", 5)) { | |
269 if (notice) { /* reply */ | |
270 sscanf(cur, "PING %lu", ×tamp); | |
271 gc = gaim_account_get_connection(irc->account); | |
272 if (!gc) | |
273 return NULL; | |
6350 | 274 buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp); |
6333 | 275 gaim_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf); |
276 g_free(buf); | |
277 return NULL; | |
278 } else { | |
279 buf = irc_format(irc, "vt:", "NOTICE", from, msg); | |
280 irc_send(irc, buf); | |
281 g_free(buf); | |
282 gc = gaim_account_get_connection(irc->account); | |
283 } | |
284 } else if (!strncmp(cur, "VERSION", 7) && !notice) { | |
285 buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Gaim IRC\001"); | |
286 irc_send(irc, buf); | |
287 g_free(buf); | |
288 } | |
289 | |
290 ctcp = g_strdup(msg + 1); | |
291 ctcp[strlen(ctcp) - 1] = '\0'; | |
292 buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from); | |
293 g_free(ctcp); | |
294 return buf; | |
295 } | |
296 | |
297 void irc_msg_table_build(struct irc_conn *irc) | |
298 { | |
299 int i; | |
300 | |
301 if (!irc || !irc->msgs) { | |
302 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n"); | |
303 return; | |
304 } | |
305 | |
306 for (i = 0; _irc_msgs[i].name; i++) { | |
307 g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]); | |
308 } | |
309 } | |
310 | |
311 void irc_cmd_table_build(struct irc_conn *irc) | |
312 { | |
313 int i; | |
314 | |
315 if (!irc || !irc->cmds) { | |
316 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n"); | |
317 return; | |
318 } | |
319 | |
320 for (i = 0; _irc_cmds[i].name ; i++) { | |
321 g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]); | |
322 } | |
323 } | |
324 | |
325 char *irc_format(struct irc_conn *irc, const char *format, ...) | |
326 { | |
327 GString *string = g_string_new(""); | |
328 char *tok, *tmp; | |
329 const char *cur; | |
330 va_list ap; | |
331 | |
332 va_start(ap, format); | |
333 for (cur = format; *cur; cur++) { | |
334 if (cur != format) | |
335 g_string_append_c(string, ' '); | |
336 | |
337 tok = va_arg(ap, char *); | |
338 switch (*cur) { | |
339 case 'v': | |
340 g_string_append(string, tok); | |
341 break; | |
342 case ':': | |
343 g_string_append_c(string, ':'); | |
344 /* no break! */ | |
345 case 't': | |
346 case 'n': | |
347 case 'c': | |
348 tmp = irc_send_convert(irc, tok); | |
349 g_string_append(string, tmp); | |
350 g_free(tmp); | |
351 break; | |
352 default: | |
353 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur); | |
354 break; | |
355 } | |
356 } | |
357 va_end(ap); | |
358 g_string_append(string, "\r\n"); | |
359 return (g_string_free(string, FALSE)); | |
360 } | |
361 | |
362 void irc_parse_msg(struct irc_conn *irc, char *input) | |
363 { | |
364 struct _irc_msg *msgent; | |
365 char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg; | |
366 int i; | |
367 | |
368 if (!strncmp(input, "PING ", 5)) { | |
369 msg = irc_format(irc, "vv", "PONG", input + 5); | |
370 irc_send(irc, msg); | |
371 g_free(msg); | |
372 return; | |
373 } else if (!strncmp(input, "ERROR ", 6)) { | |
374 gaim_connection_error(gaim_account_get_connection(irc->account), _("Disconnected")); | |
375 return; | |
376 } | |
377 | |
378 if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) { | |
379 irc_parse_error_cb(irc, input); | |
380 return; | |
381 } | |
382 | |
383 from = g_strndup(&input[1], cur - &input[1]); | |
384 cur++; | |
385 end = strchr(cur, ' '); | |
386 if (!end) | |
387 end = cur + strlen(cur); | |
388 | |
389 tmp = g_strndup(cur, end - cur); | |
390 msgname = g_ascii_strdown(tmp, -1); | |
391 g_free(tmp); | |
392 | |
393 if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) { | |
394 irc_msg_default(irc, "", from, &input); | |
395 g_free(msgname); | |
396 g_free(from); | |
397 return; | |
398 } | |
399 g_free(msgname); | |
400 | |
401 args = g_new0(char *, strlen(msgent->format)); | |
402 for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) { | |
403 switch (fmt[i]) { | |
404 case 'v': | |
405 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
406 args[i] = g_strndup(cur, end - cur); | |
407 cur += end - cur; | |
408 break; | |
409 case 't': | |
410 case 'n': | |
411 case 'c': | |
412 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
413 tmp = g_strndup(cur, end - cur); | |
414 args[i] = irc_recv_convert(irc, tmp); | |
415 g_free(tmp); | |
416 cur += end - cur; | |
417 break; | |
418 case ':': | |
419 if (*cur == ':') cur++; | |
420 args[i] = irc_recv_convert(irc, cur); | |
421 cur = cur + strlen(cur); | |
422 break; | |
423 case '*': | |
424 args[i] = g_strdup(cur); | |
425 cur = cur + strlen(cur); | |
426 break; | |
427 default: | |
428 gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]); | |
429 break; | |
430 } | |
431 } | |
432 (msgent->cb)(irc, msgent->name, from, args); | |
433 for (i = 0; i < strlen(msgent->format); i++) { | |
434 g_free(args[i]); | |
435 } | |
436 g_free(args); | |
437 g_free(from); | |
438 } | |
439 | |
440 int irc_parse_cmd(struct irc_conn *irc, const char *target, const char *cmdstr) | |
441 { | |
442 const char *cur, *end, *fmt; | |
443 char *tmp, *cmd, **args; | |
444 struct _irc_user_cmd *cmdent; | |
445 int i, ret; | |
446 | |
447 cur = cmdstr; | |
448 end = strchr(cmdstr, ' '); | |
449 if (!end) | |
450 end = cur + strlen(cur); | |
451 | |
452 tmp = g_strndup(cur, end - cur); | |
453 cmd = g_utf8_strdown(tmp, -1); | |
454 g_free(tmp); | |
455 | |
456 if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL) { | |
457 ret = irc_cmd_default(irc, cmd, target, &cmdstr); | |
458 g_free(cmd); | |
459 return ret; | |
460 } | |
461 | |
462 args = g_new0(char *, strlen(cmdent->format)); | |
463 for (cur = end, fmt = cmdent->format, i = 0; fmt[i] && *cur++; i++) { | |
464 switch (fmt[i]) { | |
465 case 'v': | |
466 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
467 args[i] = g_strndup(cur, end - cur); | |
468 cur += end - cur; | |
469 break; | |
470 case 't': | |
471 case 'n': | |
472 case 'c': | |
473 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
474 args[i] = g_strndup(cur, end - cur); | |
475 cur += end - cur; | |
476 break; | |
477 case ':': | |
478 case '*': | |
479 args[i] = g_strdup(cur); | |
480 cur = cur + strlen(cur); | |
481 break; | |
482 default: | |
483 gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid command format character '%c'\n", fmt[i]); | |
484 break; | |
485 } | |
486 } | |
487 ret = (cmdent->cb)(irc, cmd, target, (const char **)args); | |
488 for (i = 0; i < strlen(cmdent->format); i++) | |
489 g_free(args[i]); | |
490 g_free(args); | |
491 | |
492 g_free(cmd); | |
493 return ret; | |
494 } | |
495 | |
496 static void irc_parse_error_cb(struct irc_conn *irc, char *input) | |
497 { | |
498 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", input); | |
499 } |