Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / irc / msgs.c
blob096344d4fe508130f2732000bfb774db2f231eff
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 = purple_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 (!strcmp(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 (!strcmp(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 (!strcmp(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 (!strcmp(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", !strcmp(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", !strcmp(name, "314") ? "WHOWAS" : "WHOIS"
356 , args[1], irc->whois.nick);
357 return;
360 if (!strcmp(name, "301")) {
361 irc->whois.away = g_strdup(args[2]);
362 } else if (!strcmp(name, "311") || !strcmp(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 (!strcmp(name, "312")) {
367 irc->whois.server = g_strdup(args[2]);
368 irc->whois.serverinfo = g_strdup(args[3]);
369 } else if (!strcmp(name, "313")) {
370 irc->whois.ircop = 1;
371 } else if (!strcmp(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 (!strcmp(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 (!strcmp(name, "320")) {
382 irc->whois.identified = 1;
383 } else if (!strcmp(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 (!strcmp(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", !strcmp(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", !strcmp(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 (!strcmp(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 (!strcmp(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 (!strcmp(name, "321")) {
527 purple_roomlist_set_in_progress(irc->roomlist, TRUE);
528 return;
531 if (!strcmp(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 (!strcmp(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 (!strcmp(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 (!strcmp(name, "topic")) {
582 const char *current_topic = purple_chat_conversation_get_topic(chat);
583 if (!(current_topic != NULL && strcmp(tmp2, current_topic) == 0))
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 (!strcmp(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 (!strcmp(name, "375")) {
738 if (irc->motd)
739 g_string_free(irc->motd, TRUE);
740 irc->motd = g_string_new("");
741 return;
742 } else if (!strcmp(name, "376")) {
743 /* dircproxy 1.0.5 does not send 251 on reconnection, so
744 * finalize the connection here if it is not already done. */
745 irc_connected(irc, args[0]);
746 return;
747 } else if (!strcmp(name, "422")) {
748 /* in case there is no 251, and no MOTD set, finalize the connection.
749 * (and clear the motd for good measure). */
751 if (irc->motd)
752 g_string_free(irc->motd, TRUE);
754 irc_connected(irc, args[0]);
755 return;
758 if (!irc->motd) {
759 purple_debug_error("irc", "IRC server sent MOTD without STARTMOTD\n");
760 return;
763 if (!args[1])
764 return;
766 escaped = g_markup_escape_text(args[1], -1);
767 g_string_append_printf(irc->motd, "%s<br>", escaped);
768 g_free(escaped);
771 void irc_msg_time(struct irc_conn *irc, const char *name, const char *from, char **args)
773 PurpleConnection *gc;
775 gc = purple_account_get_connection(irc->account);
777 g_return_if_fail(gc);
779 purple_notify_message(gc, PURPLE_NOTIFY_MSG_INFO, _("Time Response"),
780 _("The IRC server's local time is:"), args[2], NULL, NULL,
781 purple_request_cpar_from_connection(gc));
784 void irc_msg_nochan(struct irc_conn *irc, const char *name, const char *from, char **args)
786 PurpleConnection *gc = purple_account_get_connection(irc->account);
788 g_return_if_fail(gc);
790 purple_notify_error(gc, NULL, _("No such channel"), args[1],
791 purple_request_cpar_from_connection(gc));
794 void irc_msg_nonick(struct irc_conn *irc, const char *name, const char *from, char **args)
796 PurpleConnection *gc;
797 PurpleConversation *convo;
799 convo = purple_conversations_find_with_account(args[1], irc->account);
800 if (convo) {
801 purple_conversation_write_system_message(convo,
802 PURPLE_IS_IM_CONVERSATION(convo) ? _("User is not logged in") : _("no such channel"),
803 PURPLE_MESSAGE_NO_LOG);
805 } else {
806 if ((gc = purple_account_get_connection(irc->account)) == NULL)
807 return;
808 purple_notify_error(gc, NULL, _("No such nick or channel"),
809 args[1], purple_request_cpar_from_connection(gc));
812 if (irc->whois.nick && !purple_utf8_strcasecmp(irc->whois.nick, args[1])) {
813 g_free(irc->whois.nick);
814 irc->whois.nick = NULL;
818 void irc_msg_nosend(struct irc_conn *irc, const char *name, const char *from, char **args)
820 PurpleConnection *gc;
821 PurpleChatConversation *chat;
823 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
824 if (chat) {
825 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), args[2],
826 PURPLE_MESSAGE_NO_LOG);
827 } else {
828 if ((gc = purple_account_get_connection(irc->account)) == NULL)
829 return;
830 purple_notify_error(gc, NULL, _("Could not send"), args[2],
831 purple_request_cpar_from_connection(gc));
835 void irc_msg_notinchan(struct irc_conn *irc, const char *name, const char *from, char **args)
837 PurpleChatConversation *chat = purple_conversations_find_chat_with_account(args[1], irc->account);
839 purple_debug(PURPLE_DEBUG_INFO, "irc", "We're apparently not in %s, but tried to use it\n", args[1]);
840 if (chat) {
841 /*g_slist_remove(irc->gc->buddy_chats, chat);
842 purple_conversation_set_account(chat, NULL);*/
843 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat),
844 args[2], PURPLE_MESSAGE_NO_LOG);
848 void irc_msg_notop(struct irc_conn *irc, const char *name, const char *from, char **args)
850 PurpleChatConversation *chat;
852 chat = purple_conversations_find_chat_with_account(args[1], irc->account);
853 if (!chat)
854 return;
856 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), args[2], 0);
859 void irc_msg_invite(struct irc_conn *irc, const char *name, const char *from, char **args)
861 PurpleConnection *gc = purple_account_get_connection(irc->account);
862 GHashTable *components;
863 gchar *nick;
865 g_return_if_fail(gc);
867 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
868 nick = irc_mask_nick(from);
870 g_hash_table_insert(components, g_strdup("channel"), g_strdup(args[1]));
872 purple_serv_got_chat_invite(gc, args[1], nick, NULL, components);
873 g_free(nick);
876 void irc_msg_inviteonly(struct irc_conn *irc, const char *name, const char *from, char **args)
878 PurpleConnection *gc = purple_account_get_connection(irc->account);
879 char *buf;
881 g_return_if_fail(gc);
883 buf = g_strdup_printf(_("Joining %s requires an invitation."), args[1]);
884 purple_notify_error(gc, _("Invitation only"), _("Invitation only"), buf,
885 purple_request_cpar_from_connection(gc));
886 g_free(buf);
889 void irc_msg_ison(struct irc_conn *irc, const char *name, const char *from, char **args)
891 char **nicks;
892 struct irc_buddy *ib;
893 int i;
895 nicks = g_strsplit(args[1], " ", -1);
896 for (i = 0; nicks[i]; i++) {
897 if ((ib = g_hash_table_lookup(irc->buddies, (gconstpointer)nicks[i])) == NULL) {
898 continue;
900 ib->new_online_status = TRUE;
902 g_strfreev(nicks);
904 if (irc->ison_outstanding)
905 irc_buddy_query(irc);
907 if (!irc->ison_outstanding)
908 g_hash_table_foreach(irc->buddies, (GHFunc)irc_buddy_status, (gpointer)irc);
911 static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc)
913 PurpleConnection *gc = purple_account_get_connection(irc->account);
914 PurpleBuddy *buddy = purple_blist_find_buddy(irc->account, name);
916 if (!gc || !buddy)
917 return;
919 if (ib->online && !ib->new_online_status) {
920 purple_protocol_got_user_status(irc->account, name, "offline", NULL);
921 ib->online = FALSE;
922 } else if (!ib->online && ib->new_online_status) {
923 purple_protocol_got_user_status(irc->account, name, "available", NULL);
924 ib->online = TRUE;
928 void irc_msg_join(struct irc_conn *irc, const char *name, const char *from, char **args)
930 PurpleConnection *gc = purple_account_get_connection(irc->account);
931 PurpleChatConversation *chat;
932 PurpleChatUser *cb;
934 char *nick, *userhost, *buf;
935 struct irc_buddy *ib;
936 static int id = 1;
938 g_return_if_fail(gc);
940 nick = irc_mask_nick(from);
942 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
943 /* We are joining a channel for the first time */
944 purple_serv_got_joined_chat(gc, id++, args[0]);
945 g_free(nick);
946 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
948 if (chat == NULL) {
949 purple_debug_error("irc", "tried to join %s but couldn't\n", args[0]);
950 return;
952 g_object_set_data(G_OBJECT(chat), IRC_NAMES_FLAG,
953 GINT_TO_POINTER(FALSE));
955 // Get the real name and user host for all participants.
956 buf = irc_format(irc, "vc", "WHO", args[0]);
957 irc_send(irc, buf);
958 g_free(buf);
960 /* Until purple_conversation_present does something that
961 * one would expect in Pidgin, this call produces buggy
962 * behavior both for the /join and auto-join cases. */
963 /* purple_conversation_present(chat); */
964 return;
967 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
968 if (chat == NULL) {
969 purple_debug(PURPLE_DEBUG_ERROR, "irc", "JOIN for %s failed\n", args[0]);
970 g_free(nick);
971 return;
974 userhost = irc_mask_userhost(from);
976 purple_chat_conversation_add_user(chat, nick, userhost, PURPLE_CHAT_USER_NONE, TRUE);
978 cb = purple_chat_conversation_find_user(chat, nick);
980 if (cb) {
981 g_object_set_data_full(G_OBJECT(cb), "userhost", userhost, (GDestroyNotify)g_free);
984 if ((ib = g_hash_table_lookup(irc->buddies, nick)) != NULL) {
985 ib->new_online_status = TRUE;
986 irc_buddy_status(nick, ib, irc);
989 g_free(nick);
992 void irc_msg_kick(struct irc_conn *irc, const char *name, const char *from, char **args)
994 PurpleConnection *gc = purple_account_get_connection(irc->account);
995 PurpleChatConversation *chat = purple_conversations_find_chat_with_account(args[0], irc->account);
996 char *nick, *buf;
998 g_return_if_fail(gc);
1000 nick = irc_mask_nick(from);
1002 if (!chat) {
1003 purple_debug(PURPLE_DEBUG_ERROR, "irc", "Received a KICK for unknown channel %s\n", args[0]);
1004 g_free(nick);
1005 return;
1008 if (!purple_utf8_strcasecmp(purple_connection_get_display_name(gc), args[1])) {
1009 buf = g_strdup_printf(_("You have been kicked by %s: (%s)"), nick, args[2]);
1010 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), buf, 0);
1011 g_free(buf);
1012 purple_serv_got_chat_left(gc, purple_chat_conversation_get_id(chat));
1013 } else {
1014 buf = g_strdup_printf(_("Kicked by %s (%s)"), nick, args[2]);
1015 purple_chat_conversation_remove_user(chat, args[1], buf);
1016 g_free(buf);
1019 g_free(nick);
1020 return;
1023 void irc_msg_mode(struct irc_conn *irc, const char *name, const char *from, char **args)
1025 PurpleChatConversation *chat;
1026 char *nick = irc_mask_nick(from), *buf;
1028 if (*args[0] == '#' || *args[0] == '&') { /* Channel */
1029 char *escaped;
1030 chat = purple_conversations_find_chat_with_account(args[0], irc->account);
1031 if (!chat) {
1032 purple_debug(PURPLE_DEBUG_ERROR, "irc", "MODE received for %s, which we are not in\n", args[0]);
1033 g_free(nick);
1034 return;
1036 escaped = (args[2] != NULL) ? g_markup_escape_text(args[2], -1) : NULL;
1037 buf = g_strdup_printf(_("mode (%s %s) by %s"), args[1], escaped ? escaped : "", nick);
1038 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), buf, 0);
1039 g_free(escaped);
1040 g_free(buf);
1041 if(args[2]) {
1042 PurpleChatUser *cb;
1043 PurpleChatUserFlags newflag, flags;
1044 char *mcur, *cur, *end, *user;
1045 gboolean add = FALSE;
1046 mcur = args[1];
1047 cur = args[2];
1048 while (*cur && *mcur) {
1049 if ((*mcur == '+') || (*mcur == '-')) {
1050 add = (*mcur == '+') ? TRUE : FALSE;
1051 mcur++;
1052 continue;
1054 end = strchr(cur, ' ');
1055 if (!end)
1056 end = cur + strlen(cur);
1057 user = g_strndup(cur, end - cur);
1058 cb = purple_chat_conversation_find_user(chat, user);
1059 flags = purple_chat_user_get_flags(cb);
1060 newflag = PURPLE_CHAT_USER_NONE;
1061 if (*mcur == 'o')
1062 newflag = PURPLE_CHAT_USER_OP;
1063 else if (*mcur =='h')
1064 newflag = PURPLE_CHAT_USER_HALFOP;
1065 else if (*mcur == 'v')
1066 newflag = PURPLE_CHAT_USER_VOICE;
1067 else if(irc->mode_chars
1068 && strchr(irc->mode_chars, '~') && (*mcur == 'q'))
1069 newflag = PURPLE_CHAT_USER_FOUNDER;
1070 if (newflag) {
1071 if (add)
1072 flags |= newflag;
1073 else
1074 flags &= ~newflag;
1075 purple_chat_user_set_flags(cb, flags);
1077 g_free(user);
1078 cur = end;
1079 if (*cur)
1080 cur++;
1081 if (*mcur)
1082 mcur++;
1085 } else { /* User */
1087 g_free(nick);
1090 void irc_msg_nick(struct irc_conn *irc, const char *name, const char *from, char **args)
1092 PurpleConnection *gc = purple_account_get_connection(irc->account);
1093 PurpleIMConversation *im;
1094 GSList *chats;
1095 char *nick = irc_mask_nick(from);
1097 irc->nickused = FALSE;
1099 if (!gc) {
1100 g_free(nick);
1101 return;
1103 chats = purple_connection_get_active_chats(gc);
1105 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
1106 purple_connection_set_display_name(gc, args[0]);
1109 while (chats) {
1110 PurpleChatConversation *chat = PURPLE_CHAT_CONVERSATION(chats->data);
1111 /* This is ugly ... */
1112 if (purple_chat_conversation_has_user(chat, nick))
1113 purple_chat_conversation_rename_user(chat, nick, args[0]);
1114 chats = chats->next;
1117 im = purple_conversations_find_im_with_account(nick,
1118 irc->account);
1119 if (im != NULL)
1120 purple_conversation_set_name(PURPLE_CONVERSATION(im), args[0]);
1122 g_free(nick);
1125 void irc_msg_badnick(struct irc_conn *irc, const char *name, const char *from, char **args)
1127 PurpleConnection *gc = purple_account_get_connection(irc->account);
1128 if (purple_connection_get_state(gc) == PURPLE_CONNECTION_CONNECTED) {
1129 purple_notify_error(gc, _("Invalid nickname"), _("Invalid "
1130 "nickname"), _("Your selected nickname was rejected by "
1131 "the server. It probably contains invalid characters."),
1132 purple_request_cpar_from_connection(gc));
1134 } else {
1135 purple_connection_take_error(gc, g_error_new_literal(
1136 PURPLE_CONNECTION_ERROR,
1137 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS,
1138 _("Your selected account name was rejected by the server. It probably contains invalid characters.")));
1142 void irc_msg_nickused(struct irc_conn *irc, const char *name, const char *from, char **args)
1144 char *newnick, *buf, *end;
1145 PurpleConnection *gc = purple_account_get_connection(irc->account);
1147 if (gc && purple_connection_get_state(gc) == PURPLE_CONNECTION_CONNECTED) {
1148 /* We only want to do the following dance if the connection
1149 has not been successfully completed. If it has, just
1150 notify the user that their /nick command didn't go. */
1151 buf = g_strdup_printf(_("The nickname \"%s\" is already being used."),
1152 irc->reqnick);
1153 purple_notify_error(gc, _("Nickname in use"), _("Nickname in "
1154 "use"), buf, purple_request_cpar_from_connection(gc));
1155 g_free(buf);
1156 g_free(irc->reqnick);
1157 irc->reqnick = NULL;
1158 return;
1161 if (strlen(args[1]) < strlen(irc->reqnick) || irc->nickused)
1162 newnick = g_strdup(args[1]);
1163 else
1164 newnick = g_strdup_printf("%s0", args[1]);
1165 end = newnick + strlen(newnick) - 1;
1166 /* try fallbacks */
1167 if((*end < '9') && (*end >= '1')) {
1168 *end = *end + 1;
1169 } else *end = '1';
1171 g_free(irc->reqnick);
1172 irc->reqnick = newnick;
1173 irc->nickused = TRUE;
1175 purple_connection_set_display_name(
1176 purple_account_get_connection(irc->account), newnick);
1178 buf = irc_format(irc, "vn", "NICK", newnick);
1179 irc_send(irc, buf);
1180 g_free(buf);
1183 void irc_msg_notice(struct irc_conn *irc, const char *name, const char *from, char **args)
1185 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], TRUE);
1188 void irc_msg_nochangenick(struct irc_conn *irc, const char *name, const char *from, char **args)
1190 PurpleConnection *gc = purple_account_get_connection(irc->account);
1192 g_return_if_fail(gc);
1194 purple_notify_error(gc, _("Cannot change nick"),
1195 _("Could not change nick"), args[2],
1196 purple_request_cpar_from_connection(gc));
1199 void irc_msg_part(struct irc_conn *irc, const char *name, const char *from, char **args)
1201 PurpleConnection *gc = purple_account_get_connection(irc->account);
1202 PurpleChatConversation *chat;
1203 char *nick, *msg, *channel;
1205 g_return_if_fail(gc);
1207 /* Undernet likes to :-quote the channel name, for no good reason
1208 * that I can see. This catches that. */
1209 channel = (args[0][0] == ':') ? &args[0][1] : args[0];
1211 chat = purple_conversations_find_chat_with_account(channel, irc->account);
1212 if (!chat) {
1213 purple_debug(PURPLE_DEBUG_INFO, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", channel);
1214 return;
1217 nick = irc_mask_nick(from);
1218 if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) {
1219 char *escaped = args[1] ? g_markup_escape_text(args[1], -1) : NULL;
1220 msg = g_strdup_printf(_("You have parted the channel%s%s"),
1221 (args[1] && *args[1]) ? ": " : "",
1222 (escaped && *escaped) ? escaped : "");
1223 g_free(escaped);
1224 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat), msg, 0);
1225 g_free(msg);
1226 purple_serv_got_chat_left(gc, purple_chat_conversation_get_id(chat));
1227 } else {
1228 msg = args[1] ? irc_mirc2txt(args[1]) : NULL;
1229 purple_chat_conversation_remove_user(chat, nick, msg);
1230 g_free(msg);
1232 g_free(nick);
1235 void irc_msg_ping(struct irc_conn *irc, const char *name, const char *from, char **args)
1237 char *buf;
1239 buf = irc_format(irc, "v:", "PONG", args[0]);
1240 irc_send(irc, buf);
1241 g_free(buf);
1244 void irc_msg_pong(struct irc_conn *irc, const char *name, const char *from, char **args)
1246 PurpleConversation *convo;
1247 PurpleConnection *gc;
1248 char **parts, *msg;
1249 time_t oldstamp;
1251 parts = g_strsplit(args[1], " ", 2);
1253 if (!parts[0] || !parts[1]) {
1254 g_strfreev(parts);
1255 return;
1258 if (sscanf(parts[1], "%lu", &oldstamp) != 1) {
1259 msg = g_strdup(_("Error: invalid PONG from server"));
1260 } else {
1261 msg = g_strdup_printf(_("PING reply -- Lag: %lu seconds"), time(NULL) - oldstamp);
1264 convo = purple_conversations_find_with_account(parts[0], irc->account);
1265 g_strfreev(parts);
1266 if (convo) {
1267 purple_conversation_write_system_message(convo, msg, PURPLE_MESSAGE_NO_LOG);
1268 } else {
1269 gc = purple_account_get_connection(irc->account);
1270 if (!gc) {
1271 g_free(msg);
1272 return;
1274 purple_notify_info(gc, NULL, "PONG", msg,
1275 purple_request_cpar_from_connection(gc));
1277 g_free(msg);
1280 void irc_msg_privmsg(struct irc_conn *irc, const char *name, const char *from, char **args)
1282 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], FALSE);
1285 static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name, const char *from, const char *to, const char *rawmsg, gboolean notice)
1287 PurpleConnection *gc = purple_account_get_connection(irc->account);
1288 PurpleChatConversation *chat;
1289 char *tmp;
1290 char *msg;
1291 char *nick;
1293 if (!gc)
1294 return;
1296 nick = irc_mask_nick(from);
1297 tmp = irc_parse_ctcp(irc, nick, to, rawmsg, notice);
1298 if (!tmp) {
1299 g_free(nick);
1300 return;
1303 msg = irc_escape_privmsg(tmp, -1);
1304 g_free(tmp);
1306 tmp = irc_mirc2html(msg);
1307 g_free(msg);
1308 msg = tmp;
1309 if (notice) {
1310 tmp = g_strdup_printf("(notice) %s", msg);
1311 g_free(msg);
1312 msg = tmp;
1315 if (!purple_utf8_strcasecmp(to, purple_connection_get_display_name(gc))) {
1316 purple_serv_got_im(gc, nick, msg, 0, time(NULL));
1317 } else {
1318 chat = purple_conversations_find_chat_with_account(irc_nick_skip_mode(irc, to), irc->account);
1319 if (chat) {
1320 purple_serv_got_chat_in(gc, purple_chat_conversation_get_id(chat),
1321 nick, PURPLE_MESSAGE_RECV, msg, time(NULL));
1322 } else
1323 purple_debug_error("irc", "Got a %s on %s, which does not exist\n",
1324 notice ? "NOTICE" : "PRIVMSG", to);
1326 g_free(msg);
1327 g_free(nick);
1330 void irc_msg_regonly(struct irc_conn *irc, const char *name, const char *from, char **args)
1332 PurpleConnection *gc = purple_account_get_connection(irc->account);
1333 char *msg;
1335 g_return_if_fail(gc);
1337 if (purple_conversations_find_chat_with_account(args[1], irc->account)) {
1338 /* This is a channel we're already in; for some reason,
1339 * freenode feels the need to notify us that in some
1340 * hypothetical other situation this might not have
1341 * succeeded. Suppress that. */
1342 return;
1345 msg = g_strdup_printf(_("Cannot join %s: Registration is required."), args[1]);
1346 purple_notify_error(gc, _("Cannot join channel"), msg, args[2],
1347 purple_request_cpar_from_connection(gc));
1348 g_free(msg);
1351 void irc_msg_quit(struct irc_conn *irc, const char *name, const char *from, char **args)
1353 PurpleConnection *gc = purple_account_get_connection(irc->account);
1354 struct irc_buddy *ib;
1355 char *data[2];
1357 g_return_if_fail(gc);
1359 data[0] = irc_mask_nick(from);
1360 data[1] = args[0];
1361 /* XXX this should have an API, I shouldn't grab this directly */
1362 g_slist_foreach(purple_connection_get_active_chats(gc),
1363 (GFunc)irc_chat_remove_buddy, data);
1365 if ((ib = g_hash_table_lookup(irc->buddies, data[0])) != NULL) {
1366 ib->new_online_status = FALSE;
1367 irc_buddy_status(data[0], ib, irc);
1369 g_free(data[0]);
1371 return;
1374 void irc_msg_unavailable(struct irc_conn *irc, const char *name, const char *from, char **args)
1376 PurpleConnection *gc = purple_account_get_connection(irc->account);
1378 purple_notify_error(gc, NULL, _("Nick or channel is temporarily "
1379 "unavailable."), args[1],
1380 purple_request_cpar_from_connection(gc));
1383 void irc_msg_wallops(struct irc_conn *irc, const char *name, const char *from, char **args)
1385 PurpleConnection *gc = purple_account_get_connection(irc->account);
1386 char *nick, *msg;
1388 g_return_if_fail(gc);
1390 nick = irc_mask_nick(from);
1391 msg = g_strdup_printf (_("Wallops from %s"), nick);
1392 g_free(nick);
1393 purple_notify_info(gc, NULL, msg, args[0],
1394 purple_request_cpar_from_connection(gc));
1395 g_free(msg);
1398 #ifdef HAVE_CYRUS_SASL
1399 static int
1400 irc_sasl_cb_secret(sasl_conn_t *conn, void *ctx, int id, sasl_secret_t **secret)
1402 struct irc_conn *irc = ctx;
1403 sasl_secret_t *sasl_secret;
1404 const char *pw;
1405 size_t len;
1407 pw = purple_connection_get_password(purple_account_get_connection(
1408 irc->account));
1410 if (!conn || !secret || id != SASL_CB_PASS)
1411 return SASL_BADPARAM;
1413 len = strlen(pw);
1414 /* Not an off-by-one because sasl_secret_t defines char data[1] */
1415 /* TODO: This can probably be moved to glib's allocator */
1416 sasl_secret = malloc(sizeof(sasl_secret_t) + len);
1417 if (!sasl_secret)
1418 return SASL_NOMEM;
1420 sasl_secret->len = len;
1421 strcpy((char*)sasl_secret->data, pw);
1423 *secret = sasl_secret;
1424 return SASL_OK;
1427 static int
1428 irc_sasl_cb_log(void *context, int level, const char *message)
1430 if(level <= SASL_LOG_TRACE)
1431 purple_debug_info("sasl", "%s\n", message);
1433 return SASL_OK;
1436 static int
1437 irc_sasl_cb_simple(void *ctx, int id, const char **res, unsigned *len)
1439 struct irc_conn *irc = ctx;
1440 PurpleConnection *gc = purple_account_get_connection(irc->account);
1442 switch(id) {
1443 case SASL_CB_AUTHNAME:
1444 *res = purple_connection_get_display_name(gc);
1445 break;
1446 case SASL_CB_USER:
1447 *res = "";
1448 break;
1449 default:
1450 return SASL_BADPARAM;
1452 if (len) *len = strlen((char *)*res);
1453 return SASL_OK;
1456 static void
1457 irc_auth_start_cyrus(struct irc_conn *irc)
1459 int ret = 0;
1460 char *buf;
1461 sasl_security_properties_t secprops;
1462 PurpleAccount *account = irc->account;
1463 PurpleConnection *gc = purple_account_get_connection(account);
1465 gboolean again = FALSE;
1467 /* Set up security properties and options */
1468 secprops.min_ssf = 0;
1469 secprops.security_flags = SASL_SEC_NOANONYMOUS;
1471 if (!G_IS_TLS_CONNECTION(irc->conn)) {
1472 gboolean plaintext;
1474 secprops.max_ssf = -1;
1475 secprops.maxbufsize = 4096;
1476 plaintext = purple_account_get_bool(account, "auth_plain_in_clear", FALSE);
1477 if (!plaintext)
1478 secprops.security_flags |= SASL_SEC_NOPLAINTEXT;
1479 } else {
1480 secprops.max_ssf = 0;
1481 secprops.maxbufsize = 0;
1484 secprops.property_names = 0;
1485 secprops.property_values = 0;
1487 do {
1488 again = FALSE;
1490 ret = sasl_client_new("irc", irc->server, NULL, NULL, irc->sasl_cb, 0, &irc->sasl_conn);
1492 if (ret != SASL_OK) {
1493 purple_debug_error("irc", "sasl_client_new failed: %d\n", ret);
1494 purple_connection_take_error(gc, g_error_new(
1495 PURPLE_CONNECTION_ERROR,
1496 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1497 ("Failed to initialize SASL authentication: %s"),
1498 sasl_errdetail(irc->sasl_conn)));
1499 return;
1502 sasl_setprop(irc->sasl_conn, SASL_AUTH_EXTERNAL, purple_account_get_username(irc->account));
1503 sasl_setprop(irc->sasl_conn, SASL_SEC_PROPS, &secprops);
1505 ret = sasl_client_start(irc->sasl_conn, irc->sasl_mechs->str, NULL, NULL, NULL, &irc->current_mech);
1507 switch (ret) {
1508 case SASL_OK:
1509 case SASL_CONTINUE:
1510 irc->mech_works = FALSE;
1511 break;
1512 case SASL_NOMECH:
1513 purple_connection_take_error(gc,
1514 g_error_new_literal(
1515 PURPLE_CONNECTION_ERROR,
1516 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1517 _("SASL authentication failed: No worthy authentication mechanisms found.")));
1519 irc_sasl_finish(irc);
1520 return;
1521 case SASL_BADPARAM:
1522 case SASL_NOMEM:
1523 purple_connection_take_error(gc, g_error_new(
1524 PURPLE_CONNECTION_ERROR,
1525 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1526 _("SASL authentication failed: %s"),
1527 sasl_errdetail(irc->sasl_conn)));
1529 irc_sasl_finish(irc);
1530 return;
1531 default:
1532 purple_debug_error("irc", "sasl_client_start failed: %s\n", sasl_errdetail(irc->sasl_conn));
1534 if (irc->current_mech && *irc->current_mech) {
1535 char *pos;
1536 if ((pos = strstr(irc->sasl_mechs->str, irc->current_mech))) {
1537 size_t index = pos - irc->sasl_mechs->str;
1538 g_string_erase(irc->sasl_mechs, index, strlen(irc->current_mech));
1540 /* Remove space which separated this mech from the next */
1541 if ((irc->sasl_mechs->str)[index] == ' ') {
1542 g_string_erase(irc->sasl_mechs, index, 1);
1546 again = TRUE;
1548 irc_sasl_finish(irc);
1550 } while (again);
1552 purple_debug_info("irc", "Using SASL: %s\n", irc->current_mech);
1554 buf = irc_format(irc, "vv", "AUTHENTICATE", irc->current_mech);
1555 irc_send(irc, buf);
1556 g_free(buf);
1559 /* SASL authentication */
1560 void
1561 irc_msg_cap(struct irc_conn *irc, const char *name, const char *from, char **args)
1563 int ret = 0;
1564 int id = 0;
1565 PurpleConnection *gc = purple_account_get_connection(irc->account);
1566 const char *mech_list = NULL;
1568 if (strncmp(args[2], "sasl ", 6))
1569 return;
1570 if (strncmp(args[1], "ACK", 4)) {
1571 purple_connection_take_error(gc, g_error_new_literal(
1572 PURPLE_CONNECTION_ERROR,
1573 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1574 _("SASL authentication failed: Server does not support SASL authentication.")));
1576 irc_sasl_finish(irc);
1577 return;
1580 if ((ret = sasl_client_init(NULL)) != SASL_OK) {
1581 purple_connection_take_error(gc, g_error_new_literal(
1582 PURPLE_CONNECTION_ERROR,
1583 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1584 _("SASL authentication failed: Initializing SASL failed.")));
1585 return;
1588 irc->sasl_cb = g_new0(sasl_callback_t, 5);
1590 irc->sasl_cb[id].id = SASL_CB_AUTHNAME;
1591 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_simple; /* sasl_getsimple_t */
1592 irc->sasl_cb[id].context = (void *)irc;
1593 id++;
1595 irc->sasl_cb[id].id = SASL_CB_USER;
1596 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_simple; /* sasl_getsimple_t */
1597 irc->sasl_cb[id].context = (void *)irc;
1598 id++;
1600 irc->sasl_cb[id].id = SASL_CB_PASS;
1601 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_secret; /* sasl_getsecret_t */
1602 irc->sasl_cb[id].context = (void *)irc;
1603 id++;
1605 irc->sasl_cb[id].id = SASL_CB_LOG;
1606 irc->sasl_cb[id].proc = (int (*)(void))irc_sasl_cb_log; /* sasl_log_t */
1607 irc->sasl_cb[id].context = (void *)irc;
1608 id++;
1610 irc->sasl_cb[id].id = SASL_CB_LIST_END;
1612 /* We need to do this to be able to list the mechanisms. */
1613 ret = sasl_client_new("irc", irc->server, NULL, NULL, irc->sasl_cb, 0, &irc->sasl_conn);
1615 sasl_listmech(irc->sasl_conn, NULL, "", " ", "", &mech_list, NULL, NULL);
1616 purple_debug_info("irc", "SASL: we have available: %s\n", mech_list);
1618 if (ret != SASL_OK) {
1619 purple_debug_error("irc", "sasl_client_new failed: %d\n", ret);
1621 purple_connection_take_error(gc, g_error_new(
1622 PURPLE_CONNECTION_ERROR,
1623 PURPLE_CONNECTION_ERROR_OTHER_ERROR,
1624 _("Failed to initialize SASL authentication: %s"),
1625 sasl_errdetail(irc->sasl_conn)));
1627 return;
1630 irc->sasl_mechs = g_string_new(mech_list);
1632 irc_auth_start_cyrus(irc);
1635 void
1636 irc_msg_auth(struct irc_conn *irc, char *arg)
1638 PurpleConnection *gc = purple_account_get_connection(irc->account);
1639 char *buf, *authinfo;
1640 char *serverin = NULL;
1641 gsize serverinlen = 0;
1642 const gchar *c_out;
1643 unsigned int clen;
1644 int ret;
1646 irc->mech_works = TRUE;
1648 if (!arg)
1649 return;
1651 if (arg[0] != '+')
1652 serverin = (char *)purple_base64_decode(arg, &serverinlen);
1654 ret = sasl_client_step(irc->sasl_conn, serverin, serverinlen,
1655 NULL, &c_out, &clen);
1657 if (ret != SASL_OK && ret != SASL_CONTINUE) {
1658 purple_connection_take_error(gc, g_error_new(
1659 PURPLE_CONNECTION_ERROR,
1660 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1661 _("SASL authentication failed: %s"),
1662 sasl_errdetail(irc->sasl_conn)));
1664 irc_sasl_finish(irc);
1665 g_free(serverin);
1666 return;
1669 if (clen > 0)
1670 authinfo = purple_base64_encode((const guchar*)c_out, clen);
1671 else
1672 authinfo = g_strdup("+");
1674 buf = irc_format(irc, "vv", "AUTHENTICATE", authinfo);
1675 irc_send(irc, buf);
1676 g_free(buf);
1677 g_free(authinfo);
1678 g_free(serverin);
1681 void
1682 irc_msg_authok(struct irc_conn *irc, const char *name, const char *from, char **args)
1684 char *buf;
1686 sasl_dispose(&irc->sasl_conn);
1687 irc->sasl_conn = NULL;
1688 purple_debug_info("irc", "Succesfully authenticated using SASL.\n");
1690 /* Finish auth session */
1691 buf = irc_format(irc, "vv", "CAP", "END");
1692 irc_send(irc, buf);
1693 g_free(buf);
1696 void
1697 irc_msg_authtryagain(struct irc_conn *irc, const char *name, const char *from, char **args)
1699 PurpleConnection *gc = purple_account_get_connection(irc->account);
1701 /* We already received at least one AUTHENTICATE reply from the
1702 * server. This suggests it supports this mechanism, but the
1703 * password was incorrect. It would be better to abort and inform
1704 * the user than to try again with a different mechanism, so they
1705 * aren't told the server supports no worthy mechanisms.
1707 if (irc->mech_works) {
1708 purple_connection_take_error(gc, g_error_new_literal(
1709 PURPLE_CONNECTION_ERROR,
1710 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
1711 _("Incorrect Password")));
1713 irc_sasl_finish(irc);
1715 return;
1718 if (irc->current_mech) {
1719 char *pos;
1720 if ((pos = strstr(irc->sasl_mechs->str, irc->current_mech))) {
1721 size_t index = pos - irc->sasl_mechs->str;
1722 g_string_erase(irc->sasl_mechs, index, strlen(irc->current_mech));
1724 /* Remove space which separated this mech from the next */
1725 if ((irc->sasl_mechs->str)[index] == ' ') {
1726 g_string_erase(irc->sasl_mechs, index, 1);
1730 if (*irc->sasl_mechs->str) {
1731 sasl_dispose(&irc->sasl_conn);
1733 purple_debug_info("irc", "Now trying with %s\n", irc->sasl_mechs->str);
1734 irc_auth_start_cyrus(irc);
1735 } else {
1736 purple_connection_take_error(gc, g_error_new_literal(
1737 PURPLE_CONNECTION_ERROR,
1738 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
1739 _("SASL authentication failed: No worthy mechanisms found")));
1741 irc_sasl_finish(irc);
1745 void
1746 irc_msg_authfail(struct irc_conn *irc, const char *name, const char *from, char **args)
1748 PurpleConnection *gc = purple_account_get_connection(irc->account);
1750 /* Only show an error if we did not abort ourselves. */
1751 if (irc->sasl_conn) {
1752 purple_debug_info("irc", "SASL authentication failed: %s", sasl_errdetail(irc->sasl_conn));
1754 purple_connection_take_error(gc, g_error_new_literal(
1755 PURPLE_CONNECTION_ERROR,
1756 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
1757 _("Incorrect Password")));
1760 irc_sasl_finish(irc);
1763 static void
1764 irc_sasl_finish(struct irc_conn *irc)
1766 char *buf;
1768 sasl_dispose(&irc->sasl_conn);
1769 irc->sasl_conn = NULL;
1771 g_free(irc->sasl_cb);
1772 irc->sasl_cb = NULL;
1774 /* Auth failed, abort */
1775 buf = irc_format(irc, "vv", "CAP", "END");
1776 irc_send(irc, buf);
1777 g_free(buf);
1779 #endif