* @file log.c Logging API * Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * 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 static GSList *loggers = NULL; static GaimLogLogger *html_logger; static GaimLogLogger *txt_logger; static GaimLogLogger *old_logger; struct _gaim_logsize_user { static GHashTable *logsize_users = NULL; static void log_get_log_sets_common(GHashTable *sets); static gsize html_logger_write(GaimLog *log, GaimMessageFlags type, const char *from, time_t time, const char *message); static void html_logger_finalize(GaimLog *log); static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static GList *html_logger_list_syslog(GaimAccount *account); static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags); static int html_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags); static int old_logger_size (GaimLog *log); static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets); static void old_logger_finalize(GaimLog *log); static gsize txt_logger_write(GaimLog *log, const char *from, time_t time, const char *message); static void txt_logger_finalize(GaimLog *log); static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account); static GList *txt_logger_list_syslog(GaimAccount *account); static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags); static int txt_logger_total_size(GaimLogType type, const char *name, GaimAccount *account); /************************************************************************** * PUBLIC LOGGING FUNCTIONS *********************************************** **************************************************************************/ GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, GaimConversation *conv, time_t time, const struct tm *tm) GaimLog *log = g_new0(GaimLog, 1); log->name = g_strdup(gaim_normalize(account, name)); log->tm = g_new0(struct tm, 1); #ifdef HAVE_STRUCT_TM_TM_ZONE /* XXX: This is so wrong... */ if (log->tm->tm_zone != NULL) char *tmp = g_locale_from_utf8(log->tm->tm_zone, -1, NULL, NULL, NULL); log->tm->tm_zone = (const char *)tmp; /* Just shove the UTF-8 bytes in and hope... */ log->tm->tm_zone = (const char *)g_strdup(log->tm->tm_zone); log->logger = gaim_log_logger_get(); if (log->logger && log->logger->create) log->logger->create(log); void gaim_log_free(GaimLog *log) if (log->logger && log->logger->finalize) log->logger->finalize(log); #ifdef HAVE_STRUCT_TM_TM_ZONE /* XXX: This is so wrong... */ g_free((char *)log->tm->tm_zone); void gaim_log_write(GaimLog *log, GaimMessageFlags type, const char *from, time_t time, const char *message) struct _gaim_logsize_user *lu; gsize written, total = 0; g_return_if_fail(log->logger); g_return_if_fail(log->logger->write); written = (log->logger->write)(log, type, from, time, message); lu = g_new(struct _gaim_logsize_user, 1); lu->name = g_strdup(gaim_normalize(log->account, log->name)); lu->account = log->account; if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { total = GPOINTER_TO_INT(ptrsize); g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(total)); char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) g_return_val_if_fail(log && log->logger, NULL); char *ret = (log->logger->read)(log, flags ? flags : &mflags); gaim_str_strip_char(ret, '\r'); return g_strdup(_("<b><font color=\"red\">The logger has no read function</font></b>")); int gaim_log_get_size(GaimLog *log) g_return_val_if_fail(log && log->logger, 0); return log->logger->size(log); static guint _gaim_logsize_user_hash(struct _gaim_logsize_user *lu) return g_str_hash(lu->name); static guint _gaim_logsize_user_equal(struct _gaim_logsize_user *lu1, struct _gaim_logsize_user *lu2) return (lu1->account == lu2->account && (!strcmp(lu1->name, lu2->name))); static void _gaim_logsize_user_free_key(struct _gaim_logsize_user *lu) int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account) struct _gaim_logsize_user *lu; lu = g_new(struct _gaim_logsize_user, 1); lu->name = g_strdup(gaim_normalize(account, name)); if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { size = GPOINTER_TO_INT(ptrsize); for (n = loggers; n; n = n->next) { GaimLogLogger *logger = n->data; size += (logger->total_size)(type, name, account); } else if(logger->list) { GList *logs = (logger->list)(type, name, account); GaimLog *log = (GaimLog*)(logs->data); this_size += gaim_log_get_size(log); logs = g_list_delete_link(logs, logs); g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account) GaimPluginProtocolInfo *prpl_info; prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); prpl_name = prpl_info->list_icon(account, NULL); acct_name = g_strdup(gaim_escape_filename(gaim_normalize(account, gaim_account_get_username(account)))); if (type == GAIM_LOG_CHAT) { char *temp = g_strdup_printf("%s.chat", gaim_normalize(account, name)); target = gaim_escape_filename(temp); } else if(type == GAIM_LOG_SYSTEM) { target = gaim_escape_filename(gaim_normalize(account, name)); dir = g_build_filename(gaim_user_dir(), "logs", prpl_name, acct_name, target, NULL); /**************************************************************************** * LOGGER FUNCTIONS ********************************************************* ****************************************************************************/ static GaimLogLogger *current_logger = NULL; static void logger_pref_cb(const char *name, GaimPrefType type, gconstpointer value, gpointer data) if (!strcmp(logger->id, value)) { gaim_log_logger_set(logger); gaim_log_logger_set(txt_logger); GaimLogLogger *gaim_log_logger_new(const char *id, const char *name, int functions, ...) void(*create)(GaimLog *), gsize(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), void(*finalize)(GaimLog *), GList*(*list)(GaimLogType type, const char*, GaimAccount*), char*(*read)(GaimLog*, GaimLogReadFlags*), int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), GList*(*list_syslog)(GaimAccount *account), void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets)) g_return_val_if_fail(id != NULL, NULL); g_return_val_if_fail(name != NULL, NULL); g_return_val_if_fail(functions >= 1, NULL); logger = g_new0(GaimLogLogger, 1); logger->id = g_strdup(id); logger->name = g_strdup(name); va_start(args, functions); logger->create = va_arg(args, void *); logger->write = va_arg(args, void *); logger->finalize = va_arg(args, void *); logger->list = va_arg(args, void *); logger->read = va_arg(args, void *); logger->size = va_arg(args, void *); logger->total_size = va_arg(args, void *); logger->list_syslog = va_arg(args, void *); logger->get_log_sets = va_arg(args, void *); gaim_debug_info("log", "Dropping new functions for logger: %s (%s)\n", name, id); void gaim_log_logger_free(GaimLogLogger *logger) void gaim_log_logger_add (GaimLogLogger *logger) g_return_if_fail(logger); if (g_slist_find(loggers, logger)) loggers = g_slist_append(loggers, logger); void gaim_log_logger_remove (GaimLogLogger *logger) g_return_if_fail(logger); loggers = g_slist_remove(loggers, logger); void gaim_log_logger_set (GaimLogLogger *logger) g_return_if_fail(logger); GaimLogLogger *gaim_log_logger_get() GList *gaim_log_logger_get_options(void) for (n = loggers; n; n = n->next) { list = g_list_append(list, data->name); list = g_list_append(list, data->id); gint gaim_log_compare(gconstpointer y, gconstpointer z) return b->time - a->time; GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account) for (n = loggers; n; n = n->next) { GaimLogLogger *logger = n->data; logs = g_list_concat(logger->list(type, name, account), logs); return g_list_sort(logs, gaim_log_compare); gint gaim_log_set_compare(gconstpointer y, gconstpointer z) /* This logic seems weird at first... * If either account is NULL, we pretend the accounts are * equal. This allows us to detect duplicates that will * exist if one logger knows the account and another if (a->account != NULL && b->account != NULL) { ret = strcmp(gaim_account_get_username(a->account), gaim_account_get_username(b->account)); ret = strcmp(a->normalized_name, b->normalized_name); return (gint)b->type - (gint)a->type; log_set_hash(gconstpointer key) const GaimLogSet *set = key; /* The account isn't hashed because we need GaimLogSets with NULL accounts * to be found when we search by a GaimLogSet that has a non-NULL account * but the same type and name. */ return g_int_hash((gint *)&set->type) + g_str_hash(set->name); log_set_equal(gconstpointer a, gconstpointer b) /* I realize that the choices made for GList and GHashTable * make sense for those data types, but I wish the comparison * functions were compatible. */ return !gaim_log_set_compare(a, b); log_add_log_set_to_hash(GHashTable *sets, GaimLogSet *set) GaimLogSet *existing_set = g_hash_table_lookup(sets, set); if (existing_set == NULL) g_hash_table_insert(sets, set, set); else if (existing_set->account == NULL && set->account != NULL) g_hash_table_replace(sets, set, set); GHashTable *gaim_log_get_log_sets(void) GHashTable *sets = g_hash_table_new_full(log_set_hash, log_set_equal, (GDestroyNotify)gaim_log_set_free, NULL); /* Get the log sets from all the loggers. */ for (n = loggers; n; n = n->next) { GaimLogLogger *logger = n->data; if (!logger->get_log_sets) logger->get_log_sets(log_add_log_set_to_hash, sets); log_get_log_sets_common(sets); /* Return the GHashTable of unique GaimLogSets. */ void gaim_log_set_free(GaimLogSet *set) g_return_if_fail(set != NULL); if (set->normalized_name != set->name) g_free(set->normalized_name); GList *gaim_log_get_system_logs(GaimAccount *account) for (n = loggers; n; n = n->next) { GaimLogLogger *logger = n->data; if (!logger->list_syslog) logs = g_list_concat(logger->list_syslog(account), logs); return g_list_sort(logs, gaim_log_compare); /**************************************************************************** * LOG SUBSYSTEM ************************************************************ ****************************************************************************/ gaim_log_get_handle(void) void *handle = gaim_log_get_handle(); gaim_prefs_add_none("/core/logging"); gaim_prefs_add_bool("/core/logging/log_ims", FALSE); gaim_prefs_add_bool("/core/logging/log_chats", FALSE); gaim_prefs_add_bool("/core/logging/log_system", FALSE); gaim_prefs_add_string("/core/logging/format", "txt"); html_logger = gaim_log_logger_new("html", _("HTML"), 8, html_logger_list_syslog); gaim_log_logger_add(html_logger); txt_logger = gaim_log_logger_new("txt", _("Plain text"), 8, gaim_log_logger_add(txt_logger); old_logger = gaim_log_logger_new("old", _("Old Gaim"), 9, old_logger_get_log_sets); gaim_log_logger_add(old_logger); gaim_signal_register(handle, "log-timestamp", gaim_marshal_POINTER__POINTER_POINTER, gaim_value_new(GAIM_TYPE_POINTER), 2, gaim_value_new(GAIM_TYPE_SUBTYPE, gaim_value_new(GAIM_TYPE_BOXED, gaim_prefs_connect_callback(NULL, "/core/logging/format", gaim_prefs_trigger_callback("/core/logging/format"); logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, (GEqualFunc)_gaim_logsize_user_equal, (GDestroyNotify)_gaim_logsize_user_free_key, NULL); gaim_signals_unregister_by_instance(gaim_log_get_handle()); /**************************************************************************** * LOGGERS ****************************************************************** ****************************************************************************/ static char *log_get_timestamp(GaimLog *log, time_t when) struct tm tm = *(localtime(&when)); date = gaim_signal_emit_return_1(gaim_log_get_handle(), if (log->type == GAIM_LOG_SYSTEM || time(NULL) > when + 20*60) return g_strdup(gaim_date_format_long(&tm)); return g_strdup(gaim_time_format(&tm)); void gaim_log_common_writer(GaimLog *log, const char *ext) GaimLogCommonLoggerData *data = log->logger_data; dir = gaim_log_get_log_dir(log->type, log->name, log->account); gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); tm = localtime(&log->time); tz = gaim_escape_filename(gaim_utf8_strftime("%Z", tm)); date = gaim_utf8_strftime("%Y-%m-%d.%H%M%S%z", tm); filename = g_strdup_printf("%s%s%s", date, tz, ext ? ext : ""); path = g_build_filename(dir, filename, NULL); log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); data->file = g_fopen(path, "a"); gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", path); gaim_conversation_write(log->conv, NULL, _("Logging of this conversation failed."), GAIM_MESSAGE_ERROR, time(NULL)); GList *gaim_log_common_lister(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger) path = gaim_log_get_log_dir(type, name, account); if (!(dir = g_dir_open(path, 0, NULL))) while ((filename = g_dir_read_name(dir))) if (gaim_str_has_suffix(filename, ext) && strlen(filename) >= (17 + strlen(ext))) GaimLogCommonLoggerData *data; #if defined (HAVE_TM_GMTOFF) && defined (HAVE_STRUCT_TM_TM_ZONE) time_t stamp = gaim_str_to_time(gaim_unescape_filename(filename), FALSE, &tm, &tz_off, &rest); /* As zero is a valid offset, GAIM_NO_TZ_OFF means no offset was * provided. See util.h. Yes, it's kinda ugly. */ if (tz_off != GAIM_NO_TZ_OFF) tm.tm_gmtoff = tz_off - tm.tm_gmtoff; if (rest == NULL || (end = strchr(rest, '.')) == NULL || strchr(rest, ' ') != NULL) log = gaim_log_new(type, name, account, NULL, stamp, NULL); char *tmp = g_strndup(rest, end - rest); log = gaim_log_new(type, name, account, NULL, stamp, &tm); time_t stamp = gaim_str_to_time(filename, FALSE, &tm, NULL, NULL); log = gaim_log_new(type, name, account, NULL, stamp, &tm); log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); data->path = g_build_filename(path, filename, NULL); list = g_list_prepend(list, log); int gaim_log_common_total_sizer(GaimLogType type, const char *name, GaimAccount *account, const char *ext) path = gaim_log_get_log_dir(type, name, account); if (!(dir = g_dir_open(path, 0, NULL))) while ((filename = g_dir_read_name(dir))) if (gaim_str_has_suffix(filename, ext) && strlen(filename) >= (17 + strlen(ext))) char *tmp = g_build_filename(path, filename, NULL); gaim_debug_error("log", "Error stating log file: %s\n", tmp); int gaim_log_common_sizer(GaimLog *log) GaimLogCommonLoggerData *data = log->logger_data; if (!data->path || g_stat(data->path, &st)) /* This will build log sets for all loggers that use the common logger * functions because they use the same directory structure. */ static void log_get_log_sets_common(GHashTable *sets) gchar *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); GDir *log_dir = g_dir_open(log_path, 0, NULL); while ((protocol = g_dir_read_name(log_dir)) != NULL) { gchar *protocol_path = g_build_filename(log_path, protocol, NULL); gchar *protocol_unescaped; if ((protocol_dir = g_dir_open(protocol_path, 0, NULL)) == NULL) { /* Using g_strdup() to cover the one-in-a-million chance that a * prpl's list_icon function uses gaim_unescape_filename(). */ protocol_unescaped = g_strdup(gaim_unescape_filename(protocol)); /* Find all the accounts for protocol. */ for (account_iter = gaim_accounts_get_all() ; account_iter != NULL ; account_iter = account_iter->next) { GaimPluginProtocolInfo *prpl_info; prpl = gaim_find_prpl(gaim_account_get_protocol_id((GaimAccount *)account_iter->data)); prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); if (!strcmp(protocol_unescaped, prpl_info->list_icon((GaimAccount *)account_iter->data, NULL))) accounts = g_list_prepend(accounts, account_iter->data); g_free(protocol_unescaped); while ((username = g_dir_read_name(protocol_dir)) != NULL) { gchar *username_path = g_build_filename(protocol_path, username, NULL); const gchar *username_unescaped; GaimAccount *account = NULL; if ((username_dir = g_dir_open(username_path, 0, NULL)) == NULL) { /* Find the account for username in the list of accounts for protocol. */ username_unescaped = gaim_unescape_filename(username); for (account_iter = g_list_first(accounts) ; account_iter != NULL ; account_iter = account_iter->next) { if (!strcmp(((GaimAccount *)account_iter->data)->username, username_unescaped)) { account = account_iter->data; /* Don't worry about the cast, name will point to dynamically allocated memory shortly. */ while ((name = (gchar *)g_dir_read_name(username_dir)) != NULL) { GaimLogSet *set = g_new0(GaimLogSet, 1); /* Unescape the filename. */ name = g_strdup(gaim_unescape_filename(name)); /* Get the (possibly new) length of name. */ set->normalized_name = g_strdup(gaim_normalize(account, name)); /* Chat for .chat or .system at the end of the name to determine the type. */ gchar *tmp = &name[len - 7]; if (!strcmp(tmp, ".system")) { set->type = GAIM_LOG_SYSTEM; gchar *tmp = &name[len - 5]; if (!strcmp(tmp, ".chat")) { set->type = GAIM_LOG_CHAT; /* Determine if this (account, name) combination exists as a buddy. */ set->buddy = (gaim_find_buddy(account, name) != NULL); log_add_log_set_to_hash(sets, set); g_dir_close(username_dir); g_dir_close(protocol_dir); #if 0 /* Maybe some other time. */ static const char *str_from_msg_type (GaimMessageFlags type) static void xml_logger_write(GaimLog *log, const char *from, time_t time, const char *message) /* This log is new. We could use the loggers 'new' function, but * creating a new file there would result in empty files in the case * that you open a convo with someone, but don't say anything. char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); tm = localtime(&log->time); tz = gaim_escape_filename(gaim_utf8_strftime("%Z", tm); date = gaim_utf8_strftime("%Y-%m-%d.%H%M%S%z", tm); name = g_strdup_printf("%s%s%s", date, tz, ext ? ext : ""); gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); filename = g_build_filename(dir, name, NULL); log->logger_data = g_fopen(filename, "a"); gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); date = gaim_utf8_strftime("%Y-%m-%d %H:%M:%S", localtime(&log->time)); fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", /* if we can't write to the file, give up before we hurt ourselves */ date = log_get_timestamp(log, time); gaim_markup_html_to_xhtml(message, &xhtml, NULL); fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", type & GAIM_MESSAGE_SEND ? "direction='sent'" : type & GAIM_MESSAGE_RECV ? "direction='received'" : "", fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", type & GAIM_MESSAGE_SEND ? "direction='sent'" : type & GAIM_MESSAGE_RECV ? "direction='received'" : "", fflush(log->logger_data); static void xml_logger_finalize(GaimLog *log) fprintf(log->logger_data, "</conversation>\n"); fclose(log->logger_data); static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger); static GaimLogLogger xml_logger = { /**************************** ** HTML LOGGER ************* ****************************/ static gsize html_logger_write(GaimLog *log, GaimMessageFlags type, const char *from, time_t time, const char *message) GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); GaimLogCommonLoggerData *data = log->logger_data; GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); gaim_log_common_writer(log, ".html"); /* if we can't write to the file, give up before we hurt ourselves */ date = gaim_date_format_full(localtime(&log->time)); written += fprintf(data->file, "<html><head>"); written += fprintf(data->file, "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"); written += fprintf(data->file, "<title>"); if (log->type == GAIM_LOG_SYSTEM) header = g_strdup_printf("System log for account %s (%s) connected at %s", gaim_account_get_username(log->account), prpl, date); header = g_strdup_printf("Conversation with %s at %s on %s (%s)", log->name, date, gaim_account_get_username(log->account), prpl); written += fprintf(data->file, "%s", header); written += fprintf(data->file, "</title></head><body>"); written += fprintf(data->file, "<h3>%s</h3>\n", header); /* if we can't write to the file, give up before we hurt ourselves */ gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); date = log_get_timestamp(log, time); if(log->type == GAIM_LOG_SYSTEM){ written += fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); if (type & GAIM_MESSAGE_SYSTEM) written += fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); else if (type & GAIM_MESSAGE_ERROR) written += fprintf(data->file, "<font color=\"#FF0000\"><font size=\"2\">(%s)</font><b> %s</b></font><br/>\n", date, msg_fixed); else if (type & GAIM_MESSAGE_WHISPER) written += fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", else if (type & GAIM_MESSAGE_AUTO_RESP) { if (type & GAIM_MESSAGE_SEND) written += fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); else if (type & GAIM_MESSAGE_RECV) written += fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); } else if (type & GAIM_MESSAGE_RECV) { if(gaim_message_meify(msg_fixed, -1)) written += fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", written += fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", } else if (type & GAIM_MESSAGE_SEND) { if(gaim_message_meify(msg_fixed, -1)) written += fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", written += fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", gaim_debug_error("log", "Unhandled message type."); written += fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", static void html_logger_finalize(GaimLog *log) GaimLogCommonLoggerData *data = log->logger_data; fprintf(data->file, "</body></html>\n"); static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) return gaim_log_common_lister(type, sn, account, ".html", html_logger); static GList *html_logger_list_syslog(GaimAccount *account) return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", html_logger); static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) GaimLogCommonLoggerData *data = log->logger_data; *flags = GAIM_LOG_READ_NO_NEWLINE; if (!data || !data->path) return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); if (g_file_get_contents(data->path, &read, NULL, NULL)) { char *minus_header = strchr(read, '\n'); minus_header = g_strdup(minus_header + 1); return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); static int html_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) return gaim_log_common_total_sizer(type, name, account, ".html"); /**************************** ** PLAIN TEXT LOGGER ******* ****************************/ static gsize txt_logger_write(GaimLog *log, const char *from, time_t time, const char *message) GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); GaimLogCommonLoggerData *data = log->logger_data; /* This log is new. We could use the loggers 'new' function, but * creating a new file there would result in empty files in the case * that you open a convo with someone, but don't say anything. GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); gaim_log_common_writer(log, ".txt"); /* if we can't write to the file, give up before we hurt ourselves */ if (log->type == GAIM_LOG_SYSTEM) written += fprintf(data->file, "System log for account %s (%s) connected at %s\n", gaim_account_get_username(log->account), prpl, gaim_date_format_full(localtime(&log->time))); written += fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", log->name, gaim_date_format_full(localtime(&log->time)), gaim_account_get_username(log->account), prpl); /* if we can't write to the file, give up before we hurt ourselves */ stripped = gaim_markup_strip_html(message); date = log_get_timestamp(log, time); if(log->type == GAIM_LOG_SYSTEM){ written += fprintf(data->file, "---- %s @ %s ----\n", stripped, date); if (type & GAIM_MESSAGE_SEND || type & GAIM_MESSAGE_RECV) { if (type & GAIM_MESSAGE_AUTO_RESP) { written += fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, if(gaim_message_meify(stripped, -1)) written += fprintf(data->file, "(%s) ***%s %s\n", date, from, written += fprintf(data->file, "(%s) %s: %s\n", date, from, } else if (type & GAIM_MESSAGE_SYSTEM) written += fprintf(data->file, "(%s) %s\n", date, stripped); else if (type & GAIM_MESSAGE_NO_LOG) { /* This shouldn't happen */ } else if (type & GAIM_MESSAGE_WHISPER) written += fprintf(data->file, "(%s) *%s* %s", date, from, stripped); written += fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); static void txt_logger_finalize(GaimLog *log) GaimLogCommonLoggerData *data = log->logger_data; static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) return gaim_log_common_lister(type, sn, account, ".txt", txt_logger); static GList *txt_logger_list_syslog(GaimAccount *account) return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", txt_logger); static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) char *read, *minus_header, *minus_header2; GaimLogCommonLoggerData *data = log->logger_data; if (!data || !data->path) return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); if (g_file_get_contents(data->path, &read, NULL, NULL)) { minus_header = strchr(read, '\n'); minus_header = g_strdup(read); minus_header = g_strdup(minus_header + 1); minus_header2 = g_markup_escape_text(minus_header, -1); read = gaim_markup_linkify(minus_header2); return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); static int txt_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) return gaim_log_common_total_sizer(type, name, account, ".txt"); /* The old logger doesn't write logs, only reads them. This is to include * old logs in the log viewer transparently. static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); GaimStringref *pathref = gaim_stringref_new(pathstr); time_t log_last_modified; struct old_logger_data *data = NULL; if (g_stat(gaim_stringref_value(pathref), &st)) gaim_stringref_unref(pathref); log_last_modified = st.st_mtime; /* Change the .log extension to .idx */ strcpy(pathstr + strlen(pathstr) - 3, "idx"); if (g_stat(pathstr, &st) == 0) if (st.st_mtime < log_last_modified) gaim_debug_warning("log", "Index \"%s\" exists, but is older than the log.\n", pathstr); /* The index file exists and is at least as new as the log, so open it. */ if (!(index = g_fopen(pathstr, "rb"))) gaim_debug_error("log", "Failed to open index file \"%s\" for reading: %s\n", pathstr, strerror(errno)); /* Fall through so that we'll parse the log file. */ gaim_debug_info("log", "Using index: %s\n", pathstr); while (fgets(buf, BUF_LONG, index)) if (sscanf(buf, "%d\t%d\t%lu", &lastoff, &newlen, &idx_time) == 3) log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL); log->logger = old_logger; log->time = (time_t)idx_time; data = g_new0(struct old_logger_data, 1); data->pathref = gaim_stringref_ref(pathref); list = g_list_prepend(list, log); if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { gaim_debug_error("log", "Failed to open log file \"%s\" for reading: %s\n", gaim_stringref_value(pathref), strerror(errno)); gaim_stringref_unref(pathref); index_tmp = g_strdup_printf("%s.XXXXXX", pathstr); if ((index_fd = g_mkstemp(index_tmp)) == -1) { gaim_debug_error("log", "Failed to open index temp file: %s\n", if ((index = fdopen(index_fd, "wb")) == NULL) gaim_debug_error("log", "Failed to fdopen() index temp file: %s\n", while (fgets(buf, BUF_LONG, file)) { if ((newlog = strstr(buf, "---- New C"))) { char *temp = strchr(buf, '@'); if (temp == NULL || strlen(temp) < 2) length = strcspn(temp, "-"); if (length > 31) length = 31; newlen = offset - lastoff - length; if(strstr(buf, "----</H3><BR>")) { sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + sizeof("----</H3><BR>") - 2; sizeof("---- New Conversation @ ") + sizeof("----") - 2; log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL); log->logger = old_logger; data = g_new0(struct old_logger_data, 1); data->pathref = gaim_stringref_ref(pathref); list = g_list_prepend(list, log); /* XXX: There is apparently Is there a proper way to print a time_t? */ fprintf(index, "%d\t%d\t%lu\n", data->offset, data->length, (unsigned long)log->time); g_snprintf(convostart, length, "%s", temp); sscanf(convostart, "%*s %s %d %d:%d:%d %d", month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); /* Ugly hack, in case current locale is not English */ if (strcmp(month, "Jan") == 0) { } else if (strcmp(month, "Feb") == 0) { } else if (strcmp(month, "Mar") == 0) { } else if (strcmp(month, "Apr") == 0) { } else if (strcmp(month, "May") == 0) { } else if (strcmp(month, "Jun") == 0) { } else if (strcmp(month, "Jul") == 0) { } else if (strcmp(month, "Aug") == 0) { } else if (strcmp(month, "Sep") == 0) { } else if (strcmp(month, "Oct") == 0) { } else if (strcmp(month, "Nov") == 0) { } else if (strcmp(month, "Dec") == 0) { if ((newlen = ftell(file) - lastoff) != 0) { log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL); log->logger = old_logger; data = g_new0(struct old_logger_data, 1); data->pathref = gaim_stringref_ref(pathref); list = g_list_prepend(list, log); /* XXX: Is there a proper way to print a time_t? */ fprintf(index, "%d\t%d\t%d\n", data->offset, data->length, (int)log->time); gaim_stringref_unref(pathref); g_return_val_if_reached(list); if (g_rename(index_tmp, pathstr)) gaim_debug_warning("log", "Failed to rename index temp file \"%s\" to \"%s\": %s\n", index_tmp, pathstr, strerror(errno)); gaim_debug_info("log", "Built index: %s\n", pathstr); static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); if (g_stat(pathstr, &st)) static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) struct old_logger_data *data = log->logger_data; FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); char *tmp, *read = g_malloc(data->length + 1); fseek(file, data->offset, SEEK_SET); fread(read, data->length, 1, file); read[data->length] = '\0'; *flags |= GAIM_LOG_READ_NO_NEWLINE; tmp = g_markup_escape_text(read, -1); read = gaim_markup_linkify(tmp); static int old_logger_size (GaimLog *log) struct old_logger_data *data = log->logger_data; return data ? data->length : 0; static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets) char *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); GDir *log_dir = g_dir_open(log_path, 0, NULL); GaimBlistNode *gnode, *cnode, *bnode; /* Don't worry about the cast, name will be filled with a dynamically allocated data shortly. */ while ((name = (gchar *)g_dir_read_name(log_dir)) != NULL) { /* Unescape the filename. */ name = g_strdup(gaim_unescape_filename(name)); /* Get the (possibly new) length of name. */ /* Make sure we're dealing with a log file. */ if (strcmp(ext, ".log")) { set = g_new0(GaimLogSet, 1); /* Chat for .chat at the end of the name to determine the type. */ char *tmp = &name[len - 9]; if (!strcmp(tmp, ".chat")) { set->type = GAIM_LOG_CHAT; set->name = set->normalized_name = name; /* Search the buddy list to find the account and to determine if this is a buddy. */ for (gnode = gaim_get_blist()->root; !found && gnode != NULL; gnode = gnode->next) if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) for (cnode = gnode->child; !found && cnode != NULL; cnode = cnode->next) if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) for (bnode = cnode->child; !found && bnode != NULL; bnode = bnode->next) GaimBuddy *buddy = (GaimBuddy *)bnode; if (!strcmp(buddy->name, name)) { set->account = buddy->account; static void old_logger_finalize(GaimLog *log) struct old_logger_data *data = log->logger_data; gaim_stringref_unref(data->pathref);