comparison src/protocols/irc/parse.c @ 6333:e06e04e44914

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