rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / irc / parse.c
blobd17a85ac81637da696e6de013e7395209f031791
1 /**
2 * purple
4 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include <purple.h>
24 #include "irc.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
30 static GSList *cmds = NULL;
32 static char *irc_send_convert(struct irc_conn *irc, const char *string);
33 static char *irc_recv_convert(struct irc_conn *irc, const char *string);
35 static void irc_parse_error_cb(struct irc_conn *irc, char *input);
37 static char *irc_mirc_colors[16] = {
38 "white", "black", "blue", "dark green", "red", "brown", "purple",
39 "orange", "yellow", "green", "teal", "cyan", "light blue",
40 "pink", "grey", "light grey" };
42 extern PurpleProtocol *_irc_protocol;
44 /*typedef void (*IRCMsgCallback)(struct irc_conn *irc, char *from, char *name, char **args);*/
45 static struct _irc_msg {
46 char *name;
47 char *format;
49 /** The required parameter count, based on values we use, not protocol
50 * specification. */
51 int req_cnt;
53 void (*cb)(struct irc_conn *irc, const char *name, const char *from, char **args);
54 } _irc_msgs[] = {
55 { "005", "n*", 2, irc_msg_features }, /* Feature list */
56 { "251", "n:", 1, irc_msg_luser }, /* Client & Server count */
57 { "255", "n:", 1, irc_msg_luser }, /* Client & Server count Mk. II */
58 { "301", "nn:", 3, irc_msg_away }, /* User is away */
59 { "303", "n:", 2, irc_msg_ison }, /* ISON reply */
60 { "311", "nnvvv:", 6, irc_msg_whois }, /* Whois user */
61 { "312", "nnv:", 4, irc_msg_whois }, /* Whois server */
62 { "313", "nn:", 2, irc_msg_whois }, /* Whois ircop */
63 { "314", "nnnvv:", 6, irc_msg_whois }, /* Whowas user */
64 { "315", "nt:", 0, irc_msg_who }, /* end of WHO channel */
65 { "317", "nnvv", 3, irc_msg_whois }, /* Whois idle */
66 { "318", "nt:", 2, irc_msg_endwhois }, /* End of WHOIS */
67 { "319", "nn:", 3, irc_msg_whois }, /* Whois channels */
68 { "320", "nn:", 2, irc_msg_whois }, /* Whois (fn ident) */
69 { "321", "*", 0, irc_msg_list }, /* Start of list */
70 { "322", "ncv:", 4, irc_msg_list }, /* List. */
71 { "323", ":", 0, irc_msg_list }, /* End of list. */
72 { "324", "ncv:", 3, irc_msg_chanmode }, /* Channel modes */
73 { "330", "nnv:", 4, irc_msg_whois }, /* Whois (fn login) */
74 { "331", "nc:", 3, irc_msg_topic }, /* No channel topic */
75 { "332", "nc:", 3, irc_msg_topic }, /* Channel topic */
76 { "333", "ncvv", 4, irc_msg_topicinfo }, /* Topic setter stuff */
77 { "352", "ncvvvnv:", 8, irc_msg_who }, /* Channel WHO */
78 { "353", "nvc:", 4, irc_msg_names }, /* Names list */
79 { "366", "nc:", 2, irc_msg_names }, /* End of names */
80 { "367", "ncnnv", 3, irc_msg_ban }, /* Ban list */
81 { "368", "nc:", 2, irc_msg_ban }, /* End of ban list */
82 { "369", "nt:", 2, irc_msg_endwhois }, /* End of WHOWAS */
83 { "372", "n:", 1, irc_msg_motd }, /* MOTD */
84 { "375", "n:", 1, irc_msg_motd }, /* Start MOTD */
85 { "376", "n:", 1, irc_msg_motd }, /* End of MOTD */
86 { "391", "nv:", 3, irc_msg_time }, /* Time reply */
87 { "401", "nt:", 2, irc_msg_nonick }, /* No such nick/chan */
88 { "403", "nc:", 2, irc_msg_nochan }, /* No such channel */
89 { "404", "nt:", 3, irc_msg_nosend }, /* Cannot send to chan */
90 { "406", "nt:", 2, irc_msg_nonick }, /* No such nick for WHOWAS */
91 { "421", "nv:", 2, irc_msg_unknown }, /* Unknown command */
92 { "422", "n:", 1, irc_msg_motd }, /* MOTD file missing */
93 { "432", "vn:", 0, irc_msg_badnick }, /* Erroneous nickname */
94 { "433", "vn:", 2, irc_msg_nickused }, /* Nickname already in use */
95 { "437", "nc:", 2, irc_msg_unavailable }, /* Nick/channel is unavailable */
96 { "438", "nn:", 3, irc_msg_nochangenick }, /* Nick may not change */
97 { "442", "nc:", 3, irc_msg_notinchan }, /* Not in channel */
98 { "473", "nc:", 2, irc_msg_inviteonly }, /* Tried to join invite-only */
99 { "474", "nc:", 2, irc_msg_banned }, /* Banned from channel */
100 { "477", "nc:", 3, irc_msg_regonly }, /* Registration Required */
101 { "478", "nct:", 3, irc_msg_banfull }, /* Banlist is full */
102 { "482", "nc:", 3, irc_msg_notop }, /* Need to be op to do that */
103 { "501", "n:", 2, irc_msg_badmode }, /* Unknown mode flag */
104 { "506", "nc:", 3, irc_msg_nosend }, /* Must identify to send */
105 { "515", "nc:", 3, irc_msg_regonly }, /* Registration required */
106 #ifdef HAVE_CYRUS_SASL
107 { "903", "*", 0, irc_msg_authok}, /* SASL auth successful */
108 { "904", "*", 0, irc_msg_authtryagain }, /* SASL auth failed, can recover*/
109 { "905", "*", 0, irc_msg_authfail }, /* SASL auth failed */
110 { "906", "*", 0, irc_msg_authfail }, /* SASL auth failed */
111 { "907", "*", 0, irc_msg_authfail }, /* SASL auth failed */
112 { "cap", "vv:", 3, irc_msg_cap }, /* SASL capable */
113 { "authenticate", ":", 1, irc_msg_authenticate }, /* SASL authenticate */
114 #endif
115 { "invite", "n:", 2, irc_msg_invite }, /* Invited */
116 { "join", ":", 1, irc_msg_join }, /* Joined a channel */
117 { "kick", "cn:", 3, irc_msg_kick }, /* KICK */
118 { "mode", "tv:", 2, irc_msg_mode }, /* MODE for channel */
119 { "nick", ":", 1, irc_msg_nick }, /* Nick change */
120 { "notice", "t:", 2, irc_msg_notice }, /* NOTICE recv */
121 { "part", "c:", 1, irc_msg_part }, /* Parted a channel */
122 { "ping", ":", 1, irc_msg_ping }, /* Received PING from server */
123 { "pong", "v:", 2, irc_msg_pong }, /* Received PONG from server */
124 { "privmsg", "t:", 2, irc_msg_privmsg }, /* Received private message */
125 { "topic", "c:", 2, irc_msg_topic }, /* TOPIC command */
126 { "quit", ":", 1, irc_msg_quit }, /* QUIT notice */
127 { "wallops", ":", 1, irc_msg_wallops }, /* WALLOPS command */
128 { NULL, NULL, 0, NULL }
131 static struct _irc_user_cmd {
132 char *name;
133 char *format;
134 IRCCmdCallback cb;
135 char *help;
136 } _irc_cmds[] = {
137 { "action", ":", irc_cmd_ctcp_action, N_("action &lt;action to perform&gt;: Perform an action.") },
138 { "authserv", ":", irc_cmd_service, N_("authserv: Send a command to authserv") },
139 { "away", ":", irc_cmd_away, N_("away [message]: Set an away message, or use no message to return from being away.") },
140 { "ctcp", "t:", irc_cmd_ctcp, N_("ctcp <nick> <msg>: sends ctcp msg to nick.") },
141 { "chanserv", ":", irc_cmd_service, N_("chanserv: Send a command to chanserv") },
142 { "deop", ":", irc_cmd_op, N_("deop &lt;nick1&gt; [nick2] ...: Remove channel operator status from someone. You must be a channel operator to do this.") },
143 { "devoice", ":", irc_cmd_op, N_("devoice &lt;nick1&gt; [nick2] ...: Remove channel voice status from someone, preventing them from speaking if the channel is moderated (+m). You must be a channel operator to do this.") },
144 { "invite", ":", irc_cmd_invite, N_("invite &lt;nick&gt; [room]: Invite someone to join you in the specified channel, or the current channel.") },
145 { "j", "cv", irc_cmd_join, N_("j &lt;room1&gt;[,room2][,...] [key1[,key2][,...]]: Enter one or more channels, optionally providing a channel key for each if needed.") },
146 { "join", "cv", irc_cmd_join, N_("join &lt;room1&gt;[,room2][,...] [key1[,key2][,...]]: Enter one or more channels, optionally providing a channel key for each if needed.") },
147 { "kick", "n:", irc_cmd_kick, N_("kick &lt;nick&gt; [message]: Remove someone from a channel. You must be a channel operator to do this.") },
148 { "list", ":", irc_cmd_list, N_("list: Display a list of chat rooms on the network. <i>Warning, some servers may disconnect you upon doing this.</i>") },
149 { "me", ":", irc_cmd_ctcp_action, N_("me &lt;action to perform&gt;: Perform an action.") },
150 { "memoserv", ":", irc_cmd_service, N_("memoserv: Send a command to memoserv") },
151 { "mode", ":", irc_cmd_mode, N_("mode &lt;+|-&gt;&lt;A-Za-z&gt; &lt;nick|channel&gt;: Set or unset a channel or user mode.") },
152 { "msg", "t:", irc_cmd_privmsg, N_("msg &lt;nick&gt; &lt;message&gt;: Send a private message to a user (as opposed to a channel).") },
153 { "names", "c", irc_cmd_names, N_("names [channel]: List the users currently in a channel.") },
154 { "nick", "n", irc_cmd_nick, N_("nick &lt;new nickname&gt;: Change your nickname.") },
155 { "nickserv", ":", irc_cmd_service, N_("nickserv: Send a command to nickserv") },
156 { "notice", "t:", irc_cmd_privmsg, N_("notice &lt;target&lt;: Send a notice to a user or channel.") },
157 { "op", ":", irc_cmd_op, N_("op &lt;nick1&gt; [nick2] ...: Grant channel operator status to someone. You must be a channel operator to do this.") },
158 { "operwall", ":", irc_cmd_wallops, N_("operwall &lt;message&gt;: If you don't know what this is, you probably can't use it.") },
159 { "operserv", ":", irc_cmd_service, N_("operserv: Send a command to operserv") },
160 { "part", "c:", irc_cmd_part, N_("part [room] [message]: Leave the current channel, or a specified channel, with an optional message.") },
161 { "ping", "n", irc_cmd_ping, N_("ping [nick]: Asks how much lag a user (or the server if no user specified) has.") },
162 { "query", "n:", irc_cmd_query, N_("query &lt;nick&gt; &lt;message&gt;: Send a private message to a user (as opposed to a channel).") },
163 { "quit", ":", irc_cmd_quit, N_("quit [message]: Disconnect from the server, with an optional message.") },
164 { "quote", "*", irc_cmd_quote, N_("quote [...]: Send a raw command to the server.") },
165 { "remove", "n:", irc_cmd_remove, N_("remove &lt;nick&gt; [message]: Remove someone from a room. You must be a channel operator to do this.") },
166 { "time", "", irc_cmd_time, N_("time: Displays the current local time at the IRC server.") },
167 { "topic", ":", irc_cmd_topic, N_("topic [new topic]: View or change the channel topic.") },
168 { "umode", ":", irc_cmd_mode, N_("umode &lt;+|-&gt;&lt;A-Za-z&gt;: Set or unset a user mode.") },
169 { "version", ":", irc_cmd_ctcp_version, N_("version [nick]: send CTCP VERSION request to a user") },
170 { "voice", ":", irc_cmd_op, N_("voice &lt;nick1&gt; [nick2] ...: Grant channel voice status to someone. You must be a channel operator to do this.") },
171 { "wallops", ":", irc_cmd_wallops, N_("wallops &lt;message&gt;: If you don't know what this is, you probably can't use it.") },
172 { "whois", "tt", irc_cmd_whois, N_("whois [server] &lt;nick&gt;: Get information on a user.") },
173 { "whowas", "t", irc_cmd_whowas, N_("whowas &lt;nick&gt;: Get information on a user that has logged off.") },
174 { NULL, NULL, NULL, NULL }
177 static PurpleCmdRet irc_parse_purple_cmd(PurpleConversation *conv, const gchar *cmd,
178 gchar **args, gchar **error, void *data)
180 PurpleConnection *gc;
181 struct irc_conn *irc;
182 struct _irc_user_cmd *cmdent;
184 gc = purple_conversation_get_connection(conv);
185 if (!gc)
186 return PURPLE_CMD_RET_FAILED;
188 irc = purple_connection_get_protocol_data(gc);
190 if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL)
191 return PURPLE_CMD_RET_FAILED;
193 (cmdent->cb)(irc, cmd, purple_conversation_get_name(conv), (const char **)args);
195 return PURPLE_CMD_RET_OK;
198 static void irc_register_command(struct _irc_user_cmd *c)
200 PurpleCmdId id;
201 PurpleCmdFlag f;
202 char args[10];
203 char *format;
204 size_t i;
206 f = PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_PROTOCOL_ONLY
207 | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS;
209 format = c->format;
211 for (i = 0; (i < (sizeof(args) - 1)) && *format; i++, format++)
212 switch (*format) {
213 case 'v':
214 case 'n':
215 case 'c':
216 case 't':
217 args[i] = 'w';
218 break;
219 case ':':
220 case '*':
221 args[i] = 's';
222 break;
225 args[i] = '\0';
227 id = purple_cmd_register(c->name, args, PURPLE_CMD_P_PROTOCOL, f, "prpl-irc",
228 irc_parse_purple_cmd, _(c->help), NULL);
229 cmds = g_slist_prepend(cmds, GUINT_TO_POINTER(id));
232 void irc_register_commands(void)
234 struct _irc_user_cmd *c;
236 for (c = _irc_cmds; c && c->name; c++)
237 irc_register_command(c);
240 void irc_unregister_commands(void)
242 while (cmds) {
243 PurpleCmdId id = GPOINTER_TO_UINT(cmds->data);
244 purple_cmd_unregister(id);
245 cmds = g_slist_delete_link(cmds, cmds);
249 static char *irc_send_convert(struct irc_conn *irc, const char *string)
251 char *utf8;
252 GError *err = NULL;
253 gchar **encodings;
254 const gchar *enclist;
256 enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET);
257 encodings = g_strsplit(enclist, ",", 2);
259 if (encodings[0] == NULL || !g_ascii_strcasecmp("UTF-8", encodings[0])) {
260 g_strfreev(encodings);
261 return NULL;
264 utf8 = g_convert(string, strlen(string), encodings[0], "UTF-8", NULL, NULL, &err);
265 if (err) {
266 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Send conversion error: %s\n", err->message);
267 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Sending as UTF-8 instead of %s\n", encodings[0]);
268 utf8 = g_strdup(string);
269 g_error_free(err);
271 g_strfreev(encodings);
273 return utf8;
276 static char *irc_recv_convert(struct irc_conn *irc, const char *string)
278 char *utf8 = NULL;
279 const gchar *charset, *enclist;
280 gchar **encodings;
281 gboolean autodetect;
282 int i;
284 autodetect = purple_account_get_bool(irc->account, "autodetect_utf8", IRC_DEFAULT_AUTODETECT);
286 if (autodetect && g_utf8_validate(string, -1, NULL)) {
287 return g_strdup(string);
290 enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET);
291 encodings = g_strsplit(enclist, ",", -1);
293 if (encodings[0] == NULL) {
294 g_strfreev(encodings);
295 return purple_utf8_salvage(string);
298 for (i = 0; encodings[i] != NULL; i++) {
299 charset = encodings[i];
300 while (*charset == ' ')
301 charset++;
303 if (!g_ascii_strcasecmp("UTF-8", charset)) {
304 if (g_utf8_validate(string, -1, NULL))
305 utf8 = g_strdup(string);
306 } else {
307 utf8 = g_convert(string, -1, "UTF-8", charset, NULL, NULL, NULL);
310 if (utf8) {
311 g_strfreev(encodings);
312 return utf8;
315 g_strfreev(encodings);
317 return purple_utf8_salvage(string);
320 /* This function is shamelessly stolen from glib--it is an old version of the
321 * private function append_escaped_text, used by g_markup_escape_text, whose
322 * behavior changed in glib 2.12. */
323 static void irc_append_escaped_text(GString *str, const char *text, gssize length)
325 const char *p = text;
326 const char *end = text + length;
327 const char *next = NULL;
329 while(p != end) {
330 next = g_utf8_next_char(p);
332 switch(*p) {
333 case '&':
334 g_string_append(str, "&amp;");
335 break;
336 case '<':
337 g_string_append(str, "&lt;");
338 break;
339 case '>':
340 g_string_append(str, "&gt;");
341 break;
342 case '\'':
343 g_string_append(str, "&apos;");
344 break;
345 case '"':
346 g_string_append(str, "&quot;");
347 break;
348 default:
349 g_string_append_len(str, p, next - p);
350 break;
353 p = next;
357 /* This function is shamelessly stolen from glib--it is an old version of the
358 * function g_markup_escape_text, whose behavior changed in glib 2.12. */
359 char *irc_escape_privmsg(const char *text, gssize length)
361 GString *str;
363 g_return_val_if_fail(text != NULL, NULL);
365 if(length < 0)
366 length = strlen(text);
368 str = g_string_sized_new(length);
370 irc_append_escaped_text(str, text, length);
372 return g_string_free(str, FALSE);
375 /* XXX tag closings are not necessarily correctly nested here! If we
376 * get a ^O or reach the end of the string and there are open
377 * tags, they are closed in a fixed order ... this means, for
378 * example, you might see <FONT COLOR="blue">some text <B>with
379 * various attributes</FONT></B> (notice that B and FONT overlap
380 * and are not cleanly nested). This is imminently fixable but
381 * I am not fixing it right now.
383 char *irc_mirc2html(const char *string)
385 const char *cur, *end;
386 char fg[3] = "\0\0", bg[3] = "\0\0";
387 int fgnum, bgnum;
388 int font = 0, bold = 0, underline = 0, italic = 0;
389 GString *decoded;
391 if (string == NULL)
392 return NULL;
394 decoded = g_string_sized_new(strlen(string));
396 cur = string;
397 do {
398 end = strpbrk(cur, "\002\003\007\017\026\037");
400 decoded = g_string_append_len(decoded, cur, (end ? (gssize)(end - cur) : (gssize)strlen(cur)));
401 cur = end ? end : cur + strlen(cur);
403 switch (*cur) {
404 case '\002':
405 cur++;
406 if (!bold) {
407 decoded = g_string_append(decoded, "<B>");
408 bold = TRUE;
409 } else {
410 decoded = g_string_append(decoded, "</B>");
411 bold = FALSE;
413 break;
414 case '\003':
415 cur++;
416 fg[0] = fg[1] = bg[0] = bg[1] = '\0';
417 if (isdigit(*cur))
418 fg[0] = *cur++;
419 if (isdigit(*cur))
420 fg[1] = *cur++;
421 if (*cur == ',') {
422 cur++;
423 if (isdigit(*cur))
424 bg[0] = *cur++;
425 if (isdigit(*cur))
426 bg[1] = *cur++;
428 if (font) {
429 decoded = g_string_append(decoded, "</FONT>");
430 font = FALSE;
433 if (fg[0]) {
434 fgnum = atoi(fg);
435 if (fgnum < 0 || fgnum > 15)
436 continue;
437 font = TRUE;
438 g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]);
439 if (bg[0]) {
440 bgnum = atoi(bg);
441 if (bgnum >= 0 && bgnum < 16)
442 g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]);
444 decoded = g_string_append_c(decoded, '>');
446 break;
447 case '\011':
448 cur++;
449 if (!italic) {
450 decoded = g_string_append(decoded, "<I>");
451 italic = TRUE;
452 } else {
453 decoded = g_string_append(decoded, "</I>");
454 italic = FALSE;
456 break;
457 case '\037':
458 cur++;
459 if (!underline) {
460 decoded = g_string_append(decoded, "<U>");
461 underline = TRUE;
462 } else {
463 decoded = g_string_append(decoded, "</U>");
464 underline = FALSE;
466 break;
467 case '\007':
468 case '\026':
469 cur++;
470 break;
471 case '\017':
472 cur++;
473 /* fallthrough */
474 case '\000':
475 if (bold)
476 decoded = g_string_append(decoded, "</B>");
477 if (italic)
478 decoded = g_string_append(decoded, "</I>");
479 if (underline)
480 decoded = g_string_append(decoded, "</U>");
481 if (font)
482 decoded = g_string_append(decoded, "</FONT>");
483 bold = italic = underline = font = FALSE;
484 break;
485 default:
486 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur);
488 } while (*cur);
490 return g_string_free(decoded, FALSE);
493 char *irc_mirc2txt (const char *string)
495 char *result;
496 int i, j;
498 if (string == NULL)
499 return NULL;
501 result = g_strdup (string);
503 for (i = 0, j = 0; result[i]; i++) {
504 switch (result[i]) {
505 case '\002':
506 case '\003':
507 /* Foreground color */
508 if (isdigit(result[i + 1]))
509 i++;
510 if (isdigit(result[i + 1]))
511 i++;
512 /* Optional comma and background color */
513 if (result[i + 1] == ',') {
514 i++;
515 if (isdigit(result[i + 1]))
516 i++;
517 if (isdigit(result[i + 1]))
518 i++;
520 /* Note that i still points to the last character
521 * of the color selection string. */
522 continue;
523 case '\007':
524 case '\017':
525 case '\026':
526 case '\037':
527 continue;
528 default:
529 result[j++] = result[i];
532 result[j] = '\0';
533 return result;
536 const char *irc_nick_skip_mode(struct irc_conn *irc, const char *nick)
538 static const char *default_modes = "@+%&";
539 const char *mode_chars;
541 mode_chars = irc->mode_chars ? irc->mode_chars : default_modes;
543 while (*nick && strchr(mode_chars, *nick) != NULL)
544 nick++;
546 return nick;
549 gboolean irc_ischannel(const char *string)
551 return (string[0] == '#' || string[0] == '&');
554 char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice)
556 PurpleConnection *gc;
557 const char *cur = msg + 1;
558 char *buf, *ctcp;
559 time_t timestamp;
561 /* Note that this is NOT correct w.r.t. multiple CTCPs in one
562 * message and low-level quoting ... but if you want that crap,
563 * use a real IRC client. */
565 if (msg[0] != '\001' || msg[1] == '\0' || msg[strlen(msg) - 1] != '\001')
566 return g_strdup(msg);
568 if (!strncmp(cur, "ACTION ", 7)) {
569 cur += 7;
570 buf = g_strdup_printf("/me %s", cur);
571 buf[strlen(buf) - 1] = '\0';
572 return buf;
573 } else if (!strncmp(cur, "PING ", 5)) {
574 if (notice) { /* reply */
575 gc = purple_account_get_connection(irc->account);
576 if (!gc)
577 return NULL;
578 /* TODO: Should this read in the timestamp as a double? */
579 if (sscanf(cur, "PING %lu", &timestamp) == 1) {
580 buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp);
581 purple_notify_info(gc, _("PONG"),
582 _("CTCP PING reply"), buf,
583 purple_request_cpar_from_connection(gc));
584 g_free(buf);
585 } else
586 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Unable to parse PING timestamp");
587 return NULL;
588 } else {
589 buf = irc_format(irc, "vt:", "NOTICE", from, msg);
590 irc_send(irc, buf);
591 g_free(buf);
593 } else if (!strncmp(cur, "VERSION", 7) && !notice) {
594 buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Purple IRC\001");
595 irc_send(irc, buf);
596 g_free(buf);
597 } else if (!strncmp(cur, "DCC SEND ", 9)) {
598 irc_dccsend_recv(irc, from, msg + 10);
599 return NULL;
602 ctcp = g_strdup(msg + 1);
603 ctcp[strlen(ctcp) - 1] = '\0';
604 buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from);
605 g_free(ctcp);
606 return buf;
609 void irc_msg_table_build(struct irc_conn *irc)
611 int i;
613 if (!irc || !irc->msgs) {
614 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n");
615 return;
618 for (i = 0; _irc_msgs[i].name; i++) {
619 g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]);
623 void irc_cmd_table_build(struct irc_conn *irc)
625 int i;
627 if (!irc || !irc->cmds) {
628 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n");
629 return;
632 for (i = 0; _irc_cmds[i].name ; i++) {
633 g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]);
637 char *irc_format(struct irc_conn *irc, const char *format, ...)
639 GString *string = g_string_new("");
640 char *tok, *tmp;
641 const char *cur;
642 va_list ap;
644 va_start(ap, format);
645 for (cur = format; *cur; cur++) {
646 if (cur != format)
647 g_string_append_c(string, ' ');
649 tok = va_arg(ap, char *);
650 switch (*cur) {
651 case 'v':
652 g_string_append(string, tok);
653 break;
654 case ':':
655 g_string_append_c(string, ':');
656 /* no break! */
657 case 't':
658 case 'n':
659 case 'c':
660 tmp = irc_send_convert(irc, tok);
661 g_string_append(string, tmp ? tmp : tok);
662 g_free(tmp);
663 break;
664 default:
665 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur);
666 break;
669 va_end(ap);
670 g_string_append(string, "\r\n");
671 return (g_string_free(string, FALSE));
674 void irc_parse_msg(struct irc_conn *irc, char *input)
676 struct _irc_msg *msgent;
677 char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg;
678 guint i;
679 PurpleConnection *gc = purple_account_get_connection(irc->account);
680 gboolean fmt_valid;
681 int args_cnt;
683 irc->recv_time = time(NULL);
686 * The data passed to irc-receiving-text is the raw protocol data.
687 * TODO: It should be passed as an array of bytes and a length
688 * instead of a null terminated string.
690 purple_signal_emit(_irc_protocol, "irc-receiving-text", gc, &input);
692 if (purple_debug_is_verbose()) {
693 char *clean = purple_utf8_salvage(input);
694 clean = g_strstrip(clean);
695 purple_debug_misc("irc", ">> %s\n", clean);
696 g_free(clean);
699 if (!strncmp(input, "PING ", 5)) {
700 msg = irc_format(irc, "vv", "PONG", input + 5);
701 irc_send(irc, msg);
702 g_free(msg);
703 return;
704 } else if (!strncmp(input, "ERROR ", 6)) {
705 if (g_utf8_validate(input, -1, NULL)) {
706 purple_connection_take_error(gc, g_error_new(
707 PURPLE_CONNECTION_ERROR,
708 PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
709 "%s\n%s", _("Disconnected."), input));
710 } else
711 purple_connection_take_error(gc, g_error_new_literal(
712 PURPLE_CONNECTION_ERROR,
713 PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
714 _("Disconnected.")));
715 return;
716 #ifdef HAVE_CYRUS_SASL
717 } else if (!strncmp(input, "AUTHENTICATE ", 13)) {
718 irc_msg_auth(irc, input + 13);
719 return;
720 #endif
723 if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) {
724 irc_parse_error_cb(irc, input);
725 return;
728 from = g_strndup(&input[1], cur - &input[1]);
729 cur++;
730 end = strchr(cur, ' ');
731 if (!end)
732 end = cur + strlen(cur);
734 tmp = g_strndup(cur, end - cur);
735 msgname = g_ascii_strdown(tmp, -1);
736 g_free(tmp);
738 if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) {
739 irc_msg_default(irc, "", from, &input);
740 g_free(msgname);
741 g_free(from);
742 return;
744 g_free(msgname);
746 fmt_valid = TRUE;
747 args = g_new0(char *, strlen(msgent->format));
748 args_cnt = 0;
749 for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) {
750 switch (fmt[i]) {
751 case 'v':
752 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
753 /* This is a string of unknown encoding which we do not
754 * want to transcode, but it may or may not be valid
755 * UTF-8, so we'll salvage it. If a nick/channel/target
756 * field has inadvertently been marked verbatim, this
757 * could cause weirdness. */
758 tmp = g_strndup(cur, end - cur);
759 args[i] = purple_utf8_salvage(tmp);
760 g_free(tmp);
761 cur += end - cur;
762 break;
763 case 't':
764 case 'n':
765 case 'c':
766 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
767 tmp = g_strndup(cur, end - cur);
768 args[i] = irc_recv_convert(irc, tmp);
769 g_free(tmp);
770 cur += end - cur;
771 break;
772 case ':':
773 if (*cur == ':') cur++;
774 args[i] = irc_recv_convert(irc, cur);
775 cur = cur + strlen(cur);
776 break;
777 case '*':
778 /* Ditto 'v' above; we're going to salvage this in case
779 * it leaks past the IRC protocol */
780 args[i] = purple_utf8_salvage(cur);
781 cur = cur + strlen(cur);
782 break;
783 default:
784 purple_debug(PURPLE_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]);
785 fmt_valid = FALSE;
786 break;
788 if (fmt_valid)
789 args_cnt = i + 1;
791 if (G_UNLIKELY(!fmt_valid)) {
792 purple_debug_error("irc", "message format was invalid");
793 } else if (G_LIKELY(args_cnt >= msgent->req_cnt)) {
794 tmp = irc_recv_convert(irc, from);
795 (msgent->cb)(irc, msgent->name, tmp, args);
796 g_free(tmp);
797 } else {
798 purple_debug_error("irc", "args count (%d) doesn't reach "
799 "expected value of %d for the '%s' command",
800 args_cnt, msgent->req_cnt, msgent->name);
802 for (i = 0; i < strlen(msgent->format); i++) {
803 g_free(args[i]);
805 g_free(args);
806 g_free(from);
809 static void irc_parse_error_cb(struct irc_conn *irc, char *input)
811 char *clean;
812 /* This really should be escaped somehow that you can tell what
813 * the junk was -- but as it is, it can crash glib. */
814 clean = purple_utf8_salvage(input);
815 purple_debug(PURPLE_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", clean);
816 g_free(clean);