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