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
|
178
|
124
|
|
125 if (!FileLoadLogs) return;
|
|
126
|
|
127 data = g_new(char, HBB_BLOCKSIZE+32);
|
|
128 if (!data) {
|
|
129 scr_LogPrint("Not enough memory to read history file");
|
|
130 return;
|
|
131 }
|
|
132
|
|
133 filename = user_histo_file(jid);
|
|
134
|
|
135 fp = fopen(filename, "r");
|
|
136 g_free(filename);
|
|
137 if (!fp) { g_free(data); return; }
|
|
138
|
197
|
139 // If file is large (> 512 here), display a message to inform the user
|
|
140 // (it can take a while...)
|
|
141 if (!fstat(fileno(fp), &bufstat)) {
|
|
142 if (bufstat.st_size > 524288)
|
|
143 scr_LogPrint("Reading <%s> history file...", jid);
|
|
144 }
|
|
145
|
178
|
146 /* See write_histo_line() for line format... */
|
|
147 while (!feof(fp)) {
|
241
|
148 int format_off =0;
|
178
|
149 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break;
|
248
|
150 ln++;
|
178
|
151
|
|
152 for (tail = data; *tail; tail++) ;
|
|
153
|
|
154 type = data[0];
|
|
155 info = data[1];
|
241
|
156
|
|
157 // XXX Log format detection. We can read both old and new log formats
|
|
158 // To be removed in a future version
|
|
159 oldformat = (data[11] != 'T' || data[20] != 'Z');
|
|
160 if (!oldformat)
|
|
161 format_off = 8;
|
|
162
|
178
|
163 if ((type != 'M' && type != 'S') ||
|
241
|
164 (oldformat && ((data[13] != ' ') || (data[17] != ' '))) ||
|
|
165 ((!oldformat) && ((data[21] != ' ') || (data[25] != ' ')))) {
|
193
|
166 if (!err) {
|
248
|
167 scr_LogPrint("Error in history file format (%s), l.%u", jid, ln);
|
193
|
168 err = 1;
|
|
169 }
|
186
|
170 //break;
|
|
171 continue;
|
178
|
172 }
|
241
|
173 data[13+format_off] = data[17+format_off] = 0;
|
|
174 if (oldformat)
|
|
175 timestamp = (unsigned long) atol(&data[3]);
|
|
176 else
|
|
177 timestamp = from_iso8601(&data[3], 1);
|
|
178 len = (unsigned long) atol(&data[14+format_off]);
|
178
|
179
|
|
180 // Some checks
|
|
181 if (((type == 'M') && (info != 'S' && info != 'R')) ||
|
183
|
182 ((type == 'I') && (!strchr("OAIFDCN", info)))) {
|
193
|
183 if (!err) {
|
248
|
184 scr_LogPrint("Error in history file format (%s), l.%u", 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++;
|
178
|
194 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break;
|
|
195
|
|
196 while (*tail) tail++;
|
|
197 }
|
248
|
198 // Small check for too long messages
|
|
199 if (tail+1 >= HBB_BLOCKSIZE+24 + data) {
|
|
200 scr_LogPrint("Message is too big in history file!");
|
|
201 }
|
197
|
202 // Remove last CR
|
241
|
203 if ((tail > data+18+format_off) && (*(tail-1) == '\n'))
|
178
|
204 *(tail-1) = 0;
|
|
205
|
|
206 if (type == 'M') {
|
|
207 if (info == 'S')
|
184
|
208 prefix_flags = HBB_PREFIX_OUT;
|
178
|
209 else
|
184
|
210 prefix_flags = HBB_PREFIX_IN;
|
241
|
211 hbuf_add_line(p_buddyhbuf, &data[18+format_off], timestamp, 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.
|
|
222 void hlog_enable(guint enable, 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
|