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