Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / log-away.c
1 /*
2  log-away.c : Awaylog handling
3
4     Copyright (C) 1999-2001 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 "signals.h"
23 #include "levels.h"
24 #include "log.h"
25 #include "servers.h"
26 #include "settings.h"
27
28 static LOG_REC *awaylog;
29 static int away_filepos;
30 static int away_msgs;
31
32 static void sig_log_written(LOG_REC *log)
33 {
34         if (log != awaylog) return;
35
36         away_msgs++;
37 }
38
39 static void awaylog_open(void)
40 {
41         const char *fname;
42         LOG_REC *log;
43         int level;
44
45         fname = settings_get_str("awaylog_file");
46         level = settings_get_level("awaylog_level");
47         if (*fname == '\0' || level == 0) return;
48
49         log = log_find(fname);
50         if (log != NULL && log->handle != -1)
51                 return; /* already open */
52
53         if (log == NULL) {
54                 log = log_create_rec(fname, level);
55                 log->temp = TRUE;
56                 log_update(log);
57         }
58
59         if (!log_start_logging(log)) {
60                 /* creating log file failed? close it. */
61                 log_close(log);
62                 return;
63         }
64
65         awaylog = log;
66         away_filepos = lseek(log->handle, 0, SEEK_CUR);
67         away_msgs = 0;
68 }
69
70 static void awaylog_close(void)
71 {
72         const char *fname;
73         LOG_REC *log;
74
75         fname = settings_get_str("awaylog_file");
76         if (*fname == '\0') return;
77
78         log = log_find(fname);
79         if (log == NULL || log->handle == -1) {
80                 /* awaylog not open */
81                 return;
82         }
83
84         if (awaylog == log) awaylog = NULL;
85
86         signal_emit("awaylog show", 3, log, GINT_TO_POINTER(away_msgs),
87                     GINT_TO_POINTER(away_filepos));
88         log_close(log);
89 }
90
91 static void sig_away_changed(SERVER_REC *server)
92 {
93         if (server->usermode_away)
94                 awaylog_open();
95         else
96                 awaylog_close();
97 }
98
99 void log_away_init(void)
100 {
101         char *awaylog_file;
102
103         awaylog = NULL;
104         away_filepos = 0;
105         away_msgs = 0;
106
107         awaylog_file = g_strconcat(get_irssi_dir(), "/away.log", NULL);
108         settings_add_str("log", "awaylog_file", awaylog_file);
109         g_free(awaylog_file);
110         settings_add_level("log", "awaylog_level", "msgs hilight");
111
112         signal_add("log written", (SIGNAL_FUNC) sig_log_written);
113         signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
114 }
115
116 void log_away_deinit(void)
117 {
118         signal_remove("log written", (SIGNAL_FUNC) sig_log_written);
119         signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);
120 }