Sat, 12 Apr 2008 22:59:33 +0000
Handle IRC ban lists in-channel. Channels which are not joined are
still sent to the debug window, but in a bit prettier format.
References #209
| 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 */ |
|
16474
bc8580a69dba
/whowas for IRC. Thanks, achris.
Ethan Blanton <elb@pidgin.im>
parents:
15884
diff
changeset
|
68 | { "369", "nt:", irc_msg_endwhois }, /* End of WHOWAS */ |
| 8114 | 69 | { "321", "*", irc_msg_list }, /* Start of list */ |
| 70 | { "322", "ncv:", irc_msg_list }, /* List. */ | |
| 71 | { "323", ":", irc_msg_list }, /* End of list. */ | |
| 6333 | 72 | { "324", "ncv:", irc_msg_chanmode }, /* Channel modes */ |
| 73 | { "331", "nc:", irc_msg_topic }, /* No channel topic */ | |
| 74 | { "332", "nc:", irc_msg_topic }, /* Channel topic */ | |
| 75 | { "333", "*", irc_msg_ignore }, /* Topic setter stuff */ | |
| 76 | { "353", "nvc:", irc_msg_names }, /* Names list */ | |
| 77 | { "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
|
78 | { "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
|
79 | { "368", "nc:", irc_msg_ban }, /* End of ban list */ |
| 6333 | 80 | { "372", "n:", irc_msg_motd }, /* MOTD */ |
| 81 | { "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
|
82 | { "376", "n:", irc_msg_motd }, /* End of MOTD */ |
| 10564 | 83 | { "391", "nv:", irc_msg_time }, /* Time reply */ |
| 6333 | 84 | { "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
|
85 | { "406", "nt:", irc_msg_nonick }, /* No such nick for WHOWAS */ |
| 7877 | 86 | { "403", "nc:", irc_msg_nochan }, /* No such channel */ |
| 6333 | 87 | { "404", "nt:", irc_msg_nosend }, /* Cannot send to chan */ |
| 88 | { "421", "nv:", irc_msg_unknown }, /* Unknown command */ | |
| 10633 | 89 | { "432", "vn:", irc_msg_badnick }, /* Erroneous nickname */ |
| 6333 | 90 | { "433", "vn:", irc_msg_nickused }, /* Nickname already in use */ |
|
10712
2aded9e9abd4
[gaim-migrate @ 12306]
Richard Laager <rlaager@pidgin.im>
parents:
10659
diff
changeset
|
91 | { "437", "nc:", irc_msg_unavailable }, /* Nick/channel is unavailable */ |
| 6718 | 92 | { "438", "nn:", irc_msg_nochangenick }, /* Nick may not change */ |
| 6333 | 93 | { "442", "nc:", irc_msg_notinchan }, /* Not in channel */ |
| 94 | { "473", "nc:", irc_msg_inviteonly }, /* Tried to join invite-only */ | |
| 95 | { "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
|
96 | { "477", "nc:", irc_msg_regonly }, /* Registration Required */ |
|
10659
caf16376fba0
[gaim-migrate @ 12192]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10633
diff
changeset
|
97 | { "478", "nct:", irc_msg_banfull }, /* Banlist is full */ |
| 6333 | 98 | { "482", "nc:", irc_msg_notop }, /* Need to be op to do that */ |
| 99 | { "501", "n:", irc_msg_badmode }, /* Unknown mode flag */ | |
| 8404 | 100 | { "506", "nc:", irc_msg_nosend }, /* Must identify to send */ |
| 6714 | 101 | { "515", "nc:", irc_msg_regonly }, /* Registration required */ |
| 6333 | 102 | { "invite", "n:", irc_msg_invite }, /* Invited */ |
| 103 | { "join", ":", irc_msg_join }, /* Joined a channel */ | |
| 104 | { "kick", "cn:", irc_msg_kick }, /* KICK */ | |
| 105 | { "mode", "tv:", irc_msg_mode }, /* MODE for channel */ | |
| 106 | { "nick", ":", irc_msg_nick }, /* Nick change */ | |
| 107 | { "notice", "t:", irc_msg_notice }, /* NOTICE recv */ | |
| 108 | { "part", "c:", irc_msg_part }, /* Parted a channel */ | |
| 109 | { "ping", ":", irc_msg_ping }, /* Received PING from server */ | |
| 110 | { "pong", "v:", irc_msg_pong }, /* Received PONG from server */ | |
| 111 | { "privmsg", "t:", irc_msg_privmsg }, /* Received private message */ | |
| 112 | { "topic", "c:", irc_msg_topic }, /* TOPIC command */ | |
| 113 | { "quit", ":", irc_msg_quit }, /* QUIT notice */ | |
| 114 | { "wallops", ":", irc_msg_wallops }, /* WALLOPS command */ | |
| 115 | { NULL, NULL, NULL } | |
| 116 | }; | |
| 117 | ||
| 118 | static struct _irc_user_cmd { | |
| 119 | char *name; | |
| 120 | char *format; | |
| 121 | IRCCmdCallback cb; | |
| 9255 | 122 | char *help; |
| 6333 | 123 | } _irc_cmds[] = { |
| 9255 | 124 | { "action", ":", irc_cmd_ctcp_action, N_("action <action to perform>: Perform an action.") }, |
| 125 | { "away", ":", irc_cmd_away, N_("away [message]: Set an away message, or use no message to return from being away.") }, | |
| 12013 | 126 | { "chanserv", ":", irc_cmd_service, N_("chanserv: Send a command to chanserv") }, |
| 9258 | 127 | { "deop", ":", irc_cmd_op, N_("deop <nick1> [nick2] ...: Remove channel operator status from someone. You must be a channel operator to do this.") }, |
| 128 | { "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.") }, | |
| 129 | { "invite", ":", irc_cmd_invite, N_("invite <nick> [room]: Invite someone to join you in the specified channel, or the current channel.") }, | |
| 9266 | 130 | { "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.") }, |
| 131 | { "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 | 132 | { "kick", "n:", irc_cmd_kick, N_("kick <nick> [message]: Remove someone from a channel. You must be a channel operator to do this.") }, |
| 133 | { "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 | 134 | { "me", ":", irc_cmd_ctcp_action, N_("me <action to perform>: Perform an action.") }, |
| 12013 | 135 | { "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
|
136 | { "mode", ":", irc_cmd_mode, N_("mode <+|-><A-Za-z> <nick|channel>: Set or unset a channel or user mode.") }, |
| 9258 | 137 | { "msg", "t:", irc_cmd_privmsg, N_("msg <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 138 | { "names", "c", irc_cmd_names, N_("names [channel]: List the users currently in a channel.") }, | |
| 9274 | 139 | { "nick", "n", irc_cmd_nick, N_("nick <new nickname>: Change your nickname.") }, |
| 12013 | 140 | { "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
|
141 | { "notice", "t:", irc_cmd_privmsg, N_("notice <target<: Send a notice to a user or channel.") }, |
| 9258 | 142 | { "op", ":", irc_cmd_op, N_("op <nick1> [nick2] ...: Grant channel operator status to someone. You must be a channel operator to do this.") }, |
| 9255 | 143 | { "operwall", ":", irc_cmd_wallops, N_("operwall <message>: If you don't know what this is, you probably can't use it.") }, |
| 12013 | 144 | { "operserv", ":", irc_cmd_service, N_("operserv: Send a command to operserv") }, |
| 9258 | 145 | { "part", "c:", irc_cmd_part, N_("part [room] [message]: Leave the current channel, or a specified channel, with an optional message.") }, |
| 9255 | 146 | { "ping", "n", irc_cmd_ping, N_("ping [nick]: Asks how much lag a user (or the server if no user specified) has.") }, |
| 9258 | 147 | { "query", "n:", irc_cmd_query, N_("query <nick> <message>: Send a private message to a user (as opposed to a channel).") }, |
| 9255 | 148 | { "quit", ":", irc_cmd_quit, N_("quit [message]: Disconnect from the server, with an optional message.") }, |
| 149 | { "quote", "*", irc_cmd_quote, N_("quote [...]: Send a raw command to the server.") }, | |
| 150 | { "remove", "n:", irc_cmd_remove, N_("remove <nick> [message]: Remove someone from a room. You must be a channel operator to do this.") }, | |
| 10564 | 151 | { "time", "", irc_cmd_time, N_("time: Displays the current local time at the IRC server.") }, |
| 9255 | 152 | { "topic", ":", irc_cmd_topic, N_("topic [new topic]: View or change the channel topic.") }, |
| 153 | { "umode", ":", irc_cmd_mode, N_("umode <+|-><A-Za-z>: Set or unset a user mode.") }, | |
| 13943 | 154 | { "version", ":", irc_cmd_ctcp_version, N_("version [nick]: send CTCP VERSION request to a user") }, |
| 9258 | 155 | { "voice", ":", irc_cmd_op, N_("voice <nick1> [nick2] ...: Grant channel voice status to someone. You must be a channel operator to do this.") }, |
| 9255 | 156 | { "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
|
157 | { "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
|
158 | { "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
|
159 | { NULL, NULL, NULL, NULL } |
| 6333 | 160 | }; |
| 161 | ||
| 15884 | 162 | static PurpleCmdRet irc_parse_purple_cmd(PurpleConversation *conv, const gchar *cmd, |
| 9597 | 163 | gchar **args, gchar **error, void *data) |
| 9130 | 164 | { |
| 15884 | 165 | PurpleConnection *gc; |
| 9130 | 166 | struct irc_conn *irc; |
| 167 | struct _irc_user_cmd *cmdent; | |
| 168 | ||
| 15884 | 169 | gc = purple_conversation_get_gc(conv); |
| 9130 | 170 | if (!gc) |
| 15884 | 171 | return PURPLE_CMD_RET_FAILED; |
| 9130 | 172 | |
| 173 | irc = gc->proto_data; | |
| 174 | ||
| 175 | if ((cmdent = g_hash_table_lookup(irc->cmds, cmd)) == NULL) | |
| 15884 | 176 | return PURPLE_CMD_RET_FAILED; |
| 9130 | 177 | |
| 15884 | 178 | (cmdent->cb)(irc, cmd, purple_conversation_get_name(conv), (const char **)args); |
| 9130 | 179 | |
| 15884 | 180 | return PURPLE_CMD_RET_OK; |
| 9130 | 181 | } |
| 182 | ||
| 183 | static void irc_register_command(struct _irc_user_cmd *c) | |
| 184 | { | |
| 15884 | 185 | PurpleCmdFlag f; |
| 9130 | 186 | char args[10]; |
| 187 | char *format; | |
|
12316
b4e5d5ea15fd
[gaim-migrate @ 14620]
Richard Laager <rlaager@pidgin.im>
parents:
12282
diff
changeset
|
188 | size_t i; |
| 9130 | 189 | |
| 15884 | 190 | f = PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_PRPL_ONLY |
| 191 | | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS; | |
| 9130 | 192 | |
| 193 | format = c->format; | |
| 194 | ||
| 195 | for (i = 0; (i < (sizeof(args) - 1)) && *format; i++, format++) | |
| 196 | switch (*format) { | |
| 197 | case 'v': | |
| 198 | case 'n': | |
| 199 | case 'c': | |
| 200 | case 't': | |
| 201 | args[i] = 'w'; | |
| 202 | break; | |
| 203 | case ':': | |
| 204 | case '*': | |
| 205 | args[i] = 's'; | |
| 206 | break; | |
| 207 | } | |
| 208 | ||
| 209 | args[i] = '\0'; | |
| 210 | ||
| 15884 | 211 | purple_cmd_register(c->name, args, PURPLE_CMD_P_PRPL, f, "prpl-irc", |
| 212 | irc_parse_purple_cmd, _(c->help), NULL); | |
| 9130 | 213 | } |
| 214 | ||
| 215 | void irc_register_commands(void) | |
| 216 | { | |
| 217 | struct _irc_user_cmd *c; | |
| 218 | ||
| 219 | for (c = _irc_cmds; c && c->name; c++) | |
| 220 | irc_register_command(c); | |
| 221 | } | |
| 222 | ||
| 6333 | 223 | static char *irc_send_convert(struct irc_conn *irc, const char *string) |
| 224 | { | |
| 225 | char *utf8; | |
| 226 | GError *err = NULL; | |
| 10258 | 227 | gchar **encodings; |
| 228 | const gchar *enclist; | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
229 | |
| 15884 | 230 | enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 10258 | 231 | encodings = g_strsplit(enclist, ",", 2); |
| 232 | ||
|
17138
d666ace4a8ac
strcasecmp is post-C89, so use the glib equivalent
Ethan Blanton <elb@pidgin.im>
parents:
16993
diff
changeset
|
233 | if (encodings[0] == NULL || !g_ascii_strcasecmp("UTF-8", encodings[0])) { |
| 10278 | 234 | g_strfreev(encodings); |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
235 | return g_strdup(string); |
| 10278 | 236 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
237 | |
| 10258 | 238 | utf8 = g_convert(string, strlen(string), encodings[0], "UTF-8", NULL, NULL, &err); |
| 6333 | 239 | if (err) { |
| 15884 | 240 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Send conversion error: %s\n", err->message); |
| 241 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Sending as UTF-8 instead of %s\n", encodings[0]); | |
| 6333 | 242 | utf8 = g_strdup(string); |
| 8954 | 243 | g_error_free(err); |
| 6333 | 244 | } |
| 10258 | 245 | g_strfreev(encodings); |
| 246 | ||
| 6333 | 247 | return utf8; |
| 248 | } | |
| 249 | ||
| 250 | static char *irc_recv_convert(struct irc_conn *irc, const char *string) | |
| 251 | { | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
252 | char *utf8 = NULL; |
| 10258 | 253 | const gchar *charset, *enclist; |
| 254 | gchar **encodings; | |
| 255 | int i; | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
256 | |
| 15884 | 257 | enclist = purple_account_get_string(irc->account, "encoding", IRC_DEFAULT_CHARSET); |
| 10258 | 258 | encodings = g_strsplit(enclist, ",", -1); |
| 259 | ||
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
260 | if (encodings[0] == NULL) { |
|
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
261 | g_strfreev(encodings); |
| 15884 | 262 | return purple_utf8_salvage(string); |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
263 | } |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
264 | |
| 10258 | 265 | for (i = 0; encodings[i] != NULL; i++) { |
| 266 | charset = encodings[i]; | |
| 267 | while (*charset == ' ') | |
| 268 | charset++; | |
| 269 | ||
|
17156
6d4cc0f310d1
More strcasecmp() replacements.
Richard Laager <rlaager@pidgin.im>
parents:
17138
diff
changeset
|
270 | if (!g_ascii_strcasecmp("UTF-8", charset)) { |
|
11726
6f319ff4cea5
[gaim-migrate @ 14017]
Richard Laager <rlaager@pidgin.im>
parents:
11318
diff
changeset
|
271 | if (g_utf8_validate(string, -1, NULL)) |
| 10258 | 272 | utf8 = g_strdup(string); |
| 273 | } else { | |
|
11726
6f319ff4cea5
[gaim-migrate @ 14017]
Richard Laager <rlaager@pidgin.im>
parents:
11318
diff
changeset
|
274 | utf8 = g_convert(string, -1, "UTF-8", charset, NULL, NULL, NULL); |
| 10258 | 275 | } |
| 276 | ||
| 277 | if (utf8) { | |
| 278 | g_strfreev(encodings); | |
| 279 | return utf8; | |
| 280 | } | |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
281 | } |
|
10504
eae130eefbfe
[gaim-migrate @ 11796]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10278
diff
changeset
|
282 | g_strfreev(encodings); |
|
9644
a9a0dedb52c7
[gaim-migrate @ 10492]
Mark Doliner <markdoliner@pidgin.im>
parents:
9597
diff
changeset
|
283 | |
| 15884 | 284 | return purple_utf8_salvage(string); |
| 6333 | 285 | } |
| 286 | ||
|
20217
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
287 | /* 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
|
288 | * 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
|
289 | * behavior changed in glib 2.12. */ |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
290 | 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
|
291 | { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
292 | const char *p = text; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
293 | const char *end = text + length; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
294 | const char *next = NULL; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
295 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
296 | while(p != end) { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
297 | next = g_utf8_next_char(p); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
298 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
299 | switch(*p) { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
300 | case '&': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
301 | g_string_append(str, "&"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
302 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
303 | case '<': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
304 | g_string_append(str, "<"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
305 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
306 | case '>': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
307 | g_string_append(str, ">"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
308 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
309 | case '\'': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
310 | g_string_append(str, "'"); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
311 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
312 | case '"': |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
313 | g_string_append(str, """); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
314 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
315 | default: |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
316 | g_string_append_len(str, p, next - p); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
317 | break; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
318 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
319 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
320 | p = next; |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
321 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
322 | } |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
323 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
324 | /* 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
|
325 | * 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
|
326 | char *irc_escape_privmsg(const char *text, gssize length) |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
327 | { |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
328 | GString *str; |
|
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 | g_return_val_if_fail(text != NULL, NULL); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
331 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
332 | if(length < 0) |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
333 | length = strlen(text); |
|
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 | str = g_string_sized_new(length); |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
336 | |
|
9249f67ef61e
applied changes from 6c08c628ee64e16c824829c25befc0ca09338f9d
Luke Schierer <lschiere@pidgin.im>
parents:
19859
diff
changeset
|
337 | irc_append_escaped_text(str, text, 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 | return g_string_free(str, FALSE); |
|
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 | |
| 6333 | 342 | /* XXX tag closings are not necessarily correctly nested here! If we |
| 343 | * get a ^O or reach the end of the string and there are open | |
| 344 | * tags, they are closed in a fixed order ... this means, for | |
| 345 | * example, you might see <FONT COLOR="blue">some text <B>with | |
| 346 | * various attributes</FONT></B> (notice that B and FONT overlap | |
| 347 | * and are not cleanly nested). This is imminently fixable but | |
| 348 | * I am not fixing it right now. | |
| 349 | */ | |
| 350 | char *irc_mirc2html(const char *string) | |
| 351 | { | |
| 352 | const char *cur, *end; | |
| 353 | char fg[3] = "\0\0", bg[3] = "\0\0"; | |
| 354 | int fgnum, bgnum; | |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
355 | int font = 0, bold = 0, underline = 0, italic = 0; |
| 6333 | 356 | GString *decoded = g_string_sized_new(strlen(string)); |
| 357 | ||
| 358 | cur = string; | |
| 359 | do { | |
| 6754 | 360 | end = strpbrk(cur, "\002\003\007\017\026\037"); |
| 6333 | 361 | |
| 362 | decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur)); | |
| 363 | cur = end ? end : cur + strlen(cur); | |
| 364 | ||
| 365 | switch (*cur) { | |
| 366 | case '\002': | |
| 367 | cur++; | |
| 368 | if (!bold) { | |
| 369 | decoded = g_string_append(decoded, "<B>"); | |
| 370 | bold = TRUE; | |
| 371 | } else { | |
| 372 | decoded = g_string_append(decoded, "</B>"); | |
| 373 | bold = FALSE; | |
| 374 | } | |
| 375 | break; | |
| 376 | case '\003': | |
| 377 | cur++; | |
| 378 | fg[0] = fg[1] = bg[0] = bg[1] = '\0'; | |
| 379 | if (isdigit(*cur)) | |
| 380 | fg[0] = *cur++; | |
| 381 | if (isdigit(*cur)) | |
| 382 | fg[1] = *cur++; | |
| 383 | if (*cur == ',') { | |
| 384 | cur++; | |
| 385 | if (isdigit(*cur)) | |
| 386 | bg[0] = *cur++; | |
| 387 | if (isdigit(*cur)) | |
| 388 | bg[1] = *cur++; | |
| 389 | } | |
| 390 | if (font) { | |
| 391 | decoded = g_string_append(decoded, "</FONT>"); | |
| 392 | font = FALSE; | |
| 393 | } | |
| 394 | ||
| 395 | if (fg[0]) { | |
| 396 | fgnum = atoi(fg); | |
| 397 | if (fgnum < 0 || fgnum > 15) | |
| 398 | continue; | |
| 399 | font = TRUE; | |
| 400 | g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]); | |
| 401 | if (bg[0]) { | |
| 402 | bgnum = atoi(bg); | |
| 403 | if (bgnum >= 0 && bgnum < 16) | |
| 404 | g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]); | |
| 405 | } | |
| 406 | decoded = g_string_append_c(decoded, '>'); | |
| 407 | } | |
| 408 | break; | |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
409 | case '\011': |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
410 | cur++; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
411 | if (!italic) { |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
412 | decoded = g_string_append(decoded, "<I>"); |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
413 | italic = TRUE; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
414 | } else { |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
415 | decoded = g_string_append(decoded, "</I>"); |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
416 | italic = FALSE; |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
417 | } |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
418 | break; |
| 6754 | 419 | case '\037': |
| 420 | cur++; | |
| 421 | if (!underline) { | |
| 422 | decoded = g_string_append(decoded, "<U>"); | |
| 423 | underline = TRUE; | |
| 424 | } else { | |
| 425 | decoded = g_string_append(decoded, "</U>"); | |
| 12282 | 426 | underline = FALSE; |
| 6754 | 427 | } |
| 428 | break; | |
| 6333 | 429 | case '\007': |
| 430 | case '\026': | |
| 431 | cur++; | |
| 432 | break; | |
| 433 | case '\017': | |
| 434 | cur++; | |
| 435 | /* fallthrough */ | |
| 436 | case '\000': | |
| 437 | if (bold) | |
| 6754 | 438 | decoded = g_string_append(decoded, "</B>"); |
|
12158
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
439 | if (italic) |
|
02fcec741f07
[gaim-migrate @ 14459]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
12013
diff
changeset
|
440 | decoded = g_string_append(decoded, "</I>"); |
| 6754 | 441 | if (underline) |
| 442 | decoded = g_string_append(decoded, "</U>"); | |
| 6333 | 443 | if (font) |
| 444 | decoded = g_string_append(decoded, "</FONT>"); | |
| 445 | break; | |
| 446 | default: | |
| 15884 | 447 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur); |
| 6333 | 448 | } |
| 449 | } while (*cur); | |
| 450 | ||
| 451 | return g_string_free(decoded, FALSE); | |
| 452 | } | |
| 453 | ||
| 8529 | 454 | char *irc_mirc2txt (const char *string) |
| 455 | { | |
| 456 | char *result = g_strdup (string); | |
| 457 | int i, j; | |
| 458 | ||
| 459 | for (i = 0, j = 0; result[i]; i++) { | |
| 460 | switch (result[i]) { | |
| 461 | case '\002': | |
| 462 | case '\003': | |
|
17409
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
463 | /* Foreground color */ |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
464 | 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
|
465 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
466 | 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
|
467 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
468 | /* 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
|
469 | 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
|
470 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
471 | 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
|
472 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
473 | 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
|
474 | i++; |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
475 | } |
|
1205b4f7bb82
This performs mIRC formatting code stripping for the room list, as
Ethan Blanton <elb@pidgin.im>
parents:
17156
diff
changeset
|
476 | /* 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
|
477 | * 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
|
478 | continue; |
| 8529 | 479 | case '\007': |
| 480 | case '\017': | |
| 481 | case '\026': | |
| 482 | case '\037': | |
| 483 | continue; | |
| 484 | default: | |
| 485 | result[j++] = result[i]; | |
| 486 | } | |
| 487 | } | |
| 11136 | 488 | result[j] = '\0'; |
| 8529 | 489 | return result; |
| 490 | } | |
| 491 | ||
| 10208 | 492 | gboolean irc_ischannel(const char *string) |
| 493 | { | |
| 494 | return (string[0] == '#' || string[0] == '&'); | |
| 495 | } | |
| 496 | ||
| 6333 | 497 | char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice) |
| 498 | { | |
| 15884 | 499 | PurpleConnection *gc; |
| 6333 | 500 | const char *cur = msg + 1; |
| 501 | char *buf, *ctcp; | |
| 502 | time_t timestamp; | |
| 503 | ||
| 6754 | 504 | /* Note that this is NOT correct w.r.t. multiple CTCPs in one |
| 505 | * message and low-level quoting ... but if you want that crap, | |
| 506 | * use a real IRC client. */ | |
| 507 | ||
| 6333 | 508 | if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001') |
| 509 | return g_strdup(msg); | |
| 510 | ||
| 511 | if (!strncmp(cur, "ACTION ", 7)) { | |
| 512 | cur += 7; | |
| 513 | buf = g_strdup_printf("/me %s", cur); | |
| 514 | buf[strlen(buf) - 1] = '\0'; | |
| 515 | return buf; | |
| 516 | } else if (!strncmp(cur, "PING ", 5)) { | |
| 517 | if (notice) { /* reply */ | |
|
11972
23ac6d9de1fa
[gaim-migrate @ 14265]
Mark Doliner <markdoliner@pidgin.im>
parents:
11726
diff
changeset
|
518 | /* TODO: Should this read in the timestamp as a double? */ |
| 6333 | 519 | sscanf(cur, "PING %lu", ×tamp); |
| 15884 | 520 | gc = purple_account_get_connection(irc->account); |
| 6333 | 521 | if (!gc) |
| 522 | return NULL; | |
| 6350 | 523 | buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp); |
| 15884 | 524 | purple_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf); |
| 6333 | 525 | g_free(buf); |
| 526 | return NULL; | |
| 527 | } else { | |
| 528 | buf = irc_format(irc, "vt:", "NOTICE", from, msg); | |
| 529 | irc_send(irc, buf); | |
| 530 | g_free(buf); | |
| 531 | } | |
| 532 | } else if (!strncmp(cur, "VERSION", 7) && !notice) { | |
| 15884 | 533 | buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Purple IRC\001"); |
| 6333 | 534 | irc_send(irc, buf); |
| 535 | g_free(buf); | |
| 8351 | 536 | } else if (!strncmp(cur, "DCC SEND ", 9)) { |
| 537 | irc_dccsend_recv(irc, from, msg + 10); | |
| 538 | return NULL; | |
| 6333 | 539 | } |
| 540 | ||
| 541 | ctcp = g_strdup(msg + 1); | |
| 542 | ctcp[strlen(ctcp) - 1] = '\0'; | |
| 543 | buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from); | |
| 544 | g_free(ctcp); | |
| 545 | return buf; | |
| 546 | } | |
| 547 | ||
| 548 | void irc_msg_table_build(struct irc_conn *irc) | |
| 549 | { | |
| 550 | int i; | |
| 551 | ||
| 552 | if (!irc || !irc->msgs) { | |
| 15884 | 553 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n"); |
| 6333 | 554 | return; |
| 555 | } | |
| 556 | ||
| 557 | for (i = 0; _irc_msgs[i].name; i++) { | |
| 558 | g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]); | |
| 559 | } | |
| 560 | } | |
| 561 | ||
| 562 | void irc_cmd_table_build(struct irc_conn *irc) | |
| 563 | { | |
| 564 | int i; | |
| 565 | ||
| 566 | if (!irc || !irc->cmds) { | |
| 15884 | 567 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n"); |
| 6333 | 568 | return; |
| 569 | } | |
| 570 | ||
| 571 | for (i = 0; _irc_cmds[i].name ; i++) { | |
| 572 | g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]); | |
| 573 | } | |
| 574 | } | |
| 575 | ||
| 576 | char *irc_format(struct irc_conn *irc, const char *format, ...) | |
| 577 | { | |
| 578 | GString *string = g_string_new(""); | |
| 579 | char *tok, *tmp; | |
| 580 | const char *cur; | |
| 581 | va_list ap; | |
| 582 | ||
| 583 | va_start(ap, format); | |
| 584 | for (cur = format; *cur; cur++) { | |
| 585 | if (cur != format) | |
| 586 | g_string_append_c(string, ' '); | |
| 587 | ||
| 588 | tok = va_arg(ap, char *); | |
| 589 | switch (*cur) { | |
| 590 | case 'v': | |
| 591 | g_string_append(string, tok); | |
| 592 | break; | |
| 593 | case ':': | |
| 594 | g_string_append_c(string, ':'); | |
| 595 | /* no break! */ | |
| 596 | case 't': | |
| 597 | case 'n': | |
| 598 | case 'c': | |
| 599 | tmp = irc_send_convert(irc, tok); | |
| 600 | g_string_append(string, tmp); | |
| 601 | g_free(tmp); | |
| 602 | break; | |
| 603 | default: | |
| 15884 | 604 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur); |
| 6333 | 605 | break; |
| 606 | } | |
| 607 | } | |
| 608 | va_end(ap); | |
| 609 | g_string_append(string, "\r\n"); | |
| 610 | return (g_string_free(string, FALSE)); | |
| 611 | } | |
| 612 | ||
| 613 | void irc_parse_msg(struct irc_conn *irc, char *input) | |
| 614 | { | |
| 615 | struct _irc_msg *msgent; | |
| 616 | char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg; | |
| 7631 | 617 | guint i; |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
618 | PurpleConnection *gc = purple_account_get_connection(irc->account); |
| 6333 | 619 | |
|
14544
847944da3ca0
[gaim-migrate @ 17201]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
620 | irc->recv_time = time(NULL); |
|
14984
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
621 | |
|
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
622 | /* |
|
14996
291304176e0b
[gaim-migrate @ 17706]
Mark Doliner <markdoliner@pidgin.im>
parents:
14995
diff
changeset
|
623 | * 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
|
624 | * 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
|
625 | * instead of a null terminated string. |
|
14984
aa56a88dc792
[gaim-migrate @ 17694]
Mark Doliner <markdoliner@pidgin.im>
parents:
14683
diff
changeset
|
626 | */ |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
627 | purple_signal_emit(_irc_plugin, "irc-receiving-text", gc, &input); |
| 14683 | 628 | |
| 6333 | 629 | if (!strncmp(input, "PING ", 5)) { |
| 630 | msg = irc_format(irc, "vv", "PONG", input + 5); | |
| 631 | irc_send(irc, msg); | |
| 632 | g_free(msg); | |
| 633 | return; | |
| 634 | } else if (!strncmp(input, "ERROR ", 6)) { | |
| 10154 | 635 | if (g_utf8_validate(input, -1, NULL)) { |
| 636 | char *tmp = g_strdup_printf("%s\n%s", _("Disconnected."), input); | |
| 21279 | 637 | purple_connection_error_reason (gc, |
| 638 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp); | |
| 10154 | 639 | g_free(tmp); |
| 640 | } else | |
|
20440
42e5e5445a2f
Move prpl-irc to purple_connection_error_reason.
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
641 | purple_connection_error_reason (gc, |
| 21279 | 642 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
| 643 | _("Disconnected.")); | |
| 6333 | 644 | return; |
| 645 | } | |
| 646 | ||
| 647 | if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) { | |
| 648 | irc_parse_error_cb(irc, input); | |
| 649 | return; | |
| 650 | } | |
| 651 | ||
| 652 | from = g_strndup(&input[1], cur - &input[1]); | |
| 653 | cur++; | |
| 654 | end = strchr(cur, ' '); | |
| 655 | if (!end) | |
| 656 | end = cur + strlen(cur); | |
| 657 | ||
| 658 | tmp = g_strndup(cur, end - cur); | |
| 659 | msgname = g_ascii_strdown(tmp, -1); | |
| 660 | g_free(tmp); | |
| 661 | ||
| 662 | if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) { | |
| 663 | irc_msg_default(irc, "", from, &input); | |
| 664 | g_free(msgname); | |
| 665 | g_free(from); | |
| 666 | return; | |
| 667 | } | |
| 668 | g_free(msgname); | |
| 669 | ||
| 670 | args = g_new0(char *, strlen(msgent->format)); | |
| 671 | for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) { | |
| 672 | switch (fmt[i]) { | |
| 673 | case 'v': | |
| 674 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 675 | args[i] = g_strndup(cur, end - cur); | |
| 676 | cur += end - cur; | |
| 677 | break; | |
| 678 | case 't': | |
| 679 | case 'n': | |
| 680 | case 'c': | |
| 681 | if (!(end = strchr(cur, ' '))) end = cur + strlen(cur); | |
| 682 | tmp = g_strndup(cur, end - cur); | |
| 683 | args[i] = irc_recv_convert(irc, tmp); | |
| 684 | g_free(tmp); | |
| 685 | cur += end - cur; | |
| 686 | break; | |
| 687 | case ':': | |
| 688 | if (*cur == ':') cur++; | |
| 689 | args[i] = irc_recv_convert(irc, cur); | |
| 690 | cur = cur + strlen(cur); | |
| 691 | break; | |
| 692 | case '*': | |
| 693 | args[i] = g_strdup(cur); | |
| 694 | cur = cur + strlen(cur); | |
| 695 | break; | |
| 696 | default: | |
| 15884 | 697 | purple_debug(PURPLE_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]); |
| 6333 | 698 | break; |
| 699 | } | |
| 700 | } | |
| 6970 | 701 | tmp = irc_recv_convert(irc, from); |
| 702 | (msgent->cb)(irc, msgent->name, tmp, args); | |
| 703 | g_free(tmp); | |
| 6333 | 704 | for (i = 0; i < strlen(msgent->format); i++) { |
| 705 | g_free(args[i]); | |
| 706 | } | |
| 707 | g_free(args); | |
| 708 | g_free(from); | |
| 709 | } | |
| 710 | ||
| 711 | static void irc_parse_error_cb(struct irc_conn *irc, char *input) | |
| 712 | { | |
| 15884 | 713 | purple_debug(PURPLE_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", input); |
| 6333 | 714 | } |