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