author | Mikael Berthe <mikael@lilotux.net> |
Sat, 16 Jul 2005 13:27:00 +0100 | |
changeset 325 | ff6fb51bfd78 |
parent 281 | f562b9af2de7 |
child 352 | e7e2f2fcc765 |
child 364 | 33b8e801ffa6 |
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) |
|
97 |
* - S status Info: [oaifdcn] |
|
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) { |
104 |
scr_LogPrint("Unable to write history (cannot open logfile)"); |
|
105 |
return; |
|
106 |
} |
|
178 | 107 |
|
241 | 108 |
to_iso8601(str_ts, ts); |
273 | 109 |
err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 110 |
fclose(fp); |
273 | 111 |
if (err < 0) { |
112 |
scr_LogPrint("Error while writing to log file: %s", strerror(errno)); |
|
113 |
} |
|
110 | 114 |
} |
115 |
||
178 | 116 |
// hlog_read_history() |
117 |
// Reads the jid's history logfile |
|
118 |
void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
119 |
{ |
|
120 |
char *filename; |
|
121 |
guchar type, info; |
|
122 |
char *data, *tail; |
|
184 | 123 |
time_t timestamp; |
124 |
guint prefix_flags; |
|
178 | 125 |
guint len; |
126 |
FILE *fp; |
|
197 | 127 |
struct stat bufstat; |
193 | 128 |
guint err = 0; |
248 | 129 |
guint ln = 0; // line number |
178 | 130 |
|
131 |
if (!FileLoadLogs) return; |
|
132 |
||
133 |
data = g_new(char, HBB_BLOCKSIZE+32); |
|
134 |
if (!data) { |
|
135 |
scr_LogPrint("Not enough memory to read history file"); |
|
136 |
return; |
|
137 |
} |
|
138 |
||
139 |
filename = user_histo_file(jid); |
|
140 |
||
141 |
fp = fopen(filename, "r"); |
|
142 |
g_free(filename); |
|
143 |
if (!fp) { g_free(data); return; } |
|
144 |
||
197 | 145 |
// If file is large (> 512 here), display a message to inform the user |
146 |
// (it can take a while...) |
|
147 |
if (!fstat(fileno(fp), &bufstat)) { |
|
148 |
if (bufstat.st_size > 524288) |
|
149 |
scr_LogPrint("Reading <%s> history file...", jid); |
|
150 |
} |
|
151 |
||
178 | 152 |
/* See write_histo_line() for line format... */ |
153 |
while (!feof(fp)) { |
|
259 | 154 |
if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; |
248 | 155 |
ln++; |
178 | 156 |
|
157 |
for (tail = data; *tail; tail++) ; |
|
158 |
||
159 |
type = data[0]; |
|
160 |
info = data[1]; |
|
241 | 161 |
|
178 | 162 |
if ((type != 'M' && type != 'S') || |
259 | 163 |
((data[11] != 'T') || (data[20] != 'Z') || |
164 |
(data[21] != ' ') || (data[25] != ' '))) { |
|
193 | 165 |
if (!err) { |
248 | 166 |
scr_LogPrint("Error in history file format (%s), l.%u", jid, ln); |
193 | 167 |
err = 1; |
168 |
} |
|
186 | 169 |
//break; |
170 |
continue; |
|
178 | 171 |
} |
259 | 172 |
data[21] = data[25] = 0; |
173 |
timestamp = from_iso8601(&data[3], 1); |
|
174 |
len = (guint) atoi(&data[22]); |
|
178 | 175 |
|
176 |
// Some checks |
|
177 |
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
|
178 |
((type == 'I') && (!strchr("OAIFDN", info)))) { |
193 | 179 |
if (!err) { |
248 | 180 |
scr_LogPrint("Error in history file format (%s), l.%u", jid, ln); |
193 | 181 |
err = 1; |
182 |
} |
|
186 | 183 |
//break; |
184 |
continue; |
|
178 | 185 |
} |
186 |
||
197 | 187 |
// XXX This will fail when a message is too big |
178 | 188 |
while (len--) { |
248 | 189 |
ln++; |
259 | 190 |
if (fgets(tail, HBB_BLOCKSIZE+27 - (tail-data), fp) == NULL) |
249 | 191 |
break; |
178 | 192 |
|
193 |
while (*tail) tail++; |
|
194 |
} |
|
248 | 195 |
// Small check for too long messages |
259 | 196 |
if (tail >= HBB_BLOCKSIZE+26 + data) { |
249 | 197 |
// Maybe we will have a parse error on next, because this |
198 |
// message is big (maybe too big). |
|
251 | 199 |
scr_LogPrint("A message could be too big in history file..."); |
248 | 200 |
} |
251 | 201 |
// Remove last CR (we keep it if the line is empty, too) |
259 | 202 |
if ((tail > data+26) && (*(tail-1) == '\n')) |
178 | 203 |
*(tail-1) = 0; |
204 |
||
205 |
if (type == 'M') { |
|
206 |
if (info == 'S') |
|
184 | 207 |
prefix_flags = HBB_PREFIX_OUT; |
178 | 208 |
else |
184 | 209 |
prefix_flags = HBB_PREFIX_IN; |
259 | 210 |
hbuf_add_line(p_buddyhbuf, &data[26], timestamp, |
249 | 211 |
prefix_flags, width); |
193 | 212 |
err = 0; |
178 | 213 |
} |
214 |
} |
|
215 |
fclose(fp); |
|
216 |
g_free(data); |
|
217 |
} |
|
218 |
||
110 | 219 |
// hlog_enable() |
220 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
|
177 | 221 |
// 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
|
222 |
void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 223 |
{ |
224 |
UseFileLogging = enable; |
|
177 | 225 |
FileLoadLogs = loadfiles; |
110 | 226 |
|
177 | 227 |
if (enable || loadfiles) { |
110 | 228 |
if (root_dir) { |
229 |
int l = strlen(root_dir); |
|
230 |
if (l < 1) { |
|
231 |
scr_LogPrint("root_dir too short"); |
|
232 |
UseFileLogging = FALSE; |
|
233 |
return; |
|
234 |
} |
|
235 |
// RootDir must be slash-terminated |
|
236 |
if (root_dir[l-1] == '/') |
|
237 |
RootDir = g_strdup(root_dir); |
|
238 |
else { |
|
239 |
RootDir = g_new(char, l+2); |
|
240 |
strcpy(RootDir, root_dir); |
|
241 |
strcat(RootDir, "/"); |
|
242 |
} |
|
243 |
} else { |
|
244 |
char *home = getenv("HOME"); |
|
169 | 245 |
char *dir = "/.mcabber/histo/"; |
110 | 246 |
RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
247 |
strcpy(RootDir, home); |
|
248 |
strcat(RootDir, dir); |
|
249 |
} |
|
250 |
// FIXME |
|
251 |
// We should check the directory actually exists |
|
252 |
} else // Disable history logging |
|
253 |
if (RootDir) { |
|
254 |
g_free(RootDir); |
|
255 |
} |
|
256 |
} |
|
257 |
||
113 | 258 |
inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
259 |
const char *msg) |
|
260 |
{ |
|
261 |
write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
262 |
} |
|
263 |
||
264 |
inline void hlog_write_status(const char *jid, time_t timestamp, |
|
221 | 265 |
enum imstatus status, const char *status_msg) |
113 | 266 |
{ |
267 |
// #1 XXX Check status value? |
|
268 |
// #2 We could add a user-readable comment |
|
269 |
write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
|
221 | 270 |
status_msg); |
113 | 271 |
} |
272 |