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.
31 #include "conversation.h"
32 #include "buddylist.h"
41 #ifdef HAVE_CYRUS_SASL
42 #include <sasl/sasl.h>
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
);
59 static char *irc_mask_nick(const char *mask
)
63 end
= strchr(mask
, '!');
67 buf
= g_strndup(mask
, end
- mask
);
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
);
85 if (purple_chat_conversation_has_user(chat
, data
[0]))
86 purple_chat_conversation_remove_user(chat
, data
[0], message
);
91 static void irc_connected(struct irc_conn
*irc
, const char *nick
)
96 PurpleAccount
*account
;
98 if ((gc
= purple_account_get_connection(irc
->account
)) == NULL
99 || PURPLE_CONNECTION_IS_CONNECTED(gc
))
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
));
121 g_hash_table_replace(irc
->buddies
, ib
->name
, ib
);
124 irc_blist_timeout(irc
);
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
)
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
, ' ');
142 /* Check for 3-digit numeric in second position */
145 || !isdigit(cur
[0]) || !isdigit(cur
[1])
146 || !isdigit(cur
[2])) {
149 /* Save the numeric for printing to the channel */
152 /* Don't advance cur if we're on the final iteration. */
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
165 tmp
= g_strndup(cur
, end
- cur
);
166 convname
= purple_utf8_salvage(tmp
);
169 /* Check for an existing conversation */
170 convo
= purple_conversations_find_with_account(convname
, irc
->account
);
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. */
187 /* We then print "numeric: remainder". */
188 clean
= purple_utf8_salvage(end
);
189 tmp
= g_strdup_printf("%.3s: %s", numeric
, clean
);
191 purple_conversation_write_system_message(convo
, tmp
,
192 PURPLE_MESSAGE_NO_LOG
| PURPLE_MESSAGE_RAW
|
193 PURPLE_MESSAGE_NO_LINKIFY
);
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
);
204 void irc_msg_features(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
209 features
= g_strsplit(args
[1], " ", -1);
210 for (i
= 0; features
[i
]; i
++) {
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
;
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
);
244 gc
= purple_account_get_connection(irc
->account
);
246 msg
= g_markup_escape_text(args
[2], -1);
247 purple_serv_got_im(gc
, args
[1], msg
, PURPLE_MESSAGE_AUTO_RESP
, time(NULL
));
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")) {
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
);
280 msg
= g_strdup_printf(_("Ban on %s"), args
[2]);
283 purple_conversation_write_system_message(
284 PURPLE_CONVERSATION(chat
), msg
, PURPLE_MESSAGE_NO_LOG
);
286 purple_debug_info("irc", "%s\n", msg
);
289 } else if (purple_strequal(name
, "368")) {
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
);
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
));
311 void irc_msg_banfull(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
313 PurpleChatConversation
*chat
;
316 chat
= purple_conversations_find_chat_with_account(args
[1], irc
->account
);
320 nick
= g_markup_escape_text(args
[2], -1);
321 buf
= g_strdup_printf(_("Cannot ban %s: banlist is full"), nick
);
323 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
),
324 buf
, PURPLE_MESSAGE_NO_LOG
);
328 void irc_msg_chanmode(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
330 PurpleChatConversation
*chat
;
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 */
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);
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"
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
);
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]);
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]);
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
;
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"
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
);
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
);
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
);
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
);
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
;
476 char *cur
, *userhost
, *realname
;
478 PurpleChatUserFlags flags
;
480 chat
= purple_conversations_find_chat_with_account(args
[1], irc
->account
);
482 purple_debug(PURPLE_DEBUG_ERROR
, "irc","Got a WHO response for %s, which doesn't exist\n", args
[1]);
486 cb
= purple_chat_conversation_find_user(chat
, args
[5]);
488 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Got a WHO response for %s who isn't a buddy.\n", args
[5]);
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
++) {
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.
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
)
526 if (purple_strequal(name
, "321")) {
527 purple_roomlist_set_in_progress(irc
->roomlist
, TRUE
);
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
;
538 if (purple_strequal(name
, "322")) {
539 PurpleRoomlistRoom
*room
;
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
);
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")) {
564 topic
= irc_mirc2txt (args
[1]);
567 topic
= irc_mirc2txt (args
[2]);
570 chat
= purple_conversations_find_chat_with_account(chan
, irc
->account
);
572 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Got a topic for %s, which doesn't exist\n", chan
);
577 /* If this is an interactive update, print it out */
578 tmp
= g_markup_escape_text(topic
, -1);
579 tmp2
= purple_markup_linkify(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
)))
586 nick
= irc_mask_nick(from
);
587 nick_esc
= g_markup_escape_text(nick
, -1);
588 purple_chat_conversation_set_topic(chat
, nick
, topic
);
590 msg
= g_strdup_printf(_("%s has changed the topic to: %s"), nick_esc
, tmp2
);
592 msg
= g_strdup_printf(_("%s has cleared the topic."), nick_esc
);
595 purple_conversation_write_system_message(
596 PURPLE_CONVERSATION(chat
), msg
, 0);
600 char *chan_esc
= g_markup_escape_text(chan
, -1);
601 msg
= g_strdup_printf(_("The topic for %s is: %s"), chan_esc
, tmp2
);
603 purple_chat_conversation_set_topic(chat
, NULL
, topic
);
604 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
), msg
, 0);
611 void irc_msg_topicinfo(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
613 PurpleChatConversation
*chat
;
616 char *msg
, *timestamp
, *datestamp
;
618 chat
= purple_conversations_find_chat_with_account(args
[1], irc
->account
);
620 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Got topic info for %s, which doesn't exist\n", args
[1]);
624 t
= (time_t)atol(args
[3]);
626 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Got apparently nonsensical topic timestamp %s\n", args
[3]);
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
);
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
);
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
));
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
);
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
);
669 names
= cur
= g_string_free(irc
->names
, FALSE
);
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
);
675 } else if (cur
!= NULL
) {
680 PurpleChatUserFlags f
= PURPLE_CHAT_USER_NONE
;
681 end
= strchr(cur
, ' ');
683 end
= cur
+ strlen(cur
);
685 f
= PURPLE_CHAT_USER_OP
;
687 } else if (*cur
== '%') {
688 f
= PURPLE_CHAT_USER_HALFOP
;
690 } else if(*cur
== '+') {
691 f
= PURPLE_CHAT_USER_VOICE
;
693 } else if(irc
->mode_chars
694 && strchr(irc
->mode_chars
, *cur
)) {
696 f
= PURPLE_CHAT_USER_FOUNDER
;
699 tmp
= g_strndup(cur
, end
- cur
);
700 users
= g_list_prepend(users
, tmp
);
701 flags
= g_list_prepend(flags
, GINT_TO_POINTER(f
));
710 purple_chat_conversation_add_users(PURPLE_CHAT_CONVERSATION(convo
), users
, NULL
, flags
, FALSE
);
712 for (l
= users
; l
!= NULL
; l
= l
->next
)
719 g_object_set_data(G_OBJECT(convo
), IRC_NAMES_FLAG
,
720 GINT_TO_POINTER(TRUE
));
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
)
737 if (purple_strequal(name
, "375")) {
739 g_string_free(irc
->motd
, TRUE
);
742 irc
->motd
= g_string_new("");
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]);
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). */
754 g_string_free(irc
->motd
, TRUE
);
758 irc_connected(irc
, args
[0]);
763 purple_debug_error("irc", "IRC server sent MOTD without STARTMOTD\n");
770 escaped
= g_markup_escape_text(args
[1], -1);
771 g_string_append_printf(irc
->motd
, "%s<br>", 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
);
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
);
810 if ((gc
= purple_account_get_connection(irc
->account
)) == NULL
)
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
);
829 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
), args
[2],
830 PURPLE_MESSAGE_NO_LOG
);
832 if ((gc
= purple_account_get_connection(irc
->account
)) == NULL
)
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]);
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
);
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
;
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
);
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
);
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
));
893 void irc_msg_ison(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
896 struct irc_buddy
*ib
;
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
) {
904 ib
->new_online_status
= TRUE
;
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
);
923 if (ib
->online
&& !ib
->new_online_status
) {
924 purple_protocol_got_user_status(irc
->account
, name
, "offline", NULL
);
926 } else if (!ib
->online
&& ib
->new_online_status
) {
927 purple_protocol_got_user_status(irc
->account
, name
, "available", NULL
);
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
;
938 char *nick
, *userhost
, *buf
;
939 struct irc_buddy
*ib
;
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]);
950 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
953 purple_debug_error("irc", "tried to join %s but couldn't\n", args
[0]);
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]);
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); */
971 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
973 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "JOIN for %s failed\n", args
[0]);
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
);
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
);
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
);
1002 g_return_if_fail(gc
);
1004 nick
= irc_mask_nick(from
);
1007 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Received a KICK for unknown channel %s\n", args
[0]);
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);
1016 purple_serv_got_chat_left(gc
, purple_chat_conversation_get_id(chat
));
1018 buf
= g_strdup_printf(_("Kicked by %s (%s)"), nick
, args
[2]);
1019 purple_chat_conversation_remove_user(chat
, args
[1], buf
);
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 */
1034 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
1036 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "MODE received for %s, which we are not in\n", args
[0]);
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);
1047 PurpleChatUserFlags newflag
, flags
;
1048 char *mcur
, *cur
, *end
, *user
;
1049 gboolean add
= FALSE
;
1052 while (*cur
&& *mcur
) {
1053 if ((*mcur
== '+') || (*mcur
== '-')) {
1054 add
= (*mcur
== '+') ? TRUE
: FALSE
;
1058 end
= strchr(cur
, ' ');
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
;
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
;
1079 purple_chat_user_set_flags(cb
, flags
);
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
;
1099 char *nick
= irc_mask_nick(from
);
1101 irc
->nickused
= FALSE
;
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]);
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
,
1124 purple_conversation_set_name(PURPLE_CONVERSATION(im
), args
[0]);
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
));
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."),
1157 purple_notify_error(gc
, _("Nickname in use"), _("Nickname in "
1158 "use"), buf
, purple_request_cpar_from_connection(gc
));
1160 g_free(irc
->reqnick
);
1161 irc
->reqnick
= NULL
;
1165 if (strlen(args
[1]) < strlen(irc
->reqnick
) || irc
->nickused
)
1166 newnick
= g_strdup(args
[1]);
1168 newnick
= g_strdup_printf("%s0", args
[1]);
1169 end
= newnick
+ strlen(newnick
) - 1;
1171 if((*end
< '9') && (*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
);
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
);
1217 purple_debug(PURPLE_DEBUG_INFO
, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", channel
);
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
: "");
1228 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
), msg
, 0);
1230 purple_serv_got_chat_left(gc
, purple_chat_conversation_get_id(chat
));
1232 msg
= args
[1] ? irc_mirc2txt(args
[1]) : NULL
;
1233 purple_chat_conversation_remove_user(chat
, nick
, msg
);
1239 void irc_msg_ping(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1243 buf
= irc_format(irc
, "v:", "PONG", args
[0]);
1248 void irc_msg_pong(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1250 PurpleConversation
*convo
;
1251 PurpleConnection
*gc
;
1255 parts
= g_strsplit(args
[1], " ", 2);
1257 if (!parts
[0] || !parts
[1]) {
1262 if (sscanf(parts
[1], "%lu", &oldstamp
) != 1) {
1263 msg
= g_strdup(_("Error: invalid PONG from server"));
1265 msg
= g_strdup_printf(_("PING reply -- Lag: %lu seconds"), time(NULL
) - oldstamp
);
1268 convo
= purple_conversations_find_with_account(parts
[0], irc
->account
);
1271 purple_conversation_write_system_message(convo
, msg
, PURPLE_MESSAGE_NO_LOG
);
1273 gc
= purple_account_get_connection(irc
->account
);
1278 purple_notify_info(gc
, NULL
, "PONG", msg
,
1279 purple_request_cpar_from_connection(gc
));
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
;
1300 nick
= irc_mask_nick(from
);
1301 tmp
= irc_parse_ctcp(irc
, nick
, to
, rawmsg
, notice
);
1307 msg
= irc_escape_privmsg(tmp
, -1);
1310 tmp
= irc_mirc2html(msg
);
1314 tmp
= g_strdup_printf("(notice) %s", msg
);
1319 if (!purple_utf8_strcasecmp(to
, purple_connection_get_display_name(gc
))) {
1320 purple_serv_got_im(gc
, nick
, msg
, 0, time(NULL
));
1322 chat
= purple_conversations_find_chat_with_account(irc_nick_skip_mode(irc
, to
), irc
->account
);
1324 purple_serv_got_chat_in(gc
, purple_chat_conversation_get_id(chat
),
1325 nick
, PURPLE_MESSAGE_RECV
, msg
, time(NULL
));
1327 purple_debug_error("irc", "Got a %s on %s, which does not exist\n",
1328 notice
? "NOTICE" : "PRIVMSG", to
);
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
);
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. */
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
));
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
;
1361 g_return_if_fail(gc
);
1363 data
[0] = irc_mask_nick(from
);
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
);
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
);
1392 g_return_if_fail(gc
);
1394 nick
= irc_mask_nick(from
);
1395 msg
= g_strdup_printf (_("Wallops from %s"), nick
);
1397 purple_notify_info(gc
, NULL
, msg
, args
[0],
1398 purple_request_cpar_from_connection(gc
));
1402 #ifdef HAVE_CYRUS_SASL
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
;
1411 pw
= purple_connection_get_password(purple_account_get_connection(
1414 if (!conn
|| !secret
|| id
!= SASL_CB_PASS
)
1415 return SASL_BADPARAM
;
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
);
1424 sasl_secret
->len
= len
;
1425 strcpy((char*)sasl_secret
->data
, pw
);
1427 *secret
= sasl_secret
;
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
);
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
);
1447 case SASL_CB_AUTHNAME
:
1448 *res
= purple_connection_get_display_name(gc
);
1454 return SASL_BADPARAM
;
1456 if (len
) *len
= strlen((char *)*res
);
1461 irc_auth_start_cyrus(struct irc_conn
*irc
)
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
)) {
1478 secprops
.max_ssf
= -1;
1479 secprops
.maxbufsize
= 4096;
1480 plaintext
= purple_account_get_bool(account
, "auth_plain_in_clear", FALSE
);
1482 secprops
.security_flags
|= SASL_SEC_NOPLAINTEXT
;
1484 secprops
.max_ssf
= 0;
1485 secprops
.maxbufsize
= 0;
1488 secprops
.property_names
= 0;
1489 secprops
.property_values
= 0;
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
)));
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
);
1514 irc
->mech_works
= FALSE
;
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
);
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
);
1536 purple_debug_error("irc", "sasl_client_start failed: %s\n", sasl_errdetail(irc
->sasl_conn
));
1538 if (irc
->current_mech
&& *irc
->current_mech
) {
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);
1552 irc_sasl_finish(irc
);
1556 purple_debug_info("irc", "Using SASL: %s\n", irc
->current_mech
);
1558 buf
= irc_format(irc
, "vv", "AUTHENTICATE", irc
->current_mech
);
1563 /* SASL authentication */
1565 irc_msg_cap(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1569 PurpleConnection
*gc
= purple_account_get_connection(irc
->account
);
1570 const char *mech_list
= NULL
;
1574 if (strncmp(g_strstrip(args
[2]), "sasl", 5))
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
);
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.")));
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
;
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
;
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
;
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
;
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
)));
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
);
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;
1661 irc
->mech_works
= TRUE
;
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
);
1685 authinfo
= g_base64_encode((const guchar
*)c_out
, clen
);
1687 authinfo
= g_strdup("+");
1689 buf
= irc_format(irc
, "vv", "AUTHENTICATE", authinfo
);
1697 irc_msg_authenticate(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1699 irc_msg_auth(irc
, args
[0]);
1703 irc_msg_authok(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
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");
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
);
1739 if (irc
->current_mech
) {
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
);
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
);
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
);
1785 irc_sasl_finish(struct irc_conn
*irc
)
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");