/**
* @file msgs.c
*
* gaim
*
* Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "internal.h"
#include "conversation.h"
#include "blist.h"
#include "notify.h"
#include "util.h"
#include "debug.h"
#include "irc.h"
#include <stdio.h>
static char *irc_mask_nick(const char *mask);
static char *irc_mask_userhost(const char *mask);
static void irc_chat_remove_buddy(GaimConversation *convo, char *data[2]);
static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc);
static char *irc_mask_nick(const char *mask)
{
char *end, *buf;
end = strchr(mask, '!');
if (!end)
buf = g_strdup(mask);
else
buf = g_strndup(mask, end - mask);
return buf;
}
static char *irc_mask_userhost(const char *mask)
{
return g_strdup(strchr(mask, '!') + 1);
}
static void irc_chat_remove_buddy(GaimConversation *convo, char *data[2])
{
char *message = g_strdup_printf("quit: %s", data[1]);
if (gaim_conv_chat_find_user(GAIM_CONV_CHAT(convo), data[0]))
gaim_conv_chat_remove_user(GAIM_CONV_CHAT(convo), data[0], message);
g_free(message);
}
void irc_msg_default(struct irc_conn *irc, const char *name, const char *from, char **args)
{
gaim_debug(GAIM_DEBUG_INFO, "irc", "Unrecognized message: %s\n", args[0]);
}
void irc_msg_away(struct irc_conn *irc, const char *name, const char *from, char **args)
{
GaimConnection *gc;
if (!args || !args[1])
return;
if (irc->whois.nick && !gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
/* We're doing a whois, show this in the whois dialog */
irc_msg_whois(irc, name, from, args);
return;
}
gc = gaim_account_get_connection(irc->account);
if (gc)
serv_got_im(gc, args[1], args[2], GAIM_CONV_IM_AUTO_RESP, time(NULL));
}
void irc_msg_badmode(struct irc_conn *irc, const char *name, const char *from, char **args)
{
GaimConnection *gc = gaim_account_get_connection(irc->account);
if (!args || !args[1] || !gc)
return;
gaim_notify_error(gc, NULL, _("Bad mode"), args[1]);
}
void irc_msg_banned(struct irc_conn *irc, const char *name, const char *from, char **args)
{
GaimConnection *gc = gaim_account_get_connection(irc->account);
char *buf;
if (!args || !args[1] || !gc)
return;
buf = g_strdup_printf(_("You are banned from %s."), args[1]);
gaim_notify_error(gc, _("Banned"), _("Banned"), buf);
g_free(buf);
}
void irc_msg_chanmode(struct irc_conn *irc, const char *name, const char *from, char **args)
{
GaimConversation *convo;
char *buf;
if (!args || !args[1] || !args[2])
return;
convo = gaim_find_conversation_with_account(args[1], irc->account);
if (!convo) /* XXX punt on channels we are not in for now */
return;
buf = g_strdup_printf("mode for %s: %s %s", args[1], args[2], args[3] ? args[3] : "");
gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", buf, GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
g_free(buf);
return;
}
void irc_msg_whois(struct irc_conn *irc, const char *name, const char *from, char **args)
{
if (!irc->whois.nick) {
gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unexpected WHOIS reply for %s\n", args[1]);
return;
}
if (gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
gaim_debug(GAIM_DEBUG_WARNING, "irc", "Got WHOIS reply for %s while waiting for %s\n", args[1], irc->whois.nick);
return;
}
if (!strcmp(name, "301")) {
irc->whois.away = g_strdup(args[2]);
} else if (!strcmp(name, "311")) {
irc->whois.userhost = g_strdup_printf("%s@%s", args[2], args[3]);
irc->whois.name = g_strdup(args[5]);
} else if (!strcmp(name, "312")) {
irc->whois.server = g_strdup(args[2]);
irc->whois.serverinfo = g_strdup(args[3]);
} else if (!strcmp(name, "313")) {
irc->whois.ircop = 1;
} else if (!strcmp(name, "317")) {
irc->whois.idle = atoi(args[2]);
if (args[3])
irc->whois.signon = (time_t)atoi(args[3]);
} else if (!strcmp(name, "319")) {
irc->whois.channels = g_strdup(args[2]);
} else if (!strcmp(name, "320")) {
irc->whois.identified = 1;
}
}
void irc_msg_endwhois(struct irc_conn *irc, const char *name, const char *from, char **args)
{
GaimConnection *gc;
GString *info;
char buffer[256];
char *str;
if (!irc->whois.nick) {
gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unexpected End of WHOIS for %s\n", args[1]);
return;
}
if (gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
gaim_debug(GAIM_DEBUG_WARNING, "irc", "Received end of WHOIS for %s, expecting %s\n", args[1], irc->whois.nick);
return;
}
info = g_string_new("");
g_string_append_printf(info, _("<b>%s:</b> %s"), _("Nick"), args[1]);
g_string_append_printf(info, "%s%s<br>",
irc->whois.ircop ? _(" <i>(ircop)</i>") : "",
irc->whois.identified ? _(" <i>(identified)</i>") : "");
if (irc->whois.away) {
char *tmp = g_markup_escape_text(irc->whois.away, strlen(irc->whois.away));
g_free(irc->whois.away);
g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Away"), tmp);
g_free(tmp);
}
if (irc->whois.userhost) {
char *tmp = g_markup_escape_text(irc->whois.name, strlen(irc->whois.name));
g_free(irc->whois.name);
g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Username"), irc->whois.userhost);
g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Realname"), tmp);
g_free(irc->whois.userhost);
g_free(tmp);
}
if (irc->whois.server) {
g_string_append_printf(info, _("<b>%s:</b> %s"), _("Server"), irc->whois.server);
g_string_append_printf(info, " (%s)<br>", irc->whois.serverinfo);
g_free(irc->whois.server);
g_free(irc->whois.serverinfo);
}
if (irc->whois.channels) {
g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Currently on"), irc->whois.channels);
g_free(irc->whois.channels);
}
if (irc->whois.idle) {
gchar *timex = gaim_str_seconds_to_string(irc->whois.idle);
g_string_append_printf(info, _("<b>Idle for:</b> %s<br>"), timex);
g_free(timex);
g_string_append_printf(info, _("<b>%s:</b> %s"), _("Online since"), ctime(&irc->whois.signon));
}
if (!strcmp(irc->whois.nick, "Paco-Paco")) {
g_string_append_printf(info, _("<br><b>Defining adjective:</b> Glorious<br>"));
}
gc = gaim_account_get_connection(irc->account);
str = g_string_free(info, FALSE);
g_snprintf(buffer, sizeof(buffer),
_("Buddy Information for %s"), irc->whois.nick);
gaim_notify_userinfo(gc, irc->whois.nick, NULL, buffer, NULL, str, NULL, NULL);
g_free(str);
memset(&irc->whois, 0, sizeof(irc->whois));
}
void irc_msg_list(struct irc_conn *irc, const char *name, const char *from, char **args)
{
if (!irc->roomlist)
return;
if (!strcmp(name, "321")) {
gaim_roomlist_set_in_progress(irc->roomlist, TRUE);
return;
}
if (!strcmp(name, "323")) {
gaim_roomlist_set_in_progress(irc->roomlist, FALSE);
gaim_roomlist_unref(irc->roomlist);
irc->roomlist = NULL;
}
if (!strcmp(name, "322")) {
GaimRoomlistRoom *room;
if (!args[0] || !args[1] || !args[2] || !args[3])
return;
room = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, args[1], NULL);
gaim_roomlist_room_add_field(irc->roomlist, room, args[1]);
gaim_roomlist_room_add_field(irc->roomlist, room, GINT_TO_POINTER(strtol(args[2], NULL, 10)));
gaim_roomlist_room_add_field(irc->roomlist, room, args[3]);
gaim_roomlist_room_add(irc->roomlist, room);
}
}
void irc_msg_topic(struct irc_conn *irc, const ch