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