Mercurial > pidgin
annotate src/log.c @ 9959:b6a74cbfd182
[gaim-migrate @ 10866]
and in the other tree
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Tue, 07 Sep 2004 01:46:02 +0000 |
parents | b23e70bd1215 |
children | 0ac9b01c6837 |
rev | line source |
---|---|
7431 | 1 /** |
2 * @file log.c Logging API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
7436 | 10 * |
7431 | 11 * This program is free software; you can redistribute it and/or modify |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
4184 | 24 */ |
4195 | 25 |
7431 | 26 #include "account.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
27 #include "debug.h" |
7431 | 28 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
29 #include "log.h" |
5548 | 30 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
31 #include "util.h" |
7764 | 32 #include "stringref.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
33 |
8096 | 34 static GSList *loggers = NULL; |
35 | |
7457 | 36 static GaimLogLogger html_logger; |
7431 | 37 static GaimLogLogger txt_logger; |
38 static GaimLogLogger old_logger; | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
39 |
8635 | 40 struct _gaim_logsize_user { |
41 char *name; | |
42 GaimAccount *account; | |
43 }; | |
44 static GHashTable *logsize_users = NULL; | |
45 | |
46 | |
7431 | 47 /************************************************************************** |
48 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
49 **************************************************************************/ | |
4184 | 50 |
7431 | 51 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, time_t time) |
52 { | |
53 GaimLog *log = g_new0(GaimLog, 1); | |
8635 | 54 log->name = g_strdup(gaim_normalize(account, name)); |
7431 | 55 log->account = account; |
56 log->time = time; | |
8573 | 57 log->type = type; |
8096 | 58 log->logger_data = NULL; |
7431 | 59 log->logger = gaim_log_logger_get(); |
7440 | 60 if (log->logger && log->logger->create) |
61 log->logger->create(log); | |
7431 | 62 return log; |
4184 | 63 } |
64 | |
7431 | 65 void gaim_log_free(GaimLog *log) |
4184 | 66 { |
7431 | 67 g_return_if_fail(log); |
68 if (log->logger && log->logger->finalize) | |
69 log->logger->finalize(log); | |
70 g_free(log->name); | |
71 g_free(log); | |
72 } | |
7436 | 73 |
74 void gaim_log_write(GaimLog *log, GaimMessageFlags type, | |
7431 | 75 const char *from, time_t time, const char *message) |
76 { | |
77 g_return_if_fail(log); | |
78 g_return_if_fail(log->logger); | |
7442 | 79 g_return_if_fail(log->logger->write); |
7431 | 80 |
8635 | 81 if ((log->type == GAIM_LOG_IM && |
82 gaim_prefs_get_bool("/core/logging/log_ims")) || | |
83 (log->type == GAIM_LOG_CHAT && | |
84 gaim_prefs_get_bool("/core/logging/log_chats")) || | |
85 (log->type == GAIM_LOG_SYSTEM && | |
86 gaim_prefs_get_bool("/core/logging/log_system"))) { | |
9892 | 87 struct _gaim_logsize_user *lu; |
7553 | 88 (log->logger->write)(log, type, from, time, message); |
9892 | 89 |
90 lu = g_new(struct _gaim_logsize_user, 1); | |
91 | |
92 lu->name = g_strdup(gaim_normalize(log->account, log->name)); | |
93 lu->account = log->account; | |
94 g_hash_table_remove(logsize_users, lu); | |
95 g_free(lu->name); | |
96 g_free(lu); | |
8635 | 97 } |
4184 | 98 } |
99 | |
7431 | 100 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 101 { |
7542 | 102 GaimLogReadFlags mflags; |
7431 | 103 g_return_val_if_fail(log && log->logger, NULL); |
7462 | 104 if (log->logger->read) { |
7535 | 105 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
106 gaim_str_strip_cr(ret); |
7462 | 107 return ret; |
108 } | |
7470 | 109 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
4184 | 110 } |
7616 | 111 |
7556 | 112 int gaim_log_get_size(GaimLog *log) |
113 { | |
114 g_return_val_if_fail(log && log->logger, 0); | |
8096 | 115 |
7556 | 116 if (log->logger->size) |
117 return log->logger->size(log); | |
118 return 0; | |
119 } | |
120 | |
8635 | 121 static guint _gaim_logsize_user_hash(struct _gaim_logsize_user *lu) |
122 { | |
123 return g_str_hash(lu->name); | |
124 } | |
125 | |
126 static guint _gaim_logsize_user_equal(struct _gaim_logsize_user *lu1, | |
127 struct _gaim_logsize_user *lu2) | |
128 { | |
129 return ((!strcmp(lu1->name, lu2->name)) && lu1->account == lu2->account); | |
130 } | |
131 | |
132 static void _gaim_logsize_user_free_key(struct _gaim_logsize_user *lu) | |
133 { | |
134 g_free(lu->name); | |
135 g_free(lu); | |
136 } | |
137 | |
8898 | 138 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account) |
7556 | 139 { |
9677 | 140 gpointer ptrsize; |
141 int size = 0; | |
8096 | 142 GSList *n; |
8635 | 143 struct _gaim_logsize_user *lu; |
8096 | 144 |
8635 | 145 lu = g_new(struct _gaim_logsize_user, 1); |
146 lu->name = g_strdup(gaim_normalize(account, name)); | |
147 lu->account = account; | |
148 | |
9677 | 149 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { |
150 size = GPOINTER_TO_INT(ptrsize); | |
8635 | 151 g_free(lu->name); |
152 g_free(lu); | |
153 } else { | |
154 for (n = loggers; n; n = n->next) { | |
155 GaimLogLogger *logger = n->data; | |
7616 | 156 |
8635 | 157 if(logger->total_size){ |
8898 | 158 size += (logger->total_size)(type, name, account); |
8635 | 159 } else if(logger->list) { |
8898 | 160 GList *logs = (logger->list)(type, name, account); |
8635 | 161 int this_size = 0; |
162 | |
163 while (logs) { | |
164 GList *logs2 = logs->next; | |
165 GaimLog *log = (GaimLog*)(logs->data); | |
166 this_size += gaim_log_get_size(log); | |
167 gaim_log_free(log); | |
168 g_list_free_1(logs); | |
169 logs = logs2; | |
170 } | |
171 | |
172 size += this_size; | |
8096 | 173 } |
8635 | 174 } |
8096 | 175 |
8635 | 176 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); |
7556 | 177 } |
178 return size; | |
179 } | |
4184 | 180 |
9923 | 181 static char* gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account) { |
9926 | 182 char *acct_name = g_strdup(gaim_escape_filename(gaim_normalize(account, |
183 gaim_account_get_username(account)))); | |
184 const char *target; | |
9923 | 185 /* does this seem like a bad way to get this component of the path to anyone else? --Nathan */ |
186 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO( | |
187 gaim_find_prpl(gaim_account_get_protocol_id(account)) | |
188 )->list_icon(account, NULL); | |
9926 | 189 |
9923 | 190 char *dir; |
191 | |
192 if (type == GAIM_LOG_CHAT) { | |
193 char *temp = g_strdup_printf("%s.chat", gaim_normalize(account, name)); | |
9926 | 194 target = gaim_escape_filename(temp); |
9923 | 195 g_free(temp); |
196 } else if(type == GAIM_LOG_SYSTEM) { | |
9926 | 197 target = ".system"; |
9923 | 198 } else { |
9926 | 199 target = gaim_escape_filename(gaim_normalize(account, name)); |
9923 | 200 } |
201 | |
202 | |
203 dir = g_build_filename(gaim_user_dir(), "logs", prpl, acct_name, target, NULL); | |
9926 | 204 |
9923 | 205 g_free(acct_name); |
206 return dir; | |
207 } | |
208 | |
7431 | 209 /**************************************************************************** |
210 * LOGGER FUNCTIONS ********************************************************* | |
211 ****************************************************************************/ | |
4184 | 212 |
7431 | 213 static GaimLogLogger *current_logger = NULL; |
7436 | 214 |
7431 | 215 static void logger_pref_cb(const char *name, GaimPrefType type, |
216 gpointer value, gpointer data) | |
217 { | |
218 GaimLogLogger *logger; | |
219 GSList *l = loggers; | |
220 while (l) { | |
221 logger = l->data; | |
222 if (!strcmp(logger->id, value)) { | |
223 gaim_log_logger_set(logger); | |
224 return; | |
4184 | 225 } |
7431 | 226 l = l->next; |
227 } | |
228 gaim_log_logger_set(&txt_logger); | |
229 } | |
4184 | 230 |
231 | |
8898 | 232 GaimLogLogger *gaim_log_logger_new( |
233 void(*create)(GaimLog *), | |
234 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), | |
235 void(*finalize)(GaimLog *), | |
236 GList*(*list)(GaimLogType type, const char*, GaimAccount*), | |
237 char*(*read)(GaimLog*, GaimLogReadFlags*), | |
238 int(*size)(GaimLog*)) | |
7431 | 239 { |
240 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
7440 | 241 logger->create = create; |
7431 | 242 logger->write = write; |
243 logger->finalize = finalize; | |
244 logger->list = list; | |
245 logger->read = read; | |
7556 | 246 logger->size = size; |
7431 | 247 return logger; |
4184 | 248 } |
249 | |
7431 | 250 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 251 { |
7431 | 252 g_free(logger); |
253 } | |
4184 | 254 |
7431 | 255 void gaim_log_logger_add (GaimLogLogger *logger) |
256 { | |
257 g_return_if_fail(logger); | |
258 if (g_slist_find(loggers, logger)) | |
259 return; | |
260 loggers = g_slist_append(loggers, logger); | |
261 } | |
262 | |
263 void gaim_log_logger_remove (GaimLogLogger *logger) | |
264 { | |
265 g_return_if_fail(logger); | |
266 g_slist_remove(loggers, logger); | |
4184 | 267 } |
268 | |
7431 | 269 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 270 { |
7431 | 271 g_return_if_fail(logger); |
272 current_logger = logger; | |
7436 | 273 } |
4184 | 274 |
7431 | 275 GaimLogLogger *gaim_log_logger_get() |
276 { | |
277 return current_logger; | |
278 } | |
4184 | 279 |
7431 | 280 GList *gaim_log_logger_get_options(void) |
281 { | |
282 GSList *n; | |
283 GList *list = NULL; | |
284 GaimLogLogger *data; | |
4184 | 285 |
7431 | 286 for (n = loggers; n; n = n->next) { |
287 data = n->data; | |
288 if (!data->write) | |
289 continue; | |
7494 | 290 list = g_list_append(list, _(data->name)); |
7431 | 291 list = g_list_append(list, data->id); |
4184 | 292 } |
293 | |
7431 | 294 return list; |
295 } | |
296 | |
8573 | 297 gint gaim_log_compare(gconstpointer y, gconstpointer z) |
7431 | 298 { |
7436 | 299 const GaimLog *a = y; |
300 const GaimLog *b = z; | |
301 | |
7431 | 302 return b->time - a->time; |
303 } | |
304 | |
8898 | 305 GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account) |
7431 | 306 { |
307 GList *logs = NULL; | |
308 GSList *n; | |
309 for (n = loggers; n; n = n->next) { | |
310 GaimLogLogger *logger = n->data; | |
311 if (!logger->list) | |
312 continue; | |
8898 | 313 logs = g_list_concat(logs, logger->list(type, name, account)); |
7431 | 314 } |
7436 | 315 |
8573 | 316 return g_list_sort(logs, gaim_log_compare); |
317 } | |
318 | |
319 GList *gaim_log_get_system_logs(GaimAccount *account) | |
320 { | |
321 GList *logs = NULL; | |
322 GSList *n; | |
323 for (n = loggers; n; n = n->next) { | |
324 GaimLogLogger *logger = n->data; | |
325 if (!logger->list_syslog) | |
326 continue; | |
327 logs = g_list_concat(logs, logger->list_syslog(account)); | |
328 } | |
329 | |
330 return g_list_sort(logs, gaim_log_compare); | |
7431 | 331 } |
332 | |
333 void gaim_log_init(void) | |
7436 | 334 { |
7431 | 335 gaim_prefs_add_none("/core/logging"); |
7555 | 336 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
337 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
8573 | 338 gaim_prefs_add_bool("/core/logging/log_system", FALSE); |
339 gaim_prefs_add_bool("/core/logging/log_signon_signoff", FALSE); | |
340 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); | |
341 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); | |
342 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); | |
343 | |
7431 | 344 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 345 gaim_log_logger_add(&html_logger); |
7431 | 346 gaim_log_logger_add(&txt_logger); |
347 gaim_log_logger_add(&old_logger); | |
348 gaim_prefs_connect_callback("/core/logging/format", | |
349 logger_pref_cb, NULL); | |
350 gaim_prefs_trigger_callback("/core/logging/format"); | |
8635 | 351 |
352 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, | |
353 (GEqualFunc)_gaim_logsize_user_equal, | |
354 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); | |
7431 | 355 } |
356 | |
357 /**************************************************************************** | |
358 * LOGGERS ****************************************************************** | |
359 ****************************************************************************/ | |
360 | |
7616 | 361 struct generic_logger_data { |
362 char *path; | |
363 FILE *file; | |
364 }; | |
365 | |
9763 | 366 static void log_writer_common(GaimLog *log, GaimMessageFlags type, |
9923 | 367 time_t time, const char *ext) |
9763 | 368 { |
369 char date[64]; | |
370 struct generic_logger_data *data = log->logger_data; | |
371 | |
372 if(!data) { | |
373 /* This log is new */ | |
9923 | 374 char *dir, *filename, *path; |
375 | |
376 dir = gaim_log_get_log_dir(log->type, log->name, log->account); | |
377 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
9763 | 378 |
379 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S", localtime(&log->time)); | |
380 | |
381 filename = g_strdup_printf("%s%s", date, ext ? ext : ""); | |
382 | |
383 path = g_build_filename(dir, filename, NULL); | |
384 g_free(dir); | |
385 g_free(filename); | |
386 | |
387 log->logger_data = data = g_new0(struct generic_logger_data, 1); | |
388 | |
389 data->file = fopen(path, "a"); | |
390 if (!data->file) { | |
391 gaim_debug(GAIM_DEBUG_ERROR, "log", | |
9892 | 392 "Could not create log file %s\n", path); |
9763 | 393 g_free(path); |
394 return; | |
395 } | |
396 g_free(path); | |
397 } | |
398 } | |
399 | |
8898 | 400 static GList *log_lister_common(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
7431 | 401 { |
402 GDir *dir; | |
403 GList *list = NULL; | |
7628 | 404 const char *filename; |
8111 | 405 char *path; |
406 | |
407 if(!account) | |
408 return NULL; | |
409 | |
9923 | 410 path = gaim_log_get_log_dir(type, name, account); |
7447 | 411 |
7431 | 412 if (!(dir = g_dir_open(path, 0, NULL))) { |
413 g_free(path); | |
414 return NULL; | |
415 } | |
8898 | 416 |
7431 | 417 while ((filename = g_dir_read_name(dir))) { |
8577 | 418 if (gaim_str_has_suffix(filename, ext) && |
419 strlen(filename) == 17 + strlen(ext)) { | |
7431 | 420 GaimLog *log; |
7616 | 421 struct generic_logger_data *data; |
8577 | 422 time_t stamp = gaim_str_to_time(filename, FALSE); |
7431 | 423 |
8898 | 424 log = gaim_log_new(type, name, account, stamp); |
7431 | 425 log->logger = logger; |
7616 | 426 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
427 data->path = g_build_filename(path, filename, NULL); | |
7431 | 428 list = g_list_append(list, log); |
4184 | 429 } |
430 } | |
7431 | 431 g_dir_close(dir); |
7447 | 432 g_free(path); |
7431 | 433 return list; |
434 } | |
4184 | 435 |
7556 | 436 /* Only to be used with logs listed from log_lister_common */ |
7616 | 437 int log_sizer_common(GaimLog *log) |
7556 | 438 { |
439 struct stat st; | |
7616 | 440 struct generic_logger_data *data = log->logger_data; |
7556 | 441 |
7616 | 442 if (!data->path || stat(data->path, &st)) |
7556 | 443 st.st_size = 0; |
444 | |
445 return st.st_size; | |
446 } | |
447 | |
7431 | 448 #if 0 /* Maybe some other time. */ |
7443 | 449 /**************** |
7431 | 450 ** XML LOGGER ** |
451 ****************/ | |
452 | |
453 static const char *str_from_msg_type (GaimMessageFlags type) | |
454 { | |
7443 | 455 |
7431 | 456 return ""; |
7443 | 457 |
7431 | 458 } |
459 | |
7443 | 460 static void xml_logger_write(GaimLog *log, |
461 GaimMessageFlags type, | |
7431 | 462 const char *from, time_t time, const char *message) |
463 { | |
464 char date[64]; | |
465 char *xhtml = NULL; | |
466 if (!log->logger_data) { | |
467 /* This log is new. We could use the loggers 'new' function, but | |
468 * creating a new file there would result in empty files in the case | |
469 * that you open a convo with someone, but don't say anything. | |
470 */ | |
9923 | 471 char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
7431 | 472 FILE *file; |
7453 | 473 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 474 |
7612 | 475 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7443 | 476 |
7431 | 477 char *filename = g_build_filename(dir, date, NULL); |
478 g_free(dir); | |
7443 | 479 |
7431 | 480 log->logger_data = fopen(filename, "a"); |
481 if (!log->logger_data) { | |
482 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 483 g_free(filename); |
7431 | 484 return; |
485 } | |
7564 | 486 g_free(filename); |
7431 | 487 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
488 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 489 |
7453 | 490 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 491 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
492 date, log->name, prpl); | |
493 } | |
7443 | 494 |
9923 | 495 /* if we can't write to the file, give up before we hurt ourselves */ |
496 if(!data->file) | |
497 return; | |
498 | |
7453 | 499 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 500 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
501 if (from) | |
7443 | 502 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
503 str_from_msg_type(type), | |
7431 | 504 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
505 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
506 from, date, xhtml); | |
507 else | |
7443 | 508 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
509 str_from_msg_type(type), | |
7431 | 510 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
511 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 512 date, xhtml): |
7431 | 513 fflush(log->logger_data); |
514 g_free(xhtml); | |
7443 | 515 } |
516 | |
7431 | 517 static void xml_logger_finalize(GaimLog *log) |
518 { | |
519 if (log->logger_data) { | |
520 fprintf(log->logger_data, "</conversation>\n"); | |
521 fclose(log->logger_data); | |
522 log->logger_data = NULL; | |
523 } | |
524 } | |
7443 | 525 |
8898 | 526 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 527 { |
8898 | 528 return log_lister_common(type, sn, account, ".xml", &xml_logger); |
4184 | 529 } |
530 | |
7431 | 531 static GaimLogLogger xml_logger = { |
532 N_("XML"), "xml", | |
533 NULL, | |
534 xml_logger_write, | |
535 xml_logger_finalize, | |
536 xml_logger_list, | |
8096 | 537 NULL, |
7431 | 538 NULL |
539 }; | |
540 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
541 |
7431 | 542 /**************************** |
7457 | 543 ** HTML LOGGER ************* |
544 ****************************/ | |
545 | |
546 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
547 const char *from, time_t time, const char *message) | |
548 { | |
9763 | 549 char *msg_fixed; |
7457 | 550 char date[64]; |
9613 | 551 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
552 const char *prpl_name = plugin->info->name; | |
9763 | 553 struct generic_logger_data *data = log->logger_data; |
9613 | 554 |
7618 | 555 if(!data) { |
9763 | 556 const char *prpl = |
557 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
9923 | 558 log_writer_common(log, type, time, ".html"); |
7457 | 559 |
9763 | 560 data = log->logger_data; |
7457 | 561 |
9763 | 562 /* if we can't write to the file, give up before we hurt ourselves */ |
563 if(!data->file) | |
564 return; | |
7616 | 565 |
7457 | 566 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 567 fprintf(data->file, "<html><head><title>"); |
568 fprintf(data->file, "Conversation with %s at %s on %s (%s)", | |
7457 | 569 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 570 fprintf(data->file, "</title></head><body>"); |
571 fprintf(data->file, | |
7457 | 572 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
573 log->name, date, gaim_account_get_username(log->account), prpl); | |
9763 | 574 |
7457 | 575 } |
7623 | 576 |
9892 | 577 /* if we can't write to the file, give up before we hurt ourselves */ |
578 if(!data->file) | |
579 return; | |
580 | |
7882 | 581 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
582 | |
8577 | 583 if(log->type == GAIM_LOG_SYSTEM){ |
9592 | 584 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8577 | 585 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
586 } else { | |
587 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
588 if (type & GAIM_MESSAGE_SYSTEM) | |
589 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
590 else if (type & GAIM_MESSAGE_WHISPER) | |
591 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
592 date, from, msg_fixed); | |
593 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
594 if (type & GAIM_MESSAGE_SEND) | |
595 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
596 else if (type & GAIM_MESSAGE_RECV) | |
597 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
598 } else if (type & GAIM_MESSAGE_RECV) { | |
599 if(gaim_message_meify(msg_fixed, -1)) | |
600 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 601 date, from, prpl_name, msg_fixed); |
8577 | 602 else |
603 fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 604 date, from, prpl_name, msg_fixed); |
8577 | 605 } else if (type & GAIM_MESSAGE_SEND) { |
606 if(gaim_message_meify(msg_fixed, -1)) | |
607 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 608 date, from, prpl_name, msg_fixed); |
8577 | 609 else |
610 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 611 date, from, prpl_name, msg_fixed); |
8577 | 612 } |
7564 | 613 } |
8573 | 614 |
7882 | 615 g_free(msg_fixed); |
7616 | 616 fflush(data->file); |
7457 | 617 } |
618 | |
619 static void html_logger_finalize(GaimLog *log) | |
620 { | |
7616 | 621 struct generic_logger_data *data = log->logger_data; |
622 if (data) { | |
623 if(data->file) { | |
624 fprintf(data->file, "</body></html>"); | |
625 fclose(data->file); | |
626 } | |
627 g_free(data->path); | |
7752 | 628 g_free(data); |
7463 | 629 } |
7457 | 630 } |
631 | |
8898 | 632 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7457 | 633 { |
8898 | 634 return log_lister_common(type, sn, account, ".html", &html_logger); |
7457 | 635 } |
636 | |
8573 | 637 static GList *html_logger_list_syslog(GaimAccount *account) |
638 { | |
8898 | 639 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); |
8573 | 640 } |
641 | |
7457 | 642 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
643 { | |
644 char *read, *minus_header; | |
7616 | 645 struct generic_logger_data *data = log->logger_data; |
7457 | 646 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 647 if (!data || !data->path) |
648 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
649 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 650 minus_header = strchr(read, '\n'); |
651 if (!minus_header) | |
652 minus_header = g_strdup(read); | |
653 else | |
654 minus_header = g_strdup(minus_header + 1); | |
655 g_free(read); | |
656 return minus_header; | |
657 } | |
8578 | 658 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7457 | 659 } |
660 | |
661 static GaimLogLogger html_logger = { | |
662 N_("HTML"), "html", | |
9819 | 663 NULL, |
7457 | 664 html_logger_write, |
665 html_logger_finalize, | |
666 html_logger_list, | |
7556 | 667 html_logger_read, |
8096 | 668 log_sizer_common, |
8573 | 669 NULL, |
670 html_logger_list_syslog | |
7457 | 671 }; |
672 | |
673 | |
674 | |
675 | |
676 /**************************** | |
7431 | 677 ** PLAIN TEXT LOGGER ******* |
678 ****************************/ | |
4184 | 679 |
7436 | 680 static void txt_logger_write(GaimLog *log, |
681 GaimMessageFlags type, | |
7431 | 682 const char *from, time_t time, const char *message) |
683 { | |
684 char date[64]; | |
9763 | 685 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
7616 | 686 struct generic_logger_data *data = log->logger_data; |
9763 | 687 char *stripped = NULL; |
688 | |
689 if(!data) { | |
7431 | 690 /* This log is new. We could use the loggers 'new' function, but |
691 * creating a new file there would result in empty files in the case | |
692 * that you open a convo with someone, but don't say anything. | |
693 */ | |
9763 | 694 const char *prpl = |
695 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
9923 | 696 log_writer_common(log, type, time, ".txt"); |
8898 | 697 |
9763 | 698 data = log->logger_data; |
7436 | 699 |
9763 | 700 /* if we can't write to the file, give up before we hurt ourselves */ |
701 if(!data->file) | |
702 return; | |
7616 | 703 |
7453 | 704 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 705 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 706 log->name, date, gaim_account_get_username(log->account), prpl); |
707 } | |
7436 | 708 |
7623 | 709 /* if we can't write to the file, give up before we hurt ourselves */ |
710 if(!data->file) | |
711 return; | |
712 | |
8573 | 713 stripped = gaim_markup_strip_html(message); |
714 | |
715 if(log->type == GAIM_LOG_SYSTEM){ | |
9592 | 716 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8573 | 717 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
718 } else { | |
719 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
720 if (type & GAIM_MESSAGE_SEND || | |
721 type & GAIM_MESSAGE_RECV) { | |
722 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
723 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
724 from, stripped); | |
725 } else { | |
726 if(gaim_message_meify(stripped, -1)) | |
727 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
728 stripped); | |
729 else | |
730 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
731 stripped); | |
732 } | |
733 } else if (type & GAIM_MESSAGE_SYSTEM) | |
734 fprintf(data->file, "(%s) %s\n", date, stripped); | |
735 else if (type & GAIM_MESSAGE_NO_LOG) { | |
736 /* This shouldn't happen */ | |
737 g_free(stripped); | |
738 return; | |
739 } else if (type & GAIM_MESSAGE_WHISPER) | |
740 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
741 else | |
742 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
743 from ? ":" : "", stripped); | |
744 } | |
745 | |
746 fflush(data->file); | |
747 g_free(stripped); | |
7431 | 748 } |
749 | |
750 static void txt_logger_finalize(GaimLog *log) | |
751 { | |
7616 | 752 struct generic_logger_data *data = log->logger_data; |
753 if (data) { | |
754 if(data->file) | |
755 fclose(data->file); | |
756 if(data->path) | |
757 g_free(data->path); | |
7752 | 758 g_free(data); |
7616 | 759 } |
7431 | 760 } |
761 | |
8898 | 762 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 763 { |
8898 | 764 return log_lister_common(type, sn, account, ".txt", &txt_logger); |
7431 | 765 } |
766 | |
8573 | 767 static GList *txt_logger_list_syslog(GaimAccount *account) |
768 { | |
8898 | 769 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); |
8573 | 770 } |
771 | |
7431 | 772 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
773 { | |
8517 | 774 char *read, *minus_header, *minus_header2; |
7616 | 775 struct generic_logger_data *data = log->logger_data; |
7457 | 776 *flags = 0; |
7616 | 777 if (!data || !data->path) |
778 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
779 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 780 minus_header = strchr(read, '\n'); |
781 if (!minus_header) | |
782 minus_header = g_strdup(read); | |
7436 | 783 else |
7431 | 784 minus_header = g_strdup(minus_header + 1); |
785 g_free(read); | |
8517 | 786 minus_header2 = gaim_escape_html(minus_header); |
787 g_free(minus_header); | |
788 return minus_header2; | |
7431 | 789 } |
8578 | 790 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7436 | 791 } |
7431 | 792 |
793 static GaimLogLogger txt_logger = { | |
794 N_("Plain text"), "txt", | |
9819 | 795 NULL, |
7431 | 796 txt_logger_write, |
797 txt_logger_finalize, | |
798 txt_logger_list, | |
7556 | 799 txt_logger_read, |
8096 | 800 log_sizer_common, |
8573 | 801 NULL, |
802 txt_logger_list_syslog | |
7431 | 803 }; |
804 | |
805 /**************** | |
806 * OLD LOGGER *** | |
807 ****************/ | |
808 | |
809 /* The old logger doesn't write logs, only reads them. This is to include | |
810 * old logs in the log viewer transparently. | |
811 */ | |
812 | |
813 struct old_logger_data { | |
7764 | 814 GaimStringref *pathref; |
7431 | 815 int offset; |
816 int length; | |
817 }; | |
818 | |
8898 | 819 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 820 { |
821 FILE *file; | |
822 char buf[BUF_LONG]; | |
823 struct tm tm; | |
7761 | 824 char month[4]; |
7431 | 825 struct old_logger_data *data = NULL; |
826 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 827 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
828 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 829 char *newlog; |
7761 | 830 int logfound = 0; |
831 int lastoff = 0; | |
832 int newlen; | |
7791 | 833 time_t lasttime = 0; |
7431 | 834 |
835 GaimLog *log = NULL; | |
836 GList *list = NULL; | |
837 | |
7473 | 838 g_free(logfile); |
7764 | 839 g_free(pathstr); |
7473 | 840 |
7764 | 841 if (!(file = fopen(gaim_stringref_value(pathref), "rb"))) { |
842 gaim_stringref_unref(pathref); | |
7431 | 843 return NULL; |
7447 | 844 } |
7436 | 845 |
7431 | 846 while (fgets(buf, BUF_LONG, file)) { |
847 if ((newlog = strstr(buf, "---- New C"))) { | |
848 int length; | |
849 int offset; | |
850 char convostart[32]; | |
851 char *temp = strchr(buf, '@'); | |
7436 | 852 |
7431 | 853 if (temp == NULL || strlen(temp) < 2) |
854 continue; | |
7436 | 855 |
7431 | 856 temp++; |
857 length = strcspn(temp, "-"); | |
858 if (length > 31) length = 31; | |
7436 | 859 |
7431 | 860 offset = ftell(file); |
7436 | 861 |
7761 | 862 if (logfound) { |
863 newlen = offset - lastoff - length; | |
7436 | 864 if(strstr(buf, "----</H3><BR>")) { |
7761 | 865 newlen -= |
866 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
867 sizeof("----</H3><BR>") - 2; | |
7436 | 868 } else { |
7761 | 869 newlen -= |
870 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 871 } |
872 | |
7461 | 873 if(strchr(buf, '\r')) |
7770 | 874 newlen--; |
7461 | 875 |
7761 | 876 if (newlen != 0) { |
877 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
878 log->logger = &old_logger; | |
879 log->time = lasttime; | |
880 data = g_new0(struct old_logger_data, 1); | |
881 data->offset = lastoff; | |
882 data->length = newlen; | |
7764 | 883 data->pathref = gaim_stringref_ref(pathref); |
7761 | 884 log->logger_data = data; |
7431 | 885 list = g_list_append(list, log); |
7761 | 886 } |
7431 | 887 } |
888 | |
7761 | 889 logfound = 1; |
890 lastoff = offset; | |
7436 | 891 |
7431 | 892 g_snprintf(convostart, length, "%s", temp); |
7676 | 893 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
894 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
895 /* Ugly hack, in case current locale is not English */ | |
896 if (strcmp(month, "Jan") == 0) { | |
897 tm.tm_mon= 0; | |
898 } else if (strcmp(month, "Feb") == 0) { | |
899 tm.tm_mon = 1; | |
900 } else if (strcmp(month, "Mar") == 0) { | |
901 tm.tm_mon = 2; | |
902 } else if (strcmp(month, "Apr") == 0) { | |
903 tm.tm_mon = 3; | |
904 } else if (strcmp(month, "May") == 0) { | |
905 tm.tm_mon = 4; | |
906 } else if (strcmp(month, "Jun") == 0) { | |
907 tm.tm_mon = 5; | |
908 } else if (strcmp(month, "Jul") == 0) { | |
909 tm.tm_mon = 6; | |
910 } else if (strcmp(month, "Aug") == 0) { | |
911 tm.tm_mon = 7; | |
912 } else if (strcmp(month, "Sep") == 0) { | |
913 tm.tm_mon = 8; | |
914 } else if (strcmp(month, "Oct") == 0) { | |
915 tm.tm_mon = 9; | |
916 } else if (strcmp(month, "Nov") == 0) { | |
917 tm.tm_mon = 10; | |
918 } else if (strcmp(month, "Dec") == 0) { | |
919 tm.tm_mon = 11; | |
920 } | |
921 tm.tm_year -= 1900; | |
7761 | 922 lasttime = mktime(&tm); |
4184 | 923 } |
924 } | |
7613 | 925 |
7761 | 926 if (logfound) { |
927 if ((newlen = ftell(file) - lastoff) != 0) { | |
928 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
929 log->logger = &old_logger; | |
930 log->time = lasttime; | |
931 data = g_new0(struct old_logger_data, 1); | |
932 data->offset = lastoff; | |
933 data->length = newlen; | |
7764 | 934 data->pathref = gaim_stringref_ref(pathref); |
7761 | 935 log->logger_data = data; |
7613 | 936 list = g_list_append(list, log); |
7761 | 937 } |
7613 | 938 } |
939 | |
7764 | 940 gaim_stringref_unref(pathref); |
7431 | 941 fclose(file); |
942 return list; | |
4184 | 943 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
944 |
8898 | 945 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
8096 | 946 { |
947 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
948 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
949 int size; | |
950 struct stat st; | |
951 | |
952 if (stat(pathstr, &st)) | |
953 size = 0; | |
954 else | |
955 size = st.st_size; | |
956 | |
957 g_free(logfile); | |
958 g_free(pathstr); | |
959 | |
960 return size; | |
961 } | |
962 | |
7616 | 963 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
964 { |
7431 | 965 struct old_logger_data *data = log->logger_data; |
7764 | 966 FILE *file = fopen(gaim_stringref_value(data->pathref), "rb"); |
7431 | 967 char *read = g_malloc(data->length + 1); |
968 fseek(file, data->offset, SEEK_SET); | |
969 fread(read, data->length, 1, file); | |
8370 | 970 fclose(file); |
7431 | 971 read[data->length] = '\0'; |
7436 | 972 *flags = 0; |
973 if(strstr(read, "<BR>")) | |
974 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 975 return read; |
976 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
977 |
7616 | 978 static int old_logger_size (GaimLog *log) |
7556 | 979 { |
980 struct old_logger_data *data = log->logger_data; | |
7616 | 981 return data ? data->length : 0; |
982 } | |
983 | |
984 static void old_logger_finalize(GaimLog *log) | |
985 { | |
986 struct old_logger_data *data = log->logger_data; | |
7764 | 987 gaim_stringref_unref(data->pathref); |
7616 | 988 g_free(data); |
7556 | 989 } |
990 | |
7431 | 991 static GaimLogLogger old_logger = { |
992 "old logger", "old", | |
7616 | 993 NULL, NULL, |
994 old_logger_finalize, | |
7431 | 995 old_logger_list, |
7616 | 996 old_logger_read, |
8096 | 997 old_logger_size, |
8573 | 998 old_logger_total_size, |
999 NULL | |
7431 | 1000 }; |