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