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