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
|
10504
|
238 if (encodings[0] == NULL) {
|
|
239 g_strfreev(encodings);
|
10258
|
240 return gaim_utf8_salvage(string);
|
10504
|
241 }
|
9644
|
242
|
10258
|
243 for (i = 0; encodings[i] != NULL; i++) {
|
|
244 charset = encodings[i];
|
|
245 while (*charset == ' ')
|
|
246 charset++;
|
|
247
|
|
248 if (!strcasecmp("UTF-8", charset)) {
|
|
249 if (g_utf8_validate(string, strlen(string), NULL))
|
|
250 utf8 = g_strdup(string);
|
|
251 } else {
|
|
252 utf8 = g_convert(string, strlen(string), "UTF-8", charset, NULL, NULL, NULL);
|
|
253 }
|
|
254
|
|
255 if (utf8) {
|
|
256 g_strfreev(encodings);
|
|
257 return utf8;
|
|
258 }
|
9644
|
259 }
|
10504
|
260 g_strfreev(encodings);
|
9644
|
261
|
10258
|
262 return gaim_utf8_salvage(string);
|
6333
|
263 }
|
|
264
|
|
265 /* XXX tag closings are not necessarily correctly nested here! If we
|
|
266 * get a ^O or reach the end of the string and there are open
|
|
267 * tags, they are closed in a fixed order ... this means, for
|
|
268 * example, you might see <FONT COLOR="blue">some text <B>with
|
|
269 * various attributes</FONT></B> (notice that B and FONT overlap
|
|
270 * and are not cleanly nested). This is imminently fixable but
|
|
271 * I am not fixing it right now.
|
|
272 */
|
|
273 char *irc_mirc2html(const char *string)
|
|
274 {
|
|
275 const char *cur, *end;
|
|
276 char fg[3] = "\0\0", bg[3] = "\0\0";
|
|
277 int fgnum, bgnum;
|
6754
|
278 int font = 0, bold = 0, underline = 0;
|
6333
|
279 GString *decoded = g_string_sized_new(strlen(string));
|
|
280
|
|
281 cur = string;
|
|
282 do {
|
6754
|
283 end = strpbrk(cur, "\002\003\007\017\026\037");
|
6333
|
284
|
|
285 decoded = g_string_append_len(decoded, cur, end ? end - cur : strlen(cur));
|
|
286 cur = end ? end : cur + strlen(cur);
|
|
287
|
|
288 switch (*cur) {
|
|
289 case '\002':
|
|
290 cur++;
|
|
291 if (!bold) {
|
|
292 decoded = g_string_append(decoded, "<B>");
|
|
293 bold = TRUE;
|
|
294 } else {
|
|
295 decoded = g_string_append(decoded, "</B>");
|
|
296 bold = FALSE;
|
|
297 }
|
|
298 break;
|
|
299 case '\003':
|
|
300 cur++;
|
|
301 fg[0] = fg[1] = bg[0] = bg[1] = '\0';
|
|
302 if (isdigit(*cur))
|
|
303 fg[0] = *cur++;
|
|
304 if (isdigit(*cur))
|
|
305 fg[1] = *cur++;
|
|
306 if (*cur == ',') {
|
|
307 cur++;
|
|
308 if (isdigit(*cur))
|
|
309 bg[0] = *cur++;
|
|
310 if (isdigit(*cur))
|
|
311 bg[1] = *cur++;
|
|
312 }
|
|
313 if (font) {
|
|
314 decoded = g_string_append(decoded, "</FONT>");
|
|
315 font = FALSE;
|
|
316 }
|
|
317
|
|
318 if (fg[0]) {
|
|
319 fgnum = atoi(fg);
|
|
320 if (fgnum < 0 || fgnum > 15)
|
|
321 continue;
|
|
322 font = TRUE;
|
|
323 g_string_append_printf(decoded, "<FONT COLOR=\"%s\"", irc_mirc_colors[fgnum]);
|
|
324 if (bg[0]) {
|
|
325 bgnum = atoi(bg);
|
|
326 if (bgnum >= 0 && bgnum < 16)
|
|
327 g_string_append_printf(decoded, " BACK=\"%s\"", irc_mirc_colors[bgnum]);
|
|
328 }
|
|
329 decoded = g_string_append_c(decoded, '>');
|
|
330 }
|
|
331 break;
|
6754
|
332 case '\037':
|
|
333 cur++;
|
|
334 if (!underline) {
|
|
335 decoded = g_string_append(decoded, "<U>");
|
|
336 underline = TRUE;
|
|
337 } else {
|
|
338 decoded = g_string_append(decoded, "</U>");
|
|
339 underline = TRUE;
|
|
340 }
|
|
341 break;
|
6333
|
342 case '\007':
|
|
343 case '\026':
|
|
344 cur++;
|
|
345 break;
|
|
346 case '\017':
|
|
347 cur++;
|
|
348 /* fallthrough */
|
|
349 case '\000':
|
|
350 if (bold)
|
6754
|
351 decoded = g_string_append(decoded, "</B>");
|
|
352 if (underline)
|
|
353 decoded = g_string_append(decoded, "</U>");
|
6333
|
354 if (font)
|
|
355 decoded = g_string_append(decoded, "</FONT>");
|
|
356 break;
|
|
357 default:
|
|
358 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Unexpected mIRC formatting character %d\n", *cur);
|
|
359 }
|
|
360 } while (*cur);
|
|
361
|
|
362 return g_string_free(decoded, FALSE);
|
|
363 }
|
|
364
|
8529
|
365 char *irc_mirc2txt (const char *string)
|
|
366 {
|
|
367 char *result = g_strdup (string);
|
|
368 int i, j;
|
|
369
|
|
370 for (i = 0, j = 0; result[i]; i++) {
|
|
371 switch (result[i]) {
|
|
372 case '\002':
|
|
373 case '\003':
|
|
374 case '\007':
|
|
375 case '\017':
|
|
376 case '\026':
|
|
377 case '\037':
|
|
378 continue;
|
|
379 default:
|
|
380 result[j++] = result[i];
|
|
381 }
|
|
382 }
|
|
383 result[i] = '\0';
|
|
384 return result;
|
|
385 }
|
|
386
|
10208
|
387 gboolean irc_ischannel(const char *string)
|
|
388 {
|
|
389 return (string[0] == '#' || string[0] == '&');
|
|
390 }
|
|
391
|
6333
|
392 char *irc_parse_ctcp(struct irc_conn *irc, const char *from, const char *to, const char *msg, int notice)
|
|
393 {
|
|
394 GaimConnection *gc;
|
|
395 const char *cur = msg + 1;
|
|
396 char *buf, *ctcp;
|
|
397 time_t timestamp;
|
|
398
|
6754
|
399 /* Note that this is NOT correct w.r.t. multiple CTCPs in one
|
|
400 * message and low-level quoting ... but if you want that crap,
|
|
401 * use a real IRC client. */
|
|
402
|
6333
|
403 if (msg[0] != '\001' || msg[strlen(msg) - 1] != '\001')
|
|
404 return g_strdup(msg);
|
|
405
|
|
406 if (!strncmp(cur, "ACTION ", 7)) {
|
|
407 cur += 7;
|
|
408 buf = g_strdup_printf("/me %s", cur);
|
|
409 buf[strlen(buf) - 1] = '\0';
|
|
410 return buf;
|
|
411 } else if (!strncmp(cur, "PING ", 5)) {
|
|
412 if (notice) { /* reply */
|
|
413 sscanf(cur, "PING %lu", ×tamp);
|
|
414 gc = gaim_account_get_connection(irc->account);
|
|
415 if (!gc)
|
|
416 return NULL;
|
6350
|
417 buf = g_strdup_printf(_("Reply time from %s: %lu seconds"), from, time(NULL) - timestamp);
|
6333
|
418 gaim_notify_info(gc, _("PONG"), _("CTCP PING reply"), buf);
|
|
419 g_free(buf);
|
|
420 return NULL;
|
|
421 } else {
|
|
422 buf = irc_format(irc, "vt:", "NOTICE", from, msg);
|
|
423 irc_send(irc, buf);
|
|
424 g_free(buf);
|
|
425 gc = gaim_account_get_connection(irc->account);
|
|
426 }
|
|
427 } else if (!strncmp(cur, "VERSION", 7) && !notice) {
|
|
428 buf = irc_format(irc, "vt:", "NOTICE", from, "\001VERSION Gaim IRC\001");
|
|
429 irc_send(irc, buf);
|
|
430 g_free(buf);
|
8351
|
431 } else if (!strncmp(cur, "DCC SEND ", 9)) {
|
|
432 irc_dccsend_recv(irc, from, msg + 10);
|
|
433 return NULL;
|
6333
|
434 }
|
|
435
|
|
436 ctcp = g_strdup(msg + 1);
|
|
437 ctcp[strlen(ctcp) - 1] = '\0';
|
|
438 buf = g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp, to, from);
|
|
439 g_free(ctcp);
|
|
440 return buf;
|
|
441 }
|
|
442
|
|
443 void irc_msg_table_build(struct irc_conn *irc)
|
|
444 {
|
|
445 int i;
|
|
446
|
|
447 if (!irc || !irc->msgs) {
|
|
448 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a message table on a bogus structure\n");
|
|
449 return;
|
|
450 }
|
|
451
|
|
452 for (i = 0; _irc_msgs[i].name; i++) {
|
|
453 g_hash_table_insert(irc->msgs, (gpointer)_irc_msgs[i].name, (gpointer)&_irc_msgs[i]);
|
|
454 }
|
|
455 }
|
|
456
|
|
457 void irc_cmd_table_build(struct irc_conn *irc)
|
|
458 {
|
|
459 int i;
|
|
460
|
|
461 if (!irc || !irc->cmds) {
|
|
462 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Attempt to build a command table on a bogus structure\n");
|
|
463 return;
|
|
464 }
|
|
465
|
|
466 for (i = 0; _irc_cmds[i].name ; i++) {
|
|
467 g_hash_table_insert(irc->cmds, (gpointer)_irc_cmds[i].name, (gpointer)&_irc_cmds[i]);
|
|
468 }
|
|
469 }
|
|
470
|
|
471 char *irc_format(struct irc_conn *irc, const char *format, ...)
|
|
472 {
|
|
473 GString *string = g_string_new("");
|
|
474 char *tok, *tmp;
|
|
475 const char *cur;
|
|
476 va_list ap;
|
|
477
|
|
478 va_start(ap, format);
|
|
479 for (cur = format; *cur; cur++) {
|
|
480 if (cur != format)
|
|
481 g_string_append_c(string, ' ');
|
|
482
|
|
483 tok = va_arg(ap, char *);
|
|
484 switch (*cur) {
|
|
485 case 'v':
|
|
486 g_string_append(string, tok);
|
|
487 break;
|
|
488 case ':':
|
|
489 g_string_append_c(string, ':');
|
|
490 /* no break! */
|
|
491 case 't':
|
|
492 case 'n':
|
|
493 case 'c':
|
|
494 tmp = irc_send_convert(irc, tok);
|
|
495 g_string_append(string, tmp);
|
|
496 g_free(tmp);
|
|
497 break;
|
|
498 default:
|
|
499 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Invalid format character '%c'\n", *cur);
|
|
500 break;
|
|
501 }
|
|
502 }
|
|
503 va_end(ap);
|
|
504 g_string_append(string, "\r\n");
|
|
505 return (g_string_free(string, FALSE));
|
|
506 }
|
|
507
|
|
508 void irc_parse_msg(struct irc_conn *irc, char *input)
|
|
509 {
|
|
510 struct _irc_msg *msgent;
|
|
511 char *cur, *end, *tmp, *from, *msgname, *fmt, **args, *msg;
|
7631
|
512 guint i;
|
6333
|
513
|
|
514 if (!strncmp(input, "PING ", 5)) {
|
|
515 msg = irc_format(irc, "vv", "PONG", input + 5);
|
|
516 irc_send(irc, msg);
|
|
517 g_free(msg);
|
|
518 return;
|
|
519 } else if (!strncmp(input, "ERROR ", 6)) {
|
10154
|
520 if (g_utf8_validate(input, -1, NULL)) {
|
|
521 char *tmp = g_strdup_printf("%s\n%s", _("Disconnected."), input);
|
|
522 gaim_connection_error(gaim_account_get_connection(irc->account), tmp);
|
|
523 g_free(tmp);
|
|
524 } else
|
|
525 gaim_connection_error(gaim_account_get_connection(irc->account), _("Disconnected."));
|
6333
|
526 return;
|
|
527 }
|
|
528
|
|
529 if (input[0] != ':' || (cur = strchr(input, ' ')) == NULL) {
|
|
530 irc_parse_error_cb(irc, input);
|
|
531 return;
|
|
532 }
|
|
533
|
|
534 from = g_strndup(&input[1], cur - &input[1]);
|
|
535 cur++;
|
|
536 end = strchr(cur, ' ');
|
|
537 if (!end)
|
|
538 end = cur + strlen(cur);
|
|
539
|
|
540 tmp = g_strndup(cur, end - cur);
|
|
541 msgname = g_ascii_strdown(tmp, -1);
|
|
542 g_free(tmp);
|
|
543
|
|
544 if ((msgent = g_hash_table_lookup(irc->msgs, msgname)) == NULL) {
|
|
545 irc_msg_default(irc, "", from, &input);
|
|
546 g_free(msgname);
|
|
547 g_free(from);
|
|
548 return;
|
|
549 }
|
|
550 g_free(msgname);
|
|
551
|
|
552 args = g_new0(char *, strlen(msgent->format));
|
|
553 for (cur = end, fmt = msgent->format, i = 0; fmt[i] && *cur++; i++) {
|
|
554 switch (fmt[i]) {
|
|
555 case 'v':
|
|
556 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
|
|
557 args[i] = g_strndup(cur, end - cur);
|
|
558 cur += end - cur;
|
|
559 break;
|
|
560 case 't':
|
|
561 case 'n':
|
|
562 case 'c':
|
|
563 if (!(end = strchr(cur, ' '))) end = cur + strlen(cur);
|
|
564 tmp = g_strndup(cur, end - cur);
|
|
565 args[i] = irc_recv_convert(irc, tmp);
|
|
566 g_free(tmp);
|
|
567 cur += end - cur;
|
|
568 break;
|
|
569 case ':':
|
|
570 if (*cur == ':') cur++;
|
|
571 args[i] = irc_recv_convert(irc, cur);
|
|
572 cur = cur + strlen(cur);
|
|
573 break;
|
|
574 case '*':
|
|
575 args[i] = g_strdup(cur);
|
|
576 cur = cur + strlen(cur);
|
|
577 break;
|
|
578 default:
|
|
579 gaim_debug(GAIM_DEBUG_ERROR, "irc", "invalid message format character '%c'\n", fmt[i]);
|
|
580 break;
|
|
581 }
|
|
582 }
|
6970
|
583 tmp = irc_recv_convert(irc, from);
|
|
584 (msgent->cb)(irc, msgent->name, tmp, args);
|
|
585 g_free(tmp);
|
6333
|
586 for (i = 0; i < strlen(msgent->format); i++) {
|
|
587 g_free(args[i]);
|
|
588 }
|
|
589 g_free(args);
|
|
590 g_free(from);
|
|
591 }
|
|
592
|
|
593 static void irc_parse_error_cb(struct irc_conn *irc, char *input)
|
|
594 {
|
|
595 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unrecognized string: %s\n", input);
|
|
596 }
|