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