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