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;
|
241
|
122 guint oldformat;
|
248
|
123 guint ln = 0; // line number
|
249
|
124 guint pleaseconvert = 0;
|
178
|
125
|
|
126 if (!FileLoadLogs) return;
|
|
127
|
|
128 data = g_new(char, HBB_BLOCKSIZE+32);
|
|
129 if (!data) {
|
|
130 scr_LogPrint("Not enough memory to read history file");
|
|
131 return;
|
|
132 }
|
|
133
|
|
134 filename = user_histo_file(jid);
|
|
135
|
|
136 fp = fopen(filename, "r");
|
|
137 g_free(filename);
|
|
138 if (!fp) { g_free(data); return; }
|
|
139
|
197
|
140 // If file is large (> 512 here), display a message to inform the user
|
|
141 // (it can take a while...)
|
|
142 if (!fstat(fileno(fp), &bufstat)) {
|
|
143 if (bufstat.st_size > 524288)
|
|
144 scr_LogPrint("Reading <%s> history file...", jid);
|
|
145 }
|
|
146
|
178
|
147 /* See write_histo_line() for line format... */
|
|
148 while (!feof(fp)) {
|
249
|
149 int format_off = 0;
|
|
150 if (fgets(data, HBB_BLOCKSIZE+19+format_off, fp) == NULL) break;
|
248
|
151 ln++;
|
178
|
152
|
|
153 for (tail = data; *tail; tail++) ;
|
|
154
|
|
155 type = data[0];
|
|
156 info = data[1];
|
241
|
157
|
|
158 // XXX Log format detection. We can read both old and new log formats
|
|
159 // To be removed in a future version
|
|
160 oldformat = (data[11] != 'T' || data[20] != 'Z');
|
|
161 if (!oldformat)
|
|
162 format_off = 8;
|
|
163
|
178
|
164 if ((type != 'M' && type != 'S') ||
|
241
|
165 (oldformat && ((data[13] != ' ') || (data[17] != ' '))) ||
|
|
166 ((!oldformat) && ((data[21] != ' ') || (data[25] != ' ')))) {
|
193
|
167 if (!err) {
|
248
|
168 scr_LogPrint("Error in history file format (%s), l.%u", jid, ln);
|
193
|
169 err = 1;
|
|
170 }
|
186
|
171 //break;
|
|
172 continue;
|
178
|
173 }
|
241
|
174 data[13+format_off] = data[17+format_off] = 0;
|
|
175 if (oldformat)
|
|
176 timestamp = (unsigned long) atol(&data[3]);
|
|
177 else
|
|
178 timestamp = from_iso8601(&data[3], 1);
|
|
179 len = (unsigned long) atol(&data[14+format_off]);
|
178
|
180
|
|
181 // Some checks
|
|
182 if (((type == 'M') && (info != 'S' && info != 'R')) ||
|
183
|
183 ((type == 'I') && (!strchr("OAIFDCN", info)))) {
|
193
|
184 if (!err) {
|
248
|
185 scr_LogPrint("Error in history file format (%s), l.%u", jid, ln);
|
193
|
186 err = 1;
|
|
187 }
|
186
|
188 //break;
|
|
189 continue;
|
178
|
190 }
|
|
191
|
197
|
192 // XXX This will fail when a message is too big
|
178
|
193 while (len--) {
|
248
|
194 ln++;
|
249
|
195 if (fgets(tail, HBB_BLOCKSIZE+19+format_off - (tail-data), fp) == NULL)
|
|
196 break;
|
178
|
197
|
|
198 while (*tail) tail++;
|
|
199 }
|
248
|
200 // Small check for too long messages
|
249
|
201 if (tail >= HBB_BLOCKSIZE+18+format_off + data) {
|
|
202 scr_LogPrint("A message could be too big in history file...");
|
|
203 tail-data-format_off-18);
|
|
204 // Maybe we will have a parse error on next, because this
|
|
205 // message is big (maybe too big).
|
248
|
206 }
|
197
|
207 // Remove last CR
|
241
|
208 if ((tail > data+18+format_off) && (*(tail-1) == '\n'))
|
178
|
209 *(tail-1) = 0;
|
|
210
|
249
|
211 if (oldformat)
|
|
212 pleaseconvert = 1;
|
|
213
|
178
|
214 if (type == 'M') {
|
|
215 if (info == 'S')
|
184
|
216 prefix_flags = HBB_PREFIX_OUT;
|
178
|
217 else
|
184
|
218 prefix_flags = HBB_PREFIX_IN;
|
249
|
219 hbuf_add_line(p_buddyhbuf, &data[18+format_off], timestamp,
|
|
220 prefix_flags, width);
|
193
|
221 err = 0;
|
178
|
222 }
|
|
223 }
|
|
224 fclose(fp);
|
|
225 g_free(data);
|
249
|
226 if (pleaseconvert)
|
|
227 scr_LogPrint("Please convert your history files to the new format!");
|
178
|
228 }
|
|
229
|
110
|
230 // hlog_enable()
|
|
231 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used.
|
177
|
232 // If loadfiles is TRUE, we will try to load buddies history logs from file.
|
|
233 void hlog_enable(guint enable, char *root_dir, guint loadfiles)
|
110
|
234 {
|
|
235 UseFileLogging = enable;
|
177
|
236 FileLoadLogs = loadfiles;
|
110
|
237
|
177
|
238 if (enable || loadfiles) {
|
110
|
239 if (root_dir) {
|
|
240 int l = strlen(root_dir);
|
|
241 if (l < 1) {
|
|
242 scr_LogPrint("root_dir too short");
|
|
243 UseFileLogging = FALSE;
|
|
244 return;
|
|
245 }
|
|
246 // RootDir must be slash-terminated
|
|
247 if (root_dir[l-1] == '/')
|
|
248 RootDir = g_strdup(root_dir);
|
|
249 else {
|
|
250 RootDir = g_new(char, l+2);
|
|
251 strcpy(RootDir, root_dir);
|
|
252 strcat(RootDir, "/");
|
|
253 }
|
|
254 } else {
|
|
255 char *home = getenv("HOME");
|
169
|
256 char *dir = "/.mcabber/histo/";
|
110
|
257 RootDir = g_new(char, strlen(home) + strlen(dir) + 1);
|
|
258 strcpy(RootDir, home);
|
|
259 strcat(RootDir, dir);
|
|
260 }
|
|
261 // FIXME
|
|
262 // We should check the directory actually exists
|
|
263 } else // Disable history logging
|
|
264 if (RootDir) {
|
|
265 g_free(RootDir);
|
|
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
|