Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / fe-common / core / fe-log.c
1 /*
2  fe-log.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "chat-protocols.h"
26 #include "servers.h"
27 #include "levels.h"
28 #include "misc.h"
29 #include "log.h"
30 #include "special-vars.h"
31 #include "settings.h"
32 #include "lib-config/iconfig.h"
33
34 #include "fe-windows.h"
35 #include "window-items.h"
36 #include "formats.h"
37 #include "themes.h"
38 #include "printtext.h"
39
40 /* close autologs after 5 minutes of inactivity */
41 #define AUTOLOG_INACTIVITY_CLOSE (60*5)
42
43 static int autolog_level;
44 static int autoremove_tag;
45 static const char *autolog_path;
46
47 static THEME_REC *log_theme;
48 static int skip_next_printtext;
49 static const char *log_theme_name;
50
51 static int log_dir_create_mode;
52
53 static char *log_colorizer_strip(const char *str)
54 {
55         return strip_codes(str);
56 }
57
58 static void log_add_targets(LOG_REC *log, const char *targets, const char *tag)
59 {
60         char **tmp, **items;
61
62         g_return_if_fail(log != NULL);
63         g_return_if_fail(targets != NULL);
64
65         items = g_strsplit(targets, " ", -1);
66
67         for (tmp = items; *tmp != NULL; tmp++)
68                 log_item_add(log, LOG_ITEM_TARGET, *tmp, tag);
69
70         g_strfreev(items);
71 }
72
73 /* SYNTAX: LOG OPEN [-noopen] [-autoopen] [-window] [-<server tag>]
74                     [-targets <targets>] [-colors]
75                     <fname> [<levels>] */
76 static void cmd_log_open(const char *data)
77 {
78         SERVER_REC *server;
79         GHashTable *optlist;
80         char *targetarg, *fname, *levels, *servertag;
81         void *free_arg;
82         char window[MAX_INT_STRLEN];
83         LOG_REC *log;
84         int level;
85
86         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
87                             PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_OPTIONS,
88                             "log open", &optlist, &fname, &levels))
89                 return;
90         if (*fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
91
92         level = level2bits(levels);
93         log = log_create_rec(fname, level != 0 ? level : MSGLEVEL_ALL);
94
95         /* -<server tag> */
96         server = cmd_options_get_server("log open", optlist, NULL);
97         servertag = server == NULL ? NULL : server->tag;
98
99         if (g_hash_table_lookup(optlist, "window")) {
100                 /* log by window ref# */
101                 targetarg = g_hash_table_lookup(optlist, "targets");
102                 if (targetarg == NULL || !is_numeric(targetarg, '\0')) {
103                         ltoa(window, active_win->refnum);
104                         targetarg = window;
105                 }
106                 log_item_add(log, LOG_ITEM_WINDOW_REFNUM, targetarg,
107                              servertag);
108         } else {
109                 targetarg = g_hash_table_lookup(optlist, "targets");
110                 if (targetarg != NULL && *targetarg != '\0')
111                         log_add_targets(log, targetarg, servertag);
112         }
113
114         if (g_hash_table_lookup(optlist, "autoopen"))
115                 log->autoopen = TRUE;
116
117         if (g_hash_table_lookup(optlist, "colors") == NULL)
118                 log->colorizer = log_colorizer_strip;
119
120         log_update(log);
121
122         if (log->handle == -1 && g_hash_table_lookup(optlist, "noopen") == NULL) {
123                 /* start logging */
124                 if (log_start_logging(log)) {
125                         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
126                                     TXT_LOG_OPENED, fname);
127                 } else {
128                         log_close(log);
129                 }
130         }
131
132         cmd_params_free(free_arg);
133 }
134
135 static LOG_REC *log_find_from_data(const char *data)
136 {
137         GSList *tmp;
138
139         if (!is_numeric(data, ' '))
140                 return log_find(data);
141
142         /* with index number */
143         tmp = g_slist_nth(logs, atoi(data)-1);
144         return tmp == NULL ? NULL : tmp->data;
145 }
146
147 /* SYNTAX: LOG CLOSE <id>|<file> */
148 static void cmd_log_close(const char *data)
149 {
150         LOG_REC *log;
151
152         log = log_find_from_data(data);
153         if (log == NULL)
154                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_LOG_NOT_OPEN, data);
155         else {
156                 log_close(log);
157                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, data);
158         }
159 }
160
161 /* SYNTAX: LOG START <id>|<file> */
162 static void cmd_log_start(const char *data)
163 {
164         LOG_REC *log;
165
166         log = log_find_from_data(data);
167         if (log != NULL) {
168                 log_start_logging(log);
169                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, data);
170         }
171 }
172
173 /* SYNTAX: LOG STOP <id>|<file> */
174 static void cmd_log_stop(const char *data)
175 {
176         LOG_REC *log;
177
178         log = log_find_from_data(data);
179         if (log == NULL || log->handle == -1)
180                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_LOG_NOT_OPEN, data);
181         else {
182                 log_stop_logging(log);
183                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, data);
184         }
185 }
186
187 static char *log_items_get_list(LOG_REC *log)
188 {
189         GSList *tmp;
190         GString *str;
191         char *ret;
192
193         g_return_val_if_fail(log != NULL, NULL);
194         g_return_val_if_fail(log->items != NULL, NULL);
195
196         str = g_string_new(NULL);
197         for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
198                 LOG_ITEM_REC *rec = tmp->data;
199
200                 g_string_sprintfa(str, "%s, ", rec->name);
201         }
202         g_string_truncate(str, str->len-2);
203
204         ret = str->str;
205         g_string_free(str, FALSE);
206         return ret;
207 }
208
209 static void cmd_log_list(void)
210 {
211         GSList *tmp;
212         char *levelstr, *items;
213         int index;
214
215         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST_HEADER);
216         for (tmp = logs, index = 1; tmp != NULL; tmp = tmp->next, index++) {
217                 LOG_REC *rec = tmp->data;
218
219                 levelstr = bits2level(rec->level);
220                 items = rec->items == NULL ? NULL :
221                         log_items_get_list(rec);
222
223                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST,
224                             index, rec->fname, items != NULL ? items : "",
225                             levelstr, rec->autoopen ? " -autoopen" : "");
226
227                 g_free_not_null(items);
228                 g_free(levelstr);
229         }
230         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_LOG_LIST_FOOTER);
231 }
232
233 static void cmd_log(const char *data, SERVER_REC *server, void *item)
234 {
235         if (*data == '\0')
236                 cmd_log_list();
237         else
238                 command_runsub("log", data, server, item);
239 }
240
241 static LOG_REC *logs_find_item(int type, const char *item,
242                                const char *servertag, LOG_ITEM_REC **ret_item)
243 {
244         LOG_ITEM_REC *logitem;
245         GSList *tmp;
246
247         for (tmp = logs; tmp != NULL; tmp = tmp->next) {
248                 LOG_REC *log = tmp->data;
249
250                 logitem = log_item_find(log, type, item, servertag);
251                 if (logitem != NULL) {
252                         if (ret_item != NULL) *ret_item = logitem;
253                         return log;
254                 }
255         }
256
257         return NULL;
258 }
259
260 /* SYNTAX: WINDOW LOG on|off|toggle [<filename>] */
261 static void cmd_window_log(const char *data)
262 {
263         LOG_REC *log;
264         char *set, *fname, window[MAX_INT_STRLEN];
265         void *free_arg;
266         int open_log, close_log;
267
268         if (!cmd_get_params(data, &free_arg, 2, &set, &fname))
269                 return;
270
271         ltoa(window, active_win->refnum);
272         log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, window, NULL, NULL);
273
274         open_log = close_log = FALSE;
275         if (g_strcasecmp(set, "ON") == 0)
276                 open_log = TRUE;
277         else if (g_strcasecmp(set, "OFF") == 0) {
278                 close_log = TRUE;
279         } else if (g_strcasecmp(set, "TOGGLE") == 0) {
280                 open_log = log == NULL;
281                 close_log = log != NULL;
282         } else {
283                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_TOGGLE);
284                 cmd_params_free(free_arg);
285                 return;
286         }
287
288         if (open_log && log == NULL) {
289                 /* irc.log.<windowname> or irc.log.Window<ref#> */
290                 fname = *fname != '\0' ? g_strdup(fname) :
291                         g_strdup_printf("~/irc.log.%s%s",
292                                         active_win->name != NULL ? active_win->name : "Window",
293                                         active_win->name != NULL ? "" : window);
294                 log = log_create_rec(fname, MSGLEVEL_ALL);
295                 log->colorizer = log_colorizer_strip;
296                 log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, NULL);
297                 log_update(log);
298                 g_free(fname);
299         }
300
301         if (open_log && log != NULL) {
302                 log_start_logging(log);
303                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_OPENED, log->fname);
304         } else if (close_log && log != NULL && log->handle != -1) {
305                 log_stop_logging(log);
306                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_CLOSED, log->fname);
307         }
308
309         cmd_params_free(free_arg);
310 }
311
312 /* Create log file entry to window, but don't start logging */
313 /* SYNTAX: WINDOW LOGFILE <file> */
314 static void cmd_window_logfile(const char *data)
315 {
316         LOG_REC *log;
317         char window[MAX_INT_STRLEN];
318
319         ltoa(window, active_win->refnum);
320         log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, window, NULL, NULL);
321
322         if (log != NULL) {
323                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_WINDOWLOG_FILE_LOGGING);
324                 return;
325         }
326
327         log = log_create_rec(data, MSGLEVEL_ALL);
328         log->colorizer = log_colorizer_strip;
329         log_item_add(log, LOG_ITEM_WINDOW_REFNUM, window, NULL);
330         log_update(log);
331
332         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_WINDOWLOG_FILE, data);
333 }
334
335 /* window's refnum changed - update the logs to log the new window refnum */
336 static void sig_window_refnum_changed(WINDOW_REC *window, gpointer old_refnum)
337 {
338         char winnum[MAX_INT_STRLEN];
339         LOG_REC *log;
340         LOG_ITEM_REC *item;
341
342         ltoa(winnum, GPOINTER_TO_INT(old_refnum));
343         log = logs_find_item(LOG_ITEM_WINDOW_REFNUM, winnum, NULL, &item);
344
345         if (log != NULL) {
346                 ltoa(winnum, window->refnum);
347
348                 g_free(item->name);
349                 item->name = g_strdup(winnum);
350         }
351 }
352
353 static void sig_server_disconnected(SERVER_REC *server)
354 {
355         LOG_ITEM_REC *logitem;
356         GSList *tmp, *next;
357
358         for (tmp = logs; tmp != NULL; tmp = next) {
359                 LOG_REC *log = tmp->data;
360                 next = tmp->next;
361
362                 if (!log->temp || log->items == NULL)
363                         continue;
364
365                 logitem = log->items->data;
366                 if (logitem->type == LOG_ITEM_TARGET &&
367                     logitem->servertag != NULL &&
368                     g_strcasecmp(logitem->servertag, server->tag) == 0 &&
369                     server_ischannel(server, logitem->name)) /* kludge again.. so we won't close dcc chats */
370                         log_close(log);
371         }
372 }
373
374 static void autologs_close_all(void)
375 {
376         GSList *tmp, *next;
377
378         for (tmp = logs; tmp != NULL; tmp = next) {
379                 LOG_REC *rec = tmp->data;
380
381                 next = tmp->next;
382                 if (rec->temp) log_close(rec);
383         }
384 }
385
386 /* '%' -> '%%', '/' -> '_' */
387 static char *escape_target(const char *target)
388 {
389         char *str, *p;
390
391         p = str = g_malloc(strlen(target)*2+1);
392         while (*target != '\0') {
393                 if (*target == '/')
394                         *p++ = '_';
395                 else {
396                         if (*target == '%')
397                                 *p++ = '%';
398                         *p++ = *target;
399                 }
400
401                 target++;
402         }
403         *p = '\0';
404
405         return str;
406 }
407
408 static void autolog_open(SERVER_REC *server, const char *server_tag,
409                          const char *target)
410 {
411         LOG_REC *log;
412         char *fname, *dir, *fixed_target, *params;
413
414         log = logs_find_item(LOG_ITEM_TARGET, target, server_tag, NULL);
415         if (log != NULL && !log->failed) {
416                 log_start_logging(log);
417                 return;
418         }
419
420         /* '/' -> '_' - don't even accidentally try to log to
421            #../../../file if you happen to join to such channel..
422
423            '%' -> '%%' - so strftime() won't mess with them */
424         fixed_target = escape_target(target);
425         if (CHAT_PROTOCOL(server)->case_insensitive)
426                 g_strdown(fixed_target);
427
428         /* $0 = target, $1 = server tag */
429         params = g_strconcat(fixed_target, " ", server_tag, NULL);
430         g_free(fixed_target);
431
432         fname = parse_special_string(autolog_path, server, NULL,
433                                      params, NULL, 0);
434         g_free(params);
435
436         if (log_find(fname) == NULL) {
437                 log = log_create_rec(fname, autolog_level);
438                 if (!settings_get_bool("autolog_colors"))
439                         log->colorizer = log_colorizer_strip;
440                 log_item_add(log, LOG_ITEM_TARGET, target, server_tag);
441
442                 dir = g_dirname(log->real_fname);
443                 mkpath(dir, log_dir_create_mode);
444                 g_free(dir);
445
446                 log->temp = TRUE;
447                 log_update(log);
448                 log_start_logging(log);
449         }
450         g_free(fname);
451 }
452
453 static void autolog_open_check(SERVER_REC *server, const char *server_tag,
454                                const char *target, int level)
455 {
456         char **targets, **tmp;
457
458         /* FIXME: kind of a kludge, but we don't want to reopen logs when
459            we're parting the channel with /WINDOW CLOSE.. Maybe a small
460            timeout would be nice instead of immediately closing the log file
461            after "window item destroyed" */
462         if (level == MSGLEVEL_PARTS ||
463             (autolog_level & level) == 0 || target == NULL || *target == '\0')
464                 return;
465
466         /* there can be multiple targets separated with comma */
467         targets = g_strsplit(target, ",", -1);
468         for (tmp = targets; *tmp != NULL; tmp++)
469                 autolog_open(server, server_tag, *tmp);
470         g_strfreev(targets);
471 }
472
473 static void log_single_line(WINDOW_REC *window, const char *server_tag,
474                             const char *target, int level, const char *text)
475 {
476         char windownum[MAX_INT_STRLEN];
477         char **targets, **tmp;
478         LOG_REC *log;
479
480         if (window != NULL) {
481                 /* save to log created with /WINDOW LOG */
482                 ltoa(windownum, window->refnum);
483                 log = logs_find_item(LOG_ITEM_WINDOW_REFNUM,
484                                      windownum, NULL, NULL);
485                 if (log != NULL)
486                         log_write_rec(log, text, level);
487         }
488
489         if (target == NULL)
490                 log_file_write(server_tag, NULL, level, text, FALSE);
491         else {
492                 /* there can be multiple items separated with comma */
493                 targets = g_strsplit(target, ",", -1);
494                 for (tmp = targets; *tmp != NULL; tmp++)
495                         log_file_write(server_tag, *tmp, level, text, FALSE);
496                 g_strfreev(targets);
497         }
498 }
499
500 static void log_line(TEXT_DEST_REC *dest, const char *text)
501 {
502         char **lines, **tmp;
503
504         if (dest->level == MSGLEVEL_NEVER)
505                 return;
506
507         /* let autolog open the log records */
508         autolog_open_check(dest->server, dest->server_tag,
509                            dest->target, dest->level);
510
511         if (logs == NULL)
512                 return;
513
514         /* text may contain one or more lines, log wants to eat them one
515            line at a time */
516         lines = g_strsplit(text, "\n", -1);
517         for (tmp = lines; *tmp != NULL; tmp++)
518                 log_single_line(dest->window, dest->server_tag,
519                                 dest->target, dest->level, *tmp);
520         g_strfreev(lines);
521 }
522
523 static void sig_printtext(TEXT_DEST_REC *dest, const char *text,
524                           const char *stripped)
525 {
526         if (skip_next_printtext) {
527                 skip_next_printtext = FALSE;
528                 return;
529         }
530
531         log_line(dest, text);
532 }
533
534 static void sig_print_format(THEME_REC *theme, const char *module,
535                              TEXT_DEST_REC *dest, void *formatnum, char **args)
536 {
537         char *str, *linestart, *tmp;
538
539         if (log_theme == NULL) {
540                 /* theme isn't loaded for some reason (/reload destroys it),
541                    reload it. */
542                 log_theme = theme_load(log_theme_name);
543                 if (log_theme == NULL) return;
544         }
545
546         if (theme == log_theme)
547                 return;
548
549         str = format_get_text_theme_charargs(log_theme, module, dest,
550                                              GPOINTER_TO_INT(formatnum), args);
551         skip_next_printtext = TRUE;
552
553         if (*str != '\0') {
554                 /* add the line start format */
555                 linestart = format_get_level_tag(log_theme, dest);
556                 tmp = str;
557                 str = format_add_linestart(tmp, linestart);
558                 g_free_not_null(linestart);
559                 g_free(tmp);
560
561                 /* strip colors from text, log it. */
562                 log_line(dest, str);
563         }
564         g_free(str);
565
566 }
567
568 static int sig_autoremove(void)
569 {
570         SERVER_REC *server;
571         LOG_ITEM_REC *logitem;
572         GSList *tmp, *next;
573         time_t removetime;
574
575         removetime = time(NULL)-AUTOLOG_INACTIVITY_CLOSE;
576         for (tmp = logs; tmp != NULL; tmp = next) {
577                 LOG_REC *log = tmp->data;
578
579                 next = tmp->next;
580
581                 if (!log->temp || log->last > removetime || log->items == NULL)
582                         continue;
583
584                 /* Close only logs with private messages */
585                 logitem = log->items->data;
586                 if (logitem->servertag == NULL)
587                         continue;
588
589                 server = server_find_tag(logitem->servertag);
590                 if (logitem->type == LOG_ITEM_TARGET &&
591                     server != NULL && !server_ischannel(server, logitem->name))
592                         log_close(log);
593         }
594         return 1;
595 }
596
597 static void sig_window_item_remove(WINDOW_REC *window, WI_ITEM_REC *item)
598 {
599         LOG_REC *log;
600
601         log = logs_find_item(LOG_ITEM_TARGET, item->visible_name,
602                              item->server == NULL ? NULL :
603                              item->server->tag, NULL);
604         if (log != NULL && log->temp)
605                 log_close(log);
606 }
607
608 static void sig_log_locked(LOG_REC *log)
609 {
610         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
611                     TXT_LOG_LOCKED, log->fname);
612 }
613
614 static void sig_log_create_failed(LOG_REC *log)
615 {
616         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
617                     TXT_LOG_CREATE_FAILED,
618                     log->real_fname, g_strerror(errno));
619 }
620
621 static void sig_log_new(LOG_REC *log)
622 {
623         if (!settings_get_bool("awaylog_colors") &&
624             strcmp(log->fname, settings_get_str("awaylog_file")) == 0)
625                 log->colorizer = log_colorizer_strip;
626 }
627
628 static void sig_log_config_read(LOG_REC *log, CONFIG_NODE *node)
629 {
630         if (!config_node_get_bool(node, "colors", FALSE))
631                 log->colorizer = log_colorizer_strip;
632 }
633
634 static void sig_log_config_save(LOG_REC *log, CONFIG_NODE *node)
635 {
636         if (log->colorizer == NULL)
637                 iconfig_node_set_bool(node, "colors", TRUE);
638         else
639                 iconfig_node_set_str(node, "colors", NULL);
640 }
641
642 static void sig_awaylog_show(LOG_REC *log, gpointer pmsgs, gpointer pfilepos)
643 {
644         char *str;
645         int msgs, filepos;
646
647         msgs = GPOINTER_TO_INT(pmsgs);
648         filepos = GPOINTER_TO_INT(pfilepos);
649
650         if (msgs == 0)
651                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_NO_AWAY_MSGS, log->fname);
652         else {
653                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOG_AWAY_MSGS, log->fname, msgs);
654
655                 str = g_strdup_printf("\"%s\" %d", log->fname, filepos);
656                 signal_emit("command cat", 1, str);
657                 g_free(str);
658         }
659 }
660
661 static void sig_theme_destroyed(THEME_REC *theme)
662 {
663         if (theme == log_theme)
664                 log_theme = NULL;
665 }
666
667 static void read_settings(void)
668 {
669         int old_autolog = autolog_level;
670         int log_file_create_mode;
671
672         autolog_path = settings_get_str("autolog_path");
673         autolog_level = !settings_get_bool("autolog") ? 0 :
674                 level2bits(settings_get_str("autolog_level"));
675
676         if (old_autolog && !autolog_level)
677                 autologs_close_all();
678
679         /* write to log files with different theme? */
680         if (log_theme_name != NULL)
681                 signal_remove("print format", (SIGNAL_FUNC) sig_print_format);
682         log_theme_name = settings_get_str("log_theme");
683         if (*log_theme_name == '\0')
684                 log_theme_name = NULL;
685         else
686                 signal_add("print format", (SIGNAL_FUNC) sig_print_format);
687
688         log_theme = log_theme_name == NULL ? NULL :
689                 theme_load(log_theme_name);
690
691         log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
692         log_dir_create_mode = log_file_create_mode;
693         if (log_file_create_mode & 0400) log_dir_create_mode |= 0100;
694         if (log_file_create_mode & 0040) log_dir_create_mode |= 0010;
695         if (log_file_create_mode & 0004) log_dir_create_mode |= 0001;
696 }
697
698 void fe_log_init(void)
699 {
700         autoremove_tag = g_timeout_add(60000, (GSourceFunc) sig_autoremove, NULL);
701         skip_next_printtext = FALSE;
702
703         settings_add_bool("log", "awaylog_colors", TRUE);
704         settings_add_bool("log", "autolog", FALSE);
705         settings_add_bool("log", "autolog_colors", FALSE);
706         settings_add_str("log", "autolog_path", "~/irclogs/$tag/$0.log");
707         settings_add_str("log", "autolog_level", "all -crap -clientcrap -ctcps");
708         settings_add_str("log", "log_theme", "");
709
710         autolog_level = 0;
711         log_theme_name = NULL;
712         read_settings();
713
714         command_bind("log", NULL, (SIGNAL_FUNC) cmd_log);
715         command_bind("log open", NULL, (SIGNAL_FUNC) cmd_log_open);
716         command_bind("log close", NULL, (SIGNAL_FUNC) cmd_log_close);
717         command_bind("log start", NULL, (SIGNAL_FUNC) cmd_log_start);
718         command_bind("log stop", NULL, (SIGNAL_FUNC) cmd_log_stop);
719         command_bind("window log", NULL, (SIGNAL_FUNC) cmd_window_log);
720         command_bind("window logfile", NULL, (SIGNAL_FUNC) cmd_window_logfile);
721         signal_add_first("print text", (SIGNAL_FUNC) sig_printtext);
722         signal_add("window item remove", (SIGNAL_FUNC) sig_window_item_remove);
723         signal_add("window refnum changed", (SIGNAL_FUNC) sig_window_refnum_changed);
724         signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
725         signal_add("log locked", (SIGNAL_FUNC) sig_log_locked);
726         signal_add("log create failed", (SIGNAL_FUNC) sig_log_create_failed);
727         signal_add("log new", (SIGNAL_FUNC) sig_log_new);
728         signal_add("log config read", (SIGNAL_FUNC) sig_log_config_read);
729         signal_add("log config save", (SIGNAL_FUNC) sig_log_config_save);
730         signal_add("awaylog show", (SIGNAL_FUNC) sig_awaylog_show);
731         signal_add("theme destroyed", (SIGNAL_FUNC) sig_theme_destroyed);
732         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
733
734         command_set_options("log open", "noopen autoopen -targets window colors");
735 }
736
737 void fe_log_deinit(void)
738 {
739         g_source_remove(autoremove_tag);
740         if (log_theme_name != NULL)
741                 signal_remove("print format", (SIGNAL_FUNC) sig_print_format);
742
743         command_unbind("log", (SIGNAL_FUNC) cmd_log);
744         command_unbind("log open", (SIGNAL_FUNC) cmd_log_open);
745         command_unbind("log close", (SIGNAL_FUNC) cmd_log_close);
746         command_unbind("log start", (SIGNAL_FUNC) cmd_log_start);
747         command_unbind("log stop", (SIGNAL_FUNC) cmd_log_stop);
748         command_unbind("window log", (SIGNAL_FUNC) cmd_window_log);
749         command_unbind("window logfile", (SIGNAL_FUNC) cmd_window_logfile);
750         signal_remove("print text", (SIGNAL_FUNC) sig_printtext);
751         signal_remove("window item remove", (SIGNAL_FUNC) sig_window_item_remove);
752         signal_remove("window refnum changed", (SIGNAL_FUNC) sig_window_refnum_changed);
753         signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
754         signal_remove("log locked", (SIGNAL_FUNC) sig_log_locked);
755         signal_remove("log create failed", (SIGNAL_FUNC) sig_log_create_failed);
756         signal_remove("log new", (SIGNAL_FUNC) sig_log_new);
757         signal_remove("log config read", (SIGNAL_FUNC) sig_log_config_read);
758         signal_remove("log config save", (SIGNAL_FUNC) sig_log_config_save);
759         signal_remove("awaylog show", (SIGNAL_FUNC) sig_awaylog_show);
760         signal_remove("theme destroyed", (SIGNAL_FUNC) sig_theme_destroyed);
761         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
762 }