Mercurial > pidgin
annotate src/log.c @ 7457:8bc33ec515a1
[gaim-migrate @ 8070]
this probably isn't perfect, but it werks good for me
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 08 Nov 2003 01:32:49 +0000 |
parents | 6f5918e4f668 |
children | c1ddc403fda4 |
rev | line source |
---|---|
7431 | 1 /** |
2 * @file log.c Logging API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
7 * Copyright (C) 2003 Buzz Lightyear | |
7436 | 8 * |
7431 | 9 * This program is free software; you can redistribute it and/or modify |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
4184 | 22 */ |
4195 | 23 |
7431 | 24 #include "account.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
25 #include "debug.h" |
7431 | 26 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
27 #include "log.h" |
5548 | 28 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
29 #include "util.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
30 |
7457 | 31 static GaimLogLogger html_logger; |
7431 | 32 static GaimLogLogger txt_logger; |
33 static GaimLogLogger old_logger; | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
34 |
7431 | 35 /************************************************************************** |
36 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
37 **************************************************************************/ | |
4184 | 38 |
7431 | 39 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, time_t time) |
40 { | |
41 GaimLog *log = g_new0(GaimLog, 1); | |
42 log->name = g_strdup(name); | |
43 log->account = account; | |
44 log->time = time; | |
45 log->logger = gaim_log_logger_get(); | |
7440 | 46 if (log->logger && log->logger->create) |
47 log->logger->create(log); | |
7431 | 48 return log; |
4184 | 49 } |
50 | |
7431 | 51 void gaim_log_free(GaimLog *log) |
4184 | 52 { |
7431 | 53 g_return_if_fail(log); |
54 if (log->logger && log->logger->finalize) | |
55 log->logger->finalize(log); | |
56 g_free(log->name); | |
57 g_free(log); | |
58 } | |
7436 | 59 |
4184 | 60 |
7436 | 61 void gaim_log_write(GaimLog *log, GaimMessageFlags type, |
7431 | 62 const char *from, time_t time, const char *message) |
63 { | |
64 g_return_if_fail(log); | |
65 g_return_if_fail(log->logger); | |
7442 | 66 g_return_if_fail(log->logger->write); |
7431 | 67 |
7442 | 68 (log->logger->write)(log, type, from, time, message); |
4184 | 69 } |
70 | |
7431 | 71 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 72 { |
7431 | 73 g_return_val_if_fail(log && log->logger, NULL); |
7442 | 74 if (log->logger->read) |
75 return (log->logger->read)(log, flags); | |
7431 | 76 return (_("<b><font color\"=red\">The logger has no read function</font></b>")); |
4184 | 77 } |
78 | |
7431 | 79 /**************************************************************************** |
80 * LOGGER FUNCTIONS ********************************************************* | |
81 ****************************************************************************/ | |
4184 | 82 |
7431 | 83 static GaimLogLogger *current_logger = NULL; |
84 static GSList *loggers = NULL; | |
7436 | 85 |
7431 | 86 static void logger_pref_cb(const char *name, GaimPrefType type, |
87 gpointer value, gpointer data) | |
88 { | |
89 GaimLogLogger *logger; | |
90 GSList *l = loggers; | |
91 while (l) { | |
92 logger = l->data; | |
93 if (!strcmp(logger->id, value)) { | |
94 gaim_log_logger_set(logger); | |
95 return; | |
4184 | 96 } |
7431 | 97 l = l->next; |
98 } | |
99 gaim_log_logger_set(&txt_logger); | |
100 } | |
4184 | 101 |
102 | |
7440 | 103 GaimLogLogger *gaim_log_logger_new(void(*create)(GaimLog *), |
7436 | 104 void(*write)(GaimLog *, GaimMessageFlags, const char *, |
7431 | 105 time_t, const char *), |
106 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), | |
107 char*(*read)(GaimLog*, GaimLogReadFlags*)) | |
108 { | |
109 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
7440 | 110 logger->create = create; |
7431 | 111 logger->write = write; |
112 logger->finalize = finalize; | |
113 logger->list = list; | |
114 logger->read = read; | |
115 return logger; | |
4184 | 116 } |
117 | |
7431 | 118 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 119 { |
7431 | 120 g_free(logger); |
121 } | |
4184 | 122 |
7431 | 123 void gaim_log_logger_add (GaimLogLogger *logger) |
124 { | |
125 g_return_if_fail(logger); | |
126 if (g_slist_find(loggers, logger)) | |
127 return; | |
128 loggers = g_slist_append(loggers, logger); | |
129 } | |
130 | |
131 void gaim_log_logger_remove (GaimLogLogger *logger) | |
132 { | |
133 g_return_if_fail(logger); | |
134 g_slist_remove(loggers, logger); | |
4184 | 135 } |
136 | |
7431 | 137 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 138 { |
7431 | 139 g_return_if_fail(logger); |
140 current_logger = logger; | |
7436 | 141 } |
4184 | 142 |
7431 | 143 GaimLogLogger *gaim_log_logger_get() |
144 { | |
145 return current_logger; | |
146 } | |
4184 | 147 |
7431 | 148 GList *gaim_log_logger_get_options(void) |
149 { | |
150 GSList *n; | |
151 GList *list = NULL; | |
152 GaimLogLogger *data; | |
4184 | 153 |
7431 | 154 for (n = loggers; n; n = n->next) { |
155 data = n->data; | |
156 if (!data->write) | |
157 continue; | |
158 list = g_list_append(list, data->name); | |
159 list = g_list_append(list, data->id); | |
4184 | 160 } |
161 | |
7431 | 162 return list; |
163 } | |
164 | |
7436 | 165 static gint log_compare(gconstpointer y, gconstpointer z) |
7431 | 166 { |
7436 | 167 const GaimLog *a = y; |
168 const GaimLog *b = z; | |
169 | |
7431 | 170 return b->time - a->time; |
171 } | |
172 | |
173 GList *gaim_log_get_logs(const char *name, GaimAccount *account) | |
174 { | |
175 GList *logs = NULL; | |
176 GSList *n; | |
177 for (n = loggers; n; n = n->next) { | |
178 GaimLogLogger *logger = n->data; | |
179 if (!logger->list) | |
180 continue; | |
181 logs = g_list_concat(logs, logger->list(name, account)); | |
182 } | |
7436 | 183 |
7431 | 184 return g_list_sort(logs, log_compare); |
185 } | |
186 | |
187 void gaim_log_init(void) | |
7436 | 188 { |
7431 | 189 gaim_prefs_add_none("/core/logging"); |
190 gaim_prefs_add_string("/core/logging/format", "txt"); | |
7457 | 191 gaim_log_logger_add(&html_logger); |
7431 | 192 gaim_log_logger_add(&txt_logger); |
193 gaim_log_logger_add(&old_logger); | |
194 gaim_prefs_connect_callback("/core/logging/format", | |
195 logger_pref_cb, NULL); | |
196 gaim_prefs_trigger_callback("/core/logging/format"); | |
197 } | |
198 | |
199 /**************************************************************************** | |
200 * LOGGERS ****************************************************************** | |
201 ****************************************************************************/ | |
202 | |
203 static GList *log_lister_common(const char *screenname, GaimAccount *account, const char *ext, GaimLogLogger *logger) | |
204 { | |
205 GDir *dir; | |
206 GList *list = NULL; | |
207 const char *filename; | |
208 char *me = g_strdup(gaim_normalize(account, gaim_account_get_username(account))); | |
4184 | 209 |
7431 | 210 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
211 (gaim_find_prpl(gaim_account_get_protocol(account)))->list_icon(account, NULL); | |
212 char *path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, screenname), NULL); | |
213 | |
7447 | 214 g_free(me); |
215 | |
7431 | 216 if (!(dir = g_dir_open(path, 0, NULL))) { |
217 g_free(path); | |
218 return NULL; | |
219 } | |
220 while ((filename = g_dir_read_name(dir))) { | |
221 if (g_str_has_suffix(filename, ext)) { | |
222 const char *l = filename; | |
223 struct tm time; | |
224 GaimLog *log; | |
225 char d[5]; | |
7436 | 226 |
7431 | 227 strncpy(d, l, 4); |
228 d[4] = '\0'; | |
229 time.tm_year = atoi(d) - 1900; | |
230 l = l + 5; | |
231 | |
232 strncpy(d, l, 2); | |
233 d[2] = '\0'; | |
234 time.tm_mon = atoi(d) - 1; | |
235 l = l + 3; | |
236 | |
237 strncpy(d, l, 2); | |
238 time.tm_mday = atoi(d); | |
239 l = l + 3; | |
240 | |
241 strncpy(d, l, 2); | |
242 time.tm_hour = atoi(d); | |
243 l = l + 2; | |
7436 | 244 |
7431 | 245 strncpy(d, l, 2); |
246 time.tm_min = atoi(d); | |
247 l = l + 2; | |
248 | |
249 strncpy(d, l, 2); | |
250 time.tm_sec = atoi(d); | |
251 l = l + 2; | |
252 log = gaim_log_new(GAIM_LOG_IM, screenname, account, mktime(&time)); | |
253 log->logger = logger; | |
254 log->logger_data = g_build_filename(path, filename, NULL); | |
255 list = g_list_append(list, log); | |
4184 | 256 } |
257 } | |
7431 | 258 g_dir_close(dir); |
7447 | 259 g_free(path); |
7431 | 260 return list; |
261 } | |
4184 | 262 |
7431 | 263 #if 0 /* Maybe some other time. */ |
7443 | 264 /**************** |
7431 | 265 ** XML LOGGER ** |
266 ****************/ | |
267 | |
268 static const char *str_from_msg_type (GaimMessageFlags type) | |
269 { | |
7443 | 270 |
7431 | 271 return ""; |
7443 | 272 |
7431 | 273 } |
274 | |
7443 | 275 static void xml_logger_write(GaimLog *log, |
276 GaimMessageFlags type, | |
7431 | 277 const char *from, time_t time, const char *message) |
278 { | |
279 char date[64]; | |
280 char *xhtml = NULL; | |
281 if (!log->logger_data) { | |
282 /* This log is new. We could use the loggers 'new' function, but | |
283 * creating a new file there would result in empty files in the case | |
284 * that you open a convo with someone, but don't say anything. | |
285 */ | |
286 char *ud = gaim_user_dir(); | |
287 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
288 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
289 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
290 char *dir; | |
291 FILE *file; | |
292 | |
7453 | 293 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 294 |
7431 | 295 dir = g_build_filename(ud, "logs", NULL); |
296 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
297 g_free(dir); | |
7443 | 298 dir = g_build_filename(ud, "logs", |
7431 | 299 prpl, NULL); |
300 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
301 g_free(dir); | |
7443 | 302 dir = g_build_filename(ud, "logs", |
7431 | 303 prpl, guy, NULL); |
304 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7443 | 305 g_free(dir); |
306 dir = g_build_filename(ud, "logs", | |
7431 | 307 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
308 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7447 | 309 g_free(guy); |
7443 | 310 |
7431 | 311 char *filename = g_build_filename(dir, date, NULL); |
312 g_free(dir); | |
7443 | 313 |
7431 | 314 file = fopen(dir, "r"); |
315 if(!file) | |
316 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
317 else | |
318 fclose(file); | |
7443 | 319 |
7431 | 320 log->logger_data = fopen(filename, "a"); |
321 if (!log->logger_data) { | |
322 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
323 return; | |
324 } | |
325 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" | |
326 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 327 |
7453 | 328 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 329 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
330 date, log->name, prpl); | |
331 } | |
7443 | 332 |
7453 | 333 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 334 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
335 if (from) | |
7443 | 336 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
337 str_from_msg_type(type), | |
7431 | 338 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
339 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
340 from, date, xhtml); | |
341 else | |
7443 | 342 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
343 str_from_msg_type(type), | |
7431 | 344 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
345 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 346 date, xhtml): |
7431 | 347 fflush(log->logger_data); |
348 g_free(xhtml); | |
7443 | 349 } |
350 | |
7431 | 351 static void xml_logger_finalize(GaimLog *log) |
352 { | |
353 if (log->logger_data) { | |
354 fprintf(log->logger_data, "</conversation>\n"); | |
355 fclose(log->logger_data); | |
356 log->logger_data = NULL; | |
357 } | |
358 } | |
7443 | 359 |
7431 | 360 static GList *xml_logger_list(const char *sn, GaimAccount *account) |
361 { | |
362 return log_lister_common(sn, account, ".xml", &xml_logger); | |
4184 | 363 } |
364 | |
7431 | 365 static GaimLogLogger xml_logger = { |
366 N_("XML"), "xml", | |
367 NULL, | |
368 xml_logger_write, | |
369 xml_logger_finalize, | |
370 xml_logger_list, | |
371 NULL | |
372 }; | |
373 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
374 |
7431 | 375 /**************************** |
7457 | 376 ** HTML LOGGER ************* |
377 ****************************/ | |
378 | |
379 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
380 const char *from, time_t time, const char *message) | |
381 { | |
382 char date[64]; | |
383 if(!log->logger_data) { | |
384 /* This log is new */ | |
385 char *ud = gaim_user_dir(); | |
386 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
387 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
388 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
389 char *dir; | |
390 char *filename; | |
391 FILE *file; | |
392 | |
393 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time)); | |
394 | |
395 dir = g_build_filename(ud, "logs", NULL); | |
396 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
397 g_free(dir); | |
398 dir = g_build_filename(ud, "logs", | |
399 prpl, NULL); | |
400 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
401 g_free(dir); | |
402 dir = g_build_filename(ud, "logs", | |
403 prpl, guy, NULL); | |
404 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
405 g_free(dir); | |
406 dir = g_build_filename(ud, "logs", | |
407 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
408 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
409 g_free(guy); | |
410 | |
411 filename = g_build_filename(dir, date, NULL); | |
412 g_free(dir); | |
413 | |
414 file = fopen(dir, "r"); | |
415 if(!file) | |
416 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
417 else | |
418 fclose(file); | |
419 | |
420 log->logger_data = fopen(filename, "a"); | |
421 if (!log->logger_data) { | |
422 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
423 return; | |
424 } | |
425 g_free(filename); | |
426 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
427 fprintf(log->logger_data, "<html><head><title>"); | |
428 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)", | |
429 log->name, date, gaim_account_get_username(log->account), prpl); | |
430 fprintf(log->logger_data, "</title></head><body>"); | |
431 fprintf(log->logger_data, | |
432 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", | |
433 log->name, date, gaim_account_get_username(log->account), prpl); | |
434 } | |
435 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
436 fprintf(log->logger_data, "(%s) %s%s %s<br/>\n", date, from ? from : "", from ? ":" : "", message); | |
437 fflush(log->logger_data); | |
438 } | |
439 | |
440 static void html_logger_finalize(GaimLog *log) | |
441 { | |
442 fprintf(log->logger_data, "</body></html>"); | |
443 if (log->logger_data) | |
444 fclose(log->logger_data); | |
445 } | |
446 | |
447 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
448 { | |
449 return log_lister_common(sn, account, ".html", &html_logger); | |
450 } | |
451 | |
452 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
453 { | |
454 char *read, *minus_header; | |
455 *flags = GAIM_LOG_READ_NO_NEWLINE; | |
456 if (!log->logger_data) | |
457 return g_strdup("<font color='red'><b>log->logger_data was NULL!</b></font>"); | |
458 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { | |
459 minus_header = strchr(read, '\n'); | |
460 if (!minus_header) | |
461 minus_header = g_strdup(read); | |
462 else | |
463 minus_header = g_strdup(minus_header + 1); | |
464 g_free(read); | |
465 return minus_header; | |
466 } | |
467 return g_strdup(_("<font color='red'><b>Could not read file: %s</b></font>")); | |
468 } | |
469 | |
470 static GaimLogLogger html_logger = { | |
471 N_("HTML"), "html", | |
472 NULL, | |
473 html_logger_write, | |
474 html_logger_finalize, | |
475 html_logger_list, | |
476 html_logger_read | |
477 }; | |
478 | |
479 | |
480 | |
481 | |
482 /**************************** | |
7431 | 483 ** PLAIN TEXT LOGGER ******* |
484 ****************************/ | |
4184 | 485 |
7436 | 486 static void txt_logger_write(GaimLog *log, |
487 GaimMessageFlags type, | |
7431 | 488 const char *from, time_t time, const char *message) |
489 { | |
490 char date[64]; | |
491 char *stripped = NULL; | |
492 if (!log->logger_data) { | |
493 /* This log is new. We could use the loggers 'new' function, but | |
494 * creating a new file there would result in empty files in the case | |
495 * that you open a convo with someone, but don't say anything. | |
496 */ | |
497 char *ud = gaim_user_dir(); | |
498 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
499 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
500 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
501 char *dir; | |
502 FILE *file; | |
7436 | 503 |
7453 | 504 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
7436 | 505 |
7431 | 506 dir = g_build_filename(ud, "logs", NULL); |
507 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
508 g_free(dir); | |
7436 | 509 dir = g_build_filename(ud, "logs", |
7431 | 510 prpl, NULL); |
511 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
512 g_free(dir); | |
7436 | 513 dir = g_build_filename(ud, "logs", |
7431 | 514 prpl, guy, NULL); |
515 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7436 | 516 g_free(dir); |
517 dir = g_build_filename(ud, "logs", | |
7431 | 518 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
519 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7447 | 520 g_free(guy); |
7436 | 521 |
7431 | 522 char *filename = g_build_filename(dir, date, NULL); |
523 g_free(dir); | |
7436 | 524 |
7431 | 525 file = fopen(dir, "r"); |
526 if(!file) | |
527 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
528 else | |
529 fclose(file); | |
7436 | 530 |
7431 | 531 log->logger_data = fopen(filename, "a"); |
532 if (!log->logger_data) { | |
533 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
534 return; | |
4184 | 535 } |
7447 | 536 g_free(filename); |
7453 | 537 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 538 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", |
539 log->name, date, gaim_account_get_username(log->account), prpl); | |
540 } | |
7436 | 541 |
7453 | 542 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 543 stripped = gaim_markup_strip_html(message); |
544 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); | |
545 fflush(log->logger_data); | |
546 g_free(stripped); | |
547 } | |
548 | |
549 static void txt_logger_finalize(GaimLog *log) | |
550 { | |
551 if (log->logger_data) | |
552 fclose(log->logger_data); | |
553 } | |
554 | |
555 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
556 { | |
557 return log_lister_common(sn, account, ".txt", &txt_logger); | |
558 } | |
559 | |
560 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
561 { | |
562 char *read, *minus_header; | |
7457 | 563 *flags = 0; |
7431 | 564 if (!log->logger_data) |
565 return g_strdup("<font color='red'><b>log->logger_data was NULL!</b></font>"); | |
566 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { | |
567 minus_header = strchr(read, '\n'); | |
568 if (!minus_header) | |
569 minus_header = g_strdup(read); | |
7436 | 570 else |
7431 | 571 minus_header = g_strdup(minus_header + 1); |
572 g_free(read); | |
573 return minus_header; | |
574 } | |
575 return g_strdup(_("<font color='red'><b>Could not read file: %s</b></font>")); | |
7436 | 576 } |
7431 | 577 |
578 static GaimLogLogger txt_logger = { | |
579 N_("Plain text"), "txt", | |
580 NULL, | |
581 txt_logger_write, | |
582 txt_logger_finalize, | |
583 txt_logger_list, | |
584 txt_logger_read | |
585 }; | |
586 | |
587 /**************** | |
588 * OLD LOGGER *** | |
589 ****************/ | |
590 | |
591 /* The old logger doesn't write logs, only reads them. This is to include | |
592 * old logs in the log viewer transparently. | |
593 */ | |
594 | |
595 struct old_logger_data { | |
596 char *path; | |
597 int offset; | |
598 int length; | |
599 }; | |
600 | |
7436 | 601 static GList *old_logger_list(const char *sn, GaimAccount *account) |
7431 | 602 { |
603 FILE *file; | |
604 char buf[BUF_LONG]; | |
605 struct tm tm; | |
606 struct old_logger_data *data = NULL; | |
607 char day[4], month[4], year[5]; | |
608 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
609 char *date; | |
610 char *path = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
611 char *newlog; | |
612 | |
7447 | 613 g_free(logfile); |
614 | |
7431 | 615 GaimLog *log = NULL; |
616 GList *list = NULL; | |
617 | |
7447 | 618 if (!(file = fopen(path, "r"))) { |
619 g_free(path); | |
7431 | 620 return NULL; |
7447 | 621 } |
7436 | 622 |
7431 | 623 while (fgets(buf, BUF_LONG, file)) { |
624 if ((newlog = strstr(buf, "---- New C"))) { | |
625 int length; | |
626 int offset; | |
627 GDate gdate; | |
628 char convostart[32]; | |
629 char *temp = strchr(buf, '@'); | |
7436 | 630 |
7431 | 631 if (temp == NULL || strlen(temp) < 2) |
632 continue; | |
7436 | 633 |
7431 | 634 temp++; |
635 length = strcspn(temp, "-"); | |
636 if (length > 31) length = 31; | |
7436 | 637 |
7431 | 638 offset = ftell(file); |
7436 | 639 |
7431 | 640 if (data) { |
7436 | 641 data->length = offset - data->offset - length; |
642 if(strstr(buf, "----</H3><BR>")) { | |
643 data->length -= | |
644 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
645 strlen("----</H3><BR>"); | |
646 } else { | |
647 data->length -= | |
648 strlen("---- New Conversation @ ") + strlen("----"); | |
649 } | |
650 | |
7431 | 651 if (data->length != 0) |
652 list = g_list_append(list, log); | |
653 else | |
654 gaim_log_free(log); | |
655 } | |
656 | |
657 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
658 log->logger = &old_logger; | |
659 | |
7436 | 660 data = g_new0(struct old_logger_data, 1); |
7431 | 661 data->offset = offset; |
662 data->path = path; | |
663 log->logger_data = data; | |
664 | |
7436 | 665 |
7431 | 666 g_snprintf(convostart, length, "%s", temp); |
667 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | |
668 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | |
669 date = g_strdup_printf("%s %s %s", month, day, year); | |
670 g_date_set_parse(&gdate, date); | |
671 tm.tm_mday = g_date_get_day(&gdate); | |
672 tm.tm_mon = g_date_get_month(&gdate) - 1; | |
673 tm.tm_year = g_date_get_year(&gdate) - 1900; | |
674 log->time = mktime(&tm); | |
675 | |
4184 | 676 } |
677 } | |
7431 | 678 fclose(file); |
679 return list; | |
4184 | 680 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
681 |
7431 | 682 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
683 { |
7431 | 684 struct old_logger_data *data = log->logger_data; |
685 FILE *file = fopen(data->path, "r"); | |
686 char *read = g_malloc(data->length + 1); | |
687 fseek(file, data->offset, SEEK_SET); | |
688 fread(read, data->length, 1, file); | |
689 read[data->length] = '\0'; | |
7436 | 690 *flags = 0; |
691 if(strstr(read, "<BR>")) | |
692 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 693 return read; |
694 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
695 |
7431 | 696 static GaimLogLogger old_logger = { |
697 "old logger", "old", | |
698 NULL, NULL, NULL, | |
699 old_logger_list, | |
700 old_logger_read | |
701 }; |