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
= 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
)
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 (!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
;
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 (!strcmp(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 (!strcmp(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", !strcmp(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", !strcmp(name
, "314") ? "WHOWAS" : "WHOIS"
356 , args
[1], irc
->whois
.nick
);
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]);
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]);
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
;
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"
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
);
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 (!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
;
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 (!strcmp(name
, "321")) {
527 purple_roomlist_set_in_progress(irc
->roomlist
, TRUE
);
531 if (!strcmp(name
, "323")) {
532 purple_roomlist_set_in_progress(irc
->roomlist
, FALSE
);
533 g_object_unref(irc
->roomlist
);
534 irc
->roomlist
= NULL
;
538 if (!strcmp(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 (!strcmp(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 (!strcmp(name
, "topic")) {
582 const char *current_topic
= purple_chat_conversation_get_topic(chat
);
583 if (!(current_topic
!= NULL
&& strcmp(tmp2
, current_topic
) == 0))
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 (!strcmp(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 (!strcmp(name
, "375")) {
739 g_string_free(irc
->motd
, TRUE
);
740 irc
->motd
= g_string_new("");
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]);
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). */
752 g_string_free(irc
->motd
, TRUE
);
754 irc_connected(irc
, args
[0]);
759 purple_debug_error("irc", "IRC server sent MOTD without STARTMOTD\n");
766 escaped
= g_markup_escape_text(args
[1], -1);
767 g_string_append_printf(irc
->motd
, "%s<br>", 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
);
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
);
806 if ((gc
= purple_account_get_connection(irc
->account
)) == NULL
)
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
);
825 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
), args
[2],
826 PURPLE_MESSAGE_NO_LOG
);
828 if ((gc
= purple_account_get_connection(irc
->account
)) == NULL
)
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]);
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
);
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
;
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
);
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
);
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
));
889 void irc_msg_ison(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
892 struct irc_buddy
*ib
;
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
) {
900 ib
->new_online_status
= TRUE
;
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
);
919 if (ib
->online
&& !ib
->new_online_status
) {
920 purple_protocol_got_user_status(irc
->account
, name
, "offline", NULL
);
922 } else if (!ib
->online
&& ib
->new_online_status
) {
923 purple_protocol_got_user_status(irc
->account
, name
, "available", NULL
);
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
;
934 char *nick
, *userhost
, *buf
;
935 struct irc_buddy
*ib
;
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]);
946 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
949 purple_debug_error("irc", "tried to join %s but couldn't\n", args
[0]);
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]);
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); */
967 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
969 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "JOIN for %s failed\n", args
[0]);
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
);
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
);
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
);
998 g_return_if_fail(gc
);
1000 nick
= irc_mask_nick(from
);
1003 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "Received a KICK for unknown channel %s\n", args
[0]);
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);
1012 purple_serv_got_chat_left(gc
, purple_chat_conversation_get_id(chat
));
1014 buf
= g_strdup_printf(_("Kicked by %s (%s)"), nick
, args
[2]);
1015 purple_chat_conversation_remove_user(chat
, args
[1], buf
);
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 */
1030 chat
= purple_conversations_find_chat_with_account(args
[0], irc
->account
);
1032 purple_debug(PURPLE_DEBUG_ERROR
, "irc", "MODE received for %s, which we are not in\n", args
[0]);
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);
1043 PurpleChatUserFlags newflag
, flags
;
1044 char *mcur
, *cur
, *end
, *user
;
1045 gboolean add
= FALSE
;
1048 while (*cur
&& *mcur
) {
1049 if ((*mcur
== '+') || (*mcur
== '-')) {
1050 add
= (*mcur
== '+') ? TRUE
: FALSE
;
1054 end
= strchr(cur
, ' ');
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
;
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
;
1075 purple_chat_user_set_flags(cb
, flags
);
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
;
1095 char *nick
= irc_mask_nick(from
);
1097 irc
->nickused
= FALSE
;
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]);
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
,
1120 purple_conversation_set_name(PURPLE_CONVERSATION(im
), args
[0]);
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
));
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."),
1153 purple_notify_error(gc
, _("Nickname in use"), _("Nickname in "
1154 "use"), buf
, purple_request_cpar_from_connection(gc
));
1156 g_free(irc
->reqnick
);
1157 irc
->reqnick
= NULL
;
1161 if (strlen(args
[1]) < strlen(irc
->reqnick
) || irc
->nickused
)
1162 newnick
= g_strdup(args
[1]);
1164 newnick
= g_strdup_printf("%s0", args
[1]);
1165 end
= newnick
+ strlen(newnick
) - 1;
1167 if((*end
< '9') && (*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
);
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
);
1213 purple_debug(PURPLE_DEBUG_INFO
, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", channel
);
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
: "");
1224 purple_conversation_write_system_message(PURPLE_CONVERSATION(chat
), msg
, 0);
1226 purple_serv_got_chat_left(gc
, purple_chat_conversation_get_id(chat
));
1228 msg
= args
[1] ? irc_mirc2txt(args
[1]) : NULL
;
1229 purple_chat_conversation_remove_user(chat
, nick
, msg
);
1235 void irc_msg_ping(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1239 buf
= irc_format(irc
, "v:", "PONG", args
[0]);
1244 void irc_msg_pong(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1246 PurpleConversation
*convo
;
1247 PurpleConnection
*gc
;
1251 parts
= g_strsplit(args
[1], " ", 2);
1253 if (!parts
[0] || !parts
[1]) {
1258 if (sscanf(parts
[1], "%lu", &oldstamp
) != 1) {
1259 msg
= g_strdup(_("Error: invalid PONG from server"));
1261 msg
= g_strdup_printf(_("PING reply -- Lag: %lu seconds"), time(NULL
) - oldstamp
);
1264 convo
= purple_conversations_find_with_account(parts
[0], irc
->account
);
1267 purple_conversation_write_system_message(convo
, msg
, PURPLE_MESSAGE_NO_LOG
);
1269 gc
= purple_account_get_connection(irc
->account
);
1274 purple_notify_info(gc
, NULL
, "PONG", msg
,
1275 purple_request_cpar_from_connection(gc
));
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
;
1296 nick
= irc_mask_nick(from
);
1297 tmp
= irc_parse_ctcp(irc
, nick
, to
, rawmsg
, notice
);
1303 msg
= irc_escape_privmsg(tmp
, -1);
1306 tmp
= irc_mirc2html(msg
);
1310 tmp
= g_strdup_printf("(notice) %s", msg
);
1315 if (!purple_utf8_strcasecmp(to
, purple_connection_get_display_name(gc
))) {
1316 purple_serv_got_im(gc
, nick
, msg
, 0, time(NULL
));
1318 chat
= purple_conversations_find_chat_with_account(irc_nick_skip_mode(irc
, to
), irc
->account
);
1320 purple_serv_got_chat_in(gc
, purple_chat_conversation_get_id(chat
),
1321 nick
, PURPLE_MESSAGE_RECV
, msg
, time(NULL
));
1323 purple_debug_error("irc", "Got a %s on %s, which does not exist\n",
1324 notice
? "NOTICE" : "PRIVMSG", to
);
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
);
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. */
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
));
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
;
1357 g_return_if_fail(gc
);
1359 data
[0] = irc_mask_nick(from
);
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
);
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
);
1388 g_return_if_fail(gc
);
1390 nick
= irc_mask_nick(from
);
1391 msg
= g_strdup_printf (_("Wallops from %s"), nick
);
1393 purple_notify_info(gc
, NULL
, msg
, args
[0],
1394 purple_request_cpar_from_connection(gc
));
1398 #ifdef HAVE_CYRUS_SASL
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
;
1407 pw
= purple_connection_get_password(purple_account_get_connection(
1410 if (!conn
|| !secret
|| id
!= SASL_CB_PASS
)
1411 return SASL_BADPARAM
;
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
);
1420 sasl_secret
->len
= len
;
1421 strcpy((char*)sasl_secret
->data
, pw
);
1423 *secret
= sasl_secret
;
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
);
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
);
1443 case SASL_CB_AUTHNAME
:
1444 *res
= purple_connection_get_display_name(gc
);
1450 return SASL_BADPARAM
;
1452 if (len
) *len
= strlen((char *)*res
);
1457 irc_auth_start_cyrus(struct irc_conn
*irc
)
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
)) {
1474 secprops
.max_ssf
= -1;
1475 secprops
.maxbufsize
= 4096;
1476 plaintext
= purple_account_get_bool(account
, "auth_plain_in_clear", FALSE
);
1478 secprops
.security_flags
|= SASL_SEC_NOPLAINTEXT
;
1480 secprops
.max_ssf
= 0;
1481 secprops
.maxbufsize
= 0;
1484 secprops
.property_names
= 0;
1485 secprops
.property_values
= 0;
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
)));
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
);
1510 irc
->mech_works
= FALSE
;
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
);
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
);
1532 purple_debug_error("irc", "sasl_client_start failed: %s\n", sasl_errdetail(irc
->sasl_conn
));
1534 if (irc
->current_mech
&& *irc
->current_mech
) {
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);
1548 irc_sasl_finish(irc
);
1552 purple_debug_info("irc", "Using SASL: %s\n", irc
->current_mech
);
1554 buf
= irc_format(irc
, "vv", "AUTHENTICATE", irc
->current_mech
);
1559 /* SASL authentication */
1561 irc_msg_cap(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
1565 PurpleConnection
*gc
= purple_account_get_connection(irc
->account
);
1566 const char *mech_list
= NULL
;
1568 if (strncmp(args
[2], "sasl ", 6))
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
);
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.")));
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
;
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
;
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
;
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
;
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
)));
1630 irc
->sasl_mechs
= g_string_new(mech_list
);
1632 irc_auth_start_cyrus(irc
);
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;
1646 irc
->mech_works
= TRUE
;
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
);
1670 authinfo
= purple_base64_encode((const guchar
*)c_out
, clen
);
1672 authinfo
= g_strdup("+");
1674 buf
= irc_format(irc
, "vv", "AUTHENTICATE", authinfo
);
1682 irc_msg_authok(struct irc_conn
*irc
, const char *name
, const char *from
, char **args
)
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");
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
);
1718 if (irc
->current_mech
) {
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
);
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
);
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
);
1764 irc_sasl_finish(struct irc_conn
*irc
)
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");