1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
rawlog.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "module.h"
#include "rawlog.h"
#include "modules.h"
#include "signals.h"
#include "commands.h"
#include "misc.h"
#include "write-buffer.h"
#include "settings.h"
#include "servers.h"
static int rawlog_lines;
static int signal_rawlog;
static int log_file_create_mode;
static int log_dir_create_mode;
RAWLOG_REC *rawlog_create(void)
{
RAWLOG_REC *rec;
rec = g_new0(RAWLOG_REC, 1);
return rec;
}
void rawlog_destroy(RAWLOG_REC *rawlog)
{
g_return_if_fail(rawlog != NULL);
g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
g_slist_free(rawlog->lines);
if (rawlog->logging) {
write_buffer_flush();
close(rawlog->handle);
}
g_free(rawlog);
}
/* NOTE! str must be dynamically allocated and must not be freed after! */
static void rawlog_add(RAWLOG_REC *rawlog, char *str)
{
if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
rawlog->nlines++;
else {
g_free(rawlog->lines->data);
rawlog->lines = g_slist_remove(rawlog->lines,
rawlog->lines->data);
}
if (rawlog->logging) {
write_buffer(rawlog->handle, str, strlen(str));
write_buffer(rawlog->handle, "\n", 1);
}
rawlog->lines = g_slist_append(rawlog->lines, str);
signal_emit_id(signal_rawlog, 2, rawlog, str);
}
void rawlog_input(RAWLOG_REC *rawlog, const char *str)
{
g_return_if_fail(rawlog != NULL);
g_return_if_fail(str != NULL);
rawlog_add(rawlog, g_strdup_printf(">> %s", str));
}
void rawlog_output(RAWLOG_REC *rawlog, const char *str)
{
g_return_if_fail(rawlog != NULL);
g_return_if_fail(str != NULL);
rawlog_add(rawlog, g_strdup_printf("<< %s", str));
}
void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
{
g_return_if_fail(rawlog != NULL);
g_return_if_fail(str != NULL);
rawlog_add(rawlog, g_strdup_printf("--> %s", str));
}
static void rawlog_dump(RAWLOG_REC *rawlog, int f)
{
GSList *tmp;
ssize_t ret = 0;
for (tmp = rawlog->lines; ret != -1 && tmp != NULL; tmp = tmp->next) {
ret = write(f, tmp->data, strlen((char *) tmp->data));
if (ret != -1)
ret = write(f, "\n", 1);
}
if (ret == -1) {
g_warning("rawlog write() failed: %s", strerror(errno));
}
}
void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
{
char *path;
g_return_if_fail(rawlog != NULL);
g_return_if_fail(fname != NULL);
if (rawlog->logging)
return;
path = convert_home(fname);
rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
log_file_create_mode);
g_free(path);
rawlog_dump(rawlog, rawlog->handle);
rawlog->logging = rawlog->handle != -1;
}
void rawlog_close(RAWLOG_REC *rawlog)
{
if (rawlog->logging) {
write_buffer_flush();
close(rawlog->handle);
rawlog->logging = 0;
}
}
void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
{
char *path, *dir;
int f;
dir = g_path_get_dirname(fname);
g_mkdir_with_parents(dir, log_dir_create_mode);
g_free(dir);
path = convert_home(fname);
f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
g_free(path);
if (f < 0) {
g_warning("rawlog open() failed: %s", strerror(errno));
return;
}
rawlog_dump(rawlog, f);
close(f);
}
void rawlog_set_size(int lines)
{
rawlog_lines = lines;
}
static void read_settings(void)
{
rawlog_set_size(settings_get_int("rawlog_lines"));
log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
log_dir_create_mode = log_file_create_mode;
if (log_file_create_mode & 0400) log_dir_create_mode |= 0100;
if (log_file_create_mode & 0040) log_dir_create_mode |= 0010;
if (log_file_create_mode & 0004) log_dir_create_mode |= 0001;
}
static void cmd_rawlog(const char *data, SERVER_REC *server, void *item)
{
command_runsub("rawlog", data, server, item);
}
/* SYNTAX: RAWLOG SAVE <file> */
static void cmd_rawlog_save(const char *data, SERVER_REC *server)
{
g_return_if_fail(data != NULL);
if (server == NULL || server->rawlog == NULL)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
rawlog_save(server->rawlog, data);
}
/* SYNTAX: RAWLOG OPEN <file> */
static void cmd_rawlog_open(const char *data, SERVER_REC *server)
{
g_return_if_fail(data != NULL);
if (server == NULL || server->rawlog == NULL)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
rawlog_open(server->rawlog, data);
}
/* SYNTAX: RAWLOG CLOSE */
static void cmd_rawlog_close(const char *data, SERVER_REC *server)
{
g_return_if_fail(data != NULL);
if (server == NULL || server->rawlog == NULL)
cmd_return_error(CMDERR_NOT_CONNECTED);
rawlog_close(server->rawlog);
}
void rawlog_init(void)
{
signal_rawlog = signal_get_uniq_id("rawlog");
settings_add_int("history", "rawlog_lines", 200);
read_settings();
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
command_bind("rawlog", NULL, (SIGNAL_FUNC) cmd_rawlog);
command_bind("rawlog save", NULL, (SIGNAL_FUNC) cmd_rawlog_save);
command_bind("rawlog open", NULL, (SIGNAL_FUNC) cmd_rawlog_open);
command_bind("rawlog close", NULL, (SIGNAL_FUNC) cmd_rawlog_close);
}
void rawlog_deinit(void)
{
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
command_unbind("rawlog", (SIGNAL_FUNC) cmd_rawlog);
command_unbind("rawlog save", (SIGNAL_FUNC) cmd_rawlog_save);
command_unbind("rawlog open", (SIGNAL_FUNC) cmd_rawlog_open);
command_unbind("rawlog close", (SIGNAL_FUNC) cmd_rawlog_close);
}
|