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