~ubuntu-branches/ubuntu/intrepid/irssi/intrepid-updates

« back to all changes in this revision

Viewing changes to src/core/rawlog.c

  • Committer: Bazaar Package Importer
  • Author(s): David Pashley
  • Date: 2005-12-10 21:25:51 UTC
  • Revision ID: james.westby@ubuntu.com-20051210212551-5qwm108g7inyu2f2
Tags: upstream-0.8.10
ImportĀ upstreamĀ versionĀ 0.8.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "commands.h"
 
26
#include "misc.h"
 
27
#include "write-buffer.h"
 
28
#include "settings.h"
 
29
 
 
30
#include "servers.h"
 
31
 
 
32
static int rawlog_lines;
 
33
static int signal_rawlog;
 
34
static int log_file_create_mode;
 
35
 
 
36
RAWLOG_REC *rawlog_create(void)
 
37
{
 
38
        RAWLOG_REC *rec;
 
39
 
 
40
        rec = g_new0(RAWLOG_REC, 1);
 
41
        return rec;
 
42
}
 
43
 
 
44
void rawlog_destroy(RAWLOG_REC *rawlog)
 
45
{
 
46
        g_return_if_fail(rawlog != NULL);
 
47
 
 
48
        g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
 
49
        g_slist_free(rawlog->lines);
 
50
 
 
51
        if (rawlog->logging) {
 
52
                write_buffer_flush();
 
53
                close(rawlog->handle);
 
54
        }
 
55
        g_free(rawlog);
 
56
}
 
57
 
 
58
/* NOTE! str must be dynamically allocated and must not be freed after! */
 
59
static void rawlog_add(RAWLOG_REC *rawlog, char *str)
 
60
{
 
61
        if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
 
62
                rawlog->nlines++;
 
63
        else {
 
64
                g_free(rawlog->lines->data);
 
65
                rawlog->lines = g_slist_remove(rawlog->lines,
 
66
                                               rawlog->lines->data);
 
67
        }
 
68
 
 
69
        if (rawlog->logging) {
 
70
                write_buffer(rawlog->handle, str, strlen(str));
 
71
                write_buffer(rawlog->handle, "\n", 1);
 
72
        }
 
73
 
 
74
        rawlog->lines = g_slist_append(rawlog->lines, str);
 
75
        signal_emit_id(signal_rawlog, 2, rawlog, str);
 
76
}
 
77
 
 
78
void rawlog_input(RAWLOG_REC *rawlog, const char *str)
 
79
{
 
80
        g_return_if_fail(rawlog != NULL);
 
81
        g_return_if_fail(str != NULL);
 
82
 
 
83
        rawlog_add(rawlog, g_strdup_printf(">> %s", str));
 
84
}
 
85
 
 
86
void rawlog_output(RAWLOG_REC *rawlog, const char *str)
 
87
{
 
88
        g_return_if_fail(rawlog != NULL);
 
89
        g_return_if_fail(str != NULL);
 
90
 
 
91
        rawlog_add(rawlog, g_strdup_printf("<< %s", str));
 
92
}
 
93
 
 
94
void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
 
95
{
 
96
        g_return_if_fail(rawlog != NULL);
 
97
        g_return_if_fail(str != NULL);
 
98
 
 
99
        rawlog_add(rawlog, g_strdup_printf("--> %s", str));
 
100
}
 
101
 
 
102
static void rawlog_dump(RAWLOG_REC *rawlog, int f)
 
103
{
 
104
        GSList *tmp;
 
105
 
 
106
        for (tmp = rawlog->lines; tmp != NULL; tmp = tmp->next) {
 
107
                write(f, tmp->data, strlen((char *) tmp->data));
 
108
                write(f, "\n", 1);
 
109
        }
 
110
}
 
111
 
 
112
void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
 
113
{
 
114
        char *path;
 
115
 
 
116
        g_return_if_fail(rawlog != NULL);
 
117
        g_return_if_fail(fname != NULL);
 
118
 
 
119
        if (rawlog->logging)
 
120
                return;
 
121
 
 
122
        path = convert_home(fname);
 
123
        rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
 
124
                              log_file_create_mode);
 
125
        g_free(path);
 
126
 
 
127
        rawlog_dump(rawlog, rawlog->handle);
 
128
        rawlog->logging = rawlog->handle != -1;
 
129
}
 
130
 
 
131
void rawlog_close(RAWLOG_REC *rawlog)
 
132
{
 
133
        if (rawlog->logging) {
 
134
                write_buffer_flush();
 
135
                close(rawlog->handle);
 
136
                rawlog->logging = 0;
 
137
        }
 
138
}
 
139
 
 
140
void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
 
141
{
 
142
        char *path;
 
143
        int f;
 
144
 
 
145
        path = convert_home(fname);
 
146
        f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
 
147
        g_free(path);
 
148
 
 
149
        rawlog_dump(rawlog, f);
 
150
        close(f);
 
151
}
 
152
 
 
153
void rawlog_set_size(int lines)
 
154
{
 
155
        rawlog_lines = lines;
 
156
}
 
157
 
 
158
static void read_settings(void)
 
159
{
 
160
        rawlog_set_size(settings_get_int("rawlog_lines"));
 
161
        log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
 
162
}
 
163
 
 
164
static void cmd_rawlog(const char *data, SERVER_REC *server, void *item)
 
165
{
 
166
        command_runsub("rawlog", data, server, item);
 
167
}
 
168
 
 
169
/* SYNTAX: RAWLOG SAVE <file> */
 
170
static void cmd_rawlog_save(const char *data, SERVER_REC *server)
 
171
{
 
172
        g_return_if_fail(data != NULL);
 
173
        if (server == NULL || server->rawlog == NULL)
 
174
                cmd_return_error(CMDERR_NOT_CONNECTED);
 
175
 
 
176
        if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
 
177
        rawlog_save(server->rawlog, data);
 
178
}
 
179
 
 
180
/* SYNTAX: RAWLOG OPEN <file> */
 
181
static void cmd_rawlog_open(const char *data, SERVER_REC *server)
 
182
{
 
183
        g_return_if_fail(data != NULL);
 
184
        if (server == NULL || server->rawlog == NULL)
 
185
                cmd_return_error(CMDERR_NOT_CONNECTED);
 
186
 
 
187
        if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
 
188
        rawlog_open(server->rawlog, data);
 
189
}
 
190
 
 
191
/* SYNTAX: RAWLOG CLOSE */
 
192
static void cmd_rawlog_close(const char *data, SERVER_REC *server)
 
193
{
 
194
        g_return_if_fail(data != NULL);
 
195
        if (server == NULL || server->rawlog == NULL)
 
196
                cmd_return_error(CMDERR_NOT_CONNECTED);
 
197
 
 
198
        rawlog_close(server->rawlog);
 
199
}
 
200
 
 
201
void rawlog_init(void)
 
202
{
 
203
        signal_rawlog = signal_get_uniq_id("rawlog");
 
204
 
 
205
        settings_add_int("history", "rawlog_lines", 200);
 
206
        read_settings();
 
207
 
 
208
        signal_add("setup changed", (SIGNAL_FUNC) read_settings);
 
209
 
 
210
        command_bind("rawlog", NULL, (SIGNAL_FUNC) cmd_rawlog);
 
211
        command_bind("rawlog save", NULL, (SIGNAL_FUNC) cmd_rawlog_save);
 
212
        command_bind("rawlog open", NULL, (SIGNAL_FUNC) cmd_rawlog_open);
 
213
        command_bind("rawlog close", NULL, (SIGNAL_FUNC) cmd_rawlog_close);
 
214
}
 
215
 
 
216
void rawlog_deinit(void)
 
217
{
 
218
        signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
 
219
 
 
220
        command_unbind("rawlog", (SIGNAL_FUNC) cmd_rawlog);
 
221
        command_unbind("rawlog save", (SIGNAL_FUNC) cmd_rawlog_save);
 
222
        command_unbind("rawlog open", (SIGNAL_FUNC) cmd_rawlog_open);
 
223
        command_unbind("rawlog close", (SIGNAL_FUNC) cmd_rawlog_close);
 
224
}