author | Mikael Berthe <mikael@lilotux.net> |
Thu, 14 Dec 2006 19:17:50 +0100 | |
changeset 1106 | 8e12137fab20 |
parent 1058 | c0d44a9a99bc |
child 1141 | 5be2408a6534 |
permissions | -rw-r--r-- |
110 | 1 |
/* |
699 | 2 |
* histolog.c -- File history handling |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
3 |
* |
699 | 4 |
* Copyright (C) 2005, 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
110 | 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" |
241 | 33 |
#include "utils.h" |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
34 |
#include "logprint.h" |
635
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
35 |
#include "settings.h" |
727
1c3620668857
Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
36 |
#include "utils.h" |
110 | 37 |
|
38 |
static guint UseFileLogging; |
|
177 | 39 |
static guint FileLoadLogs; |
110 | 40 |
static char *RootDir; |
41 |
||
42 |
||
1058 | 43 |
// user_histo_file(jid) |
110 | 44 |
// Returns history filename for the given jid |
45 |
// Note: the caller *must* free the filename after use (if not null). |
|
1058 | 46 |
static char *user_histo_file(const char *bjid) |
110 | 47 |
{ |
48 |
char *filename; |
|
1106 | 49 |
char *lowerid; |
50 |
||
51 |
if (!(UseFileLogging || FileLoadLogs)) |
|
52 |
return NULL; |
|
110 | 53 |
|
1058 | 54 |
lowerid = g_strdup(bjid); |
1106 | 55 |
if (!lowerid) |
56 |
return NULL; |
|
57 |
mc_strtolower(lowerid); |
|
186 | 58 |
|
1106 | 59 |
filename = g_strdup_printf("%s%s", RootDir, lowerid); |
186 | 60 |
g_free(lowerid); |
110 | 61 |
return filename; |
62 |
} |
|
63 |
||
178 | 64 |
// write_histo_line() |
110 | 65 |
// Adds a history (multi-)line to the jid's history logfile |
1058 | 66 |
static void write_histo_line(const char *bjid, |
113 | 67 |
time_t timestamp, guchar type, guchar info, const char *data) |
110 | 68 |
{ |
69 |
guint len = 0; |
|
113 | 70 |
FILE *fp; |
110 | 71 |
time_t ts; |
113 | 72 |
const char *p; |
178 | 73 |
char *filename; |
241 | 74 |
char str_ts[20]; |
273 | 75 |
int err; |
110 | 76 |
|
1106 | 77 |
if (!UseFileLogging) |
78 |
return; |
|
178 | 79 |
|
1058 | 80 |
filename = user_histo_file(bjid); |
110 | 81 |
|
82 |
// If timestamp is null, get current date |
|
83 |
if (timestamp) |
|
84 |
ts = timestamp; |
|
85 |
else |
|
86 |
time(&ts); |
|
87 |
||
88 |
if (!data) |
|
89 |
data = ""; |
|
90 |
||
91 |
// Count number of extra lines |
|
92 |
for (p=data ; *p ; p++) |
|
93 |
if (*p == '\n') len++; |
|
94 |
||
241 | 95 |
/* Line format: "TI yyyymmddThh:mm:ssZ [data]" |
96 |
* (Old format: "TI DDDDDDDDDD LLL [data])" |
|
97 |
* T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len |
|
110 | 98 |
* |
99 |
* Types: |
|
100 |
* - M message Info: S (send) R (receive) |
|
352 | 101 |
* - S status Info: [_oifdna] |
110 | 102 |
* We don't check them, we'll trust the caller. |
103 |
*/ |
|
104 |
||
113 | 105 |
fp = fopen(filename, "a"); |
155 | 106 |
g_free(filename); |
273 | 107 |
if (!fp) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
108 |
scr_LogPrint(LPRINT_LOGNORM, "Unable to write history " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
109 |
"(cannot open logfile)"); |
273 | 110 |
return; |
111 |
} |
|
178 | 112 |
|
241 | 113 |
to_iso8601(str_ts, ts); |
273 | 114 |
err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 115 |
fclose(fp); |
273 | 116 |
if (err < 0) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
117 |
scr_LogPrint(LPRINT_LOGNORM, "Error while writing to log file: %s", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
118 |
strerror(errno)); |
273 | 119 |
} |
110 | 120 |
} |
121 |
||
178 | 122 |
// hlog_read_history() |
123 |
// Reads the jid's history logfile |
|
1058 | 124 |
void hlog_read_history(const char *bjid, GList **p_buddyhbuf, guint width) |
178 | 125 |
{ |
126 |
char *filename; |
|
127 |
guchar type, info; |
|
128 |
char *data, *tail; |
|
727
1c3620668857
Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
129 |
char *xtext; |
184 | 130 |
time_t timestamp; |
131 |
guint prefix_flags; |
|
178 | 132 |
guint len; |
133 |
FILE *fp; |
|
197 | 134 |
struct stat bufstat; |
193 | 135 |
guint err = 0; |
248 | 136 |
guint ln = 0; // line number |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
137 |
time_t starttime; |
178 | 138 |
|
1106 | 139 |
if (!FileLoadLogs) |
140 |
return; |
|
178 | 141 |
|
1058 | 142 |
if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) && |
635
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
143 |
(settings_opt_get_int("load_muc_logs") != 1)) |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
144 |
return; |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
145 |
|
178 | 146 |
data = g_new(char, HBB_BLOCKSIZE+32); |
147 |
if (!data) { |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
148 |
scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file"); |
178 | 149 |
return; |
150 |
} |
|
151 |
||
1058 | 152 |
filename = user_histo_file(bjid); |
178 | 153 |
|
154 |
fp = fopen(filename, "r"); |
|
155 |
g_free(filename); |
|
1106 | 156 |
if (!fp) { |
157 |
g_free(data); |
|
158 |
return; |
|
159 |
} |
|
178 | 160 |
|
394
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
161 |
// If file is large (> 3MB here), display a message to inform the user |
197 | 162 |
// (it can take a while...) |
163 |
if (!fstat(fileno(fp), &bufstat)) { |
|
394
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
164 |
if (bufstat.st_size > 3145728) |
1058 | 165 |
scr_LogPrint(LPRINT_LOGNORM, "Reading <%s> history file...", bjid); |
197 | 166 |
} |
167 |
||
1106 | 168 |
starttime = 0L; |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
169 |
if (settings_opt_get_int("max_history_age") > 0) { |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
170 |
int maxdays = settings_opt_get_int("max_history_age"); |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
171 |
time(&starttime); |
1106 | 172 |
if (maxdays >= starttime/86400L) |
173 |
starttime = 0L; |
|
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
174 |
else |
1106 | 175 |
starttime -= maxdays * 86400L; |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
176 |
} |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
177 |
|
178 | 178 |
/* See write_histo_line() for line format... */ |
179 |
while (!feof(fp)) { |
|
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
180 |
guint dataoffset = 25; |
1106 | 181 |
|
182 |
if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) |
|
183 |
break; |
|
248 | 184 |
ln++; |
178 | 185 |
|
186 |
for (tail = data; *tail; tail++) ; |
|
187 |
||
188 |
type = data[0]; |
|
189 |
info = data[1]; |
|
241 | 190 |
|
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
191 |
if ((type != 'M' && type != 'S') || |
259 | 192 |
((data[11] != 'T') || (data[20] != 'Z') || |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
193 |
(data[21] != ' ') || |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
194 |
(data[25] != ' ' && data[26] != ' '))) { |
193 | 195 |
if (!err) { |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
196 |
scr_LogPrint(LPRINT_LOGNORM, |
1058 | 197 |
"Error in history file format (%s), l.%u", bjid, ln); |
193 | 198 |
err = 1; |
199 |
} |
|
186 | 200 |
continue; |
178 | 201 |
} |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
202 |
// The number of lines can be written with 3 or 4 bytes. |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
203 |
if (data[25] != ' ') dataoffset = 26; |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
204 |
data[21] = data[dataoffset] = 0; |
259 | 205 |
timestamp = from_iso8601(&data[3], 1); |
206 |
len = (guint) atoi(&data[22]); |
|
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
207 |
|
178 | 208 |
// Some checks |
209 |
if (((type == 'M') && (info != 'S' && info != 'R')) || |
|
277
4d7040cff8ee
Remove busy/occupied status, which does not really exist
Mikael Berthe <mikael@lilotux.net>
parents:
273
diff
changeset
|
210 |
((type == 'I') && (!strchr("OAIFDN", info)))) { |
193 | 211 |
if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
212 |
scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
1058 | 213 |
bjid, ln); |
193 | 214 |
err = 1; |
215 |
} |
|
186 | 216 |
continue; |
178 | 217 |
} |
218 |
||
197 | 219 |
// XXX This will fail when a message is too big |
178 | 220 |
while (len--) { |
248 | 221 |
ln++; |
259 | 222 |
if (fgets(tail, HBB_BLOCKSIZE+27 - (tail-data), fp) == NULL) |
249 | 223 |
break; |
178 | 224 |
|
225 |
while (*tail) tail++; |
|
226 |
} |
|
248 | 227 |
// Small check for too long messages |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
228 |
if (tail >= HBB_BLOCKSIZE+dataoffset+1 + data) { |
249 | 229 |
// Maybe we will have a parse error on next, because this |
230 |
// message is big (maybe too big). |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
231 |
scr_LogPrint(LPRINT_LOGNORM, "A message could be too big " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
232 |
"in history file..."); |
248 | 233 |
} |
251 | 234 |
// Remove last CR (we keep it if the line is empty, too) |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
235 |
if ((tail > data+dataoffset+1) && (*(tail-1) == '\n')) |
178 | 236 |
*(tail-1) = 0; |
237 |
||
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
238 |
// Check if the data is older than max_history_age |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
239 |
if (starttime) { |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
240 |
if (timestamp > starttime) |
1106 | 241 |
starttime = 0L; // From now on, load everything |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
242 |
else |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
243 |
continue; |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
244 |
} |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
245 |
|
178 | 246 |
if (type == 'M') { |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
247 |
char *converted; |
178 | 248 |
if (info == 'S') |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
727
diff
changeset
|
249 |
prefix_flags = HBB_PREFIX_OUT | HBB_PREFIX_HLIGHT; |
178 | 250 |
else |
184 | 251 |
prefix_flags = HBB_PREFIX_IN; |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
252 |
converted = from_utf8(&data[dataoffset+1]); |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
253 |
if (converted) { |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
254 |
xtext = ut_expand_tabs(converted); // Expand tabs |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
255 |
hbuf_add_line(p_buddyhbuf, xtext, timestamp, prefix_flags, width); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
256 |
if (xtext != converted) |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
257 |
g_free(xtext); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
258 |
g_free(converted); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
259 |
} |
193 | 260 |
err = 0; |
178 | 261 |
} |
262 |
} |
|
263 |
fclose(fp); |
|
264 |
g_free(data); |
|
265 |
} |
|
266 |
||
110 | 267 |
// hlog_enable() |
268 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
|
177 | 269 |
// If loadfiles is TRUE, we will try to load buddies history logs from file. |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
277
diff
changeset
|
270 |
void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 271 |
{ |
272 |
UseFileLogging = enable; |
|
177 | 273 |
FileLoadLogs = loadfiles; |
110 | 274 |
|
177 | 275 |
if (enable || loadfiles) { |
110 | 276 |
if (root_dir) { |
277 |
int l = strlen(root_dir); |
|
278 |
if (l < 1) { |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
279 |
scr_LogPrint(LPRINT_LOGNORM, "Error: logging dir name too short"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
280 |
UseFileLogging = FileLoadLogs = FALSE; |
110 | 281 |
return; |
282 |
} |
|
283 |
// RootDir must be slash-terminated |
|
284 |
if (root_dir[l-1] == '/') |
|
285 |
RootDir = g_strdup(root_dir); |
|
1106 | 286 |
else |
287 |
RootDir = g_strdup_printf("%s/", root_dir); |
|
110 | 288 |
} else { |
289 |
char *home = getenv("HOME"); |
|
1106 | 290 |
const char *dir = "/.mcabber/histo/"; |
291 |
RootDir = g_strdup_printf("%s%s", home, dir); |
|
110 | 292 |
} |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
293 |
// Check directory permissions (should not be readable by group/others) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
294 |
if (checkset_perm(RootDir, TRUE) == -1) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
295 |
// The directory does not actually exists |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
296 |
g_free(RootDir); |
1106 | 297 |
RootDir = NULL; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
298 |
scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
299 |
"history log directory, logging DISABLED"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
300 |
UseFileLogging = FileLoadLogs = FALSE; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
301 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
302 |
} else { // Disable history logging |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
772
diff
changeset
|
303 |
g_free(RootDir); |
1106 | 304 |
RootDir = NULL; |
110 | 305 |
} |
306 |
} |
|
307 |
||
1058 | 308 |
inline void hlog_write_message(const char *bjid, time_t timestamp, int sent, |
113 | 309 |
const char *msg) |
310 |
{ |
|
1058 | 311 |
write_histo_line(bjid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
113 | 312 |
} |
313 |
||
1058 | 314 |
inline void hlog_write_status(const char *bjid, time_t timestamp, |
221 | 315 |
enum imstatus status, const char *status_msg) |
113 | 316 |
{ |
317 |
// #1 XXX Check status value? |
|
318 |
// #2 We could add a user-readable comment |
|
1058 | 319 |
write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]), |
221 | 320 |
status_msg); |
113 | 321 |
} |
322 |
||
580 | 323 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |