Mercurial > hg
annotate mcabber/src/histolog.c @ 712:e1639629c87b
Rename color options
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 04 Mar 2006 18:52:29 +0100 |
parents | ee03b56b93ee |
children | 1c3620668857 |
rev | line source |
---|---|
110 | 1 /* |
699 | 2 * histolog.c -- File history handling |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
3 * |
699 | 4 * Copyright (C) 2005, 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
110 | 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" |
241 | 33 #include "utils.h" |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
34 #include "logprint.h" |
635
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
35 #include "settings.h" |
110 | 36 |
37 static guint UseFileLogging; | |
177 | 38 static guint FileLoadLogs; |
110 | 39 static char *RootDir; |
40 | |
41 | |
42 // user_histo_file() | |
43 // Returns history filename for the given jid | |
44 // Note: the caller *must* free the filename after use (if not null). | |
111 | 45 static char *user_histo_file(const char *jid) |
110 | 46 { |
47 char *filename; | |
186 | 48 char *lowerid, *p; |
178 | 49 if (!UseFileLogging && !FileLoadLogs) return NULL; |
110 | 50 |
186 | 51 lowerid = g_strdup(jid); |
52 for (p=lowerid; *p ; p++) | |
53 *p = tolower(*p); | |
54 | |
110 | 55 filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
56 strcpy(filename, RootDir); | |
186 | 57 strcat(filename, lowerid); |
58 g_free(lowerid); | |
110 | 59 return filename; |
60 } | |
61 | |
178 | 62 // write_histo_line() |
110 | 63 // Adds a history (multi-)line to the jid's history logfile |
113 | 64 static void write_histo_line(const char *jid, |
65 time_t timestamp, guchar type, guchar info, const char *data) | |
110 | 66 { |
67 guint len = 0; | |
113 | 68 FILE *fp; |
110 | 69 time_t ts; |
113 | 70 const char *p; |
178 | 71 char *filename; |
241 | 72 char str_ts[20]; |
273 | 73 int err; |
110 | 74 |
178 | 75 if (!UseFileLogging) return; |
76 | |
77 filename = user_histo_file(jid); | |
110 | 78 |
79 // If timestamp is null, get current date | |
80 if (timestamp) | |
81 ts = timestamp; | |
82 else | |
83 time(&ts); | |
84 | |
85 if (!data) | |
86 data = ""; | |
87 | |
88 // Count number of extra lines | |
89 for (p=data ; *p ; p++) | |
90 if (*p == '\n') len++; | |
91 | |
241 | 92 /* Line format: "TI yyyymmddThh:mm:ssZ [data]" |
93 * (Old format: "TI DDDDDDDDDD LLL [data])" | |
94 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len | |
110 | 95 * |
96 * Types: | |
97 * - M message Info: S (send) R (receive) | |
352 | 98 * - S status Info: [_oifdna] |
110 | 99 * We don't check them, we'll trust the caller. |
100 */ | |
101 | |
113 | 102 fp = fopen(filename, "a"); |
155 | 103 g_free(filename); |
273 | 104 if (!fp) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
105 scr_LogPrint(LPRINT_LOGNORM, "Unable to write history " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
106 "(cannot open logfile)"); |
273 | 107 return; |
108 } | |
178 | 109 |
241 | 110 to_iso8601(str_ts, ts); |
273 | 111 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 112 fclose(fp); |
273 | 113 if (err < 0) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
114 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
|
115 strerror(errno)); |
273 | 116 } |
110 | 117 } |
118 | |
178 | 119 // hlog_read_history() |
120 // Reads the jid's history logfile | |
121 void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) | |
122 { | |
123 char *filename; | |
124 guchar type, info; | |
125 char *data, *tail; | |
184 | 126 time_t timestamp; |
127 guint prefix_flags; | |
178 | 128 guint len; |
129 FILE *fp; | |
197 | 130 struct stat bufstat; |
193 | 131 guint err = 0; |
248 | 132 guint ln = 0; // line number |
178 | 133 |
134 if (!FileLoadLogs) return; | |
135 | |
635
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
136 if ((roster_gettype(jid) & ROSTER_TYPE_ROOM) && |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
137 (settings_opt_get_int("load_muc_logs") != 1)) |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
138 return; |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
139 |
178 | 140 data = g_new(char, HBB_BLOCKSIZE+32); |
141 if (!data) { | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
142 scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file"); |
178 | 143 return; |
144 } | |
145 | |
146 filename = user_histo_file(jid); | |
147 | |
148 fp = fopen(filename, "r"); | |
149 g_free(filename); | |
150 if (!fp) { g_free(data); return; } | |
151 | |
394
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
152 // If file is large (> 3MB here), display a message to inform the user |
197 | 153 // (it can take a while...) |
154 if (!fstat(fileno(fp), &bufstat)) { | |
394
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
155 if (bufstat.st_size > 3145728) |
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
156 scr_LogPrint(LPRINT_LOGNORM, "Reading <%s> history file...", jid); |
197 | 157 } |
158 | |
178 | 159 /* See write_histo_line() for line format... */ |
160 while (!feof(fp)) { | |
259 | 161 if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; |
248 | 162 ln++; |
178 | 163 |
164 for (tail = data; *tail; tail++) ; | |
165 | |
166 type = data[0]; | |
167 info = data[1]; | |
241 | 168 |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
169 if ((type != 'M' && type != 'S') || |
259 | 170 ((data[11] != 'T') || (data[20] != 'Z') || |
171 (data[21] != ' ') || (data[25] != ' '))) { | |
193 | 172 if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
173 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
|
174 jid, ln); |
193 | 175 err = 1; |
176 } | |
186 | 177 //break; |
178 continue; | |
178 | 179 } |
259 | 180 data[21] = data[25] = 0; |
181 timestamp = from_iso8601(&data[3], 1); | |
182 len = (guint) atoi(&data[22]); | |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
183 |
178 | 184 // Some checks |
185 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
|
186 ((type == 'I') && (!strchr("OAIFDN", info)))) { |
193 | 187 if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
188 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
|
189 jid, ln); |
193 | 190 err = 1; |
191 } | |
186 | 192 //break; |
193 continue; | |
178 | 194 } |
195 | |
197 | 196 // XXX This will fail when a message is too big |
178 | 197 while (len--) { |
248 | 198 ln++; |
259 | 199 if (fgets(tail, HBB_BLOCKSIZE+27 - (tail-data), fp) == NULL) |
249 | 200 break; |
178 | 201 |
202 while (*tail) tail++; | |
203 } | |
248 | 204 // Small check for too long messages |
259 | 205 if (tail >= HBB_BLOCKSIZE+26 + data) { |
249 | 206 // Maybe we will have a parse error on next, because this |
207 // message is big (maybe too big). | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
208 scr_LogPrint(LPRINT_LOGNORM, "A message could be too big " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
209 "in history file..."); |
248 | 210 } |
251 | 211 // Remove last CR (we keep it if the line is empty, too) |
259 | 212 if ((tail > data+26) && (*(tail-1) == '\n')) |
178 | 213 *(tail-1) = 0; |
214 | |
215 if (type == 'M') { | |
216 if (info == 'S') | |
184 | 217 prefix_flags = HBB_PREFIX_OUT; |
178 | 218 else |
184 | 219 prefix_flags = HBB_PREFIX_IN; |
259 | 220 hbuf_add_line(p_buddyhbuf, &data[26], timestamp, |
249 | 221 prefix_flags, width); |
193 | 222 err = 0; |
178 | 223 } |
224 } | |
225 fclose(fp); | |
226 g_free(data); | |
227 } | |
228 | |
110 | 229 // hlog_enable() |
230 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. | |
177 | 231 // 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
|
232 void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 233 { |
234 UseFileLogging = enable; | |
177 | 235 FileLoadLogs = loadfiles; |
110 | 236 |
177 | 237 if (enable || loadfiles) { |
110 | 238 if (root_dir) { |
239 int l = strlen(root_dir); | |
240 if (l < 1) { | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
241 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
|
242 UseFileLogging = FileLoadLogs = FALSE; |
110 | 243 return; |
244 } | |
245 // RootDir must be slash-terminated | |
246 if (root_dir[l-1] == '/') | |
247 RootDir = g_strdup(root_dir); | |
248 else { | |
249 RootDir = g_new(char, l+2); | |
250 strcpy(RootDir, root_dir); | |
251 strcat(RootDir, "/"); | |
252 } | |
253 } else { | |
254 char *home = getenv("HOME"); | |
169 | 255 char *dir = "/.mcabber/histo/"; |
110 | 256 RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
257 strcpy(RootDir, home); | |
258 strcat(RootDir, dir); | |
259 } | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
260 // 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
|
261 if (checkset_perm(RootDir, TRUE) == -1) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
262 // The directory does not actually exists |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
263 g_free(RootDir); |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
264 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
265 "history log directory, logging DISABLED"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
266 UseFileLogging = FileLoadLogs = FALSE; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
267 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
268 } else { // Disable history logging |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
269 if (RootDir) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
270 g_free(RootDir); |
110 | 271 } |
272 } | |
273 | |
113 | 274 inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
275 const char *msg) | |
276 { | |
277 write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); | |
278 } | |
279 | |
280 inline void hlog_write_status(const char *jid, time_t timestamp, | |
221 | 281 enum imstatus status, const char *status_msg) |
113 | 282 { |
283 // #1 XXX Check status value? | |
284 // #2 We could add a user-readable comment | |
285 write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), | |
221 | 286 status_msg); |
113 | 287 } |
288 | |
580 | 289 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |