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