imported.
[crypto.git] / apps / irssi / src / core / rawlog.c
1 /*
2  rawlog.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 "rawlog.h"
23 #include "modules.h"
24 #include "signals.h"
25 #include "misc.h"
26 #include "write-buffer.h"
27 #include "settings.h"
28
29 static int rawlog_lines;
30 static int signal_rawlog;
31 static int log_file_create_mode;
32
33 RAWLOG_REC *rawlog_create(void)
34 {
35         RAWLOG_REC *rec;
36
37         rec = g_new0(RAWLOG_REC, 1);
38         return rec;
39 }
40
41 void rawlog_destroy(RAWLOG_REC *rawlog)
42 {
43         g_return_if_fail(rawlog != NULL);
44
45         g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
46         g_slist_free(rawlog->lines);
47
48         if (rawlog->logging) {
49                 write_buffer_flush();
50                 close(rawlog->handle);
51         }
52         g_free(rawlog);
53 }
54
55 /* NOTE! str must be dynamically allocated and must not be freed after! */
56 static void rawlog_add(RAWLOG_REC *rawlog, char *str)
57 {
58         if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
59                 rawlog->nlines++;
60         else {
61                 g_free(rawlog->lines->data);
62                 rawlog->lines = g_slist_remove(rawlog->lines,
63                                                rawlog->lines->data);
64         }
65
66         if (rawlog->logging) {
67                 write_buffer(rawlog->handle, str, strlen(str));
68                 write_buffer(rawlog->handle, "\n", 1);
69         }
70
71         rawlog->lines = g_slist_append(rawlog->lines, str);
72         signal_emit_id(signal_rawlog, 2, rawlog, str);
73 }
74
75 void rawlog_input(RAWLOG_REC *rawlog, const char *str)
76 {
77         g_return_if_fail(rawlog != NULL);
78         g_return_if_fail(str != NULL);
79
80         rawlog_add(rawlog, g_strdup_printf(">> %s", str));
81 }
82
83 void rawlog_output(RAWLOG_REC *rawlog, const char *str)
84 {
85         g_return_if_fail(rawlog != NULL);
86         g_return_if_fail(str != NULL);
87
88         rawlog_add(rawlog, g_strdup_printf("<< %s", str));
89 }
90
91 void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
92 {
93         g_return_if_fail(rawlog != NULL);
94         g_return_if_fail(str != NULL);
95
96         rawlog_add(rawlog, g_strdup_printf("--> %s", str));
97 }
98
99 static void rawlog_dump(RAWLOG_REC *rawlog, int f)
100 {
101         GSList *tmp;
102
103         for (tmp = rawlog->lines; tmp != NULL; tmp = tmp->next) {
104                 write(f, tmp->data, strlen((char *) tmp->data));
105                 write(f, "\n", 1);
106         }
107 }
108
109 void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
110 {
111         char *path;
112
113         g_return_if_fail(rawlog != NULL);
114         g_return_if_fail(fname != NULL);
115
116         if (rawlog->logging)
117                 return;
118
119         path = convert_home(fname);
120         rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
121                               log_file_create_mode);
122         g_free(path);
123
124         rawlog_dump(rawlog, rawlog->handle);
125         rawlog->logging = rawlog->handle != -1;
126 }
127
128 void rawlog_close(RAWLOG_REC *rawlog)
129 {
130         if (rawlog->logging) {
131                 write_buffer_flush();
132                 close(rawlog->handle);
133                 rawlog->logging = 0;
134         }
135 }
136
137 void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
138 {
139         char *path;
140         int f;
141
142         path = convert_home(fname);
143         f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
144         g_free(path);
145
146         rawlog_dump(rawlog, f);
147         close(f);
148 }
149
150 void rawlog_set_size(int lines)
151 {
152         rawlog_lines = lines;
153 }
154
155 static void read_settings(void)
156 {
157         rawlog_set_size(settings_get_int("rawlog_lines"));
158         log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
159 }
160
161 void rawlog_init(void)
162 {
163         signal_rawlog = signal_get_uniq_id("rawlog");
164
165         settings_add_int("history", "rawlog_lines", 200);
166         read_settings();
167
168         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
169 }
170
171 void rawlog_deinit(void)
172 {
173         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
174 }