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