author | Mikael Berthe <mikael@lilotux.net> |
Mon, 25 Jul 2005 21:46:35 +0100 | |
changeset 374 | bd5638c21834 |
parent 366 | c30c0d0eb129 |
child 378 | 2e6c7b1440d1 |
permissions | -rw-r--r-- |
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]; |
273 | 72 |
int err; |
110 | 73 |
|
178 | 74 |
if (!UseFileLogging) return; |
75 |
||
76 |
filename = user_histo_file(jid); |
|
110 | 77 |
|
78 |
// If timestamp is null, get current date |
|
79 |
if (timestamp) |
|
80 |
ts = timestamp; |
|
81 |
else |
|
82 |
time(&ts); |
|
83 |
||
84 |
if (!data) |
|
85 |
data = ""; |
|
86 |
||
87 |
// Count number of extra lines |
|
88 |
for (p=data ; *p ; p++) |
|
89 |
if (*p == '\n') len++; |
|
90 |
||
241 | 91 |
/* Line format: "TI yyyymmddThh:mm:ssZ [data]" |
92 |
* (Old format: "TI DDDDDDDDDD LLL [data])" |
|
93 |
* T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len |
|
110 | 94 |
* |
95 |
* Types: |
|
96 |
* - M message Info: S (send) R (receive) |
|
352 | 97 |
* - S status Info: [_oifdna] |
110 | 98 |
* We don't check them, we'll trust the caller. |
99 |
*/ |
|
100 |
||
113 | 101 |
fp = fopen(filename, "a"); |
155 | 102 |
g_free(filename); |
273 | 103 |
if (!fp) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
104 |
scr_LogPrint(LPRINT_LOGNORM, "Unable to write history " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
105 |
"(cannot open logfile)"); |
273 | 106 |
return; |
107 |
} |
|
178 | 108 |
|
241 | 109 |
to_iso8601(str_ts, ts); |
273 | 110 |
err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 111 |
fclose(fp); |
273 | 112 |
if (err < 0) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
113 |
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
|
114 |
strerror(errno)); |
273 | 115 |
} |
110 | 116 |
} |
117 |
||
178 | 118 |
// hlog_read_history() |
119 |
// Reads the jid's history logfile |
|
120 |
void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
121 |
{ |
|
122 |
char *filename; |
|
123 |
guchar type, info; |
|
124 |
char *data, *tail; |
|
184 | 125 |
time_t timestamp; |
126 |
guint prefix_flags; |
|
178 | 127 |
guint len; |
128 |
FILE *fp; |
|
197 | 129 |
struct stat bufstat; |
193 | 130 |
guint err = 0; |
248 | 131 |
guint ln = 0; // line number |
178 | 132 |
|
133 |
if (!FileLoadLogs) return; |
|
134 |
||
135 |
data = g_new(char, HBB_BLOCKSIZE+32); |
|
136 |
if (!data) { |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
137 |
scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file"); |
178 | 138 |
return; |
139 |
} |
|
140 |
||
141 |
filename = user_histo_file(jid); |
|
142 |
||
143 |
fp = fopen(filename, "r"); |
|
144 |
g_free(filename); |
|
145 |
if (!fp) { g_free(data); return; } |
|
146 |
||
197 | 147 |
// If file is large (> 512 here), display a message to inform the user |
148 |
// (it can take a while...) |
|
149 |
if (!fstat(fileno(fp), &bufstat)) { |
|
150 |
if (bufstat.st_size > 524288) |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
151 |
scr_LogPrint(LPRINT_NORMAL, "Reading <%s> history file...", jid); |
197 | 152 |
} |
153 |
||
178 | 154 |
/* See write_histo_line() for line format... */ |
155 |
while (!feof(fp)) { |
|
259 | 156 |
if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; |
248 | 157 |
ln++; |
178 | 158 |
|
159 |
for (tail = data; *tail; tail++) ; |
|
160 |
||
161 |
type = data[0]; |
|
162 |
info = data[1]; |
|
241 | 163 |
|
178 | 164 |
if ((type != 'M' && type != 'S') || |
259 | 165 |
((data[11] != 'T') || (data[20] != 'Z') || |
166 |
(data[21] != ' ') || (data[25] != ' '))) { |
|
193 | 167 |
if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
168 |
scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
169 |
jid, ln); |
193 | 170 |
err = 1; |
171 |
} |
|
186 | 172 |
//break; |
173 |
continue; |
|
178 | 174 |
} |
259 | 175 |
data[21] = data[25] = 0; |
176 |
timestamp = from_iso8601(&data[3], 1); |
|
177 |
len = (guint) atoi(&data[22]); |
|
178 | 178 |
|
179 |
// Some checks |
|
180 |
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
|
181 |
((type == 'I') && (!strchr("OAIFDN", info)))) { |
193 | 182 |
if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
183 |
scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
184 |
jid, ln); |
193 | 185 |
err = 1; |
186 |
} |
|
186 | 187 |
//break; |
188 |
continue; |
|
178 | 189 |
} |
190 |
||
197 | 191 |
// XXX This will fail when a message is too big |
178 | 192 |
while (len--) { |
248 | 193 |
ln++; |
259 | 194 |
if (fgets(tail, HBB_BLOCKSIZE+27 - (tail-data), fp) == NULL) |
249 | 195 |
break; |
178 | 196 |
|
197 |
while (*tail) tail++; |
|
198 |
} |
|
248 | 199 |
// Small check for too long messages |
259 | 200 |
if (tail >= HBB_BLOCKSIZE+26 + data) { |
249 | 201 |
// Maybe we will have a parse error on next, because this |
202 |
// message is big (maybe too big). |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
203 |
scr_LogPrint(LPRINT_LOGNORM, "A message could be too big " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
204 |
"in history file..."); |
248 | 205 |
} |
251 | 206 |
// Remove last CR (we keep it if the line is empty, too) |
259 | 207 |
if ((tail > data+26) && (*(tail-1) == '\n')) |
178 | 208 |
*(tail-1) = 0; |
209 |
||
210 |
if (type == 'M') { |
|
211 |
if (info == 'S') |
|
184 | 212 |
prefix_flags = HBB_PREFIX_OUT; |
178 | 213 |
else |
184 | 214 |
prefix_flags = HBB_PREFIX_IN; |
259 | 215 |
hbuf_add_line(p_buddyhbuf, &data[26], timestamp, |
249 | 216 |
prefix_flags, width); |
193 | 217 |
err = 0; |
178 | 218 |
} |
219 |
} |
|
220 |
fclose(fp); |
|
221 |
g_free(data); |
|
222 |
} |
|
223 |
||
110 | 224 |
// hlog_enable() |
225 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
|
177 | 226 |
// 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
|
227 |
void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 228 |
{ |
229 |
UseFileLogging = enable; |
|
177 | 230 |
FileLoadLogs = loadfiles; |
110 | 231 |
|
177 | 232 |
if (enable || loadfiles) { |
110 | 233 |
if (root_dir) { |
234 |
int l = strlen(root_dir); |
|
235 |
if (l < 1) { |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
236 |
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
|
237 |
UseFileLogging = FileLoadLogs = FALSE; |
110 | 238 |
return; |
239 |
} |
|
240 |
// RootDir must be slash-terminated |
|
241 |
if (root_dir[l-1] == '/') |
|
242 |
RootDir = g_strdup(root_dir); |
|
243 |
else { |
|
244 |
RootDir = g_new(char, l+2); |
|
245 |
strcpy(RootDir, root_dir); |
|
246 |
strcat(RootDir, "/"); |
|
247 |
} |
|
248 |
} else { |
|
249 |
char *home = getenv("HOME"); |
|
169 | 250 |
char *dir = "/.mcabber/histo/"; |
110 | 251 |
RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
252 |
strcpy(RootDir, home); |
|
253 |
strcat(RootDir, dir); |
|
254 |
} |
|
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
255 |
// 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
|
256 |
if (checkset_perm(RootDir, TRUE) == -1) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
257 |
// The directory does not actually exists |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
258 |
g_free(RootDir); |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
259 |
scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
260 |
"history log directory, logging DISABLED"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
261 |
UseFileLogging = FileLoadLogs = FALSE; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
262 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
263 |
} else { // Disable history logging |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
264 |
if (RootDir) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
265 |
g_free(RootDir); |
110 | 266 |
} |
267 |
} |
|
268 |
||
113 | 269 |
inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
270 |
const char *msg) |
|
271 |
{ |
|
272 |
write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
273 |
} |
|
274 |
||
275 |
inline void hlog_write_status(const char *jid, time_t timestamp, |
|
221 | 276 |
enum imstatus status, const char *status_msg) |
113 | 277 |
{ |
278 |
// #1 XXX Check status value? |
|
279 |
// #2 We could add a user-readable comment |
|
280 |
write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
|
221 | 281 |
status_msg); |
113 | 282 |
} |
283 |