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