4 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 static GSList
*cmds
= NULL
;
32 static char *irc_send_convert(struct irc_conn
*irc
, const char *string
);
33 static char *irc_recv_convert(struct irc_conn
*irc
, const char *string
);
35 static void irc_parse_error_cb(struct irc_conn
*irc
, char *input
);
37 static char *irc_mirc_colors
[16] = {
38 "white", "black", "blue", "dark green", "red", "brown", "purple",
39 "orange", "yellow", "green", "teal", "cyan", "light blue",
40 "pink", "grey", "light grey" };
42 extern PurpleProtocol
*_irc_protocol
;
44 /*typedef void (*IRCMsgCallback)(struct irc_conn *irc, char *from, char *name, char **args);*/
45 static struct _irc_msg
{
49 /** The required parameter count, based on values we use, not protocol
53 void (*cb
)(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
);
55 { "005", "n*", 2, irc_msg_features
}, /* Feature list */
56 { "251", "n:", 1, irc_msg_luser
}, /* Client & Server count */
57 { "255", "n:", 1, irc_msg_luser
}, /* Client & Server count Mk. II */
58 { "301", "nn:", 3, irc_msg_away
}, /* User is away */
59 { "303", "n:", 2, irc_msg_ison
}, /* ISON reply */
60 { "311", "nnvvv:", 6, irc_msg_whois
}, /* Whois user */
61 { "312", "nnv:", 4, irc_msg_whois
}, /* Whois server */
62 { "313", "nn:", 2, irc_msg_whois
}, /* Whois ircop */
63 { "314", "nnnvv:", 6, irc_msg_whois
}, /* Whowas user */
64 { "315", "nt:", 0, irc_msg_who
}, /* end of WHO channel */
65 { "317", "nnvv", 3, irc_msg_whois
}, /* Whois idle */
66 { "318", "nt:", 2, irc_msg_endwhois
}, /* End of WHOIS */
67 { "319", "nn:", 3, irc_msg_whois
}, /* Whois channels */
68 { "320", "nn:", 2, irc_msg_whois
}, /* Whois (fn ident) */
69 { "321", "*", 0, irc_msg_list
}, /* Start of list */
70 { "322", "ncv:", 4, irc_msg_list
}, /* List. */
71 { "323", ":", 0, irc_msg_list
}, /* End of list. */
72 { "324", "ncv:", 3, irc_msg_chanmode
}, /* Channel modes */
73 { "330", "nnv:", 4, irc_msg_whois
}, /* Whois (fn login) */
74 { "331", "nc:", 3, irc_msg_topic
}, /* No channel topic */
75 { "332", "nc:", 3, irc_msg_topic
}, /* Channel topic */
76 { "333", "ncvv", 4, irc_msg_topicinfo
}, /* Topic setter stuff */
77 { "352", "ncvvvnv:", 8, irc_msg_who
}, /* Channel WHO */
78 { "353", "nvc:", 4, irc_msg_names
}, /* Names list */
79 { "366", "nc:", 2, irc_msg_names
}, /* End of names */
80 { "367", "ncnnv", 3, irc_msg_ban
}, /* Ban list */
81 { "368", "nc:", 2, irc_msg_ban
}, /* End of ban list */
82 { "369", "nt:", 2, irc_msg_endwhois
}, /* End of WHOWAS */
83 { "372", "n:", 1, irc_msg_motd
}, /* MOTD */
84 { "375", "n:", 1, irc_msg_motd
}, /* Start MOTD */
85 { "376", "n:", 1, irc_msg_motd
}, /* End of MOTD */
86 { "391", "nv:", 3, irc_msg_time
}, /* Time reply */
87 { "401", "nt:", 2, irc_msg_nonick
}, /* No such nick/chan */
88 { "403", "nc:", 2, irc_msg_nochan
}, /* No such channel */
89 { "404", "nt:", 3, irc_msg_nosend
}, /* Cannot send to chan */
90 { "406", "nt:", 2, irc_msg_nonick
}, /* No such nick for WHOWAS */
91 { "421", "nv:", 2, irc_msg_unknown
}, /* Unknown command */
92 { "422", "n:", 1, irc_msg_motd
}, /* MOTD file missing */
93 { "432", "vn:", 0, irc_msg_badnick
}, /* Erroneous nickname */
94 { "433", "vn:", 2, irc_msg_nickused
}, /* Nickname already in use */
95 { "437", "nc:", 2, irc_msg_unavailable
}, /* Nick/channel is unavailable */
96 { "438", "nn:", 3, irc_msg_nochangenick
}, /* Nick may not change */
97 { "442", "nc:", 3, irc_msg_notinchan
}, /* Not in channel */
98 { "473", "nc:", 2, irc_msg_inviteonly
}, /* Tried to join invite-only */
99 { "474", "nc:", 2, irc_msg_banned
}, /* Banned from channel */
100 { "477", "nc:", 3, irc_msg_regonly
}, /* Registration Required */
101 { "478", "nct:", 3, irc_msg_banfull
}, /* Banlist is full */
102 { "482", "nc:", 3, irc_msg_notop
}, /* Need to be op to do that */
103 { "501", "n:", 2, irc_msg_badmode
}, /* Unknown mode flag */
104 { "506", "nc:", 3, irc_msg_nosend
}, /* Must identify to send */
105 { "515", "nc:", 3, irc_msg_regonly
}, /* Registration required */
106 #ifdef HAVE_CYRUS_SASL
107 { "903", "*", 0, irc_msg_authok
}, /* SASL auth successful */
108 { "904", "*", 0, irc_msg_authtryagain
}, /* SASL auth failed, can recover*/
109 { "905", "*", 0, irc_msg_authfail
}, /* SASL auth failed */
110 { "906", "*", 0, irc_msg_authfail
}, /* SASL auth failed */
111 { "907", "*", 0, irc_msg_authfail
}, /* SASL auth failed */
112 { "cap", "vv:", 3, irc_msg_cap
}, /* SASL capable */
113 { "authenticate", ":", 1, irc_msg_authenticate
}, /* SASL authenticate */
115 { "invite", "n:", 2, irc_msg_invite
}, /* Invited */
116 { "join", ":", 1, irc_msg_join
}, /* Joined a channel */
117 { "kick", "cn:", 3, irc_msg_kick
}, /* KICK */
118 { "mode", "tv:", 2, irc_msg_mode
}, /* MODE for channel */
119 { "nick", ":", 1, irc_msg_nick
}, /* Nick change */
120 { "notice", "t:", 2, irc_msg_notice
}, /* NOTICE recv */
121 { "part", "c:", 1, irc_msg_part
}, /* Parted a channel */
122 { "ping", ":", 1, irc_msg_ping
}, /* Received PING from server */
123 { "pong", "v:", 2, irc_msg_pong
}, /* Received PONG from server */
124 { "privmsg", "t:", 2, irc_msg_privmsg
}, /* Received private message */
125 { "topic", "c:", 2, irc_msg_topic
}, /* TOPIC command */
126 { "quit", ":", 1, irc_msg_quit
}, /* QUIT notice */
127 { "wallops", ":", 1, irc_msg_wallops
}, /* WALLOPS command */
128 { NULL
, NULL
, 0, NULL
}
131 static struct _irc_user_cmd
{
137 { "action", ":", irc_cmd_ctcp_action
, N_("action <action to perform>: Perform an action.") },
138 { "authserv", ":", irc_cmd_service
, N_("authserv: Send a command to authserv") },
139 { "away", ":", irc_cmd_away
, N_("away [message]: Set an away message, or use no message to return from being away.") },
140 { "ctcp", "t:", irc_cmd_ctcp
, N_("ctcp <nick> <msg>: sends ctcp msg to nick.") },
141 { "chanserv", ":", irc_cmd_service
, N_("chanserv: Send a command to chanserv") },
142 { "deop", ":", irc_cmd_op
, N_("deop <nick1> [nick2] ...: Remove channel operator status from someone. You must be a channel operator to do this.") },
143 { "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.") },
144 { "invite", ":", irc_cmd_invite
, N_("invite <nick> [room]: Invite someone to join you in the specified channel, or the current channel.") },
145 { "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.") },
146 { "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.") },
147 { "kick", "n:", irc_cmd_kick
, N_("kick <nick> [message]: Remove someone from a channel. You must be a channel operator to do this.") },
148 { "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>") },
149 { "me", ":", irc_cmd_ctcp_action
, N_("me <action to perform>: Perform an action.") },
150 { "memoserv", ":", irc_cmd_service
, N_("memoserv: Send a command to memoserv") },
151 { "mode", ":", irc_cmd_mode
, N_("mode <+|-><A-Za-z> <nick|channel>: Set or unset a channel or user mode.") },
152 { "msg", "t:", irc_cmd_privmsg
, N_("msg <nick> <message>: Send a private message to a user (as opposed to a channel).") },
153 { "names", "c", irc_cmd_names
, N_("names [channel]: List the users currently in a channel.") },
154 { "nick", "n", irc_cmd_nick
, N_("nick <new nickname>: Change your nickname.") },
155 { "nickserv", ":", irc_cmd_service
, N_("nickserv: Send a command to nickserv") },
156 { "notice", "t:", irc_cmd_privmsg
, N_("notice <target<: Send a notice to a user or channel.") },
157 { "op", ":", irc_cmd_op
, N_("op <nick1> [nick2] ...: Grant channel operator status to someone. You must be a channel operator to do this.") },
158 { "operwall", ":", irc_cmd_wallops
, N_("operwall <message>: If you don't know what this is, you probably can't use it.") },
159 { "operserv", ":", irc_cmd_service
, N_("operserv: Send a command to operserv") },
160 { "part", "c:", irc_cmd_part
, N_("part [room] [message]: Leave the current channel, or a specified channel, with an optional message.") },
161 { "ping", "n", irc_cmd_ping
, N_("ping [nick]: Asks how much lag a user (or the server if no user specified) has.") },
162 { "query", "n:", irc_cmd_query
, N_("query <nick> <message>: Send a private message to a user (as opposed to a channel).") },
163 { "quit", ":", irc_cmd_quit
, N_("quit [message]: Disconnect from the server, with an optional message.") },
164 { "quote", "*", irc_cmd_quote
, N_("quote [...]: Send a raw command to the server.") },
165 { "remove", "n:", irc_cmd_remove
, N_("remove <nick> [message]: Remove someone from a room. You must be a channel operator to do this.") },
166 { "time", "", irc_cmd_time
, N_("time: Displays the current local time at the IRC server.") },
167 { "topic", ":", irc_cmd_topic
, N_("topic [new topic]: View or change the channel topic.") },
168 { "umode", ":", irc_cmd_mode
, N_("umode <+|-><A-Za-z>: Set or unset a user mode.") },
169 { "version", ":", irc_cmd_ctcp_version
, N_("version [nick]: send CTCP VERSION request to a user") },
170 { "voice", ":", irc_cmd_op
, N_("voice <nick1> [nick2] ...: Grant channel voice status to someone. You must be a channel operator to do this.") },
171 { "wallops", ":", irc_cmd_wallops
, N_("wallops <message>: If you don't know what this is, you probably can't use it.") },
172 { "whois", "tt", irc_cmd_whois
, N_("whois [server] <nick>: Get information on a user.") },
173 { "whowas", "t", irc_cmd_whowas
, N_("whowas <nick>: Get information on a user that has logged off.") },
174 { NULL
, NULL
, NULL
, NULL
}
177 static PurpleCmdRet
irc_parse_purple_cmd(PurpleConversation
*conv
, const gchar
*cmd
,
178 gchar
**args
, gchar
**error
, void *data
)
180 PurpleConnection
*gc
;
181 struct irc_conn
*irc
;
182 struct _irc_user_cmd
*cmdent
;
184 gc
= purple_conversation_get_connection(conv
);
186 return PURPLE_CMD_RET_FAILED
;
188 irc
= purple_connection_get_protocol_data(gc
);
190 if ((cmdent
= g_hash_table_lookup(irc
->cmds
, cmd
)) == NULL
)
191 return PURPLE_CMD_RET_FAILED
;
193 (cmdent
->cb
)(irc
, cmd
, purple_conversation_get_name(conv
), (const char **)args
);
195 return PURPLE_CMD_RET_OK
;
198 static void irc_register_command(struct _irc_user_cmd
*c
)
206 f
= PURPLE_CMD_FLAG_CHAT
| PURPLE_CMD_FLAG_IM
| PURPLE_CMD_FLAG_PROTOCOL_ONLY
207 | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS
;
211 for (i
= 0; (i
< (sizeof(args
) - 1)) && *format
; i
++, format
++)
227 id
= purple_cmd_register(c
->name
, args
, PURPLE_CMD_P_PROTOCOL
, f
, "prpl-irc",
228 irc_parse_purple_cmd
, _(c
->help
), NULL
);
229 cmds
= g_slist_prepend(cmds
, GUINT_TO_POINTER(id
));
232 void irc_register_commands(void)
234 struct _irc_user_cmd
*c
;
236 for (c
= _irc_cmds
; c
&& c
->name
; c
++)
237 irc_register_command(c
);
240 void irc_unregister_commands(void)
243 PurpleCmdId id
= GPOINTER_TO_UINT(cmds
->data
);
244 purple_cmd_unregister(id
);
245 cmds
= g_slist_delete_link(cmds
, cmds
);
249 static char *irc_send_convert(struct irc_conn
*irc
, const char *string
)
254 const gchar
*enclist
;
256 enclist
= purple_account_get_string(irc
->account
, "encoding", IRC_DEFAULT_CHARSET
);
257 encodings
= g_strsplit(enclist
, ",", 2);
259 if (encodings
[0] == NULL
|| !g_ascii_strcasecmp("UTF-8", encodings
[0])) {
260 g_strfreev(encodings
);
264 utf8
= g_convert(string
, strlen(string
), encodings
[0], "UTF-8", NULL
, NULL
, &err
);
266 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Send conversion error: %s\n", err
->message
);
267 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Sending as UTF-8 instead of %s\n", encodings
[0]);
268 utf8
= g_strdup(string
);
271 g_strfreev(encodings
);
276 static char *irc_recv_convert(struct irc_conn
*irc
, const char *string
)
279 const gchar
*charset
, *enclist
;
284 autodetect
= purple_account_get_bool(irc
->account
, "autodetect_utf8", IRC_DEFAULT_AUTODETECT
);
286 if (autodetect
&& g_utf8_validate(string
, -1, NULL
)) {
287 return g_strdup(string
);
290 enclist
= purple_account_get_string(irc
->account
, "encoding", IRC_DEFAULT_CHARSET
);
291 encodings
= g_strsplit(enclist
, ",", -1);
293 if (encodings
[0] == NULL
) {
294 g_strfreev(encodings
);
295 return purple_utf8_salvage(string
);
298 for (i
= 0; encodings
[i
] != NULL
; i
++) {
299 charset
= encodings
[i
];
300 while (*charset
== ' ')
303 if (!g_ascii_strcasecmp("UTF-8", charset
)) {
304 if (g_utf8_validate(string
, -1, NULL
))
305 utf8
= g_strdup(string
);
307 utf8
= g_convert(string
, -1, "UTF-8", charset
, NULL
, NULL
, NULL
);
311 g_strfreev(encodings
);
315 g_strfreev(encodings
);
317 return purple_utf8_salvage(string
);
320 /* This function is shamelessly stolen from glib--it is an old version of the
321 * private function append_escaped_text, used by g_markup_escape_text, whose
322 * behavior changed in glib 2.12. */
323 static void irc_append_escaped_text(GString
*str
, const char *text
, gssize length
)
325 const char *p
= text
;
326 const char *end
= text
+ length
;
327 const char *next
= NULL
;
330 next
= g_utf8_next_char(p
);
334 g_string_append(str
, "&");
337 g_string_append(str
, "<");
340 g_string_append(str
, ">");
343 g_string_append(str
, "'");
346 g_string_append(str
, """);
349 g_string_append_len(str
, p
, next
- p
);
357 /* This function is shamelessly stolen from glib--it is an old version of the
358 * function g_markup_escape_text, whose behavior changed in glib 2.12. */
359 char *irc_escape_privmsg(const char *text
, gssize length
)
363 g_return_val_if_fail(text
!= NULL
, NULL
);
366 length
= strlen(text
);
368 str
= g_string_sized_new(length
);
370 irc_append_escaped_text(str
, text
, length
);
372 return g_string_free(str
, FALSE
);
375 /* XXX tag closings are not necessarily correctly nested here! If we
376 * get a ^O or reach the end of the string and there are open
377 * tags, they are closed in a fixed order ... this means, for
378 * example, you might see <FONT COLOR="blue">some text <B>with
379 * various attributes</FONT></B> (notice that B and FONT overlap
380 * and are not cleanly nested). This is imminently fixable but
381 * I am not fixing it right now.
383 char *irc_mirc2html(const char *string
)
385 const char *cur
, *end
;
386 char fg
[3] = "\0\0", bg
[3] = "\0\0";
388 int font
= 0, bold
= 0, underline
= 0, italic
= 0;
394 decoded
= g_string_sized_new(strlen(string
));
398 end
= strpbrk(cur
, "\002\003\007\017\026\037");
400 decoded
= g_string_append_len(decoded
, cur
, (end
? (gssize
)(end
- cur
) : (gssize
)strlen(cur
)));
401 cur
= end
? end
: cur
+ strlen(cur
);
407 decoded
= g_string_append(decoded
, "<B>");
410 decoded
= g_string_append(decoded
, "</B>");
416 fg
[0] = fg
[1] = bg
[0] = bg
[1] = '\0';
429 decoded
= g_string_append(decoded
, "</FONT>");
435 if (fgnum
< 0 || fgnum
> 15)
438 g_string_append_printf(decoded
, "<FONT COLOR=\"%s\"", irc_mirc_colors
[fgnum
]);
441 if (bgnum
>= 0 && bgnum
< 16)
442 g_string_append_printf(decoded
, " BACK=\"%s\"", irc_mirc_colors
[bgnum
]);
444 decoded
= g_string_append_c(decoded
, '>');
450 decoded
= g_string_append(decoded
, "<I>");
453 decoded
= g_string_append(decoded
, "</I>");
460 decoded
= g_string_append(decoded
, "<U>");
463 decoded
= g_string_append(decoded
, "</U>");
476 decoded
= g_string_append(decoded
, "</B>");
478 decoded
= g_string_append(decoded
, "</I>");
480 decoded
= g_string_append(decoded
, "</U>");
482 decoded
= g_string_append(decoded
, "</FONT>");
483 bold
= italic
= underline
= font
= FALSE
;
486 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Unexpected mIRC formatting character %d\n", *cur
);
490 return g_string_free(decoded
, FALSE
);
493 char *irc_mirc2txt (const char *string
)
501 result
= g_strdup (string
);
503 for (i
= 0, j
= 0; result
[i
]; i
++) {
507 /* Foreground color */
508 if (isdigit(result
[i
+ 1]))
510 if (isdigit(result
[i
+ 1]))
512 /* Optional comma and background color */
513 if (result
[i
+ 1] == ',') {
515 if (isdigit(result
[i
+ 1]))
517 if (isdigit(result
[i
+ 1]))
520 /* Note that i still points to the last character
521 * of the color selection string. */
529 result
[j
++] = result
[i
];
536 const char *irc_nick_skip_mode(struct irc_conn
*irc
, const char *nick
)
538 static const char *default_modes
= "@+%&";
539 const char *mode_chars
;
541 mode_chars
= irc
->mode_chars
? irc
->mode_chars
: default_modes
;
543 while (*nick
&& strchr(mode_chars
, *nick
) != NULL
)
549 gboolean
irc_ischannel(const char *string
)
551 return (string
[0] == '#' || string
[0] == '&');
554 char *irc_parse_ctcp(struct irc_conn
*irc
, const char *from
, const char *to
, const char *msg
, int notice
)
556 PurpleConnection
*gc
;
557 const char *cur
= msg
+ 1;
561 /* Note that this is NOT correct w.r.t. multiple CTCPs in one
562 * message and low-level quoting ... but if you want that crap,
563 * use a real IRC client. */
565 if (msg
[0] != '\001' || msg
[1] == '\0' || msg
[strlen(msg
) - 1] != '\001')
566 return g_strdup(msg
);
568 if (!strncmp(cur
, "ACTION ", 7)) {
570 buf
= g_strdup_printf("/me %s", cur
);
571 buf
[strlen(buf
) - 1] = '\0';
573 } else if (!strncmp(cur
, "PING ", 5)) {
574 if (notice
) { /* reply */
575 gc
= purple_account_get_connection(irc
->account
);
578 /* TODO: Should this read in the timestamp as a double? */
579 if (sscanf(cur
, "PING %lu", ×tamp
) == 1) {
580 buf
= g_strdup_printf(_("Reply time from %s: %lu seconds"), from
, time(NULL
) - timestamp
);
581 purple_notify_info(gc
, _("PONG"),
582 _("CTCP PING reply"), buf
,
583 purple_request_cpar_from_connection(gc
));
586 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Unable to parse PING timestamp");
589 buf
= irc_format(irc
, "vt:", "NOTICE", from
, msg
);
593 } else if (!strncmp(cur
, "VERSION", 7) && !notice
) {
594 buf
= irc_format(irc
, "vt:", "NOTICE", from
, "\001VERSION Purple IRC\001");
597 } else if (!strncmp(cur
, "DCC SEND ", 9)) {
598 irc_dccsend_recv(irc
, from
, msg
+ 10);
602 ctcp
= g_strdup(msg
+ 1);
603 ctcp
[strlen(ctcp
) - 1] = '\0';
604 buf
= g_strdup_printf("Received CTCP '%s' (to %s) from %s", ctcp
, to
, from
);
609 void irc_msg_table_build(struct irc_conn
*irc
)
613 if (!irc
|| !irc
->msgs
) {
614 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Attempt to build a message table on a bogus structure\n");
618 for (i
= 0; _irc_msgs
[i
].name
; i
++) {
619 g_hash_table_insert(irc
->msgs
, (gpointer
)_irc_msgs
[i
].name
, (gpointer
)&_irc_msgs
[i
]);
623 void irc_cmd_table_build(struct irc_conn
*irc
)
627 if (!irc
|| !irc
->cmds
) {
628 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Attempt to build a command table on a bogus structure\n");
632 for (i
= 0; _irc_cmds
[i
].name
; i
++) {
633 g_hash_table_insert(irc
->cmds
, (gpointer
)_irc_cmds
[i
].name
, (gpointer
)&_irc_cmds
[i
]);
637 char *irc_format(struct irc_conn
*irc
, const char *format
, ...)
639 GString
*string
= g_string_new("");
644 va_start(ap
, format
);
645 for (cur
= format
; *cur
; cur
++) {
647 g_string_append_c(string
, ' ');
649 tok
= va_arg(ap
, char *);
652 g_string_append(string
, tok
);
655 g_string_append_c(string
, ':');
660 tmp
= irc_send_convert(irc
, tok
);
661 g_string_append(string
, tmp
? tmp
: tok
);
665 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Invalid format character '%c'\n", *cur
);
670 g_string_append(string
, "\r\n");
671 return (g_string_free(string
, FALSE
));
674 void irc_parse_msg(struct irc_conn
*irc
, char *input
)
676 struct _irc_msg
*msgent
;
677 char *cur
, *end
, *tmp
, *from
, *msgname
, *fmt
, **args
, *msg
;
679 PurpleConnection
*gc
= purple_account_get_connection(irc
->account
);
683 irc
->recv_time
= time(NULL
);
686 * The data passed to irc-receiving-text is the raw protocol data.
687 * TODO: It should be passed as an array of bytes and a length
688 * instead of a null terminated string.
690 purple_signal_emit(_irc_protocol
, "irc-receiving-text", gc
, &input
);
692 if (purple_debug_is_verbose()) {
693 char *clean
= purple_utf8_salvage(input
);
694 clean
= g_strstrip(clean
);
695 purple_debug_misc("irc", ">> %s\n", clean
);
699 if (!strncmp(input
, "PING ", 5)) {
700 msg
= irc_format(irc
, "vv", "PONG", input
+ 5);
704 } else if (!strncmp(input
, "ERROR ", 6)) {
705 if (g_utf8_validate(input
, -1, NULL
)) {
706 purple_connection_take_error(gc
, g_error_new(
707 PURPLE_CONNECTION_ERROR
,
708 PURPLE_CONNECTION_ERROR_NETWORK_ERROR
,
709 "%s\n%s", _("Disconnected."), input
));
711 purple_connection_take_error(gc
, g_error_new_literal(
712 PURPLE_CONNECTION_ERROR
,
713 PURPLE_CONNECTION_ERROR_NETWORK_ERROR
,
714 _("Disconnected.")));
716 #ifdef HAVE_CYRUS_SASL
717 } else if (!strncmp(input
, "AUTHENTICATE ", 13)) {
718 irc_msg_auth(irc
, input
+ 13);
723 if (input
[0] != ':' || (cur
= strchr(input
, ' ')) == NULL
) {
724 irc_parse_error_cb(irc
, input
);
728 from
= g_strndup(&input
[1], cur
- &input
[1]);
730 end
= strchr(cur
, ' ');
732 end
= cur
+ strlen(cur
);
734 tmp
= g_strndup(cur
, end
- cur
);
735 msgname
= g_ascii_strdown(tmp
, -1);
738 if ((msgent
= g_hash_table_lookup(irc
->msgs
, msgname
)) == NULL
) {
739 irc_msg_default(irc
, "", from
, &input
);
747 args
= g_new0(char *, strlen(msgent
->format
));
749 for (cur
= end
, fmt
= msgent
->format
, i
= 0; fmt
[i
] && *cur
++; i
++) {
752 if (!(end
= strchr(cur
, ' '))) end
= cur
+ strlen(cur
);
753 /* This is a string of unknown encoding which we do not
754 * want to transcode, but it may or may not be valid
755 * UTF-8, so we'll salvage it. If a nick/channel/target
756 * field has inadvertently been marked verbatim, this
757 * could cause weirdness. */
758 tmp
= g_strndup(cur
, end
- cur
);
759 args
[i
] = purple_utf8_salvage(tmp
);
766 if (!(end
= strchr(cur
, ' '))) end
= cur
+ strlen(cur
);
767 tmp
= g_strndup(cur
, end
- cur
);
768 args
[i
] = irc_recv_convert(irc
, tmp
);
773 if (*cur
== ':') cur
++;
774 args
[i
] = irc_recv_convert(irc
, cur
);
775 cur
= cur
+ strlen(cur
);
778 /* Ditto 'v' above; we're going to salvage this in case
779 * it leaks past the IRC protocol */
780 args
[i
] = purple_utf8_salvage(cur
);
781 cur
= cur
+ strlen(cur
);
784 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "invalid message format character '%c'\n", fmt
[i
]);
791 if (G_UNLIKELY(!fmt_valid
)) {
792 purple_debug_error("irc", "message format was invalid");
793 } else if (G_LIKELY(args_cnt
>= msgent
->req_cnt
)) {
794 tmp
= irc_recv_convert(irc
, from
);
795 (msgent
->cb
)(irc
, msgent
->name
, tmp
, args
);
798 purple_debug_error("irc", "args count (%d) doesn't reach "
799 "expected value of %d for the '%s' command",
800 args_cnt
, msgent
->req_cnt
, msgent
->name
);
802 for (i
= 0; i
< strlen(msgent
->format
); i
++) {
809 static void irc_parse_error_cb(struct irc_conn
*irc
, char *input
)
812 /* This really should be escaped somehow that you can tell what
813 * the junk was -- but as it is, it can crash glib. */
814 clean
= purple_utf8_salvage(input
);
815 purple_debug(PURPLE_DEBUG_WARNING
, "irc", "Unrecognized string: %s\n", clean
);