2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
fe-log.c : irssi
|
|
|
|
|
2000-04-28 04:07:42 -04:00
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
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 "module.h"
|
|
|
|
#include "module-formats.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "levels.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "special-vars.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2000-11-17 11:27:14 -05:00
|
|
|
#include "fe-windows.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "window-items.h"
|
2000-10-28 16:14:19 -04:00
|
|
|
#include "formats.h"
|
2000-10-27 23:01:11 -04:00
|
|
|
#include "themes.h"
|
2000-10-28 16:14:19 -04:00
|
|
|
#include "printtext.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* close autologs after 5 minutes of inactivity */
|
|
|
|
#define AUTOLOG_INACTIVITY_CLOSE (60*5)
|
|
|
|
|
|
|
|
#define LOG_DIR_CREATE_MODE 0770
|
|
|
|
|
|
|
|
static int autolog_level;
|
|
|
|
static int autoremove_tag;
|
|
|
|
static const char *autolog_path;
|
|
|
|
|
2000-10-27 23:01:11 -04:00
|
|
|
static THEME_REC *log_theme;
|
|
|
|
static int skip_next_printtext;
|
|
|
|
static const char *log_theme_name;
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static void log_add_targets(LOG_REC *log, const char *targets, const char *tag)
|
2000-10-17 19:37:21 -04:00
|
|
|
{
|
|
|
|
char **tmp, **items;
|
|
|
|
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
g_return_if_fail(targets != NULL);
|
|
|
|
|
|
|
|
items = g_strsplit(targets, " ", -1);
|
|
|
|
|
|
|
|
for (tmp = items; *tmp != NULL; tmp++)
|
2001-02-17 14:44:22 -05:00
|
|
|
log_item_add(log, LOG_ITEM_TARGET, *tmp, tag);
|
2000-10-17 19:37:21 -04:00
|
|
|
|
|
|
|
g_strfreev(items);
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
/* SYNTAX: LOG OPEN [-noopen] [-autoopen] [-window] [-<server tag>]
|
|
|
|
[-targets <targets>] <fname> [<levels>] */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_log_open(const char *data)
|
|
|
|
{
|
2001-02-17 14:44:22 -05:00
|
|
|
SERVER_REC *server;
|
2000-06-17 21:18:12 -04:00
|
|
|
GHashTable *optlist;
|
2001-02-19 20:54:14 -05:00
|
|
|
char *targetarg, *fname, *levels, *servertag;
|
2000-06-17 21:18:12 -04:00
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
char window[MAX_INT_STRLEN];
|
|
|
|
LOG_REC *log;
|
2000-08-11 18:07:42 -04:00
|
|
|
int level;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
|
|
|
|
PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_OPTIONS,
|
2000-06-17 21:18:12 -04:00
|
|
|
"log open", &optlist, &fname, &levels))
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
if (*fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
|
|
|
|
|
|
|
|
level = level2bits(levels);
|
2000-10-17 19:37:21 -04:00
|
|
|
log = log_create_rec(fname, level != 0 ? level : MSGLEVEL_ALL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
/* -<server tag> */
|
2001-05-11 10:26:22 -04:00
|
|
|
server = cmd_options_get_server("log open", optlist, NULL);
|
2001-02-19 20:54:14 -05:00
|
|
|
servertag = server == NULL ? NULL : server->tag;
|
2001-02-17 14:44:22 -05:00
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (g_hash_table_lookup(optlist, "window")) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* log by window ref# */
|
|
|
|
ltoa(window, active_win->refnum);
|
2001-02-19 20:54:14 -05:00
|
|
|
log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window,
|
|
|
|
servertag);
|
2000-10-17 19:37:21 -04:00
|
|
|
} else {
|
|
|
|
targetarg = g_hash_table_lookup(optlist, "targets");
|
|
|
|
if (targetarg != NULL && *targetarg != '\0')
|
2001-02-19 20:54:14 -05:00
|
|
|
log_add_targets(log, targetarg, servertag);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
if (g_hash_table_lookup(optlist, "autoopen"))
|
|
|
|
log->autoopen = TRUE;
|
|
|
|
|
|
|
|
log_update(log);
|
2000-06-04 08:59:46 -04:00
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
if (log->handle == -1 && g_hash_table_lookup(optlist, "noopen") == NULL) {
|
|
|
|
/* start logging */
|
|
|
|
if (log_start_logging(log)) {
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_LOG_OPENED, fname);
|
2000-10-17 19:37:21 -04:00
|
|
|
} else {
|
|
|
|
log_close(log);
|
2000-06-04 08:59:46 -04:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-04 08:59:46 -04:00
|
|
|
static LOG_REC *log_find_from_data(const char *data)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
if (!is_numeric(data, ' '))
|
|
|
|
return log_find(data);
|
|
|
|
|
|
|
|
/* with index number */
|
|
|
|
tmp = g_slist_nth(logs, atoi(data)-1);
|
|
|
|
return tmp == NULL ? NULL : tmp->data;
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: LOG CLOSE <id>|<file> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_log_close(const char *data)
|
|
|
|
{
|
|
|
|
LOG_REC *log;
|
|
|
|
|
2000-06-04 08:59:46 -04:00
|
|
|
log = log_find_from_data(data);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (log == NULL)
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_LOG_NOT_OPEN, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
else {
|
|
|
|
log_close(log);
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: LOG START <id>|<file> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_log_start(const char *data)
|
|
|
|
{
|
|
|
|
LOG_REC *log;
|
|
|
|
|
2000-06-04 08:59:46 -04:00
|
|
|
log = log_find_from_data(data);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (log != NULL) {
|
|
|
|
log_start_logging(log);
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: LOG STOP <id>|<file> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_log_stop(const char *data)
|
|
|
|
{
|
|
|
|
LOG_REC *log;
|
|
|
|
|
2000-06-04 08:59:46 -04:00
|
|
|
log = log_find_from_data(data);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (log == NULL || log->handle == -1)
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_LOG_NOT_OPEN, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
else {
|
|
|
|
log_stop_logging(log);
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static char *log_items_get_list(LOG_REC *log)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
GString *str;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(log != NULL, NULL);
|
|
|
|
g_return_val_if_fail(log->items != NULL, NULL);
|
|
|
|
|
|
|
|
str = g_string_new(NULL);
|
|
|
|
for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
|
|
|
|
LOG_ITEM_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
g_string_sprintfa(str, "%s, ", rec->name);
|
|
|
|
}
|
|
|
|
g_string_truncate(str, str->len-2);
|
|
|
|
|
|
|
|
ret = str->str;
|
|
|
|
g_string_free(str, FALSE);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_log_list(void)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
2000-08-11 18:07:42 -04:00
|
|
|
char *levelstr, *items;
|
2000-06-04 08:59:46 -04:00
|
|
|
int index;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST_HEADER);
|
2000-06-04 08:59:46 -04:00
|
|
|
for (tmp = logs, index = 1; tmp != NULL; tmp = tmp->next, index++) {
|
2000-04-26 04:03:38 -04:00
|
|
|
LOG_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
levelstr = bits2level(rec->level);
|
|
|
|
items = rec->items == NULL ? NULL :
|
2000-10-17 19:37:21 -04:00
|
|
|
log_items_get_list(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST,
|
2000-06-04 08:59:46 -04:00
|
|
|
index, rec->fname, items != NULL ? items : "",
|
2000-08-11 18:07:42 -04:00
|
|
|
levelstr, rec->autoopen ? " -autoopen" : "");
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_free_not_null(items);
|
|
|
|
g_free(levelstr);
|
|
|
|
}
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST_FOOTER);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_log(const char *data, SERVER_REC *server, void *item)
|
|
|
|
{
|
2000-06-28 13:15:37 -04:00
|
|
|
if (*data == '\0')
|
|
|
|
cmd_log_list();
|
|
|
|
else
|
|
|
|
command_runsub("log", data, server, item);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static LOG_REC *logs_find_item(int type, const char *item,
|
2001-02-17 14:44:22 -05:00
|
|
|
const char *servertag, LOG_ITEM_REC **ret_item)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_ITEM_REC *logitem;
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = tmp->next) {
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_REC *log = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
logitem = log_item_find(log, type, item, servertag);
|
2000-10-17 19:37:21 -04:00
|
|
|
if (logitem != NULL) {
|
|
|
|
if (ret_item != NULL) *ret_item = logitem;
|
|
|
|
return log;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: WINDOW LOG on|off|toggle [<filename>] */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_window_log(const char *data)
|
|
|
|
{
|
|
|
|
LOG_REC *log;
|
2000-06-17 21:18:12 -04:00
|
|
|
char *set, *fname, window[MAX_INT_STRLEN];
|
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
int open_log, close_log;
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2, &set, &fname))
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
ltoa(window, active_win->refnum);
|
2000-10-17 19:37:21 -04:00
|
|
|
log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, window, NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
open_log = close_log = FALSE;
|
|
|
|
if (g_strcasecmp(set, "ON") == 0)
|
|
|
|
open_log = TRUE;
|
|
|
|
else if (g_strcasecmp(set, "OFF") == 0) {
|
|
|
|
close_log = TRUE;
|
|
|
|
} else if (g_strcasecmp(set, "TOGGLE") == 0) {
|
|
|
|
open_log = log == NULL;
|
|
|
|
close_log = log != NULL;
|
|
|
|
} else {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_NOT_TOGGLE);
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open_log && log == NULL) {
|
|
|
|
/* irc.log.<windowname> or irc.log.Window<ref#> */
|
|
|
|
fname = *fname != '\0' ? g_strdup(fname) :
|
|
|
|
g_strdup_printf("~/irc.log.%s%s",
|
|
|
|
active_win->name != NULL ? active_win->name : "Window",
|
|
|
|
active_win->name != NULL ? "" : window);
|
2000-10-17 19:37:21 -04:00
|
|
|
log = log_create_rec(fname, MSGLEVEL_ALL);
|
|
|
|
log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, NULL);
|
|
|
|
log_update(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open_log && log != NULL) {
|
|
|
|
log_start_logging(log);
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, log->fname);
|
2000-04-26 04:03:38 -04:00
|
|
|
} else if (close_log && log != NULL && log->handle != -1) {
|
|
|
|
log_stop_logging(log);
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, log->fname);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create log file entry to window, but don't start logging */
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: WINDOW LOGFILE <file> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_window_logfile(const char *data)
|
|
|
|
{
|
|
|
|
LOG_REC *log;
|
|
|
|
char window[MAX_INT_STRLEN];
|
|
|
|
|
|
|
|
ltoa(window, active_win->refnum);
|
2000-10-17 19:37:21 -04:00
|
|
|
log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, window, NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (log != NULL) {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_WINDOWLOG_FILE_LOGGING);
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
log = log_create_rec(data, MSGLEVEL_ALL);
|
|
|
|
log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, NULL);
|
|
|
|
log_update(log);
|
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_WINDOWLOG_FILE, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
/* window's refnum changed - update the logs to log the new window refnum */
|
|
|
|
static void sig_window_refnum_changed(WINDOW_REC *window, gpointer old_refnum)
|
|
|
|
{
|
|
|
|
char winnum[MAX_INT_STRLEN];
|
|
|
|
LOG_REC *log;
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_ITEM_REC *item;
|
2000-05-04 06:32:42 -04:00
|
|
|
|
|
|
|
ltoa(winnum, GPOINTER_TO_INT(old_refnum));
|
2000-10-17 19:37:21 -04:00
|
|
|
log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, winnum, NULL, &item);
|
2000-05-04 06:32:42 -04:00
|
|
|
|
|
|
|
if (log != NULL) {
|
|
|
|
ltoa(winnum, window->refnum);
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
g_free(item->name);
|
|
|
|
item->name = g_strdup(winnum);
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-20 19:21:45 -05:00
|
|
|
static void sig_server_disconnected(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
LOG_ITEM_REC *logitem;
|
|
|
|
GSList *tmp, *next;
|
|
|
|
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = next) {
|
|
|
|
LOG_REC *log = tmp->data;
|
2001-03-25 21:02:50 -05:00
|
|
|
next = tmp->next;
|
|
|
|
|
2001-03-28 17:38:52 -05:00
|
|
|
if (!log->temp || log->items == NULL)
|
2001-03-25 21:02:50 -05:00
|
|
|
continue;
|
2001-03-20 19:21:45 -05:00
|
|
|
|
2001-03-25 21:02:50 -05:00
|
|
|
logitem = log->items->data;
|
|
|
|
if (logitem->type == LOG_ITEM_TARGET &&
|
2001-06-10 20:14:48 -04:00
|
|
|
logitem->servertag != NULL &&
|
2001-03-20 19:21:45 -05:00
|
|
|
g_strcasecmp(logitem->servertag, server->tag) == 0)
|
|
|
|
log_close(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void autologs_close_all(void)
|
|
|
|
{
|
|
|
|
GSList *tmp, *next;
|
|
|
|
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = next) {
|
|
|
|
LOG_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (rec->temp) log_close(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static void autolog_open(SERVER_REC *server, const char *target)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
LOG_REC *log;
|
2001-02-17 14:44:22 -05:00
|
|
|
char *fname, *dir, *fixed_target, *tag;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
tag = server == NULL ? NULL : server->tag;
|
|
|
|
log = logs_find_item(LOG_ITEM_TARGET, target, tag, NULL);
|
2000-10-13 17:53:25 -04:00
|
|
|
if (log != NULL && !log->failed) {
|
2000-07-22 12:02:50 -04:00
|
|
|
log_start_logging(log);
|
|
|
|
return;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-18 12:15:14 -05:00
|
|
|
/* '/' -> '_' - don't even accidentally try to log to
|
|
|
|
#../../../file if you happen to join to such channel.. */
|
|
|
|
fixed_target = g_strdup(target);
|
|
|
|
replace_chars(fixed_target, '/', '_');
|
2000-12-17 02:39:22 -05:00
|
|
|
fname = parse_special_string(autolog_path, server, NULL,
|
2000-12-18 12:15:14 -05:00
|
|
|
fixed_target, NULL, 0);
|
|
|
|
g_free(fixed_target);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (log_find(fname) == NULL) {
|
2000-11-25 11:30:56 -05:00
|
|
|
log = log_create_rec(fname, autolog_level);
|
2001-02-17 14:44:22 -05:00
|
|
|
log_item_add(log, LOG_ITEM_TARGET, target, tag);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-11-25 11:30:56 -05:00
|
|
|
dir = g_dirname(log->real_fname);
|
2000-08-12 11:50:50 -04:00
|
|
|
mkpath(dir, LOG_DIR_CREATE_MODE);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(dir);
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
log->temp = TRUE;
|
|
|
|
log_update(log);
|
|
|
|
log_start_logging(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
g_free(fname);
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static void autolog_open_check(SERVER_REC *server, const char *target,
|
|
|
|
int level)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-11 16:13:49 -04:00
|
|
|
char **targets, **tmp;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-20 19:21:45 -05:00
|
|
|
if (level == MSGLEVEL_PARTS || /* FIXME: kind of a kludge, but we don't want to reopen logs when we're parting the channel with /WINDOW CLOSE.. */
|
|
|
|
(autolog_level & level) == 0 || target == NULL || *target == '\0')
|
2000-10-28 17:04:01 -04:00
|
|
|
return;
|
2000-05-15 04:25:45 -04:00
|
|
|
|
2000-10-28 17:04:01 -04:00
|
|
|
/* there can be multiple targets separated with comma */
|
|
|
|
targets = g_strsplit(target, ",", -1);
|
|
|
|
for (tmp = targets; *tmp != NULL; tmp++)
|
|
|
|
autolog_open(server, *tmp);
|
|
|
|
g_strfreev(targets);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-10-28 17:04:01 -04:00
|
|
|
static void log_single_line(WINDOW_REC *window, void *server,
|
|
|
|
const char *target, int level, const char *text)
|
|
|
|
{
|
|
|
|
char windownum[MAX_INT_STRLEN];
|
|
|
|
char **targets, **tmp;
|
|
|
|
LOG_REC *log;
|
|
|
|
|
|
|
|
/* save to log created with /WINDOW LOG */
|
2000-05-15 04:25:45 -04:00
|
|
|
ltoa(windownum, window->refnum);
|
2000-10-28 17:04:01 -04:00
|
|
|
log = logs_find_item(LOG_ITEM_WINDOW_REFNUM,
|
|
|
|
windownum, NULL, NULL);
|
2001-02-17 07:08:31 -05:00
|
|
|
if (log != NULL) log_write_rec(log, text, level);
|
2000-10-27 23:01:11 -04:00
|
|
|
|
2000-10-28 17:04:01 -04:00
|
|
|
if (target == NULL)
|
2000-10-27 23:01:11 -04:00
|
|
|
log_file_write(server, NULL, level, text, FALSE);
|
|
|
|
else {
|
|
|
|
/* there can be multiple items separated with comma */
|
|
|
|
targets = g_strsplit(target, ",", -1);
|
|
|
|
for (tmp = targets; *tmp != NULL; tmp++)
|
|
|
|
log_file_write(server, *tmp, level, text, FALSE);
|
|
|
|
g_strfreev(targets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static void log_line(WINDOW_REC *window, SERVER_REC *server,
|
2000-10-28 17:04:01 -04:00
|
|
|
const char *target, int level, const char *text)
|
|
|
|
{
|
|
|
|
char **lines, **tmp;
|
|
|
|
|
|
|
|
if (level == MSGLEVEL_NEVER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* let autolog open the log records */
|
|
|
|
autolog_open_check(server, target, level);
|
|
|
|
|
|
|
|
if (logs == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* text may contain one or more lines, log wants to eat them one
|
|
|
|
line at a time */
|
|
|
|
lines = g_strsplit(text, "\n", -1);
|
|
|
|
for (tmp = lines; *tmp != NULL; tmp++)
|
|
|
|
log_single_line(window, server, target, level, *tmp);
|
|
|
|
g_strfreev(lines);
|
|
|
|
}
|
|
|
|
|
2001-05-31 18:35:49 -04:00
|
|
|
static void sig_printtext(TEXT_DEST_REC *dest, const char *text,
|
|
|
|
const char *stripped)
|
2000-10-27 23:01:11 -04:00
|
|
|
{
|
|
|
|
if (skip_next_printtext) {
|
|
|
|
skip_next_printtext = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-11-12 15:20:05 -05:00
|
|
|
log_line(dest->window, dest->server, dest->target,
|
2001-05-31 18:35:49 -04:00
|
|
|
dest->level, stripped);
|
2000-10-27 23:01:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_print_format(THEME_REC *theme, const char *module,
|
2000-11-12 15:20:05 -05:00
|
|
|
TEXT_DEST_REC *dest, void *formatnum, char **args)
|
2000-10-27 23:01:11 -04:00
|
|
|
{
|
2000-10-28 16:14:19 -04:00
|
|
|
char *str, *stripped, *linestart, *tmp;
|
2000-10-27 23:01:11 -04:00
|
|
|
|
|
|
|
if (log_theme == NULL) {
|
|
|
|
/* theme isn't loaded for some reason (/reload destroys it),
|
|
|
|
reload it. */
|
|
|
|
log_theme = theme_load(log_theme_name);
|
|
|
|
if (log_theme == NULL) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theme == log_theme)
|
|
|
|
return;
|
|
|
|
|
2000-11-12 15:20:05 -05:00
|
|
|
str = format_get_text_theme_charargs(log_theme, module, dest,
|
|
|
|
GPOINTER_TO_INT(formatnum), args);
|
2000-10-28 16:14:19 -04:00
|
|
|
skip_next_printtext = TRUE;
|
2000-10-27 23:01:11 -04:00
|
|
|
|
|
|
|
if (*str != '\0') {
|
2000-10-28 16:14:19 -04:00
|
|
|
/* add the line start format */
|
2000-11-20 22:00:05 -05:00
|
|
|
linestart = format_get_level_tag(log_theme, dest);
|
2000-10-28 16:14:19 -04:00
|
|
|
tmp = str;
|
|
|
|
str = format_add_linestart(tmp, linestart);
|
|
|
|
g_free_not_null(linestart);
|
|
|
|
g_free(tmp);
|
2000-10-27 23:01:11 -04:00
|
|
|
|
|
|
|
/* strip colors from text, log it. */
|
|
|
|
stripped = strip_codes(str);
|
|
|
|
log_line(dest->window, dest->server, dest->target,
|
|
|
|
dest->level, stripped);
|
|
|
|
g_free(stripped);
|
|
|
|
}
|
|
|
|
g_free(str);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sig_autoremove(void)
|
|
|
|
{
|
2000-10-17 19:37:21 -04:00
|
|
|
SERVER_REC *server;
|
|
|
|
LOG_ITEM_REC *logitem;
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *tmp, *next;
|
|
|
|
time_t removetime;
|
|
|
|
|
|
|
|
removetime = time(NULL)-AUTOLOG_INACTIVITY_CLOSE;
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = next) {
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_REC *log = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
next = tmp->next;
|
2000-10-17 19:37:21 -04:00
|
|
|
|
|
|
|
if (!log->temp || log->last > removetime || log->items == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Close only logs with private messages */
|
|
|
|
logitem = log->items->data;
|
2000-10-22 11:38:53 -04:00
|
|
|
if (logitem->servertag == NULL)
|
|
|
|
continue;
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
server = server_find_tag(logitem->servertag);
|
|
|
|
if (logitem->type == LOG_ITEM_TARGET &&
|
2001-01-01 11:57:25 -05:00
|
|
|
server != NULL && !server->ischannel(logitem->name))
|
2000-10-17 19:37:21 -04:00
|
|
|
log_close(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-12-02 14:08:21 -05:00
|
|
|
static void sig_window_item_destroy(WINDOW_REC *window, WI_ITEM_REC *item)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_REC *log;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
log = logs_find_item(LOG_ITEM_TARGET, item->name,
|
|
|
|
item->server == NULL ? NULL :
|
|
|
|
item->server->tag, NULL);
|
2000-11-15 18:34:32 -05:00
|
|
|
if (log != NULL && log->temp)
|
|
|
|
log_close(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_log_locked(LOG_REC *log)
|
|
|
|
{
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_LOG_LOCKED, log->fname);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-05-18 04:46:56 -04:00
|
|
|
static void sig_log_create_failed(LOG_REC *log)
|
|
|
|
{
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_LOG_CREATE_FAILED, log->fname, g_strerror(errno));
|
2000-05-18 04:46:56 -04:00
|
|
|
}
|
|
|
|
|
2000-06-01 21:15:51 -04:00
|
|
|
static void sig_awaylog_show(LOG_REC *log, gpointer pmsgs, gpointer pfilepos)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int msgs, filepos;
|
|
|
|
|
|
|
|
msgs = GPOINTER_TO_INT(pmsgs);
|
|
|
|
filepos = GPOINTER_TO_INT(pfilepos);
|
|
|
|
|
|
|
|
if (msgs == 0)
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_NO_AWAY_MSGS, log->fname);
|
2000-06-01 21:15:51 -04:00
|
|
|
else {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_AWAY_MSGS, log->fname, msgs);
|
2000-06-01 21:15:51 -04:00
|
|
|
|
|
|
|
str = g_strdup_printf("\"%s\" %d", log->fname, filepos);
|
|
|
|
signal_emit("command cat", 1, str);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-27 23:01:11 -04:00
|
|
|
static void sig_theme_destroyed(THEME_REC *theme)
|
|
|
|
{
|
|
|
|
if (theme == log_theme)
|
|
|
|
log_theme = NULL;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void read_settings(void)
|
|
|
|
{
|
|
|
|
int old_autolog = autolog_level;
|
|
|
|
|
|
|
|
autolog_path = settings_get_str("autolog_path");
|
|
|
|
autolog_level = !settings_get_bool("autolog") ? 0 :
|
|
|
|
level2bits(settings_get_str("autolog_level"));
|
|
|
|
|
|
|
|
if (old_autolog && !autolog_level)
|
|
|
|
autologs_close_all();
|
2000-10-27 23:01:11 -04:00
|
|
|
|
|
|
|
/* write to log files with different theme? */
|
2000-10-28 17:04:01 -04:00
|
|
|
if (log_theme_name != NULL)
|
|
|
|
signal_remove("print format", (SIGNAL_FUNC) sig_print_format);
|
2000-10-27 23:01:11 -04:00
|
|
|
log_theme_name = settings_get_str("log_theme");
|
2000-10-28 17:04:01 -04:00
|
|
|
if (*log_theme_name == '\0')
|
|
|
|
log_theme_name = NULL;
|
|
|
|
else
|
2000-10-27 23:01:11 -04:00
|
|
|
signal_add("print format", (SIGNAL_FUNC) sig_print_format);
|
|
|
|
|
2000-10-28 17:04:01 -04:00
|
|
|
log_theme = log_theme_name == NULL ? NULL :
|
2000-10-27 23:01:11 -04:00
|
|
|
theme_load(log_theme_name);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fe_log_init(void)
|
|
|
|
{
|
|
|
|
autoremove_tag = g_timeout_add(60000, (GSourceFunc) sig_autoremove, NULL);
|
2000-10-27 23:01:11 -04:00
|
|
|
skip_next_printtext = FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
settings_add_str("log", "autolog_path", "~/irclogs/$tag/$0.log");
|
2001-02-06 16:56:51 -05:00
|
|
|
settings_add_str("log", "autolog_level", "all -crap -clientcrap -ctcps");
|
2000-04-26 04:03:38 -04:00
|
|
|
settings_add_bool("log", "autolog", FALSE);
|
2000-10-27 23:01:11 -04:00
|
|
|
settings_add_str("log", "log_theme", "");
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
autolog_level = 0;
|
2000-10-28 17:04:01 -04:00
|
|
|
log_theme_name = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
read_settings();
|
|
|
|
|
|
|
|
command_bind("log", NULL, (SIGNAL_FUNC) cmd_log);
|
|
|
|
command_bind("log open", NULL, (SIGNAL_FUNC) cmd_log_open);
|
|
|
|
command_bind("log close", NULL, (SIGNAL_FUNC) cmd_log_close);
|
|
|
|
command_bind("log start", NULL, (SIGNAL_FUNC) cmd_log_start);
|
|
|
|
command_bind("log stop", NULL, (SIGNAL_FUNC) cmd_log_stop);
|
|
|
|
command_bind("window log", NULL, (SIGNAL_FUNC) cmd_window_log);
|
|
|
|
command_bind("window logfile", NULL, (SIGNAL_FUNC) cmd_window_logfile);
|
2001-05-31 18:35:49 -04:00
|
|
|
signal_add_first("print text", (SIGNAL_FUNC) sig_printtext);
|
2000-12-02 14:08:21 -05:00
|
|
|
signal_add("window item destroy", (SIGNAL_FUNC) sig_window_item_destroy);
|
2000-05-04 06:32:42 -04:00
|
|
|
signal_add("window refnum changed", (SIGNAL_FUNC) sig_window_refnum_changed);
|
2001-03-20 19:21:45 -05:00
|
|
|
signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("log locked", (SIGNAL_FUNC) sig_log_locked);
|
2000-05-18 04:46:56 -04:00
|
|
|
signal_add("log create failed", (SIGNAL_FUNC) sig_log_create_failed);
|
2000-06-01 21:15:51 -04:00
|
|
|
signal_add("awaylog show", (SIGNAL_FUNC) sig_awaylog_show);
|
2000-10-27 23:01:11 -04:00
|
|
|
signal_add("theme destroyed", (SIGNAL_FUNC) sig_theme_destroyed);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-06-17 21:18:12 -04:00
|
|
|
|
2000-08-11 18:07:42 -04:00
|
|
|
command_set_options("log open", "noopen autoopen -targets window");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fe_log_deinit(void)
|
|
|
|
{
|
|
|
|
g_source_remove(autoremove_tag);
|
2000-10-28 17:04:01 -04:00
|
|
|
if (log_theme_name != NULL)
|
2000-10-27 23:01:11 -04:00
|
|
|
signal_remove("print format", (SIGNAL_FUNC) sig_print_format);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
command_unbind("log", (SIGNAL_FUNC) cmd_log);
|
|
|
|
command_unbind("log open", (SIGNAL_FUNC) cmd_log_open);
|
|
|
|
command_unbind("log close", (SIGNAL_FUNC) cmd_log_close);
|
|
|
|
command_unbind("log start", (SIGNAL_FUNC) cmd_log_start);
|
|
|
|
command_unbind("log stop", (SIGNAL_FUNC) cmd_log_stop);
|
|
|
|
command_unbind("window log", (SIGNAL_FUNC) cmd_window_log);
|
|
|
|
command_unbind("window logfile", (SIGNAL_FUNC) cmd_window_logfile);
|
2001-05-31 18:35:49 -04:00
|
|
|
signal_remove("print text", (SIGNAL_FUNC) sig_printtext);
|
2000-12-02 14:08:21 -05:00
|
|
|
signal_remove("window item destroy", (SIGNAL_FUNC) sig_window_item_destroy);
|
2000-05-04 06:32:42 -04:00
|
|
|
signal_remove("window refnum changed", (SIGNAL_FUNC) sig_window_refnum_changed);
|
2001-03-20 19:21:45 -05:00
|
|
|
signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("log locked", (SIGNAL_FUNC) sig_log_locked);
|
2000-05-18 04:46:56 -04:00
|
|
|
signal_remove("log create failed", (SIGNAL_FUNC) sig_log_create_failed);
|
2000-06-01 21:15:51 -04:00
|
|
|
signal_remove("awaylog show", (SIGNAL_FUNC) sig_awaylog_show);
|
2000-10-27 23:01:11 -04:00
|
|
|
signal_remove("theme destroyed", (SIGNAL_FUNC) sig_theme_destroyed);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
}
|