Wed, 23 Feb 2005 13:21:38 +0000
[gaim-migrate @ 12113]
Handle bad nick errors
| 6333 | 1 | /** |
| 2 | * @file parse.c | |
| 8351 | 3 | * |
| 6333 | 4 | * gaim |
| 5 | * | |
| 6 | * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu> | |
| 8351 | 7 | * |
| 6333 | 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" | |
| 10258 | 29 | #include "util.h" |
| 9130 | 30 | #include "cmds.h" |
| 6333 | 31 | #include "irc.h" |
| 32 | ||
| 33 | #include <stdio.h> | |
| 34 | #include <stdlib.h> | |
| 35 | #include <ctype.h> | |
| 36 | ||
| 37 | static char *irc_send_convert(struct irc_conn *irc, const char *string); | |
| 38 | static char *irc_recv_convert(struct irc_conn *irc, 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) */ | |
| 8114 | 62 | { "321", "*", irc_msg_list }, /* Start of list */ |
| 63 | { "322", "ncv:", irc_msg_list }, /* List. */ | |
| 64 | { "323", ":", irc_msg_list }, /* End of list. */ | |
| 6333 | 65 | { "324", "ncv:", irc_msg_chanmode }, /* Channel modes */ |
| 66 | { "331", "nc:", irc_msg_topic }, /* No channel topic */ | |
| 67 | { "332", "nc:", irc_msg_topic }, /* Channel topic */ | |
| 68 | { "333", "*", irc_msg_ignore }, /* Topic setter stuff */ | |
| 69 | { "353", "nvc:", irc_msg_names }, /* Names list */ | |
| 70 | { "366", "nc:", irc_msg_names }, /* End of names */ | |
| 71 | { "372", "n:", irc_msg_motd }, /* MOTD */ | |
| 72 | { "375", "n:", irc_msg_motd }, /* Start MOTD */ | |
| 73 | { "376", "n:", irc_msg_endmotd }, /* End of MOTD */ | |
| 10564 | 74 | { "391", "nv:", irc_msg_time }, /* Time reply */ |
| 6333 | 75 | { "401", "nt:", irc_msg_nonick }, /* No such nick/chan */ |
| 7877 | 76 | { "403", "nc:", irc_msg_nochan }, /* No such channel */ |
| 6333 | 77 | { "404", "nt:", irc_msg_nosend }, /* Cannot send to chan */ |
| 78 | { "421", "nv:", irc_msg_unknown }, /* Unknown command */ | |
| 6350 | 79 | { "422", "nv:", irc_msg_endmotd }, /* No MOTD available */ |
| 10633 | 80 | { "432", "vn:", irc_msg_badnick }, /* Erroneous nickname */ |
| 6333 | 81 | { "433", "vn:", irc_msg_nickused }, /* Nickname already in use */ |
| 6718 | 82 | { "438", "nn:", irc_msg_nochangenick }, /* Nick may not change */ |
| 6333 | 83 | { "442", "nc:", irc_msg_notinchan }, /* Not in channel */ |
| 84 | { "473", "nc:", irc_msg_inviteonly }, /* Tried to join invite-only */ | |
| 85 | { "474", "nc:", irc_msg_banned }, /* Banned from channel */ | |
| 86 | { "482", "nc:", irc_msg_notop }, /* Need to be op to do that */ | |
| 87 | { "501", "n:", irc_msg_badmode }, /* Unknown mode flag */ | |
| 8404 | 88 | { "506", "nc:", irc_msg_nosend }, /* Must identify to send */ |
| 6714 | 89 | { "515", "nc:", irc_msg_regonly }, /* Registration required */ |
| 6333 | 90 | { "invite", "n:", irc_msg_invite }, /* Invited */ |
| 91 | { "join", ":", irc_msg_join }, /* Joined a channel */ | |
| 92 | { "kick", "cn:", irc_msg_kick }, /* KICK */ | |
| 93 | { "mode", "tv:", irc_msg_mode }, /* MODE for channel */ | |
| 94 | { "nick", ":", irc_msg_nick }, /* Nick change */ | |
| 95 | { "notice", "t:", irc_msg_notice }, /* NOTICE recv */ | |
| 96 | { "part", "c:", irc_msg_part }, /* Parted a channel */ | |
| 97 | { "ping", ":", irc_msg_ping }, /* Received PING from server */ | |
| 98 | { "pong", "v:", irc_msg_pong }, /* Received PONG from server */ | |
| 99 | { "privmsg", "t:", irc_msg_privmsg }, /* Received private message */ | |
| 100 | { "topic", "c:", irc_msg_topic }, /* TOPIC command */ | |
| 101 | { "quit", ":", irc_msg_quit }, /* QUIT notice */ | |
| 102 | { "wallops", ":", irc_msg_wallops }, /* WALLOPS command */ | |
| 103 | { NULL, NULL, NULL } | |
| 104 | }; | |
| 105 | ||
| 106 | static struct _irc_user_cmd { | |
| 107 | char *name; | |
| 108 | char *format; | |
| 109 | IRCCmdCallback cb; | |
| 9255 | 110 | char *help; |
| 6333 | 111 | } _irc_cmds[] = { |
| 9255 | 112 | { "action", ":", irc_cmd_ctcp_action, N_("action <action to perform>: Perform an action.") }, |
| 113 | { "away", ":", irc_cmd_away, N_("away [message]: Set an away message, or use no message to return from being away.") }, | |
| 9258 | 114 | { "deop", ":", irc_cmd_op, N_("deop <nick1> [nick2] ...: Remove channel operator status from someone. You must be a channel operator to do this.") }, |
| 115 | { "devoice", ":", irc_cmd_op, N_("devoice <nick1> [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.") }, | |
| 116 | { "invite", ":", irc_cmd_invite, N_("invite <nick> [room]: Invite someone to join you in the specified channel, or the current channel.") }, | |
| 9266 | 117 | { "j", "cv", irc_cmd_join, N_("j <room1>[,room2][,...] [key1[,key2][,...]]: Enter one or more channels, optionally providing a channel key for each if needed.") }, |
| 118 | { "join", "cv", irc_cmd_join, N_("join <room1>[,room2][,...] [key1[,key2][,...]]: Enter one or more channels, optionally providing a channel key for each if needed.") }, | |
| 9258 | 119 | { "kick", "n:", irc_cmd_kick, N_("kick <nick> [message]: Remove someone from a channel. You must be a channel operator to do this.") }, |
| 120 | { "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>") }, | |
| 9255 | 121 | { "me", ":", irc_cmd_ctcp_action, N_("me <action to perform>: Perform an action.") }, |
|
10609
170a5af61448
[gaim-migrate @ 12055]
Richard Laager <rlaager@pidgin.im>
parents:
10564
diff
changeset
|
122 | { "mode", ":", irc_cmd_mode, N_("mode <+|-><A-Za-z> <nick|channel>: Set or unset a channel or user mode.") }, |
| 9258 | 123 | { "msg", "t:", irc_cmd_privmsg, N_("msg <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 124 | { "names", "c", irc_cmd_names, N_("names [channel]: List the users currently in a channel.") }, | |
| 9274 | 125 | { "nick", "n", irc_cmd_nick, N_("nick <new nickname>: Change your nickname.") }, |
| 9258 | 126 | { "op", ":", irc_cmd_op, N_("op <nick1> [nick2] ...: Grant channel operator status to someone. You must be a channel operator to do this.") }, |
| 9255 | 127 | { "operwall", ":", irc_cmd_wallops, N_("operwall <message>: If you don't know what this is, you probably can't use it.") }, |
| 9258 | 128 | { "part", "c:", irc_cmd_part, N_("part [room] [message]: Leave the current channel, or a specified channel, with an optional message.") }, |
| 9255 | 129 | { "ping", "n", irc_cmd_ping, N_("ping [nick]: Asks how much lag a user (or the server if no user specified) has.") }, |
| 9258 | 130 | { "query", "n:", irc_cmd_query, N_("query <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 9255 | 131 | { "quit", ":", irc_cmd_quit, N_("quit [message]: Disconnect from the server, with an optional message.") }, |
| 132 | { "quote", "*", irc_cmd_quote, N_("quote [...]: Send a raw command to the server.") }, | |
| 133 | { "remove", "n:", irc_cmd_remove, N_("remove <nick> [message]: Remove someone from a room. You must be a channel operator to do this.") }, | |
| 10564 | 134 | { "time", "", irc_cmd_time, N_("time: Displays the current local time at the IRC server.") }, |
| 9255 | 135 | { "topic", ":", irc_cmd_topic, N_("topic [new topic]: View or change the channel topic.") }, |
| 136 | { "umode", ":", irc_cmd_mode, N_("umode <+|-><A-Za-z>: Set or unset a user mode.") }, | |
| 9258 | 137 | { "voice", ":", irc_cmd_op, N_("voice <nick1> [nick2] ...: Grant channel voice status to someone. You must be a channel operator to do this.") }, |
| 9255 | 138 | { "wallops", ":", irc_cmd_wallops, N_("wallops <message>: If you don't know what this is, you probably can't use it.") }, |
|
10609
170a5af61448
[gaim-migrate @ 12055]
Richard Laager <rlaager@pidgin.im>
parents:
10564
diff
changeset
|
139 | { "whois", "tt", irc_cmd_whois, N_("whois [server] <nick>: Get information on a user.") }, |
| 7631 | 140 | { NULL, NULL, NULL } |
| 6333 | 141 | }; |
| 142 | ||
| 9130 | 143 | static GaimCmdRet irc_parse_gaim_cmd(GaimConversation *conv, const gchar *cmd, |
| 9597 | 144 | gchar **args, gchar **error, void *data) |
| 9130 | 145 | { |
| 146 | GaimConnection *gc; | |
| 147 | struct irc_conn *irc; | |
| 148 | struct _irc_user_cmd *cmdent; | |
| 149 | ||
| 150 | gc = gaim_conversation_get_gc(conv); | |
| 151 | if (!gc) | |
| 152 | return GAIM_CMD_RET_FAILED; | |
| 153 | ||
| 154 | irc = gc->proto_data; | |
| 155 | ||
| 156 | if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL) | |
| 157 | return GAIM_CMD_RET_FAILED; | |
| 158 | ||
| 159 | (cmdent->cb)(irc, cmd, gaim_conversation_get_name(conv), (const char **)args); | |
| 160 | ||
| 161 | return GAIM_CMD_RET_OK; | |
| 162 | } | |
| 163 | ||
| 164 | static void irc_register_command(struct _irc_user_cmd *c) | |
| 165 | { | |
| 166 | GaimCmdFlag f; | |
| 167 | char args[10]; | |
| 168 | char *format; | |
| 169 | int i; | |
| 170 | ||
| 171 | f = GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_PRPL_ONLY | |
| 172 | | GAIM_CMD_FLAG_ALLOW_WRONG_ARGS; | |
| 173 | ||
| 174 | format = c->format; | |
| 175 | ||
| 176 | for (i = 0; (i < (sizeof(args) - 1)) && *format; i++, format++) | |
| 177 | switch (*format) { | |
| 178 | case 'v': | |
| 179 | case 'n': | |
| 180 | case 'c': | |
| 181 | case 't': | |
| 182 | args[i] = 'w'; | |
| 183 | break; | |
| 184 | case ':': | |
| 185 | case '*': | |
| 186 | args[i] = 's'; | |
| 187 | break; | |
| 188 | } | |
| 189 | ||
| 190 | args[i] = '\0'; | |
| 191 | ||
| 9597 | 192 | gaim_cmd_register(c->name, args, GAIM_CMD_P_PRPL, f, "prpl-irc", |
| 193 | irc_parse_gaim_cmd, _(c->help), NULL); | |
| 9130 | 194 | } |
| 195 | ||
| 196 | void irc_register_commands(void) | |
| 197 | { | |
| 198 | struct _irc_user_cmd *c; | |
| 199 | ||
| 200 | for (c = _irc_cmds; c && c->name; c++) | |
| 201 | irc_register_command(c); | |
| 202 | } | |
| 203 | ||
| 6333 | 204 | static char *irc_send_convert(struct irc_conn *irc, const char *string) |
| 205 | { | |
| 206 | char *utf8; | |
| 207 | GError *err = NULL; | |
| 10258 | 208 | gchar **encodings; |
| 209 | const gchar *enclist; | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
210 | |
| 10258 | 211 | enclist = gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 212 | encodings = g_strsplit(enclist, ",", 2); | |
| 213 | ||
| 10278 | 214 | if (encodings[0] == NULL || !strcasecmp("UTF-8", encodings[0])) { |
| 215 | g_strfreev(encodings); | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
216 | return g_strdup(string); |
| 10278 | 217 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
218 | |
| 10258 | 219 | utf8 = g_convert(string, strlen(string), encodings[0], "UTF-8", NULL, NULL, &err); |
| 6333 | 220 | if (err) { |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
221 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Send conversion error: %s\n", err->message); |
| 10258 | 222 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Sending as UTF-8 instead of %s\n", encodings[0]); |
| 6333 | 223 | utf8 = g_strdup(string); |
| 8954 | 224 | g_error_free(err); |
| 6333 | 225 | } |
| 10258 | 226 | g_strfreev(encodings); |
| 227 | ||
| 6333 | 228 | return utf8; |
| 229 | } | |
| 230 | ||
| 231 | static char *irc_recv_convert(struct irc_conn *irc, const char *string) | |
| 232 | { | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
233 | char *utf8 = NULL; |
| 10258 | 234 | const gchar *charset, *enclist; |
| 235 | gchar **encodings; | |
| 236 | int i; | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
237 | |
| 10258 | 238 | enclist = gaim_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 239 | encodings = g_strsplit(enclist, ",", -1); | |
| 240 | ||
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
241 | if (encodings[0] == NULL) { |
|
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
242 | g_strfreev(encodings); |
| 10258 | 243 | return gaim_utf8_salvage(string); |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
244 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
245 | |
| 10258 | 246 | for (i = 0; encodings[i] != NULL; i++) { |
| 247 | charset = encodings[i]; | |
| 248 | while (*charset == ' ') | |
| 249 | charset++; | |
| 250 | ||
| 251 | if (!strcasecmp("UTF-8", charset)) { | |
| 252 | if (g_utf8_validate(string, strlen(string), NULL)) | |
| 253 | utf8 = g_strdup(string); | |
| 254 | } else { | |
| 255 | utf8 = g_convert(string, strlen(string), "UTF-8", charset, NULL, NULL, NULL); | |
| 256 | } | |
| 257 | ||
| 258 | if (utf8) { | |
| 259 | g_strfreev(encodings); | |
| 260 | return utf8; | |
| 261 | } | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
262 | } |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
263 | g_strfreev(encodings); |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
264 | |
| 10258 | 265 | return gaim_utf8_salvage(string); |
| 6333 | 266 | } |
| 267 | ||
| 268 | /* XXX tag closings are not necessarily correctly nested here! If we | |
| 269 | * get a ^O or reach the end of the string and there are open | |
| 270 | * tags, they are closed in a fixed order ... this means, for | |
| 271 | * example, you might see <FONT COLOR="blue">some text <B>with | |
| 272 | * various attributes</FONT></B> (notice that B and FONT overlap | |
| 273 | * and are not cleanly nested). This is imminently fixable but | |
| 274 | * I am not fixing it right now. | |
| 275 | */ | |
| 276 | char *irc_mirc2html(const char *string) | |
| 277 | { | |
| 278 | const char *cur, *end; | |
| 279 | char fg[3] = "\0\0", bg[3] = "\0\0"; | |
| 280 | int fgnum, bgnum; | |
| 6754 | 281 | int font = 0, bold = 0, underline = 0; |
| 6333 | 282 | GString *decoded = g_string_sized_new(strlen(string)); |
| 283 | ||
| 284 | cur = string; | |
| 285 | do { | |
| 6754 | 286 | end = strpbrk(cur, "\002\003\007\017\026\037"); |
| 6333 | 287 | |
| 288 | decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur)); | |
| 289 | cur = end ? end : cur + strlen(cur); | |
| 290 | ||
| 291 | switch (*cur) { | |
| 292 | case '\002': | |
| 293 | cur++; | |
| 294 | if (!bold) { | |
| 295 | decoded = g_string_append(decoded, "<B>"); | |
| 296 | bold = TRUE; | |
| 297 | } else { | |
| 298 | decoded = g_string_append(decoded, "</B>"); | |
| 299 | bold = FALSE; | |
| 300 | } | |
| 301 | break; | |
| 302 | case '\003': | |
| 303 | cur++; | |
| 304 | fg[0] = fg[1] = bg[0] = bg[1] = '\0'; | |
| 305 | if (isdigit(*cur)) | |
| 306 | fg[0] = *cur++; | |
| 307 | if (isdigit(*cur)) | |
| 308 | fg[1] = *cur++; | |
| 309 | if (*cur == ',') { | |
| 310 | cur++; | |
| 311 | if (isdigit(*cur)) | |
| 312 | bg[0] = *cur++; | |
| 313 | if (isdigit(*cur)) | |
| 314 | bg[1] = *cur++; | |
| 315 | } | |
| 316 | if (font) { | |
| 317 | decoded = g_string_append(decoded, "</FONT>"); | |
| 318 | font = FALSE; | |
| 319 | } | |
| 320 | ||
| 321 | if (fg[0]) { | |
| 322 | fgnum = atoi(fg); | |
| 323 | if (fgnum < 0 || fgnum > 15) | |
| 324 | continue; | |
| 325 | font = TRUE; | |
| 326 | g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]); | |
| 327 | if (bg[0]) { | |
| 328 | bgnum = atoi(bg); | |
| 329 | if (bgnum >= 0 && bgnum < 16) | |
| 330 | g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]); | |
| 331 | } | |
| 332 | decoded = g_string_append_c(decoded, '>'); | |
| 333 | } | |
| 334 | break; | |
| 6754 | 335 | case '\037': |
| 336 | cur++; | |
| 337 | if (!underline) { | |
| 338 | decoded = g_string_append(decoded, "<U>"); | |
| 339 | underline = TRUE; | |
| 340 | } else { | |
| 341 | decoded = g_string_append(decoded, "</U>"); | |
| 342 | underline = TRUE; | |
| 343 | } | |
| 344 | break; | |
| 6333 | 345 | case '\007': |
| 346 | case '\026': | |
| 347 | cur++; | |
| 348 | break; | |
| 349 | case '\017': | |
| 350 | cur++; | |
| 351 | /* fallthrough */ | |
| 352 | case '\000': | |
| 353 | if (bold) | |
| 6754 | 354 | decoded = g_string_append(decoded, "</B>"); |
| 355 | if (underline) | |
| 356 | decoded = g_string_append(decoded, "</U>"); | |
| 6333 | 357 | if (font) |
| 358 | decoded = g_string_append(decoded, "</FONT>"); | |
| 359 | break; | |
| 360 | default: | |
| 361 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur); | |
| 362 | } | |
| 363 | } while (*cur); | |
| 364 | ||
| 365 | return g_string_free(decoded, FALSE); | |
| 366 | } | |
| 367 | ||
| 8529 | 368 | char *irc_mirc2txt (const char *string) |
| 369 | { | |
| 370 | char *result = g_strdup (string); | |
| 371 | int i, j; | |
| 372 | ||
| 373 | for (i = 0, j = 0; result[i]; i++) { | |
| 374 | switch (result[i]) { | |
| 375 | case '\002': | |
| 376 | case '\003': | |
| 377 | case '\007': | |
| 378 | case '\017': | |
| 379 | case '\026': | |
| 380 | case '\037': | |
| 381 | continue; | |
| 382 | default: | |
| 383 | result[j++] = result[i]; | |
| 384 | } | |
| 385 | } | |
| 386 | result[i] = '\0'; | |
| 387 | return result; | |
| 388 | } | |
| 389 | ||
| 10208 | 390 | gboolean irc_ischannel(const char *string) |
| 391 | { | |
| 392 | return (string[0] == '#' || string[0] == '&'); | |
| 393 | } | |
| 394 | ||
| 6333 | 395 | char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice) |
| 396 | { | |
| 397 | GaimConnection *gc; | |
| 398 | const char *cur = msg + 1; | |
| 399 | char *buf, *ctcp; | |
| 400 | time_t timestamp; | |
| 401 | ||
| 6754 | 402 | /* Note that this is NOT correct w.r.t. multiple CTCPs in one |
| 403 | * message and low-level quoting ... but if you want that crap, | |
| 404 | * use a real IRC client. */ | |
| 405 | ||
| 6333 | 406 | if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001') |
| 407 | return g_strdup(msg); | |
| 408 | ||
| 409 | if (!strncmp(cur, "ACTION ", 7)) { | |
| 410 | cur += 7; | |
| 411 | buf = g_strdup_printf("/me %s", cur); | |
| 412 | buf[strlen(buf) - 1] = '\0'; | |
| 413 | return buf; | |
| 414 | } else if (!strncmp(cur, "PING ", 5)) { | |
| 415 | if (notice) { /* reply */ | |
| 416 | sscanf(cur, "PING %lu", ×tamp); | |
| 417 | gc = gaim_account_get_connection(irc->account); | |
| 418 | if (!gc) | |
| 419 | return NULL; | |
| 6350 | 420 | buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp); |
| 6333 | 421 | gaim_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf); |
| 422 | g_free(buf); | |
| 423 | return NULL; | |
| 424 | } else { | |
| 425 | buf = irc_format(irc, "vt:", "NOTICE", from, msg); | |
| 426 | irc_send(irc, buf); | |
| 427 | g_free(buf); | |
| 428 | gc = gaim_account_get_connection(irc->account); | |
| 429 | } | |
| 430 | } else if (!strncmp(cur, "VERSION", 7) && !notice) { | |
| 431 | buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Gaim IRC\001"); | |
| 432 | irc_send(irc, buf); | |
| 433 | g_free(buf); | |
| 8351 | 434 | } else if (!strncmp(cur, "DCC SEND ", 9)) { |
| 435 | irc_dccsend_recv(irc, from, msg + 10); | |
| 436 | return NULL; | |
| 6333 | 437 | } |
| 438 | ||
| 439 | ctcp = g_strdup(msg + 1); | |
| 440 | ctcp[strlen(ctcp) - 1] = '\0'; | |
| 441 | buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from); | |
| 442 | g_free(ctcp); | |
| 443 | return buf; | |
| 444 | } | |
| 445 | ||
| 446 | void irc_msg_table_build(struct irc_conn *irc) | |
| 447 | { | |
| 448 | int i; | |
| 449 | ||
| 450 | if (!irc || !irc->msgs) { | |
| 451 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n"); | |
| 452 | return; | |
| 453 | } | |
| 454 | ||
| 455 | for (i = 0; _irc_msgs[i].name; i++) { | |
| 456 | g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]); | |
| 457 | } | |
| 458 | } | |
| 459 | ||
| 460 | void irc_cmd_table_build(struct irc_conn *irc) | |
| 461 | { | |
| 462 | int i; | |
| 463 | ||
| 464 | if (!irc || !irc->cmds) { | |
| 465 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n"); | |
| 466 | return; | |
| 467 | } | |
| 468 | ||
| 469 | for (i = 0; _irc_cmds[i].name ; i++) { | |
| 470 | g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]); | |
| 471 | } | |
| 472 | } | |
| 473 | ||
| 474 | char *irc_format(struct irc_conn *irc, const char *format, ...) | |
| 475 | { | |
| 476 | GString *string = g_string_new(""); | |
| 477 | char *tok, *tmp; | |
| 478 | const char *cur; | |
| 479 | va_list ap; | |
| 480 | ||
| 481 | va_start(ap, format); | |
| 482 | for (cur = format; *cur; cur++) { | |
| 483 | if (cur != format) | |
| 484 | g_string_append_c(string, ' '); | |
| 485 | ||
| 486 | tok = va_arg(ap, char *); | |
| 487 | switch (*cur) { | |
| 488 | case 'v': | |
| 489 | g_string_append(string, tok); | |
| 490 | break; | |
| 491 | case ':': | |
| 492 | g_string_append_c(string, ':'); | |
| 493 | /* no break! */ | |
| 494 | case 't': | |
| 495 | case 'n': | |
| 496 | case 'c': | |
| 497 | tmp = irc_send_convert(irc, tok); | |
| 498 | g_string_append(string, tmp); | |
| 499 | g_free(tmp); | |
| 500 | break; | |
| 501 | default: | |
| 502 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur); | |
| 503 | break; | |
| 504 | } | |
| 505 | } | |
| 506 | va_end(ap); | |
| 507 | g_string_append(string, "\r\n"); | |
| 508 | return (g_string_free(string, FALSE)); | |
| 509 | } | |
| 510 | ||
| 511 | void irc_parse_msg(struct irc_conn *irc, char *input) | |
| 512 | { | |
| 513 | struct _irc_msg *msgent; | |
| 514 | char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg; | |
| 7631 | 515 | guint i; |
| 6333 | 516 | |
| 517 | if (!strncmp(input, "PING ", 5)) { | |
| 518 | msg = irc_format(irc, "vv", "PONG", input + 5); | |
| 519 | irc_send(irc, msg); | |
| 520 | g_free(msg); | |
| 521 | return; | |
| 522 | } else if (!strncmp(input, "ERROR ", 6)) { | |
| 10154 | 523 | if (g_utf8_validate(input, -1, NULL)) { |
| 524 | char *tmp = g_strdup_printf("%s\n%s", _("Disconnected."), input); | |
| 525 | gaim_connection_error(gaim_account_get_connection(irc->account), tmp); | |
| 526 | g_free(tmp); | |
| 527 | } else | |
| 528 | gaim_connection_error(gaim_account_get_connection(irc->account), _("Disconnected.")); | |
| 6333 | 529 | return; |
| 530 | } | |
| 531 | ||
| 532 | if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) { | |
| 533 | irc_parse_error_cb(irc, input); | |
| 534 | return; | |
| 535 | } | |
| 536 | ||
| 537 | from = g_strndup(&input[1], cur - &input[1]); | |
| 538 | cur++; | |
| 539 | end = strchr(cur, ' '); | |
| 540 | if (!end) | |
| 541 | end = cur + strlen(cur); | |
| 542 | ||
| 543 | tmp = g_strndup(cur, end - cur); | |
| 544 | msgname = g_ascii_strdown(tmp, -1); | |
| 545 | g_free(tmp); | |
| 546 | ||
| 547 | if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) { | |
| 548 | irc_msg_default(irc, "", from, &input); | |
| 549 | g_free(msgname); | |
| 550 | g_free(from); | |
| 551 | return; | |
| 552 | } | |
| 553 | g_free(msgname); | |
| 554 | ||
| 555 | args = g_new0(char *, strlen(msgent->format)); | |
| 556 | for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) { | |
| 557 | switch (fmt[i]) { | |
| 558 | case 'v': | |
| 559 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 560 | args[i] = g_strndup(cur, end - cur); | |
| 561 | cur += end - cur; | |
| 562 | break; | |
| 563 | case 't': | |
| 564 | case 'n': | |
| 565 | case 'c': | |
| 566 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 567 | tmp = g_strndup(cur, end - cur); | |
| 568 | args[i] = irc_recv_convert(irc, tmp); | |
| 569 | g_free(tmp); | |
| 570 | cur += end - cur; | |
| 571 | break; | |
| 572 | case ':': | |
| 573 | if (*cur == ':') cur++; | |
| 574 | args[i] = irc_recv_convert(irc, cur); | |
| 575 | cur = cur + strlen(cur); | |
| 576 | break; | |
| 577 | case '*': | |
| 578 | args[i] = g_strdup(cur); | |
| 579 | cur = cur + strlen(cur); | |
| 580 | break; | |
| 581 | default: | |
| 582 | gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]); | |
| 583 | break; | |
| 584 | } | |
| 585 | } | |
| 6970 | 586 | tmp = irc_recv_convert(irc, from); |
| 587 | (msgent->cb)(irc, msgent->name, tmp, args); | |
| 588 | g_free(tmp); | |
| 6333 | 589 | for (i = 0; i < strlen(msgent->format); i++) { |
| 590 | g_free(args[i]); | |
| 591 | } | |
| 592 | g_free(args); | |
| 593 | g_free(from); | |
| 594 | } | |
| 595 | ||
| 596 | static void irc_parse_error_cb(struct irc_conn *irc, char *input) | |
| 597 | { | |
| 598 | gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", input); | |
| 599 | } |