/src/irssi/src/core/log-away.c
Line | Count | Source |
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 along |
17 | | with this program; if not, write to the Free Software Foundation, Inc., |
18 | | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | | */ |
20 | | |
21 | | #include "module.h" |
22 | | #include <irssi/src/core/signals.h> |
23 | | #include <irssi/src/core/levels.h> |
24 | | #include <irssi/src/core/log.h> |
25 | | #include <irssi/src/core/servers.h> |
26 | | #include <irssi/src/core/settings.h> |
27 | | #include <irssi/src/core/write-buffer.h> |
28 | | |
29 | | static LOG_REC *awaylog; |
30 | | static int away_filepos; |
31 | | static int away_msgs; |
32 | | |
33 | | static void sig_log_written(LOG_REC *log) |
34 | 63.1k | { |
35 | 63.1k | if (log != awaylog) return; |
36 | | |
37 | 63.1k | away_msgs++; |
38 | 63.1k | } |
39 | | |
40 | | static void awaylog_open(void) |
41 | 9.82k | { |
42 | 9.82k | const char *fname; |
43 | 9.82k | LOG_REC *log; |
44 | 9.82k | int level; |
45 | | |
46 | 9.82k | fname = settings_get_str("awaylog_file"); |
47 | 9.82k | level = settings_get_level("awaylog_level"); |
48 | 9.82k | if (*fname == '\0' || level == 0) return; |
49 | | |
50 | 9.82k | log = log_find(fname); |
51 | 9.82k | if (log != NULL && log->handle != -1) |
52 | 2.64k | return; /* already open */ |
53 | | |
54 | 7.18k | if (log == NULL) { |
55 | 7.18k | log = log_create_rec(fname, level); |
56 | 7.18k | log->temp = TRUE; |
57 | 7.18k | log_update(log); |
58 | 7.18k | } |
59 | | |
60 | 7.18k | if (!log_start_logging(log)) { |
61 | | /* creating log file failed? close it. */ |
62 | 0 | log_close(log); |
63 | 0 | return; |
64 | 0 | } |
65 | | |
66 | | /* Flush the dirty buffers to disk before acquiring the file position */ |
67 | 7.18k | write_buffer_flush(); |
68 | | |
69 | 7.18k | awaylog = log; |
70 | 7.18k | away_filepos = lseek(log->handle, 0, SEEK_CUR); |
71 | 7.18k | away_msgs = 0; |
72 | 7.18k | } |
73 | | |
74 | | static void awaylog_close(void) |
75 | 10.7k | { |
76 | 10.7k | const char *fname; |
77 | 10.7k | LOG_REC *log; |
78 | | |
79 | 10.7k | fname = settings_get_str("awaylog_file"); |
80 | 10.7k | if (*fname == '\0') return; |
81 | | |
82 | 10.7k | log = log_find(fname); |
83 | 10.7k | if (log == NULL || log->handle == -1) { |
84 | | /* awaylog not open */ |
85 | 3.59k | return; |
86 | 3.59k | } |
87 | | |
88 | 7.18k | if (awaylog == log) awaylog = NULL; |
89 | | |
90 | | /* Flush the dirty buffers to disk before showing the away log */ |
91 | 7.18k | write_buffer_flush(); |
92 | | |
93 | 7.18k | signal_emit("awaylog show", 3, log, GINT_TO_POINTER(away_msgs), |
94 | 7.18k | GINT_TO_POINTER(away_filepos)); |
95 | 7.18k | log_close(log); |
96 | 7.18k | } |
97 | | |
98 | | static void sig_away_changed(SERVER_REC *server) |
99 | 20.6k | { |
100 | 20.6k | if (server->usermode_away) |
101 | 9.82k | awaylog_open(); |
102 | 10.7k | else |
103 | 10.7k | awaylog_close(); |
104 | 20.6k | } |
105 | | |
106 | | void log_away_init(void) |
107 | 8 | { |
108 | 8 | char *awaylog_file; |
109 | | |
110 | 8 | awaylog = NULL; |
111 | 8 | away_filepos = 0; |
112 | 8 | away_msgs = 0; |
113 | | |
114 | 8 | awaylog_file = g_strconcat(get_irssi_dir(), "/away.log", NULL); |
115 | 8 | settings_add_str("log", "awaylog_file", awaylog_file); |
116 | 8 | g_free(awaylog_file); |
117 | 8 | settings_add_level("log", "awaylog_level", "msgs hilight"); |
118 | | |
119 | 8 | signal_add("log written", (SIGNAL_FUNC) sig_log_written); |
120 | 8 | signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed); |
121 | 8 | } |
122 | | |
123 | | void log_away_deinit(void) |
124 | 0 | { |
125 | 0 | signal_remove("log written", (SIGNAL_FUNC) sig_log_written); |
126 | | signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed); |
127 | 0 | } |