110
|
1 |
/* |
|
2 |
* histolog.c -- File history handling |
|
3 |
* |
|
4 |
* Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
|
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 (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* 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 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
|
|
22 |
#include <string.h> |
|
23 |
#include <stdlib.h> |
|
24 |
#include <time.h> |
113
|
25 |
#include <ctype.h> |
|
26 |
#include <sys/types.h> |
|
27 |
#include <sys/stat.h> |
|
28 |
#include <fcntl.h> |
110
|
29 |
|
|
30 |
#include "histolog.h" |
178
|
31 |
#include "hbuf.h" |
113
|
32 |
#include "jabglue.h" |
110
|
33 |
#include "screen.h" |
|
34 |
|
|
35 |
static guint UseFileLogging; |
177
|
36 |
static guint FileLoadLogs; |
110
|
37 |
static char *RootDir; |
|
38 |
|
|
39 |
|
|
40 |
// user_histo_file() |
|
41 |
// Returns history filename for the given jid |
|
42 |
// Note: the caller *must* free the filename after use (if not null). |
111
|
43 |
static char *user_histo_file(const char *jid) |
110
|
44 |
{ |
|
45 |
char *filename; |
186
|
46 |
char *lowerid, *p; |
178
|
47 |
if (!UseFileLogging && !FileLoadLogs) return NULL; |
110
|
48 |
|
186
|
49 |
lowerid = g_strdup(jid); |
|
50 |
for (p=lowerid; *p ; p++) |
|
51 |
*p = tolower(*p); |
|
52 |
|
110
|
53 |
filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
|
54 |
strcpy(filename, RootDir); |
186
|
55 |
strcat(filename, lowerid); |
|
56 |
g_free(lowerid); |
110
|
57 |
return filename; |
|
58 |
} |
|
59 |
|
178
|
60 |
// write_histo_line() |
110
|
61 |
// Adds a history (multi-)line to the jid's history logfile |
113
|
62 |
static void write_histo_line(const char *jid, |
|
63 |
time_t timestamp, guchar type, guchar info, const char *data) |
110
|
64 |
{ |
|
65 |
guint len = 0; |
113
|
66 |
FILE *fp; |
110
|
67 |
time_t ts; |
113
|
68 |
const char *p; |
178
|
69 |
char *filename; |
110
|
70 |
|
178
|
71 |
if (!UseFileLogging) return; |
|
72 |
|
|
73 |
filename = user_histo_file(jid); |
110
|
74 |
|
|
75 |
// If timestamp is null, get current date |
|
76 |
if (timestamp) |
|
77 |
ts = timestamp; |
|
78 |
else |
|
79 |
time(&ts); |
|
80 |
|
|
81 |
if (!data) |
|
82 |
data = ""; |
|
83 |
|
|
84 |
// Count number of extra lines |
|
85 |
for (p=data ; *p ; p++) |
|
86 |
if (*p == '\n') len++; |
|
87 |
|
118
|
88 |
/* Line format: "TI DDDDDDDDDD LLL [data]" |
|
89 |
* T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len |
110
|
90 |
* |
|
91 |
* Types: |
|
92 |
* - M message Info: S (send) R (receive) |
|
93 |
* - S status Info: [oaifdcn] |
|
94 |
* We don't check them, we'll trust the caller. |
|
95 |
*/ |
|
96 |
|
113
|
97 |
fp = fopen(filename, "a"); |
155
|
98 |
g_free(filename); |
178
|
99 |
if (!fp) return; |
|
100 |
|
118
|
101 |
fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
113
|
102 |
fclose(fp); |
110
|
103 |
} |
|
104 |
|
178
|
105 |
// hlog_read_history() |
|
106 |
// Reads the jid's history logfile |
|
107 |
void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
108 |
{ |
|
109 |
char *filename; |
|
110 |
guchar type, info; |
|
111 |
char *data, *tail; |
184
|
112 |
time_t timestamp; |
|
113 |
guint prefix_flags; |
178
|
114 |
guint len; |
|
115 |
FILE *fp; |
197
|
116 |
struct stat bufstat; |
193
|
117 |
guint err = 0; |
178
|
118 |
|
|
119 |
if (!FileLoadLogs) return; |
|
120 |
|
|
121 |
data = g_new(char, HBB_BLOCKSIZE+32); |
|
122 |
if (!data) { |
|
123 |
scr_LogPrint("Not enough memory to read history file"); |
|
124 |
return; |
|
125 |
} |
|
126 |
|
|
127 |
filename = user_histo_file(jid); |
|
128 |
|
|
129 |
fp = fopen(filename, "r"); |
|
130 |
g_free(filename); |
|
131 |
if (!fp) { g_free(data); return; } |
|
132 |
|
197
|
133 |
// If file is large (> 512 here), display a message to inform the user |
|
134 |
// (it can take a while...) |
|
135 |
if (!fstat(fileno(fp), &bufstat)) { |
|
136 |
if (bufstat.st_size > 524288) |
|
137 |
scr_LogPrint("Reading <%s> history file...", jid); |
|
138 |
} |
|
139 |
|
178
|
140 |
/* See write_histo_line() for line format... */ |
|
141 |
while (!feof(fp)) { |
|
142 |
if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break; |
|
143 |
|
|
144 |
for (tail = data; *tail; tail++) ; |
|
145 |
|
|
146 |
type = data[0]; |
|
147 |
info = data[1]; |
|
148 |
if ((type != 'M' && type != 'S') || |
|
149 |
(data[13] != ' ') || (data[17] != ' ')) { |
193
|
150 |
if (!err) { |
|
151 |
scr_LogPrint("Error in history file format (%s)", jid); |
|
152 |
err = 1; |
|
153 |
} |
186
|
154 |
//break; |
|
155 |
continue; |
178
|
156 |
} |
|
157 |
data[13] = data[17] = 0; |
|
158 |
timestamp = (unsigned long) atol(&data[3]); |
|
159 |
len = (unsigned long) atol(&data[14]); |
|
160 |
|
|
161 |
// Some checks |
|
162 |
if (((type == 'M') && (info != 'S' && info != 'R')) || |
183
|
163 |
((type == 'I') && (!strchr("OAIFDCN", info)))) { |
193
|
164 |
if (!err) { |
|
165 |
scr_LogPrint("Error in history file format (%s)", jid); |
|
166 |
err = 1; |
|
167 |
} |
186
|
168 |
//break; |
|
169 |
continue; |
178
|
170 |
} |
|
171 |
|
197
|
172 |
// XXX This will fail when a message is too big |
178
|
173 |
while (len--) { |
|
174 |
if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break; |
|
175 |
|
|
176 |
while (*tail) tail++; |
|
177 |
} |
197
|
178 |
// Remove last CR |
178
|
179 |
if ((tail > data+18) && (*(tail-1) == '\n')) |
|
180 |
*(tail-1) = 0; |
|
181 |
|
|
182 |
if (type == 'M') { |
|
183 |
if (info == 'S') |
184
|
184 |
prefix_flags = HBB_PREFIX_OUT; |
178
|
185 |
else |
184
|
186 |
prefix_flags = HBB_PREFIX_IN; |
|
187 |
hbuf_add_line(p_buddyhbuf, &data[18], timestamp, prefix_flags, width); |
193
|
188 |
err = 0; |
178
|
189 |
} |
|
190 |
} |
|
191 |
fclose(fp); |
|
192 |
g_free(data); |
|
193 |
} |
|
194 |
|
110
|
195 |
// hlog_enable() |
|
196 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
177
|
197 |
// If loadfiles is TRUE, we will try to load buddies history logs from file. |
|
198 |
void hlog_enable(guint enable, char *root_dir, guint loadfiles) |
110
|
199 |
{ |
|
200 |
UseFileLogging = enable; |
177
|
201 |
FileLoadLogs = loadfiles; |
110
|
202 |
|
177
|
203 |
if (enable || loadfiles) { |
110
|
204 |
if (root_dir) { |
|
205 |
int l = strlen(root_dir); |
|
206 |
if (l < 1) { |
|
207 |
scr_LogPrint("root_dir too short"); |
|
208 |
UseFileLogging = FALSE; |
|
209 |
return; |
|
210 |
} |
|
211 |
// RootDir must be slash-terminated |
|
212 |
if (root_dir[l-1] == '/') |
|
213 |
RootDir = g_strdup(root_dir); |
|
214 |
else { |
|
215 |
RootDir = g_new(char, l+2); |
|
216 |
strcpy(RootDir, root_dir); |
|
217 |
strcat(RootDir, "/"); |
|
218 |
} |
|
219 |
} else { |
|
220 |
char *home = getenv("HOME"); |
169
|
221 |
char *dir = "/.mcabber/histo/"; |
110
|
222 |
RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
|
223 |
strcpy(RootDir, home); |
|
224 |
strcat(RootDir, dir); |
|
225 |
} |
|
226 |
// FIXME |
|
227 |
// We should check the directory actually exists |
|
228 |
} else // Disable history logging |
|
229 |
if (RootDir) { |
|
230 |
g_free(RootDir); |
|
231 |
} |
|
232 |
} |
|
233 |
|
113
|
234 |
inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
|
235 |
const char *msg) |
|
236 |
{ |
|
237 |
write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
238 |
} |
|
239 |
|
|
240 |
inline void hlog_write_status(const char *jid, time_t timestamp, |
221
|
241 |
enum imstatus status, const char *status_msg) |
113
|
242 |
{ |
|
243 |
// #1 XXX Check status value? |
|
244 |
// #2 We could add a user-readable comment |
|
245 |
write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
221
|
246 |
status_msg); |
113
|
247 |
} |
|
248 |
|