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