Beispiel #1
0
static void autolog_open(SERVER_REC *server, const char *server_tag,
			 const char *target)
{
	LOG_REC *log;
	char *fname, *dir, *fixed_target, *params;

	log = logs_find_item(LOG_ITEM_TARGET, target, server_tag, NULL);
	if (log != NULL && !log->failed) {
		log_start_logging(log);
		return;
	}

	/* '/' -> '_' - don't even accidentally try to log to
	   #../../../file if you happen to join to such channel..
	   similar for some characters that are metacharacters
	   and/or illegal in Windows filenames.

	   '%' -> '%%' - so strftime() won't mess with them */
	fixed_target = escape_target(target);
	if (CHAT_PROTOCOL(server)->case_insensitive)
		ascii_strdown(fixed_target);

        /* $0 = target, $1 = server tag */
        params = g_strconcat(fixed_target, " ", server_tag, NULL);
	g_free(fixed_target);

	fname = parse_special_string(autolog_path, server, NULL,
				     params, NULL, 0);
	g_free(params);

	if (log_find(fname) == NULL) {
		log = log_create_rec(fname, autolog_level);
                if (!settings_get_bool("autolog_colors"))
			log->colorizer = log_colorizer_strip;
		log_item_add(log, LOG_ITEM_TARGET, target, server_tag);

		dir = g_path_get_dirname(log->real_fname);
#ifdef HAVE_CAPSICUM
		capsicum_mkdir_with_parents_wrapper(dir, log_dir_create_mode);
#else
		g_mkdir_with_parents(dir, log_dir_create_mode);
#endif
		g_free(dir);

		log->temp = TRUE;
		log_update(log);
		log_start_logging(log);
	}
	g_free(fname);
}
Beispiel #2
0
static void awaylog_open(void)
{
	const char *fname, *levelstr;
	LOG_REC *log;
	int level;

	fname = settings_get_str("awaylog_file");
	levelstr = settings_get_str("awaylog_level");
	if (*fname == '\0' || *levelstr == '\0') return;

	level = level2bits(levelstr);
	if (level == 0) return;

	log = log_find(fname);
	if (log != NULL && log->handle != -1)
		return; /* already open */

	if (log == NULL) {
		log = log_create_rec(fname, level);
		log->temp = TRUE;
		log_update(log);
	}

	if (!log_start_logging(log)) {
		/* creating log file failed? close it. */
		log_close(log);
		return;
	}

	awaylog = log;
	away_filepos = lseek(log->handle, 0, SEEK_CUR);
	away_msgs = 0;
}
Beispiel #3
0
/* SYNTAX: LOG OPEN [-noopen] [-autoopen] [-window] [-<server tag>]
                    [-targets <targets>] [-colors]
		    <fname> [<levels>] */
static void cmd_log_open(const char *data)
{
        SERVER_REC *server;
        GHashTable *optlist;
	char *targetarg, *fname, *levels, *servertag;
	void *free_arg;
	char window[MAX_INT_STRLEN];
	LOG_REC *log;
	int level;

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
			    PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_OPTIONS,
			    "log open", &optlist, &fname, &levels))
		return;
	if (*fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	level = level2bits(levels);
	log = log_create_rec(fname, level != 0 ? level : MSGLEVEL_ALL);

	/* -<server tag> */
	server = cmd_options_get_server("log open", optlist, NULL);
	servertag = server == NULL ? NULL : server->tag;

	if (g_hash_table_lookup(optlist, "window")) {
		/* log by window ref# */
		targetarg = g_hash_table_lookup(optlist, "targets");
		if (targetarg == NULL || !is_numeric(targetarg, '\0')) {
			ltoa(window, active_win->refnum);
			targetarg = window;
		}
		log_item_add(log, LOG_ITEM_WINDOW_REFNUM, targetarg,
			     servertag);
	} else {
		targetarg = g_hash_table_lookup(optlist, "targets");
		if (targetarg != NULL && *targetarg != '\0')
			log_add_targets(log, targetarg, servertag);
		else if (servertag != NULL)
			log_add_targets(log, "*", servertag);
	}

	if (g_hash_table_lookup(optlist, "autoopen"))
		log->autoopen = TRUE;

	if (g_hash_table_lookup(optlist, "colors") == NULL)
		log->colorizer = log_colorizer_strip;

	log_update(log);

	if (log->handle == -1 && g_hash_table_lookup(optlist, "noopen") == NULL) {
		/* start logging */
		if (log_start_logging(log)) {
			printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
				    TXT_LOG_OPENED, fname);
		} else {
			log_close(log);
		}
	}

        cmd_params_free(free_arg);
}
Beispiel #4
0
static void autolog_log(void *server, const char *target)
{
    LOG_REC *log;
    char *fname, *dir, *str;

    log = log_find_item(target);
    if (log != NULL) return;

    fname = parse_special_string(autolog_path, server, NULL, target, NULL);
    if (log_find(fname) == NULL) {
        str = convert_home(fname);
        dir = g_dirname(str);
        g_free(str);

        mkdir(dir, LOG_DIR_CREATE_MODE);
        g_free(dir);

        log = log_create_rec(fname, autolog_level, target);
        if (log != NULL) {
            log->temp = TRUE;
            log_update(log);
            log_start_logging(log);
        }
    }
    g_free(fname);
}
Beispiel #5
0
/* SYNTAX: LOG START <id>|<file> */
static void cmd_log_start(const char *data)
{
	LOG_REC *log;

	log = log_find_from_data(data);
	if (log != NULL) {
		log_start_logging(log);
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, data);
	}
}
Beispiel #6
0
static void log_read_config(void)
{
	CONFIG_NODE *node;
	LOG_REC *log;
	GSList *tmp, *next, *fnames;

	/* close old logs, save list of open logs */
	fnames = NULL;
	for (tmp = logs; tmp != NULL; tmp = next) {
		log = tmp->data;

		next = tmp->next;
		if (log->temp)
			continue;

		if (log->handle != -1)
			fnames = g_slist_append(fnames, g_strdup(log->fname));
		log_destroy(log);
	}

	node = iconfig_node_traverse("logs", FALSE);
	if (node == NULL) return;

	tmp = config_node_first(node->value);
	for (; tmp != NULL; tmp = config_node_next(tmp)) {
		node = tmp->data;

		if (node->type != NODE_TYPE_BLOCK)
			continue;

		log = g_new0(LOG_REC, 1);
		logs = g_slist_append(logs, log);

		log->handle = -1;
		log->fname = g_strdup(node->key);
		log->autoopen = config_node_get_bool(node, "auto_open", FALSE);
		log->level = level2bits(config_node_get_str(node, "level", 0));

		signal_emit("log config read", 2, log, node);

		node = config_node_section(node, "items", -1);
		if (node != NULL)
			log_items_read_config(node, log);

		if (log->autoopen || gslist_find_string(fnames, log->fname))
			log_start_logging(log);
	}

	g_slist_foreach(fnames, (GFunc) g_free, NULL);
	g_slist_free(fnames);
}
Beispiel #7
0
/* SYNTAX: WINDOW LOG on|off|toggle [<filename>] */
static void cmd_window_log(const char *data)
{
	LOG_REC *log;
	char *set, *fname, window[MAX_INT_STRLEN];
	void *free_arg;
	int open_log, close_log;

	if (!cmd_get_params(data, &free_arg, 2, &set, &fname))
		return;

        ltoa(window, active_win->refnum);
	log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, window, NULL, NULL);

        open_log = close_log = FALSE;
	if (g_ascii_strcasecmp(set, "ON") == 0)
		open_log = TRUE;
	else if (g_ascii_strcasecmp(set, "OFF") == 0) {
		close_log = TRUE;
	} else if (g_ascii_strcasecmp(set, "TOGGLE") == 0) {
                open_log = log == NULL;
                close_log = log != NULL;
	} else {
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_TOGGLE);
		cmd_params_free(free_arg);
		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);
		log = log_create_rec(fname, MSGLEVEL_ALL);
		log->colorizer = log_colorizer_strip;
                log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, NULL);
		log_update(log);
		g_free(fname);
	}

	if (open_log && log != NULL) {
		log_start_logging(log);
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, log->fname);
	} else if (close_log && log != NULL && log->handle != -1) {
		log_stop_logging(log);
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, log->fname);
	}

        cmd_params_free(free_arg);
}
Beispiel #8
0
static void cmd_log_open(const char *data)
{
    /* /LOG OPEN [-noopen] [-autoopen] [-targets <targets>] [-window]
                 [-rotate hour|day|week|month] <fname> [<levels>] */
    char *params, *args, *targetarg, *rotatearg, *fname, *levels;
    char window[MAX_INT_STRLEN];
    LOG_REC *log;
    int level, rotate;

    args = "targets rotate";
    params = cmd_get_params(data, 5 | PARAM_FLAG_MULTIARGS | PARAM_FLAG_GETREST,
                            &args, &targetarg, &rotatearg, &fname, &levels);
    if (*fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

    rotate = LOG_ROTATE_NEVER;
    if (stristr(args, "-rotate")) {
        rotate = log_str2rotate(rotatearg);
        if (rotate < 0) rotate = LOG_ROTATE_NEVER;
    }

    level = level2bits(levels);
    if (level == 0) level = MSGLEVEL_ALL;

    if (stristr(args, "-window")) {
        /* log by window ref# */
        ltoa(window, active_win->refnum);
        targetarg = window;
    }

    log = log_create_rec(fname, level, targetarg);
    if (log != NULL) {
        if (stristr(args, "-autoopen"))
            log->autoopen = TRUE;
        log->rotate = rotate;
        log_update(log);

        if (log->handle == -1 && stristr(args, "-noopen") == NULL) {
            /* start logging */
            if (log_start_logging(log)) {
                printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
                            IRCTXT_LOG_OPENED, fname);
            } else {
                log_close(log);
            }
        }
    }

    g_free(params);
}
Beispiel #9
0
static void cmd_window_log(const char *data)
{
    /* /WINDOW LOG ON|OFF|TOGGLE [<filename>] */
    LOG_REC *log;
    char *params, *set, *fname, window[MAX_INT_STRLEN];
    int open_log, close_log;

    params = cmd_get_params(data, 2, &set, &fname);

    ltoa(window, active_win->refnum);
    log = log_find_item(window);

    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 {
        printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, IRCTXT_NOT_TOGGLE);
        g_free(params);
        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);
        log = log_create_rec(fname, MSGLEVEL_ALL, window);
        if (log != NULL) log_update(log);
        g_free(fname);
    }

    if (open_log && log != NULL) {
        log_start_logging(log);
        printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, IRCTXT_LOG_OPENED, log->fname);
    } else if (close_log && log != NULL && log->handle != -1) {
        log_stop_logging(log);
        printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, IRCTXT_LOG_CLOSED, log->fname);
    }

    g_free(params);
}
Beispiel #10
0
static void log_rotate_check(LOG_REC *log)
{
	char *new_fname;

	g_return_if_fail(log != NULL);

	if (log->handle == -1 || log->real_fname == NULL)
		return;

	new_fname = log_filename(log);
	if (strcmp(new_fname, log->real_fname) != 0) {
		/* rotate log */
		log_stop_logging(log);
		signal_emit("log rotated", 1, log);

		log_start_logging(log);
	}
	g_free(new_fname);
}