724e4b4a1c8b4eee6639eea2a9996baeca029eb0
[silc.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, *levelstr;
42         LOG_REC *log;
43         int level;
44
45         fname = settings_get_str("awaylog_file");
46         levelstr = settings_get_str("awaylog_level");
47         if (*fname == '\0' || *levelstr == '\0') return;
48
49         level = level2bits(levelstr);
50         if (level == 0) return;
51
52         log = log_find(fname);
53         if (log != NULL && log->handle != -1)
54                 return; /* already open */
55
56         if (log == NULL) {
57                 log = log_create_rec(fname, level);
58                 log->temp = TRUE;
59                 log_update(log);
60         }
61
62         if (!log_start_logging(log)) {
63                 /* creating log file failed? close it. */
64                 log_close(log);
65                 return;
66         }
67
68         awaylog = log;
69         away_filepos = lseek(log->handle, 0, SEEK_CUR);
70         away_msgs = 0;
71 }
72
73 static void awaylog_close(void)
74 {
75         const char *fname;
76         LOG_REC *log;
77
78         fname = settings_get_str("awaylog_file");
79         if (*fname == '\0') return;
80
81         log = log_find(fname);
82         if (log == NULL || log->handle == -1) {
83                 /* awaylog not open */
84                 return;
85         }
86
87         if (awaylog == log) awaylog = NULL;
88
89         signal_emit("awaylog show", 3, log, GINT_TO_POINTER(away_msgs),
90                     GINT_TO_POINTER(away_filepos));
91         log_close(log);
92 }
93
94 static void sig_away_changed(SERVER_REC *server)
95 {
96         if (server->usermode_away)
97                 awaylog_open();
98         else
99                 awaylog_close();
100 }
101
102 void log_away_init(void)
103 {
104         awaylog = NULL;
105         away_filepos = 0;
106         away_msgs = 0;
107
108         settings_add_str("log", "awaylog_file", IRSSI_DIR_SHORT"/away.log");
109         settings_add_str("log", "awaylog_level", "msgs hilight");
110
111         signal_add("log written", (SIGNAL_FUNC) sig_log_written);
112         signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
113 }
114
115 void log_away_deinit(void)
116 {
117         signal_remove("log written", (SIGNAL_FUNC) sig_log_written);
118         signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);
119 }