mark PurpleImageClass as private
[pidgin-git.git] / libpurple / protocols / irc / msgs.c
blob040115e1299c12df6148e36174349e5de46f6802
1 /**
2 * @file msgs.c
4 * purple
6 * Copyright (C) 2003, 2012 Ethan Blanton <elb@pidgin.im>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 * Note: If you change any of these functions to use additional args you
25 * MUST ensure the arg count is correct in parse.c. Otherwise it may be
26 * possible for a malicious server or man-in-the-middle to trigger a crash.
29 #include "internal.h"
31 #include "conversation.h"
32 #include "buddylist.h"
33 #include "notify.h"
34 #include "util.h"
35 #include "debug.h"
36 #include "irc.h"
38 #include <stdio.h>
39 #include <stdlib.h>
41 #ifdef HAVE_CYRUS_SASL
42 #include <sasl/sasl.h>
43 #endif
45 static char *irc_mask_nick(const char *mask);
46 static char *irc_mask_userhost(const char *mask);
47 static void irc_chat_remove_buddy(PurpleChatConversation *chat, char *data[2]);
48 static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc);
49 static void irc_connected(struct irc_conn *irc, const char *nick);
51 static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name,
52 const char *from, const char *to,
53 const char *rawmsg, gboolean notice);
55 #ifdef HAVE_CYRUS_SASL
56 static void irc_sasl_finish(struct irc_conn *irc);
57 #endif
59 static char *irc_mask_nick(const char *mask)
61 char *end, *buf;
63 end = strchr(mask, '!');
64 if (!end)
65 buf = g_strdup(mask);
66 else
67 buf = g_strndup(mask, end - mask);
69 return buf;
72 static char *irc_mask_userhost(const char *mask)
74 return g_strdup(strchr(mask, '!') + 1);
77 static void irc_chat_remove_buddy(PurpleChatConversation *chat, char *data[2])
79 char *message, *stripped;
81 stripped = data[1] ? irc_mirc2txt(data[1]) : NULL;
82 message = g_strdup_printf("quit: %s", stripped);
83 g_free(stripped);
85 if (purple_chat_conversation_has_user(chat, data[0]))
86 purple_chat_conversation_remove_user(chat, data[0], message);
88 g_free(message);
91 static void irc_connected(struct irc_conn *irc, const char *nick)
93 PurpleConnection *gc;
94 PurpleStatus *status;
95 GSList *buddies;
96 PurpleAccount *account;
98 if ((gc = purple_account_get_connection(irc->account)) == NULL
99 || PURPLE_CONNECTION_IS_CONNECTED(gc))
100 return;
102 purple_connection_set_display_name(gc, nick);
103 purple_connection_set_state(gc, PURPLE_CONNECTION_CONNECTED);
104 account = purple_connection_get_account(gc);
106 /* If we're away then set our away message */
107 status = purple_account_get_active_status(irc->account);
108 if (purple_status_type_get_primitive(purple_status_get_status_type(status)) != PURPLE_STATUS_AVAILABLE) {
109 PurpleProtocol *protocol = purple_connection_get_protocol(gc);
110 purple_protocol_server_iface_set_status(protocol, irc->account, status);
113 /* this used to be in the core, but it's not now */
114 for (buddies = purple_blist_find_buddies(account, NULL); buddies;
115 buddies = g_slist_delete_link(buddies, buddies))
117 PurpleBuddy *b = buddies->data;
118 struct irc_buddy *ib = g_new0(struct irc_buddy, 1);
119 ib->name = g_strdup(purple_buddy_get_name(b));
120 ib->ref = 1;
121 g_hash_table_replace(irc->buddies, ib->name, ib);
124 irc_blist_timeout(irc);
125 if (!irc->timer)
126 irc->timer = g_timeout_add_seconds(45, (GSourceFunc)irc_blist_timeout, (gpointer)irc);
129 /* This function is ugly, but it's really an error handler. */
130 void irc_msg_default(struct irc_conn *irc, const char *name, const char *from, char **args)
132 int i;
133 const char *end, *cur, *numeric = NULL;
134 char *clean, *tmp, *convname;
135 PurpleConversation *convo;
137 for (cur = args[0], i = 0; i < 4; i++) {
138 end = strchr(cur, ' ');
139 if (end == NULL) {
140 goto undirected;
142 /* Check for 3-digit numeric in second position */
143 if (i == 1) {
144 if (end - cur != 3
145 || !isdigit(cur[0]) || !isdigit(cur[1])
146 || !isdigit(cur[2])) {
147 goto undirected;
149 /* Save the numeric for printing to the channel */
150 numeric = cur;
152 /* Don't advance cur if we're on the final iteration. */
153 if (i != 3) {
154 cur = end + 1;
158 /* At this point, cur is the beginning of the fourth position,
159 * end is the following space, and there are remaining
160 * arguments. We'll check to see if this argument is a
161 * currently active conversation (private message or channel,
162 * either one), and print the numeric to that conversation if it
163 * is. */
165 tmp = g_strndup(cur, end - cur);
166 convname = purple_utf8_salvage(tmp);
167 g_free(tmp);
169 /* Check for an existing conversation */
170 convo = purple_conversations_find_with_account(convname, irc->account);
171 g_free(convname);
173 if (convo == NULL) {
174 goto undirected;
177 /* end + 1 is the first argument past the target. The initial
178 * arguments we've skipped are routing info, numeric, recipient
179 * (this account's nick, most likely), and target (this
180 * channel). If end + 1 is an ASCII :, skip it, because it's
181 * meaningless in this context. This won't catch all
182 * :-arguments, but it'll catch the easy case. */
183 if (*++end == ':') {
184 end++;
187 /* We then print "numeric: remainder". */
188 clean = purple_utf8_salvage(end);
189 tmp = g_strdup_printf("%.3s: %s", numeric, clean);
190 g_free(clean);
191 purple_conversation_write_system_message(convo, tmp,
192 PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_RAW |
193 PURPLE_MESSAGE_NO_LINKIFY);
194 g_free(tmp);
195 return;
197 undirected:
198 /* This, too, should be escaped somehow (smarter) */
199 clean = purple_utf8_salvage(args[0]);
200 purple_debug(PURPLE_DEBUG_INFO, "irc", "Unrecognized message: %s\n", clean);
201 g_free(clean);
204 void irc_msg_features(struct irc_conn *irc, const char *name, const char *from, char **args)
206 gchar **features;
207 int i;
209 features = g_strsplit(args[1], " ", -1);
210 for (i = 0; features[i]; i++) {
211 char *val;
212 if (!strncmp(features[i], "PREFIX=", 7)) {
213 if ((val = strchr(features[i] + 7, ')')) != NULL)
214 irc->mode_chars = g_strdup(val + 1);
218 g_strfreev(features);
221 void irc_msg_luser(struct irc_conn *irc, const char *name, const char *from, char **args)
223 if (purple_strequal(name, "251")) {
224 /* 251 is required, so we pluck our nick from here and
225 * finalize connection */
226 irc_connected(irc, args[0]);
227 /* Some IRC servers seem to not send a 255 numeric, so
228 * I guess we can't require it; 251 will do. */
229 /* } else if (purple_strequal(name, "255")) { */
233 void irc_msg_away(struct irc_conn *irc, const char *name, const char *from, char **args)
235 PurpleConnection *gc;
236 char *msg;
238 if (irc->whois.nick && !purple_utf8_strcasecmp(irc->whois.nick, args[1])) {
239 /* We're doing a whois, show this in the whois dialog */
240 irc_msg_whois(irc, name, from, args);
241 return;
244 gc = purple_account_get_connection(irc->account);
245 if (gc) {
246 msg = g_markup_escape_text(args[2], -1);
247 purple_serv_got_im(gc, args[1], msg, PURPLE_MESSAGE_AUTO_RESP, time(NULL));
248 g_free(msg);
252 void irc_msg_badmode(struct irc_conn *irc, const char *name, const char *from, char **args)
254 PurpleConnection *gc = purple_account_get_connection(irc->account);
256 g_return_if_fail(gc);
258 purple_notify_error(gc, NULL, _("Bad mode"), args[1],
259 purple_request_cpar_from_connection(gc));
262 void irc_msg_ban(struct irc_conn *irc, const char *name, const char *from, char **args)
264 PurpleChatConversation *chat;
266 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
268 if (purple_strequal(name, "367")) {
269 char *msg = NULL;
270 /* Ban list entry */
271 if (args[3] && args[4]) {
272 /* This is an extended syntax, not in RFC 1459 */
273 int t1 = atoi(args[4]);
274 time_t t2 = time(NULL);
275 char *time = purple_str_seconds_to_string(t2 - t1);
276 msg = g_strdup_printf(_("Ban on %s by %s, set %s ago"),
277 args[2], args[3], time);
278 g_free(time);
279 } else {
280 msg = g_strdup_printf(_("Ban on %s"), args[2]);
282 if (chat) {
283 purple_conversation_write_system_message(
284 PURPLE_CONVERSATION(chat), msg, PURPLE_MESSAGE_NO_LOG);
285 } else {
286 purple_debug_info("irc", "%s\n", msg);
288 g_free(msg);
289 } else if (purple_strequal(name, "368")) {
290 if (!chat)
291 return;
292 /* End of ban list */
293 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat),
294 _("End of ban list"), PURPLE_MESSAGE_NO_LOG);
298 void irc_msg_banned(struct irc_conn *irc, const char *name, const char *from, char **args)
300 PurpleConnection *gc = purple_account_get_connection(irc->account);
301 char *buf;
303 g_return_if_fail(gc);
305 buf = g_strdup_printf(_("You are banned from %s."), args[1]);
306 purple_notify_error(gc, _("Banned"), _("Banned"), buf,
307 purple_request_cpar_from_connection(gc));
308 g_free(buf);
311 void irc_msg_banfull(struct irc_conn *irc, const char *name, const char *from, char **args)
313 PurpleChatConversation *chat;
314 char *buf, *nick;
316 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
317 if (!chat)
318 return;
320 nick = g_markup_escape_text(args[2], -1);
321 buf = g_strdup_printf(_("Cannot ban %s: banlist is full"), nick);
322 g_free(nick);
323 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat),
324 buf, PURPLE_MESSAGE_NO_LOG);
325 g_free(buf);
328 void irc_msg_chanmode(struct irc_conn *irc, const char *name, const char *from, char **args)
330 PurpleChatConversation *chat;
331 char *buf, *escaped;
333 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
334 if (!chat) /* XXX punt on channels we are not in for now */
335 return;
337 escaped = (args[3] != NULL) ? g_markup_escape_text(args[3], -1) : NULL;
338 buf = g_strdup_printf("mode for %s: %s %s", args[1], args[2], escaped ? escaped : "");
339 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), buf, 0);
340 g_free(escaped);
341 g_free(buf);
343 return;
346 void irc_msg_whois(struct irc_conn *irc, const char *name, const char *from, char **args)
348 if (!irc->whois.nick) {
349 purple_debug(PURPLE_DEBUG_WARNING, "irc", "Unexpected %s reply for %s\n", purple_strequal(name, "314") ? "WHOWAS" : "WHOIS"
350 , args[1]);
351 return;
354 if (purple_utf8_strcasecmp(irc->whois.nick, args[1])) {
355 purple_debug(PURPLE_DEBUG_WARNING, "irc", "Got %s reply for %s while waiting for %s\n", purple_strequal(name, "314") ? "WHOWAS" : "WHOIS"
356 , args[1], irc->whois.nick);
357 return;
360 if (purple_strequal(name, "301")) {
361 irc->whois.away = g_strdup(args[2]);
362 } else if (purple_strequal(name, "311") || purple_strequal(name, "314")) {
363 irc->whois.ident = g_strdup(args[2]);
364 irc->whois.host = g_strdup(args[3]);
365 irc->whois.real = g_strdup(args[5]);
366 } else if (purple_strequal(name, "312")) {
367 irc->whois.server = g_strdup(args[2]);
368 irc->whois.serverinfo = g_strdup(args[3]);
369 } else if (purple_strequal(name, "313")) {
370 irc->whois.ircop = 1;
371 } else if (purple_strequal(name, "317")) {
372 irc->whois.idle = atoi(args[2]);
373 if (args[3])
374 irc->whois.signon = (time_t)atoi(args[3]);
375 } else if (purple_strequal(name, "319")) {
376 if (irc->whois.channels == NULL) {
377 irc->whois.channels = g_string_new(args[2]);
378 } else {
379 irc->whois.channels = g_string_append(irc->whois.channels, args[2]);
381 } else if (purple_strequal(name, "320")) {
382 irc->whois.identified = 1;
383 } else if (purple_strequal(name, "330")) {
384 purple_debug(PURPLE_DEBUG_INFO, "irc", "330 %s: 1=[%s] 2=[%s] 3=[%s]",
385 name, args[1], args[2], args[3]);
386 if (purple_strequal(args[3], "is logged in as"))
387 irc->whois.login = g_strdup(args[2]);
391 void irc_msg_endwhois(struct irc_conn *irc, const char *name, const char *from, char **args)
393 PurpleConnection *gc;
394 char *tmp, *tmp2;
395 PurpleNotifyUserInfo *user_info;
397 if (!irc->whois.nick) {
398 purple_debug(PURPLE_DEBUG_WARNING, "irc", "Unexpected End of %s for %s\n", purple_strequal(name, "369") ? "WHOWAS" : "WHOIS"
399 , args[1]);
400 return;
402 if (purple_utf8_strcasecmp(irc->whois.nick, args[1])) {
403 purple_debug(PURPLE_DEBUG_WARNING, "irc", "Received end of %s for %s, expecting %s\n", purple_strequal(name, "369") ? "WHOWAS" : "WHOIS"
404 , args[1], irc->whois.nick);
405 return;
408 user_info = purple_notify_user_info_new();
410 tmp2 = g_markup_escape_text(args[1], -1);
411 tmp = g_strdup_printf("%s%s%s", tmp2,
412 (irc->whois.ircop ? _(" <i>(ircop)</i>") : ""),
413 (irc->whois.identified ? _(" <i>(identified)</i>") : ""));
414 purple_notify_user_info_add_pair_html(user_info, _("Nick"), tmp);
415 g_free(tmp2);
416 g_free(tmp);
418 if (irc->whois.away) {
419 purple_notify_user_info_add_pair_plaintext(user_info, _("Away"), irc->whois.away);
420 g_free(irc->whois.away);
422 if (irc->whois.real) {
423 purple_notify_user_info_add_pair_plaintext(user_info, _("Real name"), irc->whois.real);
424 g_free(irc->whois.real);
426 if (irc->whois.login) {
427 purple_notify_user_info_add_pair_plaintext(user_info, _("Login name"), irc->whois.login);
428 g_free(irc->whois.login);
430 if (irc->whois.ident) {
431 purple_notify_user_info_add_pair_plaintext(user_info, _("Ident name"), irc->whois.ident);
432 g_free(irc->whois.ident);
434 if (irc->whois.host) {
435 purple_notify_user_info_add_pair_plaintext(user_info, _("Host name"), irc->whois.host);
436 g_free(irc->whois.host);
438 if (irc->whois.server) {
439 tmp = g_strdup_printf("%s (%s)", irc->whois.server, irc->whois.serverinfo);
440 purple_notify_user_info_add_pair_plaintext(user_info, _("Server"), tmp);
441 g_free(tmp);
442 g_free(irc->whois.server);
443 g_free(irc->whois.serverinfo);
445 if (irc->whois.channels) {
446 purple_notify_user_info_add_pair_plaintext(user_info, _("Currently on"), irc->whois.channels->str);
447 g_string_free(irc->whois.channels, TRUE);
449 if (irc->whois.idle) {
450 gchar *timex = purple_str_seconds_to_string(irc->whois.idle);
451 purple_notify_user_info_add_pair_plaintext(user_info, _("Idle for"), timex);
452 g_free(timex);
453 purple_notify_user_info_add_pair_plaintext(user_info,
454 _("Online since"), purple_date_format_full(localtime(&irc->whois.signon)));
456 if (purple_strequal(irc->whois.nick, "elb")) {
457 purple_notify_user_info_add_pair_plaintext(user_info,
458 _("<b>Defining adjective:</b>"), _("Glorious"));
461 gc = purple_account_get_connection(irc->account);
463 purple_notify_userinfo(gc, irc->whois.nick, user_info, NULL, NULL);
464 purple_notify_user_info_destroy(user_info);
466 g_free(irc->whois.nick);
467 memset(&irc->whois, 0, sizeof(irc->whois));
470 void irc_msg_who(struct irc_conn *irc, const char *name, const char *from, char **args)
472 if (purple_strequal(name, "352")) {
473 PurpleChatConversation *chat;
474 PurpleChatUser *cb;
476 char *cur, *userhost, *realname;
478 PurpleChatUserFlags flags;
480 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
481 if (!chat) {
482 purple_debug(PURPLE_DEBUG_ERROR, "irc","Got a WHO response for %s, which doesn't exist\n", args[1]);
483 return;
486 cb = purple_chat_conversation_find_user(chat, args[5]);
487 if (!cb) {
488 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Got a WHO response for %s who isn't a buddy.\n", args[5]);
489 return;
492 userhost = g_strdup_printf("%s@%s", args[2], args[3]);
494 /* The final argument is a :-argument, but annoyingly
495 * contains two "words", the hop count and real name. */
496 for (cur = args[7]; *cur; cur++) {
497 if (*cur == ' ') {
498 cur++;
499 break;
502 realname = g_strdup(cur);
504 g_object_set_data_full(G_OBJECT(cb), "userhost", userhost, (GDestroyNotify)g_free);
505 g_object_set_data_full(G_OBJECT(cb), "realname", realname, (GDestroyNotify)g_free);
507 flags = purple_chat_user_get_flags(cb);
509 /* FIXME: I'm not sure this is really a good idea, now
510 * that we no longer do periodic WHO. It seems to me
511 * like it's more likely to be confusing than not.
512 * Comments? */
513 if (args[6][0] == 'G' && !(flags & PURPLE_CHAT_USER_AWAY)) {
514 purple_chat_user_set_flags(cb, flags | PURPLE_CHAT_USER_AWAY);
515 } else if(args[6][0] == 'H' && (flags & PURPLE_CHAT_USER_AWAY)) {
516 purple_chat_user_set_flags(cb, flags & ~PURPLE_CHAT_USER_AWAY);
521 void irc_msg_list(struct irc_conn *irc, const char *name, const char *from, char **args)
523 if (!irc->roomlist)
524 return;
526 if (purple_strequal(name, "321")) {
527 purple_roomlist_set_in_progress(irc->roomlist, TRUE);
528 return;
531 if (purple_strequal(name, "323")) {
532 purple_roomlist_set_in_progress(irc->roomlist, FALSE);
533 g_object_unref(irc->roomlist);
534 irc->roomlist = NULL;
535 return;
538 if (purple_strequal(name, "322")) {
539 PurpleRoomlistRoom *room;
540 char *topic;
542 if (!purple_roomlist_get_in_progress(irc->roomlist)) {
543 purple_debug_warning("irc", "Buggy server didn't send RPL_LISTSTART.\n");
544 purple_roomlist_set_in_progress(irc->roomlist, TRUE);
547 room = purple_roomlist_room_new(PURPLE_ROOMLIST_ROOMTYPE_ROOM, args[1], NULL);
548 purple_roomlist_room_add_field(irc->roomlist, room, args[1]);
549 purple_roomlist_room_add_field(irc->roomlist, room, GINT_TO_POINTER(strtol(args[2], NULL, 10)));
550 topic = irc_mirc2txt(args[3]);
551 purple_roomlist_room_add_field(irc->roomlist, room, topic);
552 g_free(topic);
553 purple_roomlist_room_add(irc->roomlist, room);
557 void irc_msg_topic(struct irc_conn *irc, const char *name, const char *from, char **args)
559 char *chan, *topic, *msg, *nick, *tmp, *tmp2;
560 PurpleChatConversation *chat;
562 if (purple_strequal(name, "topic")) {
563 chan = args[0];
564 topic = irc_mirc2txt (args[1]);
565 } else {
566 chan = args[1];
567 topic = irc_mirc2txt (args[2]);
570 chat = purple_conversations_find_chat_with_account(chan, irc->account);
571 if (!chat) {
572 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Got a topic for %s, which doesn't exist\n", chan);
573 g_free(topic);
574 return;
577 /* If this is an interactive update, print it out */
578 tmp = g_markup_escape_text(topic, -1);
579 tmp2 = purple_markup_linkify(tmp);
580 g_free(tmp);
581 if (purple_strequal(name, "topic")) {
582 const char *current_topic = purple_chat_conversation_get_topic(chat);
583 if (!(current_topic != NULL && purple_strequal(tmp2, current_topic)))
585 char *nick_esc;
586 nick = irc_mask_nick(from);
587 nick_esc = g_markup_escape_text(nick, -1);
588 purple_chat_conversation_set_topic(chat, nick, topic);
589 if (*tmp2)
590 msg = g_strdup_printf(_("%s has changed the topic to: %s"), nick_esc, tmp2);
591 else
592 msg = g_strdup_printf(_("%s has cleared the topic."), nick_esc);
593 g_free(nick_esc);
594 g_free(nick);
595 purple_conversation_write_system_message(
596 PURPLE_CONVERSATION(chat), msg, 0);
597 g_free(msg);
599 } else {
600 char *chan_esc = g_markup_escape_text(chan, -1);
601 msg = g_strdup_printf(_("The topic for %s is: %s"), chan_esc, tmp2);
602 g_free(chan_esc);
603 purple_chat_conversation_set_topic(chat, NULL, topic);
604 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), msg, 0);
605 g_free(msg);
607 g_free(tmp2);
608 g_free(topic);
611 void irc_msg_topicinfo(struct irc_conn *irc, const char *name, const char *from, char **args)
613 PurpleChatConversation *chat;
614 struct tm *tm;
615 time_t t;
616 char *msg, *timestamp, *datestamp;
618 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
619 if (!chat) {
620 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Got topic info for %s, which doesn't exist\n", args[1]);
621 return;
624 t = (time_t)atol(args[3]);
625 if (t == 0) {
626 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Got apparently nonsensical topic timestamp %s\n", args[3]);
627 return;
629 tm = localtime(&t);
631 timestamp = g_strdup(purple_time_format(tm));
632 datestamp = g_strdup(purple_date_format_short(tm));
633 msg = g_strdup_printf(_("Topic for %s set by %s at %s on %s"), args[1], args[2], timestamp, datestamp);
634 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat),
635 msg, PURPLE_MESSAGE_NO_LINKIFY);
636 g_free(timestamp);
637 g_free(datestamp);
638 g_free(msg);
641 void irc_msg_unknown(struct irc_conn *irc, const char *name, const char *from, char **args)
643 PurpleConnection *gc = purple_account_get_connection(irc->account);
644 char *buf;
646 g_return_if_fail(gc);
648 buf = g_strdup_printf(_("Unknown message '%s'"), args[1]);
649 purple_notify_error(gc, _("Unknown message"), buf, _("The IRC server "
650 "received a message it did not understand."),
651 purple_request_cpar_from_connection(gc));
652 g_free(buf);
655 void irc_msg_names(struct irc_conn *irc, const char *name, const char *from, char **args)
657 char *names, *cur, *end, *tmp, *msg;
658 PurpleConversation *convo;
660 if (purple_strequal(name, "366")) {
661 convo = purple_conversations_find_with_account(args[1], irc->account);
662 if (!convo) {
663 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Got a NAMES list for %s, which doesn't exist\n", args[1]);
664 g_string_free(irc->names, TRUE);
665 irc->names = NULL;
666 return;
669 names = cur = g_string_free(irc->names, FALSE);
670 irc->names = NULL;
671 if (g_object_get_data(G_OBJECT(convo), IRC_NAMES_FLAG)) {
672 msg = g_strdup_printf(_("Users on %s: %s"), args[1], names ? names : "");
673 purple_conversation_write_system_message(convo, msg, PURPLE_MESSAGE_NO_LOG);
674 g_free(msg);
675 } else if (cur != NULL) {
676 GList *users = NULL;
677 GList *flags = NULL;
679 while (*cur) {
680 PurpleChatUserFlags f = PURPLE_CHAT_USER_NONE;
681 end = strchr(cur, ' ');
682 if (!end)
683 end = cur + strlen(cur);
684 if (*cur == '@') {
685 f = PURPLE_CHAT_USER_OP;
686 cur++;
687 } else if (*cur == '%') {
688 f = PURPLE_CHAT_USER_HALFOP;
689 cur++;
690 } else if(*cur == '+') {
691 f = PURPLE_CHAT_USER_VOICE;
692 cur++;
693 } else if(irc->mode_chars
694 && strchr(irc->mode_chars, *cur)) {
695 if (*cur == '~')
696 f = PURPLE_CHAT_USER_FOUNDER;
697 cur++;
699 tmp = g_strndup(cur, end - cur);
700 users = g_list_prepend(users, tmp);
701 flags = g_list_prepend(flags, GINT_TO_POINTER(f));
702 cur = end;
703 if (*cur)
704 cur++;
707 if (users != NULL) {
708 GList *l;
710 purple_chat_conversation_add_users(PURPLE_CHAT_CONVERSATION(convo), users, NULL, flags, FALSE);
712 for (l = users; l != NULL; l = l->next)
713 g_free(l->data);
715 g_list_free(users);
716 g_list_free(flags);
719 g_object_set_data(G_OBJECT(convo), IRC_NAMES_FLAG,
720 GINT_TO_POINTER(TRUE));
722 g_free(names);
723 } else {
724 if (!irc->names)
725 irc->names = g_string_new("");
727 if (irc->names->len && irc->names->str[irc->names->len - 1] != ' ')
728 irc->names = g_string_append_c(irc->names, ' ');
729 irc->names = g_string_append(irc->names, args[3]);
733 void irc_msg_motd(struct irc_conn *irc, const char *name, const char *from, char **args)
735 char *escaped;
737 if (purple_strequal(name, "375")) {
738 if (irc->motd) {
739 g_string_free(irc->motd, TRUE);
740 irc->motd = NULL;
742 irc->motd = g_string_new("");
743 return;
744 } else if (purple_strequal(name, "376")) {
745 /* dircproxy 1.0.5 does not send 251 on reconnection, so
746 * finalize the connection here if it is not already done. */
747 irc_connected(irc, args[0]);
748 return;
749 } else if (purple_strequal(name, "422")) {
750 /* in case there is no 251, and no MOTD set, finalize the connection.
751 * (and clear the motd for good measure). */
753 if (irc->motd) {
754 g_string_free(irc->motd, TRUE);
755 irc->motd = NULL;
758 irc_connected(irc, args[0]);
759 return;
762 if (!irc->motd) {
763 purple_debug_error("irc", "IRC server sent MOTD without STARTMOTD\n");
764 return;
767 if (!args[1])
768 return;
770 escaped = g_markup_escape_text(args[1], -1);
771 g_string_append_printf(irc->motd, "%s<br>", escaped);
772 g_free(escaped);
775 void irc_msg_time(struct irc_conn *irc, const char *name, const char *from, char **args)
777 PurpleConnection *gc;
779 gc = purple_account_get_connection(irc->account);
781 g_return_if_fail(gc);
783 purple_notify_message(gc, PURPLE_NOTIFY_MSG_INFO, _("Time Response"),
784 _("The IRC server's local time is:"), args[2], NULL, NULL,
785 purple_request_cpar_from_connection(gc));
788 void irc_msg_nochan(struct irc_conn *irc, const char *name, const char *from, char **args)
790 PurpleConnection *gc = purple_account_get_connection(irc->account);
792 g_return_if_fail(gc);
794 purple_notify_error(gc, NULL, _("No such channel"), args[1],
795 purple_request_cpar_from_connection(gc));
798 void irc_msg_nonick(struct irc_conn *irc, const char *name, const char *from, char **args)
800 PurpleConnection *gc;
801 PurpleConversation *convo;
803 convo = purple_conversations_find_with_account(args[1], irc->account);
804 if (convo) {
805 purple_conversation_write_system_message(convo,
806 PURPLE_IS_IM_CONVERSATION(convo) ? _("User is not logged in") : _("no such channel"),
807 PURPLE_MESSAGE_NO_LOG);
809 } else {
810 if ((gc = purple_account_get_connection(irc->account)) == NULL)
811 return;
812 purple_notify_error(gc, NULL, _("No such nick or channel"),
813 args[1], purple_request_cpar_from_connection(gc));
816 if (irc->whois.nick && !purple_utf8_strcasecmp(irc->whois.nick, args[1])) {
817 g_free(irc->whois.nick);
818 irc->whois.nick = NULL;
822 void irc_msg_nosend(struct irc_conn *irc, const char *name, const char *from, char **args)
824 PurpleConnection *gc;
825 PurpleChatConversation *chat;
827 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
828 if (chat) {
829 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), args[2],
830 PURPLE_MESSAGE_NO_LOG);
831 } else {
832 if ((gc = purple_account_get_connection(irc->account)) == NULL)
833 return;
834 purple_notify_error(gc, NULL, _("Could not send"), args[2],
835 purple_request_cpar_from_connection(gc));
839 void irc_msg_notinchan(struct irc_conn *irc, const char *name, const char *from, char **args)
841 PurpleChatConversation *chat = purple_conversations_find_chat_with_account(args[1], irc->account);
843 purple_debug(PURPLE_DEBUG_INFO, "irc", "We're apparently not in %s, but tried to use it\n", args[1]);
844 if (chat) {
845 /*g_slist_remove(irc->gc->buddy_chats, chat);
846 purple_conversation_set_account(chat, NULL);*/
847 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat),
848 args[2], PURPLE_MESSAGE_NO_LOG);
852 void irc_msg_notop(struct irc_conn *irc, const char *name, const char *from, char **args)
854 PurpleChatConversation *chat;
856 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
857 if (!chat)
858 return;
860 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), args[2], 0);
863 void irc_msg_invite(struct irc_conn *irc, const char *name, const char *from, char **args)
865 PurpleConnection *gc = purple_account_get_connection(irc->account);
866 GHashTable *components;
867 gchar *nick;
869 g_return_if_fail(gc);
871 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
872 nick = irc_mask_nick(from);
874 g_hash_table_insert(components, g_strdup("channel"), g_strdup(args[1]));
876 purple_serv_got_chat_invite(gc, args[1], nick, NULL, components);
877 g_free(nick);
880 void irc_msg_inviteonly(struct irc_conn *irc, const char *name, const char *from, char **args)
882 PurpleConnection *gc = purple_account_get_connection(irc->account);
883 char *buf;
885 g_return_if_fail(gc);
887 buf = g_strdup_printf(_("Joining %s requires an invitation."), args[1]);
888 purple_notify_error(gc, _("Invitation only"), _("Invitation only"), buf,
889 purple_request_cpar_from_connection(gc));
890 g_free(buf);
893 void irc_msg_ison(struct irc_conn *irc, const char *name, const char *from, char **args)
895 char **nicks;
896 struct irc_buddy *ib;
897 int i;
899 nicks = g_strsplit(args[1], " ", -1);
900 for (i = 0; nicks[i]; i++) {
901 if ((ib = g_hash_table_lookup(irc->buddies, (gconstpointer)nicks[i])) == NULL) {
902 continue;
904 ib->new_online_status = TRUE;
906 g_strfreev(nicks);
908 if (irc->ison_outstanding)
909 irc_buddy_query(irc);
911 if (!irc->ison_outstanding)
912 g_hash_table_foreach(irc->buddies, (GHFunc)irc_buddy_status, (gpointer)irc);
915 static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc)
917 PurpleConnection *gc = purple_account_get_connection(irc->account);
918 PurpleBuddy *buddy = purple_blist_find_buddy(irc->account, name);
920 if (!gc || !buddy)
921 return;
923 if (ib->online && !ib->new_online_status) {
924 purple_protocol_got_user_status(irc->account, name, "offline", NULL);
925 ib->online = FALSE;
926 } else if (!ib->online && ib->new_online_status) {
927 purple_protocol_got_user_status(irc->account, name, "available", NULL);
928 ib->online = TRUE;
932 void irc_msg_join(struct irc_conn *irc, const char *name, const char *from, char **args)
934 PurpleConnection *gc = purple_account_get_connection(irc->account);
935 PurpleChatConversation *chat;
936 PurpleChatUser *cb;
938 char *nick, *userhost, *buf;
939 struct irc_buddy *ib;
940 static int id = 1;
942 g_return_if_fail(gc);
944 nick = irc_mask_nick(from);
946 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
947 /* We are joining a channel for the first time */
948 purple_serv_got_joined_chat(gc, id++, args[0]);
949 g_free(nick);
950 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
952 if (chat == NULL) {
953 purple_debug_error("irc", "tried to join %s but couldn't\n", args[0]);
954 return;
956 g_object_set_data(G_OBJECT(chat), IRC_NAMES_FLAG,
957 GINT_TO_POINTER(FALSE));
959 // Get the real name and user host for all participants.
960 buf = irc_format(irc, "vc", "WHO", args[0]);
961 irc_send(irc, buf);
962 g_free(buf);
964 /* Until purple_conversation_present does something that
965 * one would expect in Pidgin, this call produces buggy
966 * behavior both for the /join and auto-join cases. */
967 /* purple_conversation_present(chat); */
968 return;
971 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
972 if (chat == NULL) {
973 purple_debug(PURPLE_DEBUG_ERROR, "irc", "JOIN for %s failed\n", args[0]);
974 g_free(nick);
975 return;
978 userhost = irc_mask_userhost(from);
980 purple_chat_conversation_add_user(chat, nick, userhost, PURPLE_CHAT_USER_NONE, TRUE);
982 cb = purple_chat_conversation_find_user(chat, nick);
984 if (cb) {
985 g_object_set_data_full(G_OBJECT(cb), "userhost", userhost, (GDestroyNotify)g_free);
988 if ((ib = g_hash_table_lookup(irc->buddies, nick)) != NULL) {
989 ib->new_online_status = TRUE;
990 irc_buddy_status(nick, ib, irc);
993 g_free(nick);
996 void irc_msg_kick(struct irc_conn *irc, const char *name, const char *from, char **args)
998 PurpleConnection *gc = purple_account_get_connection(irc->account);
999 PurpleChatConversation *chat = purple_conversations_find_chat_with_account(args[0], irc->account);
1000 char *nick, *buf;
1002 g_return_if_fail(gc);
1004 nick = irc_mask_nick(from);
1006 if (!chat) {
1007 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Received a KICK for unknown channel %s\n", args[0]);
1008 g_free(nick);
1009 return;
1012 if (!purple_utf8_strcasecmp(purple_connection_get_display_name(gc), args[1])) {
1013 buf = g_strdup_printf(_("You have been kicked by %s: (%s)"), nick, args[2]);
1014 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), buf, 0);
1015 g_free(buf);
1016 purple_serv_got_chat_left(gc, purple_chat_conversation_get_id(chat));
1017 } else {
1018 buf = g_strdup_printf(_("Kicked by %s (%s)"), nick, args[2]);
1019 purple_chat_conversation_remove_user(chat, args[1], buf);
1020 g_free(buf);
1023 g_free(nick);
1024 return;
1027 void irc_msg_mode(struct irc_conn *irc, const char *name, const char *from, char **args)
1029 PurpleChatConversation *chat;
1030 char *nick = irc_mask_nick(from), *buf;
1032 if (*args[0] == '#' || *args[0] == '&') { /* Channel */
1033 char *escaped;
1034 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
1035 if (!chat) {
1036 purple_debug(PURPLE_DEBUG_ERROR, "irc", "MODE received for %s, which we are not in\n", args[0]);
1037 g_free(nick);
1038 return;
1040 escaped = (args[2] != NULL) ? g_markup_escape_text(args[2], -1) : NULL;
1041 buf = g_strdup_printf(_("mode (%s %s) by %s"), args[1], escaped ? escaped : "", nick);
1042 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), buf, 0);
1043 g_free(escaped);
1044 g_free(buf);
1045 if(args[2]) {
1046 PurpleChatUser *cb;
1047 PurpleChatUserFlags newflag, flags;
1048 char *mcur, *cur, *end, *user;
1049 gboolean add = FALSE;
1050 mcur = args[1];
1051 cur = args[2];
1052 while (*cur && *mcur) {
1053 if ((*mcur == '+') || (*mcur == '-')) {
1054 add = (*mcur == '+') ? TRUE : FALSE;
1055 mcur++;
1056 continue;
1058 end = strchr(cur, ' ');
1059 if (!end)
1060 end = cur + strlen(cur);
1061 user = g_strndup(cur, end - cur);
1062 cb = purple_chat_conversation_find_user(chat, user);
1063 flags = purple_chat_user_get_flags(cb);
1064 newflag = PURPLE_CHAT_USER_NONE;
1065 if (*mcur == 'o')
1066 newflag = PURPLE_CHAT_USER_OP;
1067 else if (*mcur =='h')
1068 newflag = PURPLE_CHAT_USER_HALFOP;
1069 else if (*mcur == 'v')
1070 newflag = PURPLE_CHAT_USER_VOICE;
1071 else if(irc->mode_chars
1072 && strchr(irc->mode_chars, '~') && (*mcur == 'q'))
1073 newflag = PURPLE_CHAT_USER_FOUNDER;
1074 if (newflag) {
1075 if (add)
1076 flags |= newflag;
1077 else
1078 flags &= ~newflag;
1079 purple_chat_user_set_flags(cb, flags);
1081 g_free(user);
1082 cur = end;
1083 if (*cur)
1084 cur++;
1085 if (*mcur)
1086 mcur++;
1089 } else { /* User */
1091 g_free(nick);
1094 void irc_msg_nick(struct irc_conn *irc, const char *name, const char *from, char **args)
1096 PurpleConnection *gc = purple_account_get_connection(irc->account);
1097 PurpleIMConversation *im;
1098 GSList *chats;
1099 char *nick = irc_mask_nick(from);
1101 irc->nickused = FALSE;
1103 if (!gc) {
1104 g_free(nick);
1105 return;
1107 chats = purple_connection_get_active_chats(gc);
1109 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
1110 purple_connection_set_display_name(gc, args[0]);
1113 while (chats) {
1114 PurpleChatConversation *chat = PURPLE_CHAT_CONVERSATION(chats->data);
1115 /* This is ugly ... */
1116 if (purple_chat_conversation_has_user(chat, nick))
1117 purple_chat_conversation_rename_user(chat, nick, args[0]);
1118 chats = chats->next;
1121 im = purple_conversations_find_im_with_account(nick,
1122 irc->account);
1123 if (im != NULL)
1124 purple_conversation_set_name(PURPLE_CONVERSATION(im), args[0]);
1126 g_free(nick);
1129 void irc_msg_badnick(struct irc_conn *irc, const char *name, const char *from, char **args)
1131 PurpleConnection *gc = purple_account_get_connection(irc->account);
1132 if (purple_connection_get_state(gc) == PURPLE_CONNECTION_CONNECTED) {
1133 purple_notify_error(gc, _("Invalid nickname"), _("Invalid "
1134 "nickname"), _("Your selected nickname was rejected by "
1135 "the server. It probably contains invalid characters."),
1136 purple_request_cpar_from_connection(gc));
1138 } else {
1139 purple_connection_take_error(gc, g_error_new_literal(
1140 PURPLE_CONNECTION_ERROR,
1141 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS,
1142 _("Your selected account name was rejected by the server. It probably contains invalid characters.")));
1146 void irc_msg_nickused(struct irc_conn *irc, const char *name, const char *from, char **args)
1148 char *newnick, *buf, *end;
1149 PurpleConnection *gc = purple_account_get_connection(irc->account);
1151 if (gc && purple_connection_get_state(gc) == PURPLE_CONNECTION_CONNECTED) {
1152 /* We only want to do the following dance if the connection
1153 has not been successfully completed. If it has, just
1154 notify the user that their /nick command didn't go. */
1155 buf = g_strdup_printf(_("The nickname \"%s\" is already being used."),
1156 irc->reqnick);
1157 purple_notify_error(gc, _("Nickname in use"), _("Nickname in "
1158 "use"), buf, purple_request_cpar_from_connection(gc));
1159 g_free(buf);
1160 g_free(irc->reqnick);
1161 irc->reqnick = NULL;
1162 return;
1165 if (strlen(args[1]) < strlen(irc->reqnick) || irc->nickused)
1166 newnick = g_strdup(args[1]);
1167 else
1168 newnick = g_strdup_printf("%s0", args[1]);
1169 end = newnick + strlen(newnick) - 1;
1170 /* try fallbacks */
1171 if((*end < '9') && (*end >= '1')) {
1172 *end = *end + 1;
1173 } else *end = '1';
1175 g_free(irc->reqnick);
1176 irc->reqnick = newnick;
1177 irc->nickused = TRUE;
1179 purple_connection_set_display_name(
1180 purple_account_get_connection(irc->account), newnick);
1182 buf = irc_format(irc, "vn", "NICK", newnick);
1183 irc_send(irc, buf);
1184 g_free(buf);
1187 void irc_msg_notice(struct irc_conn *irc, const char *name, const char *from, char **args)
1189 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], TRUE);
1192 void irc_msg_nochangenick(struct irc_conn *irc, const char *name, const char *from, char **args)
1194 PurpleConnection *gc = purple_account_get_connection(irc->account);
1196 g_return_if_fail(gc);
1198 purple_notify_error(gc, _("Cannot change nick"),
1199 _("Could not change nick"), args[2],
1200 purple_request_cpar_from_connection(gc));
1203 void irc_msg_part(struct irc_conn *irc, const char *name, const char *from, char **args)
1205 PurpleConnection *gc = purple_account_get_connection(irc->account);
1206 PurpleChatConversation *chat;
1207 char *nick, *msg, *channel;
1209 g_return_if_fail(gc);
1211 /* Undernet likes to :-quote the channel name, for no good reason
1212 * that I can see. This catches that. */
1213 channel = (args[0][0] == ':') ? &args[0][1] : args[0];
1215 chat = purple_conversations_find_chat_with_account(channel, irc->account);
1216 if (!chat) {
1217 purple_debug(PURPLE_DEBUG_INFO, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", channel);
1218 return;
1221 nick = irc_mask_nick(from);
1222 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
1223 char *escaped = args[1] ? g_markup_escape_text(args[1], -1) : NULL;
1224 msg = g_strdup_printf(_("You have parted the channel%s%s"),
1225 (args[1] && *args[1]) ? ": " : "",
1226 (escaped && *escaped) ? escaped : "");
1227 g_free(escaped);
1228 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), msg, 0);
1229 g_free(msg);
1230 purple_serv_got_chat_left(gc, purple_chat_conversation_get_id(chat));
1231 } else {
1232 msg = args[1] ? irc_mirc2txt(args[1]) : NULL;
1233 purple_chat_conversation_remove_user(chat, nick, msg);
1234 g_free(msg);
1236 g_free(nick);
1239 void irc_msg_ping(struct irc_conn *irc, const char *name, const char *from, char **args)
1241 char *buf;
1243 buf = irc_format(irc, "v:", "PONG", args[0]);
1244 irc_send(irc, buf);
1245 g_free(buf);
1248 void irc_msg_pong(struct irc_conn *irc, const char *name, const char *from, char **args)
1250 PurpleConversation *convo;
1251 PurpleConnection *gc;
1252 char **parts, *msg;
1253 time_t oldstamp;
1255 parts = g_strsplit(args[1], " ", 2);
1257 if (!parts[0] || !parts[1]) {
1258 g_strfreev(parts);
1259 return;
1262 if (sscanf(parts[1], "%lu", &oldstamp) != 1) {
1263 msg = g_strdup(_("Error: invalid PONG from server"));
1264 } else {
1265 msg = g_strdup_printf(_("PING reply -- Lag: %lu seconds"), time(NULL) - oldstamp);
1268 convo = purple_conversations_find_with_account(parts[0], irc->account);
1269 g_strfreev(parts);
1270 if (convo) {
1271 purple_conversation_write_system_message(convo, msg, PURPLE_MESSAGE_NO_LOG);
1272 } else {
1273 gc = purple_account_get_connection(irc->account);
1274 if (!gc) {
1275 g_free(msg);
1276 return;
1278 purple_notify_info(gc, NULL, "PONG", msg,
1279 purple_request_cpar_from_connection(gc));
1281 g_free(msg);
1284 void irc_msg_privmsg(struct irc_conn *irc, const char *name, const char *from, char **args)
1286 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], FALSE);
1289 static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name, const char *from, const char *to, const char *rawmsg, gboolean notice)
1291 PurpleConnection *gc = purple_account_get_connection(irc->account);
1292 PurpleChatConversation *chat;
1293 char *tmp;
1294 char *msg;
1295 char *nick;
1297 if (!gc)
1298 return;
1300 nick = irc_mask_nick(from);
1301 tmp = irc_parse_ctcp(irc, nick, to, rawmsg, notice);
1302 if (!tmp) {
1303 g_free(nick);
1304 return;
1307 msg = irc_escape_privmsg(tmp, -1);
1308 g_free(tmp);
1310 tmp = irc_mirc2html(msg);
1311 g_free(msg);
1312 msg = tmp;
1313 if (notice) {
1314 tmp = g_strdup_printf("(notice) %s", msg);
1315 g_free(msg);
1316 msg = tmp;
1319 if (!purple_utf8_strcasecmp(to, purple_connection_get_display_name(gc))) {
1320 purple_serv_got_im(gc, nick, msg, 0, time(NULL));
1321 } else {
1322 chat = purple_conversations_find_chat_with_account(irc_nick_skip_mode(irc, to), irc->account);
1323 if (chat) {
1324 purple_serv_got_chat_in(gc, purple_chat_conversation_get_id(chat),
1325 nick, PURPLE_MESSAGE_RECV, msg, time(NULL));
1326 } else
1327 purple_debug_error("irc", "Got a %s on %s, which does not exist\n",
1328 notice ? "NOTICE" : "PRIVMSG", to);
1330 g_free(msg);
1331 g_free(nick);
1334 void irc_msg_regonly(struct irc_conn *irc, const char *name, const char *from, char **args)
1336 PurpleConnection *gc = purple_account_get_connection(irc->account);
1337 char *msg;
1339 g_return_if_fail(gc);
1341 if (purple_conversations_find_chat_with_account(args[1], irc->account)) {
1342 /* This is a channel we're already in; for some reason,
1343 * freenode feels the need to notify us that in some
1344 * hypothetical other situation this might not have
1345 * succeeded. Suppress that. */
1346 return;
1349 msg = g_strdup_printf(_("Cannot join %s: Registration is required."), args[1]);
1350 purple_notify_error(gc, _("Cannot join channel"), msg, args[2],
1351 purple_request_cpar_from_connection(gc));
1352 g_free(msg);
1355 void irc_msg_quit(struct irc_conn *irc, const char *name, const char *from, char **args)
1357 PurpleConnection *gc = purple_account_get_connection(irc->account);
1358 struct irc_buddy *ib;
1359 char *data[2];
1361 g_return_if_fail(gc);
1363 data[0] = irc_mask_nick(from);
1364 data[1] = args[0];
1365 /* XXX this should have an API, I shouldn't grab this directly */
1366 g_slist_foreach(purple_connection_get_active_chats(gc),
1367 (GFunc)irc_chat_remove_buddy, data);
1369 if ((ib = g_hash_table_lookup(irc->buddies, data[0])) != NULL) {
1370 ib->new_online_status = FALSE;
1371 irc_buddy_status(data[0], ib, irc);
1373 g_free(data[0]);
1375 return;
1378 void irc_msg_unavailable(struct irc_conn *irc, const char *name, const char *from, char **args)
1380 PurpleConnection *gc = purple_account_get_connection(irc->account);
1382 purple_notify_error(gc, NULL, _("Nick or channel is temporarily "
1383 "unavailable."), args[1],
1384 purple_request_cpar_from_connection(gc));
1387 void irc_msg_wallops(struct irc_conn *irc, const char *name, const char *from, char **args)
1389 PurpleConnection *gc = purple_account_get_connection(irc->account);
1390 char *nick, *msg;
1392 g_return_if_fail(gc);
1394 nick = irc_mask_nick(from);
1395 msg = g_strdup_printf (_("Wallops from %s"), nick);
1396 g_free(nick);
1397 purple_notify_info(gc, NULL, msg, args[0],
1398 purple_request_cpar_from_connection(gc));
1399 g_free(msg);
1402 #ifdef HAVE_CYRUS_SASL
1403 static int
1404 irc_sasl_cb_secret(sasl_conn_t *conn, void *ctx, int id, sasl_secret_t **secret)
1406 struct irc_conn *irc = ctx;
1407 sasl_secret_t *sasl_secret;
1408 const char *pw;
1409 size_t len;
1411 pw = purple_connection_get_password(purple_account_get_connection(
1412 irc->account));
1414 if (!conn || !secret || id != SASL_CB_PASS)
1415 return SASL_BADPARAM;
1417 len = strlen(pw);
1418 /* Not an off-by-one because sasl_secret_t defines char data[1] */
1419 /* TODO: This can probably be moved to glib's allocator */
1420 sasl_secret = malloc(sizeof(sasl_secret_t) + len);
1421 if (!sasl_secret)
1422 return SASL_NOMEM;
1424 sasl_secret->len = len;
1425 strcpy((char*)sasl_secret->data, pw);
1427 *secret = sasl_secret;
1428 return SASL_OK;
1431 static int
1432 irc_sasl_cb_log(void *context, int level, const char *message)
1434 if(level <= SASL_LOG_TRACE)
1435 purple_debug_info("sasl", "%s\n", message);
1437 return SASL_OK;
1440 static int
1441 irc_sasl_cb_simple(void *ctx, int id, const char **res, unsigned *len)
1443 struct irc_conn *irc = ctx;
1444 PurpleConnection *gc = purple_account_get_connection(irc->account);
1446 switch(id) {
1447 case SASL_CB_AUTHNAME:
1448 *res = purple_connection_get_display_name(gc);
1449 break;
1450 case SASL_CB_USER:
1451 *res = "";
1452 break;
1453 default:
1454 return SASL_BADPARAM;
1456 if (len) *len = strlen((char *)*res);
1457 return SASL_OK;
1460 static void
1461 irc_auth_start_cyrus(struct irc_conn *irc)
1463 int ret = 0;
1464 char *buf;
1465 sasl_security_properties_t secprops;
1466 PurpleAccount *account = irc->account;
1467 PurpleConnection *gc = purple_account_get_connection(account);
1469 gboolean again = FALSE;
1471 /* Set up security properties and options */
1472 secprops.min_ssf = 0;
1473 secprops.security_flags = SASL_SEC_NOANONYMOUS;
1475 if (!G_IS_TLS_CONNECTION(irc->conn)) {
1476 gboolean plaintext;
1478 secprops.max_ssf = -1;
1479 secprops.maxbufsize = 4096;
1480 plaintext = purple_account_get_bool(account, "auth_plain_in_clear", FALSE);
1481 if (!plaintext)
1482 secprops.security_flags |= SASL_SEC_NOPLAINTEXT;
1483 } else {
1484 secprops.max_ssf = 0;
1485 secprops.maxbufsize = 0;
1488 secprops.property_names = 0;
1489 secprops.property_values = 0;
1491 do {
1492 again = FALSE;
1494 ret = sasl_client_new("irc", irc->server, NULL, NULL, irc->sasl_cb, 0, &irc->sasl_conn);
1496 if (ret != SASL_OK) {
1497 purple_debug_error("irc", "sasl_client_new failed: %d\n", ret);
1498 purple_connection_take_error(gc, g_error_new(
1499 PURPLE_CONNECTION_ERROR,
1500 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1501 ("Failed to initialize SASL authentication: %s"),
1502 sasl_errdetail(irc->sasl_conn)));
1503 return;
1506 sasl_setprop(irc->sasl_conn, SASL_AUTH_EXTERNAL, purple_account_get_username(irc->account));
1507 sasl_setprop(irc->sasl_conn, SASL_SEC_PROPS, &secprops);
1509 ret = sasl_client_start(irc->sasl_conn, irc->sasl_mechs->str, NULL, NULL, NULL, &irc->current_mech);
1511 switch (ret) {
1512 case SASL_OK:
1513 case SASL_CONTINUE:
1514 irc->mech_works = FALSE;
1515 break;
1516 case SASL_NOMECH:
1517 purple_connection_take_error(gc,
1518 g_error_new_literal(
1519 PURPLE_CONNECTION_ERROR,
1520 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1521 _("SASL authentication failed: No worthy authentication mechanisms found.")));
1523 irc_sasl_finish(irc);
1524 return;
1525 case SASL_BADPARAM:
1526 case SASL_NOMEM:
1527 purple_connection_take_error(gc, g_error_new(
1528 PURPLE_CONNECTION_ERROR,
1529 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1530 _("SASL authentication failed: %s"),
1531 sasl_errdetail(irc->sasl_conn)));
1533 irc_sasl_finish(irc);
1534 return;
1535 default:
1536 purple_debug_error("irc", "sasl_client_start failed: %s\n", sasl_errdetail(irc->sasl_conn));
1538 if (irc->current_mech && *irc->current_mech) {
1539 char *pos;
1540 if ((pos = strstr(irc->sasl_mechs->str, irc->current_mech))) {
1541 size_t index = pos - irc->sasl_mechs->str;
1542 g_string_erase(irc->sasl_mechs, index, strlen(irc->current_mech));
1544 /* Remove space which separated this mech from the next */
1545 if ((irc->sasl_mechs->str)[index] == ' ') {
1546 g_string_erase(irc->sasl_mechs, index, 1);
1550 again = TRUE;
1552 irc_sasl_finish(irc);
1554 } while (again);
1556 purple_debug_info("irc", "Using SASL: %s\n", irc->current_mech);
1558 buf = irc_format(irc, "vv", "AUTHENTICATE", irc->current_mech);
1559 irc_send(irc, buf);
1560 g_free(buf);
1563 /* SASL authentication */
1564 void
1565 irc_msg_cap(struct irc_conn *irc, const char *name, const char *from, char **args)
1567 int ret = 0;
1568 int id = 0;
1569 PurpleConnection *gc = purple_account_get_connection(irc->account);
1570 const char *mech_list = NULL;
1571 char *pos;
1572 size_t index;
1574 if (strncmp(g_strstrip(args[2]), "sasl", 5))
1575 return;
1576 if (strncmp(args[1], "ACK", 4)) {
1577 purple_connection_take_error(gc, g_error_new_literal(
1578 PURPLE_CONNECTION_ERROR,
1579 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1580 _("SASL authentication failed: Server does not support SASL authentication.")));
1582 irc_sasl_finish(irc);
1583 return;
1586 if ((ret = sasl_client_init(NULL)) != SASL_OK) {
1587 purple_connection_take_error(gc, g_error_new_literal(
1588 PURPLE_CONNECTION_ERROR,
1589 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1590 _("SASL authentication failed: Initializing SASL failed.")));
1591 return;
1594 irc->sasl_cb = g_new0(sasl_callback_t, 5);
1596 irc->sasl_cb[id].id = SASL_CB_AUTHNAME;
1597 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_simple; /* sasl_getsimple_t */
1598 irc->sasl_cb[id].context = (void *)irc;
1599 id++;
1601 irc->sasl_cb[id].id = SASL_CB_USER;
1602 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_simple; /* sasl_getsimple_t */
1603 irc->sasl_cb[id].context = (void *)irc;
1604 id++;
1606 irc->sasl_cb[id].id = SASL_CB_PASS;
1607 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_secret; /* sasl_getsecret_t */
1608 irc->sasl_cb[id].context = (void *)irc;
1609 id++;
1611 irc->sasl_cb[id].id = SASL_CB_LOG;
1612 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_log; /* sasl_log_t */
1613 irc->sasl_cb[id].context = (void *)irc;
1614 id++;
1616 irc->sasl_cb[id].id = SASL_CB_LIST_END;
1618 /* We need to do this to be able to list the mechanisms. */
1619 ret = sasl_client_new("irc", irc->server, NULL, NULL, irc->sasl_cb, 0, &irc->sasl_conn);
1621 sasl_listmech(irc->sasl_conn, NULL, "", " ", "", &mech_list, NULL, NULL);
1622 purple_debug_info("irc", "SASL: we have available: %s\n", mech_list);
1624 if (ret != SASL_OK) {
1625 purple_debug_error("irc", "sasl_client_new failed: %d\n", ret);
1627 purple_connection_take_error(gc, g_error_new(
1628 PURPLE_CONNECTION_ERROR,
1629 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1630 _("Failed to initialize SASL authentication: %s"),
1631 sasl_errdetail(irc->sasl_conn)));
1633 return;
1636 irc->sasl_mechs = g_string_new(mech_list);
1637 /* Drop EXTERNAL mechanism since we don't support it */
1638 if ((pos = strstr(irc->sasl_mechs->str, "EXTERNAL"))) {
1639 index = pos - irc->sasl_mechs->str;
1640 g_string_erase(irc->sasl_mechs, index, strlen("EXTERNAL"));
1641 /* Remove space which separated this mech from the next */
1642 if ((irc->sasl_mechs->str)[index] == ' ') {
1643 g_string_erase(irc->sasl_mechs, index, 1);
1647 irc_auth_start_cyrus(irc);
1650 void
1651 irc_msg_auth(struct irc_conn *irc, char *arg)
1653 PurpleConnection *gc = purple_account_get_connection(irc->account);
1654 char *buf, *authinfo;
1655 char *serverin = NULL;
1656 gsize serverinlen = 0;
1657 const gchar *c_out;
1658 unsigned int clen;
1659 int ret;
1661 irc->mech_works = TRUE;
1663 if (!arg)
1664 return;
1666 if (arg[0] != '+')
1667 serverin = (char *)g_base64_decode(arg, &serverinlen);
1669 ret = sasl_client_step(irc->sasl_conn, serverin, serverinlen,
1670 NULL, &c_out, &clen);
1672 if (ret != SASL_OK && ret != SASL_CONTINUE) {
1673 purple_connection_take_error(gc, g_error_new(
1674 PURPLE_CONNECTION_ERROR,
1675 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1676 _("SASL authentication failed: %s"),
1677 sasl_errdetail(irc->sasl_conn)));
1679 irc_sasl_finish(irc);
1680 g_free(serverin);
1681 return;
1684 if (clen > 0)
1685 authinfo = g_base64_encode((const guchar*)c_out, clen);
1686 else
1687 authinfo = g_strdup("+");
1689 buf = irc_format(irc, "vv", "AUTHENTICATE", authinfo);
1690 irc_send(irc, buf);
1691 g_free(buf);
1692 g_free(authinfo);
1693 g_free(serverin);
1696 void
1697 irc_msg_authenticate(struct irc_conn *irc, const char *name, const char *from, char **args)
1699 irc_msg_auth(irc, args[0]);
1702 void
1703 irc_msg_authok(struct irc_conn *irc, const char *name, const char *from, char **args)
1705 char *buf;
1707 sasl_dispose(&irc->sasl_conn);
1708 irc->sasl_conn = NULL;
1709 purple_debug_info("irc", "Succesfully authenticated using SASL.\n");
1711 /* Finish auth session */
1712 buf = irc_format(irc, "vv", "CAP", "END");
1713 irc_send(irc, buf);
1714 g_free(buf);
1717 void
1718 irc_msg_authtryagain(struct irc_conn *irc, const char *name, const char *from, char **args)
1720 PurpleConnection *gc = purple_account_get_connection(irc->account);
1722 /* We already received at least one AUTHENTICATE reply from the
1723 * server. This suggests it supports this mechanism, but the
1724 * password was incorrect. It would be better to abort and inform
1725 * the user than to try again with a different mechanism, so they
1726 * aren't told the server supports no worthy mechanisms.
1728 if (irc->mech_works) {
1729 purple_connection_take_error(gc, g_error_new_literal(
1730 PURPLE_CONNECTION_ERROR,
1731 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
1732 _("Incorrect Password")));
1734 irc_sasl_finish(irc);
1736 return;
1739 if (irc->current_mech) {
1740 char *pos;
1741 if ((pos = strstr(irc->sasl_mechs->str, irc->current_mech))) {
1742 size_t index = pos - irc->sasl_mechs->str;
1743 g_string_erase(irc->sasl_mechs, index, strlen(irc->current_mech));
1745 /* Remove space which separated this mech from the next */
1746 if ((irc->sasl_mechs->str)[index] == ' ') {
1747 g_string_erase(irc->sasl_mechs, index, 1);
1751 if (*irc->sasl_mechs->str) {
1752 sasl_dispose(&irc->sasl_conn);
1754 purple_debug_info("irc", "Now trying with %s\n", irc->sasl_mechs->str);
1755 irc_auth_start_cyrus(irc);
1756 } else {
1757 purple_connection_take_error(gc, g_error_new_literal(
1758 PURPLE_CONNECTION_ERROR,
1759 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1760 _("SASL authentication failed: No worthy mechanisms found")));
1762 irc_sasl_finish(irc);
1766 void
1767 irc_msg_authfail(struct irc_conn *irc, const char *name, const char *from, char **args)
1769 PurpleConnection *gc = purple_account_get_connection(irc->account);
1771 /* Only show an error if we did not abort ourselves. */
1772 if (irc->sasl_conn) {
1773 purple_debug_info("irc", "SASL authentication failed: %s", sasl_errdetail(irc->sasl_conn));
1775 purple_connection_take_error(gc, g_error_new_literal(
1776 PURPLE_CONNECTION_ERROR,
1777 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
1778 _("Incorrect Password")));
1781 irc_sasl_finish(irc);
1784 static void
1785 irc_sasl_finish(struct irc_conn *irc)
1787 char *buf;
1789 sasl_dispose(&irc->sasl_conn);
1790 irc->sasl_conn = NULL;
1792 g_free(irc->sasl_cb);
1793 irc->sasl_cb = NULL;
1795 /* Auth failed, abort */
1796 buf = irc_format(irc, "vv", "CAP", "END");
1797 irc_send(irc, buf);
1798 g_free(buf);
1800 #endif