Mon, 21 Feb 2011 01:31:32 +0000
Whitespace fix
| 6333 | 1 | /** |
| 2 | * @file parse.c | |
| 8351 | 3 | * |
| 15884 | 4 | * purple |
| 6333 | 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 | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
17409
diff
changeset
|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 6333 | 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 | ||
| 15884 | 47 | extern PurplePlugin *_irc_plugin; |
| 14683 | 48 | |
| 6333 | 49 | /*typedef void (*IRCMsgCallback)(struct irc_conn *irc, char *from, char *name, char **args);*/ |
| 50 | static struct _irc_msg { | |
| 51 | char *name; | |
| 52 | char *format; | |
| 53 | void (*cb)(struct irc_conn *irc, const char *name, const char *from, char **args); | |
| 54 | } _irc_msgs[] = { | |
|
15501
8547fa591578
Recognize non-standard mode characters communicated in an 005 numeric for IRC
Ethan Blanton <elb@pidgin.im>
parents:
15435
diff
changeset
|
55 | { "005", "n*", irc_msg_features }, /* Feature list */ |
|
15508
51a669b06193
This should fix up IRC logins for servers which do not provide an
Ethan Blanton <elb@pidgin.im>
parents:
15501
diff
changeset
|
56 | { "251", "n:", irc_msg_luser }, /* Client & Server count */ |
|
51a669b06193
This should fix up IRC logins for servers which do not provide an
Ethan Blanton <elb@pidgin.im>
parents:
15501
diff
changeset
|
57 | { "255", "n:", irc_msg_luser }, /* Client & Server count Mk. II */ |
| 6333 | 58 | { "301", "nn:", irc_msg_away }, /* User is away */ |
| 59 | { "303", "n:", irc_msg_ison }, /* ISON reply */ | |
| 60 | { "311", "nnvvv:", irc_msg_whois }, /* Whois user */ | |
| 61 | { "312", "nnv:", irc_msg_whois }, /* Whois server */ | |
| 62 | { "313", "nn:", irc_msg_whois }, /* Whois ircop */ | |
| 63 | { "317", "nnvv", irc_msg_whois }, /* Whois idle */ | |
| 64 | { "318", "nt:", irc_msg_endwhois }, /* End of WHOIS */ | |
| 65 | { "319", "nn:", irc_msg_whois }, /* Whois channels */ | |
| 66 | { "320", "nn:", irc_msg_whois }, /* Whois (fn ident) */ | |
|
21720
235394d5c7f4
Pull a bunch of bugfix only changes to im.pidgin.pidgin.2.3.1,
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
21279
diff
changeset
|
67 | { "314", "nnnvv:", irc_msg_whois }, /* Whowas user */ |
| 31452 | 68 | { "315", "nt:", irc_msg_who }, /* end of WHO channel */ |
|
16474
bc8580a69dba
/whowas for IRC. Thanks, achris.
Ethan Blanton <elb@pidgin.im>
parents:
15884
diff
changeset
|
69 | { "369", "nt:", irc_msg_endwhois }, /* End of WHOWAS */ |
| 8114 | 70 | { "321", "*", irc_msg_list }, /* Start of list */ |
| 71 | { "322", "ncv:", irc_msg_list }, /* List. */ | |
| 72 | { "323", ":", irc_msg_list }, /* End of list. */ | |
| 6333 | 73 | { "324", "ncv:", irc_msg_chanmode }, /* Channel modes */ |
| 74 | { "331", "nc:", irc_msg_topic }, /* No channel topic */ | |
| 75 | { "332", "nc:", irc_msg_topic }, /* Channel topic */ | |
| 76 | { "333", "*", irc_msg_ignore }, /* Topic setter stuff */ | |
| 31452 | 77 | { "352", "nvcvnvvv:", irc_msg_who },/* Channel WHO */ |
| 6333 | 78 | { "353", "nvc:", irc_msg_names }, /* Names list */ |
| 79 | { "366", "nc:", irc_msg_names }, /* End of names */ | |
|
22798
a501b4137c64
Handle IRC ban lists in-channel. Channels which are not joined are
Ethan Blanton <elb@pidgin.im>
parents:
22092
diff
changeset
|
80 | { "367", "ncnnv", irc_msg_ban }, /* Ban list */ |
|
a501b4137c64
Handle IRC ban lists in-channel. Channels which are not joined are
Ethan Blanton <elb@pidgin.im>
parents:
22092
diff
changeset
|
81 | { "368", "nc:", irc_msg_ban }, /* End of ban list */ |
| 6333 | 82 | { "372", "n:", irc_msg_motd }, /* MOTD */ |
| 83 | { "375", "n:", irc_msg_motd }, /* Start MOTD */ | |
|
15508
51a669b06193
This should fix up IRC logins for servers which do not provide an
Ethan Blanton <elb@pidgin.im>
parents:
15501
diff
changeset
|
84 | { "376", "n:", irc_msg_motd }, /* End of MOTD */ |
| 10564 | 85 | { "391", "nv:", irc_msg_time }, /* Time reply */ |
| 6333 | 86 | { "401", "nt:", irc_msg_nonick }, /* No such nick/chan */ |
|
16474
bc8580a69dba
/whowas for IRC. Thanks, achris.
Ethan Blanton <elb@pidgin.im>
parents:
15884
diff
changeset
|
87 | { "406", "nt:", irc_msg_nonick }, /* No such nick for WHOWAS */ |
| 7877 | 88 | { "403", "nc:", irc_msg_nochan }, /* No such channel */ |
| 6333 | 89 | { "404", "nt:", irc_msg_nosend }, /* Cannot send to chan */ |
| 90 | { "421", "nv:", irc_msg_unknown }, /* Unknown command */ | |
|
31229
4bef370d8a92
Process 422 (ERR_NOMOTD) as a valid replacement for an actual MOTD
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
28468
diff
changeset
|
91 | { "422", "n:", irc_msg_motd }, /* MOTD file missing */ |
| 10633 | 92 | { "432", "vn:", irc_msg_badnick }, /* Erroneous nickname */ |
| 6333 | 93 | { "433", "vn:", irc_msg_nickused }, /* Nickname already in use */ |
|
10712
2aded9e9abd4
[gaim-migrate @ 12306]
Richard Laager <rlaager@pidgin.im>
parents:
10659
diff
changeset
|
94 | { "437", "nc:", irc_msg_unavailable }, /* Nick/channel is unavailable */ |
| 6718 | 95 | { "438", "nn:", irc_msg_nochangenick }, /* Nick may not change */ |
| 6333 | 96 | { "442", "nc:", irc_msg_notinchan }, /* Not in channel */ |
| 97 | { "473", "nc:", irc_msg_inviteonly }, /* Tried to join invite-only */ | |
| 98 | { "474", "nc:", irc_msg_banned }, /* Banned from channel */ | |
|
16993
536c1485c139
Handle channel joins when registration is required more gracefully.
Ethan Blanton <elb@pidgin.im>
parents:
16474
diff
changeset
|
99 | { "477", "nc:", irc_msg_regonly }, /* Registration Required */ |
|
10659
caf16376fba0
[gaim-migrate @ 12192]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10633
diff
changeset
|
100 | { "478", "nct:", irc_msg_banfull }, /* Banlist is full */ |
| 6333 | 101 | { "482", "nc:", irc_msg_notop }, /* Need to be op to do that */ |
| 102 | { "501", "n:", irc_msg_badmode }, /* Unknown mode flag */ | |
| 8404 | 103 | { "506", "nc:", irc_msg_nosend }, /* Must identify to send */ |
| 6714 | 104 | { "515", "nc:", irc_msg_regonly }, /* Registration required */ |
| 6333 | 105 | { "invite", "n:", irc_msg_invite }, /* Invited */ |
| 106 | { "join", ":", irc_msg_join }, /* Joined a channel */ | |
| 107 | { "kick", "cn:", irc_msg_kick }, /* KICK */ | |
| 108 | { "mode", "tv:", irc_msg_mode }, /* MODE for channel */ | |
| 109 | { "nick", ":", irc_msg_nick }, /* Nick change */ | |
| 110 | { "notice", "t:", irc_msg_notice }, /* NOTICE recv */ | |
| 111 | { "part", "c:", irc_msg_part }, /* Parted a channel */ | |
| 112 | { "ping", ":", irc_msg_ping }, /* Received PING from server */ | |
| 113 | { "pong", "v:", irc_msg_pong }, /* Received PONG from server */ | |
| 114 | { "privmsg", "t:", irc_msg_privmsg }, /* Received private message */ | |
| 115 | { "topic", "c:", irc_msg_topic }, /* TOPIC command */ | |
| 116 | { "quit", ":", irc_msg_quit }, /* QUIT notice */ | |
| 117 | { "wallops", ":", irc_msg_wallops }, /* WALLOPS command */ | |
| 118 | { NULL, NULL, NULL } | |
| 119 | }; | |
| 120 | ||
| 121 | static struct _irc_user_cmd { | |
| 122 | char *name; | |
| 123 | char *format; | |
| 124 | IRCCmdCallback cb; | |
| 9255 | 125 | char *help; |
| 6333 | 126 | } _irc_cmds[] = { |
| 9255 | 127 | { "action", ":", irc_cmd_ctcp_action, N_("action <action to perform>: Perform an action.") }, |
| 128 | { "away", ":", irc_cmd_away, N_("away [message]: Set an away message, or use no message to return from being away.") }, | |
|
23895
4d883627111f
/ctcp command for IRC
Vladislav Guberinic <neosisani@gmail.com>
parents:
23532
diff
changeset
|
129 | { "ctcp", "t:", irc_cmd_ctcp, N_("ctcp <nick> <msg>: sends ctcp msg to nick.") }, |
| 12013 | 130 | { "chanserv", ":", irc_cmd_service, N_("chanserv: Send a command to chanserv") }, |
| 9258 | 131 | { "deop", ":", irc_cmd_op, N_("deop <nick1> [nick2] ...: Remove channel operator status from someone. You must be a channel operator to do this.") }, |
| 132 | { "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.") }, | |
| 133 | { "invite", ":", irc_cmd_invite, N_("invite <nick> [room]: Invite someone to join you in the specified channel, or the current channel.") }, | |
| 9266 | 134 | { "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.") }, |
| 135 | { "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 | 136 | { "kick", "n:", irc_cmd_kick, N_("kick <nick> [message]: Remove someone from a channel. You must be a channel operator to do this.") }, |
| 137 | { "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 | 138 | { "me", ":", irc_cmd_ctcp_action, N_("me <action to perform>: Perform an action.") }, |
| 12013 | 139 | { "memoserv", ":", irc_cmd_service, N_("memoserv: Send a command to memoserv") }, |
|
10609
170a5af61448
[gaim-migrate @ 12055]
Richard Laager <rlaager@pidgin.im>
parents:
10564
diff
changeset
|
140 | { "mode", ":", irc_cmd_mode, N_("mode <+|-><A-Za-z> <nick|channel>: Set or unset a channel or user mode.") }, |
| 9258 | 141 | { "msg", "t:", irc_cmd_privmsg, N_("msg <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 142 | { "names", "c", irc_cmd_names, N_("names [channel]: List the users currently in a channel.") }, | |
| 9274 | 143 | { "nick", "n", irc_cmd_nick, N_("nick <new nickname>: Change your nickname.") }, |
| 12013 | 144 | { "nickserv", ":", irc_cmd_service, N_("nickserv: Send a command to nickserv") }, |
|
22092
52c00ca73f6f
Added /notice support for IRC. If I didn't do this properly, feel free
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
21720
diff
changeset
|
145 | { "notice", "t:", irc_cmd_privmsg, N_("notice <target<: Send a notice to a user or channel.") }, |
| 9258 | 146 | { "op", ":", irc_cmd_op, N_("op <nick1> [nick2] ...: Grant channel operator status to someone. You must be a channel operator to do this.") }, |
| 9255 | 147 | { "operwall", ":", irc_cmd_wallops, N_("operwall <message>: If you don't know what this is, you probably can't use it.") }, |
| 12013 | 148 | { "operserv", ":", irc_cmd_service, N_("operserv: Send a command to operserv") }, |
| 9258 | 149 | { "part", "c:", irc_cmd_part, N_("part [room] [message]: Leave the current channel, or a specified channel, with an optional message.") }, |
| 9255 | 150 | { "ping", "n", irc_cmd_ping, N_("ping [nick]: Asks how much lag a user (or the server if no user specified) has.") }, |
| 9258 | 151 | { "query", "n:", irc_cmd_query, N_("query <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 9255 | 152 | { "quit", ":", irc_cmd_quit, N_("quit [message]: Disconnect from the server, with an optional message.") }, |
| 153 | { "quote", "*", irc_cmd_quote, N_("quote [...]: Send a raw command to the server.") }, | |
| 154 | { "remove", "n:", irc_cmd_remove, N_("remove <nick> [message]: Remove someone from a room. You must be a channel operator to do this.") }, | |
| 10564 | 155 | { "time", "", irc_cmd_time, N_("time: Displays the current local time at the IRC server.") }, |
| 9255 | 156 | { "topic", ":", irc_cmd_topic, N_("topic [new topic]: View or change the channel topic.") }, |
| 157 | { "umode", ":", irc_cmd_mode, N_("umode <+|-><A-Za-z>: Set or unset a user mode.") }, | |
| 13943 | 158 | { "version", ":", irc_cmd_ctcp_version, N_("version [nick]: send CTCP VERSION request to a user") }, |
| 9258 | 159 | { "voice", ":", irc_cmd_op, N_("voice <nick1> [nick2] ...: Grant channel voice status to someone. You must be a channel operator to do this.") }, |
| 9255 | 160 | { "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
|
161 | { "whois", "tt", irc_cmd_whois, N_("whois [server] <nick>: Get information on a user.") }, |
|
16474
bc8580a69dba
/whowas for IRC. Thanks, achris.
Ethan Blanton <elb@pidgin.im>
parents:
15884
diff
changeset
|
162 | { "whowas", "t", irc_cmd_whowas, N_("whowas <nick>: Get information on a user that has logged off.") }, |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
11136
diff
changeset
|
163 | { NULL, NULL, NULL, NULL } |
| 6333 | 164 | }; |
| 165 | ||
| 15884 | 166 | static PurpleCmdRet irc_parse_purple_cmd(PurpleConversation *conv, const gchar *cmd, |
| 9597 | 167 | gchar **args, gchar **error, void *data) |
| 9130 | 168 | { |
| 15884 | 169 | PurpleConnection *gc; |
| 9130 | 170 | struct irc_conn *irc; |
| 171 | struct _irc_user_cmd *cmdent; | |
| 172 | ||
| 15884 | 173 | gc = purple_conversation_get_gc(conv); |
| 9130 | 174 | if (!gc) |
| 15884 | 175 | return PURPLE_CMD_RET_FAILED; |
| 9130 | 176 | |
| 177 | irc = gc->proto_data; | |
| 178 | ||
| 179 | if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL) | |
| 15884 | 180 | return PURPLE_CMD_RET_FAILED; |
| 9130 | 181 | |
| 15884 | 182 | (cmdent->cb)(irc, cmd, purple_conversation_get_name(conv), (const char **)args); |
| 9130 | 183 | |
| 15884 | 184 | return PURPLE_CMD_RET_OK; |
| 9130 | 185 | } |
| 186 | ||
| 187 | static void irc_register_command(struct _irc_user_cmd *c) | |
| 188 | { | |
| 15884 | 189 | PurpleCmdFlag f; |
| 9130 | 190 | char args[10]; |
| 191 | char *format; | |
|
12316
b4e5d5ea15fd
[gaim-migrate @ 14620]
Richard Laager <rlaager@pidgin.im>
parents:
12282
diff
changeset
|
192 | size_t i; |
| 9130 | 193 | |
| 15884 | 194 | f = PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_PRPL_ONLY |
| 195 | | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS; | |
| 9130 | 196 | |
| 197 | format = c->format; | |
| 198 | ||
| 199 | for (i = 0; (i < (sizeof(args) - 1)) && *format; i++, format++) | |
| 200 | switch (*format) { | |
| 201 | case 'v': | |
| 202 | case 'n': | |
| 203 | case 'c': | |
| 204 | case 't': | |
| 205 | args[i] = 'w'; | |
| 206 | break; | |
| 207 | case ':': | |
| 208 | case '*': | |
| 209 | args[i] = 's'; | |
| 210 | break; | |
| 211 | } | |
| 212 | ||
| 213 | args[i] = '\0'; | |
| 214 | ||
| 15884 | 215 | purple_cmd_register(c->name, args, PURPLE_CMD_P_PRPL, f, "prpl-irc", |
| 216 | irc_parse_purple_cmd, _(c->help), NULL); | |
| 9130 | 217 | } |
| 218 | ||
| 219 | void irc_register_commands(void) | |
| 220 | { | |
| 221 | struct _irc_user_cmd *c; | |
| 222 | ||
| 223 | for (c = _irc_cmds; c && c->name; c++) | |
| 224 | irc_register_command(c); | |
| 225 | } | |
| 226 | ||
| 6333 | 227 | static char *irc_send_convert(struct irc_conn *irc, const char *string) |
| 228 | { | |
| 229 | char *utf8; | |
| 230 | GError *err = NULL; | |
| 10258 | 231 | gchar **encodings; |
| 232 | const gchar *enclist; | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
233 | |
| 15884 | 234 | enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 10258 | 235 | encodings = g_strsplit(enclist, ",", 2); |
| 236 | ||
|
17138
d666ace4a8ac
strcasecmp is post-C89, so use the glib equivalent
Ethan Blanton <elb@pidgin.im>
parents:
16993
diff
changeset
|
237 | if (encodings[0] == NULL || !g_ascii_strcasecmp("UTF-8", encodings[0])) { |
| 10278 | 238 | g_strfreev(encodings); |
|
23392
9268a4fc4786
Remove an unnecessary strdup when sending messages over IRC.
Sadrul Habib Chowdhury <sadrul@pidgin.im>
parents:
22798
diff
changeset
|
239 | return NULL; |
| 10278 | 240 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
241 | |
| 10258 | 242 | utf8 = g_convert(string, strlen(string), encodings[0], "UTF-8", NULL, NULL, &err); |
| 6333 | 243 | if (err) { |
| 15884 | 244 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Send conversion error: %s\n", err->message); |
| 245 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Sending as UTF-8 instead of %s\n", encodings[0]); | |
| 6333 | 246 | utf8 = g_strdup(string); |
| 8954 | 247 | g_error_free(err); |
| 6333 | 248 | } |
| 10258 | 249 | g_strfreev(encodings); |
| 250 | ||
| 6333 | 251 | return utf8; |
| 252 | } | |
| 253 | ||
| 254 | static char *irc_recv_convert(struct irc_conn *irc, const char *string) | |
| 255 | { | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
256 | char *utf8 = NULL; |
| 10258 | 257 | const gchar *charset, *enclist; |
| 258 | gchar **encodings; | |
|
23906
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
259 | gboolean autodetect; |
| 10258 | 260 | int i; |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
261 | |
| 15884 | 262 | enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 10258 | 263 | encodings = g_strsplit(enclist, ",", -1); |
| 264 | ||
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
265 | if (encodings[0] == NULL) { |
|
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
266 | g_strfreev(encodings); |
| 15884 | 267 | return purple_utf8_salvage(string); |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
268 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
269 | |
|
23906
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
270 | autodetect = purple_account_get_bool(irc->account, "autodetect_utf8", IRC_DEFAULT_AUTODETECT); |
|
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
271 | |
|
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
272 | if (autodetect && g_utf8_validate(string, -1, NULL)) { |
|
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
273 | return g_strdup(string); |
|
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
274 | } |
|
6962e96ddd38
This adds an "auto-detect UTF-8" option to IRC which, when enabled,
Ethan Blanton <elb@pidgin.im>
parents:
23895
diff
changeset
|
275 | |
| 10258 | 276 | for (i = 0; encodings[i] != NULL; i++) { |
| 277 | charset = encodings[i]; | |
| 278 | while (*charset == ' ') | |
| 279 | charset++; | |
| 280 | ||
|
17156
6d4cc0f310d1
More strcasecmp() replacements.
Richard Laager <rlaager@pidgin.im>
parents:
17138
diff
changeset
|
281 | if (!g_ascii_strcasecmp("UTF-8", charset)) { |
|
11726
6f319ff4cea5
[gaim-migrate @ 14017]
Richard Laager <rlaager@pidgin.im>
parents:
11318
diff
changeset
|
282 | if (g_utf8_validate(string, -1, NULL)) |
| 10258 | 283 | utf8 = g_strdup(string); |
| 284 | } else { | |
|
11726
6f319ff4cea5
[gaim-migrate @ 14017]
Richard Laager <rlaager@pidgin.im>
parents:
11318
diff
changeset
|
285 | utf8 = g_convert(string, -1, "UTF-8", charset, NULL, NULL, NULL); |
| 10258 | 286 | } |
| 287 | ||
| 288 | if (utf8) { | |
| 289 | g_strfreev(encodings); | |
| 290 | return utf8; | |
| 291 | } | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
292 | } |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
293 | g_strfreev(encodings); |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
294 | |
| 15884 | 295 | return purple_utf8_salvage(string); |
| 6333 | 296 | } |
| 297 | ||
|
20217
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
298 | /* This function is shamelessly stolen from glib--it is an old version of the |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
299 | * private function append_escaped_text, used by g_markup_escape_text, whose |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
300 | * behavior changed in glib 2.12. */ |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
301 | static void irc_append_escaped_text(GString *str, const char *text, gssize length) |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
302 | { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
303 | const char *p = text; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
304 | const char *end = text + length; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
305 | const char *next = NULL; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
306 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
307 | while(p != end) { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
308 | next = g_utf8_next_char(p); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
309 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
310 | switch(*p) { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
311 | case '&': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
312 | g_string_append(str, "&"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
313 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
314 | case '<': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
315 | g_string_append(str, "<"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
316 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
317 | case '>': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
318 | g_string_append(str, ">"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
319 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
320 | case '\'': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
321 | g_string_append(str, "'"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
322 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
323 | case '"': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
324 | g_string_append(str, """); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
325 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
326 | default: |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
327 | g_string_append_len(str, p, next - p); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
328 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
329 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
330 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
331 | p = next; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
332 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
333 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
334 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
335 | /* This function is shamelessly stolen from glib--it is an old version of the |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
336 | * function g_markup_escape_text, whose behavior changed in glib 2.12. */ |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
337 | char *irc_escape_privmsg(const char *text, gssize length) |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
338 | { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
339 | GString *str; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
340 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
341 | g_return_val_if_fail(text != NULL, NULL); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
342 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
343 | if(length < 0) |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
344 | length = strlen(text); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
345 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
346 | str = g_string_sized_new(length); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
347 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
348 | irc_append_escaped_text(str, text, length); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
349 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
350 | return g_string_free(str, FALSE); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
351 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
352 | |
| 6333 | 353 | /* XXX tag closings are not necessarily correctly nested here! If we |
| 354 | * get a ^O or reach the end of the string and there are open | |
| 355 | * tags, they are closed in a fixed order ... this means, for | |
| 356 | * example, you might see <FONT COLOR="blue">some text <B>with | |
| 357 | * various attributes</FONT></B> (notice that B and FONT overlap | |
| 358 | * and are not cleanly nested). This is imminently fixable but | |
| 359 | * I am not fixing it right now. | |
| 360 | */ | |
| 361 | char *irc_mirc2html(const char *string) | |
| 362 | { | |
| 363 | const char *cur, *end; | |
| 364 | char fg[3] = "\0\0", bg[3] = "\0\0"; | |
| 365 | int fgnum, bgnum; | |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
366 | int font = 0, bold = 0, underline = 0, italic = 0; |
|
28468
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
367 | GString *decoded; |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
368 | |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
369 | if (string == NULL) |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
370 | return NULL; |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
371 | |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
372 | decoded = g_string_sized_new(strlen(string)); |
| 6333 | 373 | |
| 374 | cur = string; | |
| 375 | do { | |
| 6754 | 376 | end = strpbrk(cur, "\002\003\007\017\026\037"); |
| 6333 | 377 | |
| 378 | decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur)); | |
| 379 | cur = end ? end : cur + strlen(cur); | |
| 380 | ||
| 381 | switch (*cur) { | |
| 382 | case '\002': | |
| 383 | cur++; | |
| 384 | if (!bold) { | |
| 385 | decoded = g_string_append(decoded, "<B>"); | |
| 386 | bold = TRUE; | |
| 387 | } else { | |
| 388 | decoded = g_string_append(decoded, "</B>"); | |
| 389 | bold = FALSE; | |
| 390 | } | |
| 391 | break; | |
| 392 | case '\003': | |
| 393 | cur++; | |
| 394 | fg[0] = fg[1] = bg[0] = bg[1] = '\0'; | |
| 395 | if (isdigit(*cur)) | |
| 396 | fg[0] = *cur++; | |
| 397 | if (isdigit(*cur)) | |
| 398 | fg[1] = *cur++; | |
| 399 | if (*cur == ',') { | |
| 400 | cur++; | |
| 401 | if (isdigit(*cur)) | |
| 402 | bg[0] = *cur++; | |
| 403 | if (isdigit(*cur)) | |
| 404 | bg[1] = *cur++; | |
| 405 | } | |
| 406 | if (font) { | |
| 407 | decoded = g_string_append(decoded, "</FONT>"); | |
| 408 | font = FALSE; | |
| 409 | } | |
| 410 | ||
| 411 | if (fg[0]) { | |
| 412 | fgnum = atoi(fg); | |
| 413 | if (fgnum < 0 || fgnum > 15) | |
| 414 | continue; | |
| 415 | font = TRUE; | |
| 416 | g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]); | |
| 417 | if (bg[0]) { | |
| 418 | bgnum = atoi(bg); | |
| 419 | if (bgnum >= 0 && bgnum < 16) | |
| 420 | g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]); | |
| 421 | } | |
| 422 | decoded = g_string_append_c(decoded, '>'); | |
| 423 | } | |
| 424 | break; | |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
425 | case '\011': |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
426 | cur++; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
427 | if (!italic) { |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
428 | decoded = g_string_append(decoded, "<I>"); |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
429 | italic = TRUE; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
430 | } else { |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
431 | decoded = g_string_append(decoded, "</I>"); |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
432 | italic = FALSE; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
433 | } |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
434 | break; |
| 6754 | 435 | case '\037': |
| 436 | cur++; | |
| 437 | if (!underline) { | |
| 438 | decoded = g_string_append(decoded, "<U>"); | |
| 439 | underline = TRUE; | |
| 440 | } else { | |
| 441 | decoded = g_string_append(decoded, "</U>"); | |
| 12282 | 442 | underline = FALSE; |
| 6754 | 443 | } |
| 444 | break; | |
| 6333 | 445 | case '\007': |
| 446 | case '\026': | |
| 447 | cur++; | |
| 448 | break; | |
| 449 | case '\017': | |
| 450 | cur++; | |
| 451 | /* fallthrough */ | |
| 452 | case '\000': | |
| 453 | if (bold) | |
| 6754 | 454 | decoded = g_string_append(decoded, "</B>"); |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
455 | if (italic) |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
456 | decoded = g_string_append(decoded, "</I>"); |
| 6754 | 457 | if (underline) |
| 458 | decoded = g_string_append(decoded, "</U>"); | |
| 6333 | 459 | if (font) |
| 460 | decoded = g_string_append(decoded, "</FONT>"); | |
| 461 | break; | |
| 462 | default: | |
| 15884 | 463 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur); |
| 6333 | 464 | } |
| 465 | } while (*cur); | |
| 466 | ||
| 467 | return g_string_free(decoded, FALSE); | |
| 468 | } | |
| 469 | ||
| 8529 | 470 | char *irc_mirc2txt (const char *string) |
| 471 | { | |
|
28468
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
472 | char *result; |
| 8529 | 473 | int i, j; |
| 474 | ||
|
28468
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
475 | if (string == NULL) |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
476 | return NULL; |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
477 | |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
478 | result = g_strdup (string); |
|
aa8975185a48
Head irc_mirc2* bugs off at the pass with a NULL check
Ethan Blanton <elb@pidgin.im>
parents:
24763
diff
changeset
|
479 | |
| 8529 | 480 | for (i = 0, j = 0; result[i]; i++) { |
| 481 | switch (result[i]) { | |
| 482 | case '\002': | |
| 483 | case '\003': | |
|
17409
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
484 | /* Foreground color */ |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
485 | if (isdigit(result[i + 1])) |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
486 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
487 | if (isdigit(result[i + 1])) |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
488 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
489 | /* Optional comma and background color */ |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
490 | if (result[i + 1] == ',') { |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
491 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
492 | if (isdigit(result[i + 1])) |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
493 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
494 | if (isdigit(result[i + 1])) |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
495 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
496 | } |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
497 | /* Note that i still points to the last character |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
498 | * of the color selection string. */ |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
499 | continue; |
| 8529 | 500 | case '\007': |
| 501 | case '\017': | |
| 502 | case '\026': | |
| 503 | case '\037': | |
| 504 | continue; | |
| 505 | default: | |
| 506 | result[j++] = result[i]; | |
| 507 | } | |
| 508 | } | |
| 11136 | 509 | result[j] = '\0'; |
|
24763
67d9d4c975c0
Replace spaces wth tabs in indentation and tabs with spaces in line continuations.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
24574
diff
changeset
|
510 | return result; |
| 8529 | 511 | } |
| 512 | ||
|
24574
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
513 | const char *irc_nick_skip_mode(struct irc_conn *irc, const char *nick) |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
514 | { |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
515 | static const char *default_modes = "@+%&"; |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
516 | const char *mode_chars; |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
517 | |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
518 | mode_chars = irc->mode_chars ? irc->mode_chars : default_modes; |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
519 | |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
520 | while (strchr(mode_chars, *nick) != NULL) |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
521 | nick++; |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
522 | |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
523 | return nick; |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
524 | } |
|
4ee71b4f335f
Strip multiple leading mode characters from incoming nicknames.
Marcos García Ochoa <magao@bigfoot.com>
parents:
23906
diff
changeset
|
525 | |
| 10208 | 526 | gboolean irc_ischannel(const char *string) |
| 527 | { | |
| 528 | return (string[0] == '#' || string[0] == '&'); | |
| 529 | } | |
| 530 | ||
| 6333 | 531 | char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice) |
| 532 | { | |
| 15884 | 533 | PurpleConnection *gc; |
| 6333 | 534 | const char *cur = msg + 1; |
| 535 | char *buf, *ctcp; | |
| 536 | time_t timestamp; | |
| 537 | ||
| 6754 | 538 | /* Note that this is NOT correct w.r.t. multiple CTCPs in one |
| 539 | * message and low-level quoting ... but if you want that crap, | |
| 540 | * use a real IRC client. */ | |
| 541 | ||
| 6333 | 542 | if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001') |
| 543 | return g_strdup(msg); | |
| 544 | ||
| 545 | if (!strncmp(cur, "ACTION ", 7)) { | |
| 546 | cur += 7; | |
| 547 | buf = g_strdup_printf("/me %s", cur); | |
| 548 | buf[strlen(buf) - 1] = '\0'; | |
| 549 | return buf; | |
| 550 | } else if (!strncmp(cur, "PING ", 5)) { | |
| 551 | if (notice) { /* reply */ | |
|
11972
23ac6d9de1fa
[gaim-migrate @ 14265]
Mark Doliner <markdoliner@pidgin.im>
parents:
11726
diff
changeset
|
552 | /* TODO: Should this read in the timestamp as a double? */ |
| 6333 | 553 | sscanf(cur, "PING %lu", ×tamp); |
| 15884 | 554 | gc = purple_account_get_connection(irc->account); |
| 6333 | 555 | if (!gc) |
| 556 | return NULL; | |
| 6350 | 557 | buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp); |
| 15884 | 558 | purple_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf); |
| 6333 | 559 | g_free(buf); |
| 560 | return NULL; | |
| 561 | } else { | |
| 562 | buf = irc_format(irc, "vt:", "NOTICE", from, msg); | |
| 563 | irc_send(irc, buf); | |
| 564 | g_free(buf); | |
| 565 | } | |
| 566 | } else if (!strncmp(cur, "VERSION", 7) && !notice) { | |
| 15884 | 567 | buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Purple IRC\001"); |
| 6333 | 568 | irc_send(irc, buf); |
| 569 | g_free(buf); | |
| 8351 | 570 | } else if (!strncmp(cur, "DCC SEND ", 9)) { |
| 571 | irc_dccsend_recv(irc, from, msg + 10); | |
| 572 | return NULL; | |
| 6333 | 573 | } |
| 574 | ||
| 575 | ctcp = g_strdup(msg + 1); | |
| 576 | ctcp[strlen(ctcp) - 1] = '\0'; | |
| 577 | buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from); | |
| 578 | g_free(ctcp); | |
| 579 | return buf; | |
| 580 | } | |
| 581 | ||
| 582 | void irc_msg_table_build(struct irc_conn *irc) | |
| 583 | { | |
| 584 | int i; | |
| 585 | ||
| 586 | if (!irc || !irc->msgs) { | |
| 15884 | 587 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n"); |
| 6333 | 588 | return; |
| 589 | } | |
| 590 | ||
| 591 | for (i = 0; _irc_msgs[i].name; i++) { | |
| 592 | g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]); | |
| 593 | } | |
| 594 | } | |
| 595 | ||
| 596 | void irc_cmd_table_build(struct irc_conn *irc) | |
| 597 | { | |
| 598 | int i; | |
| 599 | ||
| 600 | if (!irc || !irc->cmds) { | |
| 15884 | 601 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n"); |
| 6333 | 602 | return; |
| 603 | } | |
| 604 | ||
| 605 | for (i = 0; _irc_cmds[i].name ; i++) { | |
| 606 | g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]); | |
| 607 | } | |
| 608 | } | |
| 609 | ||
| 610 | char *irc_format(struct irc_conn *irc, const char *format, ...) | |
| 611 | { | |
| 612 | GString *string = g_string_new(""); | |
| 613 | char *tok, *tmp; | |
| 614 | const char *cur; | |
| 615 | va_list ap; | |
| 616 | ||
| 617 | va_start(ap, format); | |
| 618 | for (cur = format; *cur; cur++) { | |
| 619 | if (cur != format) | |
| 620 | g_string_append_c(string, ' '); | |
| 621 | ||
| 622 | tok = va_arg(ap, char *); | |
| 623 | switch (*cur) { | |
| 624 | case 'v': | |
| 625 | g_string_append(string, tok); | |
| 626 | break; | |
| 627 | case ':': | |
| 628 | g_string_append_c(string, ':'); | |
| 629 | /* no break! */ | |
| 630 | case 't': | |
| 631 | case 'n': | |
| 632 | case 'c': | |
| 633 | tmp = irc_send_convert(irc, tok); | |
|
23392
9268a4fc4786
Remove an unnecessary strdup when sending messages over IRC.
Sadrul Habib Chowdhury <sadrul@pidgin.im>
parents:
22798
diff
changeset
|
634 | g_string_append(string, tmp ? tmp : tok); |
| 6333 | 635 | g_free(tmp); |
| 636 | break; | |
| 637 | default: | |
| 15884 | 638 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur); |
| 6333 | 639 | break; |
| 640 | } | |
| 641 | } | |
| 642 | va_end(ap); | |
| 643 | g_string_append(string, "\r\n"); | |
| 644 | return (g_string_free(string, FALSE)); | |
| 645 | } | |
| 646 | ||
| 647 | void irc_parse_msg(struct irc_conn *irc, char *input) | |
| 648 | { | |
| 649 | struct _irc_msg *msgent; | |
| 650 | char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg; | |
| 7631 | 651 | guint i; |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
652 | PurpleConnection *gc = purple_account_get_connection(irc->account); |
| 6333 | 653 | |
|
14544
847944da3ca0
[gaim-migrate @ 17201]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
654 | irc->recv_time = time(NULL); |
|
14984
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
655 | |
|
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
656 | /* |
|
14996
291304176e0b
[gaim-migrate @ 17706]
Mark Doliner <markdoliner@pidgin.im>
parents:
14995
diff
changeset
|
657 | * The data passed to irc-receiving-text is the raw protocol data. |
|
14995
fc9f51b8f6d5
[gaim-migrate @ 17705]
Mark Doliner <markdoliner@pidgin.im>
parents:
14984
diff
changeset
|
658 | * TODO: It should be passed as an array of bytes and a length |
|
fc9f51b8f6d5
[gaim-migrate @ 17705]
Mark Doliner <markdoliner@pidgin.im>
parents:
14984
diff
changeset
|
659 | * instead of a null terminated string. |
|
14984
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
660 | */ |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
661 | purple_signal_emit(_irc_plugin, "irc-receiving-text", gc, &input); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
31229
diff
changeset
|
662 | |
| 6333 | 663 | if (!strncmp(input, "PING ", 5)) { |
| 664 | msg = irc_format(irc, "vv", "PONG", input + 5); | |
| 665 | irc_send(irc, msg); | |
| 666 | g_free(msg); | |
| 667 | return; | |
| 668 | } else if (!strncmp(input, "ERROR ", 6)) { | |
| 10154 | 669 | if (g_utf8_validate(input, -1, NULL)) { |
| 670 | char *tmp = g_strdup_printf("%s\n%s", _("Disconnected."), input); | |
| 21279 | 671 | purple_connection_error_reason (gc, |
| 672 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp); | |
| 10154 | 673 | g_free(tmp); |
| 674 | } else | |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
675 | purple_connection_error_reason (gc, |
| 21279 | 676 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
| 677 | _("Disconnected.")); | |
| 6333 | 678 | return; |
| 679 | } | |
| 680 | ||
| 681 | if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) { | |
| 682 | irc_parse_error_cb(irc, input); | |
| 683 | return; | |
| 684 | } | |
| 685 | ||
| 686 | from = g_strndup(&input[1], cur - &input[1]); | |
| 687 | cur++; | |
| 688 | end = strchr(cur, ' '); | |
| 689 | if (!end) | |
| 690 | end = cur + strlen(cur); | |
| 691 | ||
| 692 | tmp = g_strndup(cur, end - cur); | |
| 693 | msgname = g_ascii_strdown(tmp, -1); | |
| 694 | g_free(tmp); | |
| 695 | ||
| 696 | if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) { | |
| 697 | irc_msg_default(irc, "", from, &input); | |
| 698 | g_free(msgname); | |
| 699 | g_free(from); | |
| 700 | return; | |
| 701 | } | |
| 702 | g_free(msgname); | |
| 703 | ||
| 704 | args = g_new0(char *, strlen(msgent->format)); | |
| 705 | for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) { | |
| 706 | switch (fmt[i]) { | |
| 707 | case 'v': | |
| 708 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 709 | args[i] = g_strndup(cur, end - cur); | |
| 710 | cur += end - cur; | |
| 711 | break; | |
| 712 | case 't': | |
| 713 | case 'n': | |
| 714 | case 'c': | |
| 715 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 716 | tmp = g_strndup(cur, end - cur); | |
| 717 | args[i] = irc_recv_convert(irc, tmp); | |
| 718 | g_free(tmp); | |
| 719 | cur += end - cur; | |
| 720 | break; | |
| 721 | case ':': | |
| 722 | if (*cur == ':') cur++; | |
| 723 | args[i] = irc_recv_convert(irc, cur); | |
| 724 | cur = cur + strlen(cur); | |
| 725 | break; | |
| 726 | case '*': | |
| 727 | args[i] = g_strdup(cur); | |
| 728 | cur = cur + strlen(cur); | |
| 729 | break; | |
| 730 | default: | |
| 15884 | 731 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]); |
| 6333 | 732 | break; |
| 733 | } | |
| 734 | } | |
| 6970 | 735 | tmp = irc_recv_convert(irc, from); |
| 736 | (msgent->cb)(irc, msgent->name, tmp, args); | |
| 737 | g_free(tmp); | |
| 6333 | 738 | for (i = 0; i < strlen(msgent->format); i++) { |
| 739 | g_free(args[i]); | |
| 740 | } | |
| 741 | g_free(args); | |
| 742 | g_free(from); | |
| 743 | } | |
| 744 | ||
| 745 | static void irc_parse_error_cb(struct irc_conn *irc, char *input) | |
| 746 | { | |
|
23433
0094a64519cb
Make the IRC "unknown message" debugging messages UTF-8 safe.
Ethan Blanton <elb@pidgin.im>
parents:
23392
diff
changeset
|
747 | char *clean; |
|
24763
67d9d4c975c0
Replace spaces wth tabs in indentation and tabs with spaces in line continuations.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
24574
diff
changeset
|
748 | /* This really should be escaped somehow that you can tell what |
|
67d9d4c975c0
Replace spaces wth tabs in indentation and tabs with spaces in line continuations.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
24574
diff
changeset
|
749 | * the junk was -- but as it is, it can crash glib. */ |
|
67d9d4c975c0
Replace spaces wth tabs in indentation and tabs with spaces in line continuations.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
24574
diff
changeset
|
750 | clean = purple_utf8_salvage(input); |
|
23433
0094a64519cb
Make the IRC "unknown message" debugging messages UTF-8 safe.
Ethan Blanton <elb@pidgin.im>
parents:
23392
diff
changeset
|
751 | purple_debug(PURPLE_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", clean); |
|
24763
67d9d4c975c0
Replace spaces wth tabs in indentation and tabs with spaces in line continuations.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
24574
diff
changeset
|
752 | g_free(clean); |
| 6333 | 753 | } |