Mercurial > pidgin.yaz
annotate src/log.c @ 10608:adf7adc7da76
[gaim-migrate @ 12045]
This should allow configure to detect X screensaver extension and X session
management support for people who have the headers installed in standard
places.
committer: Tailor Script <tailor@pidgin.im>
author | Stu Tomlinson <stu@nosnilmot.com> |
---|---|
date | Thu, 17 Feb 2005 17:37:59 +0000 |
parents | 0f7452b1f777 |
children | 6480697e907b |
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)); |
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); | |
9923 | 571 log_writer_common(log, type, 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 |
9892 | 590 /* if we can't write to the file, give up before we hurt ourselves */ |
591 if(!data->file) | |
592 return; | |
593 | |
7882 | 594 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
595 | |
8577 | 596 if(log->type == GAIM_LOG_SYSTEM){ |
9592 | 597 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8577 | 598 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
599 } else { | |
600 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
601 if (type & GAIM_MESSAGE_SYSTEM) | |
602 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
603 else if (type & GAIM_MESSAGE_WHISPER) | |
604 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
605 date, from, msg_fixed); | |
606 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
607 if (type & GAIM_MESSAGE_SEND) | |
608 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
609 else if (type & GAIM_MESSAGE_RECV) | |
610 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
611 } else if (type & GAIM_MESSAGE_RECV) { | |
612 if(gaim_message_meify(msg_fixed, -1)) | |
613 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 614 date, from, prpl_name, msg_fixed); |
8577 | 615 else |
616 fprintf(data->file, "<font color=\"#A82F2F\"><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 if (type & GAIM_MESSAGE_SEND) { |
619 if(gaim_message_meify(msg_fixed, -1)) | |
620 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 621 date, from, prpl_name, msg_fixed); |
8577 | 622 else |
623 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
9613 | 624 date, from, prpl_name, msg_fixed); |
8577 | 625 } |
7564 | 626 } |
8573 | 627 |
7882 | 628 g_free(msg_fixed); |
7616 | 629 fflush(data->file); |
7457 | 630 } |
631 | |
632 static void html_logger_finalize(GaimLog *log) | |
633 { | |
7616 | 634 struct generic_logger_data *data = log->logger_data; |
635 if (data) { | |
636 if(data->file) { | |
637 fprintf(data->file, "</body></html>"); | |
638 fclose(data->file); | |
639 } | |
640 g_free(data->path); | |
7752 | 641 g_free(data); |
7463 | 642 } |
7457 | 643 } |
644 | |
8898 | 645 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7457 | 646 { |
8898 | 647 return log_lister_common(type, sn, account, ".html", &html_logger); |
7457 | 648 } |
649 | |
8573 | 650 static GList *html_logger_list_syslog(GaimAccount *account) |
651 { | |
8898 | 652 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); |
8573 | 653 } |
654 | |
7457 | 655 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
656 { | |
657 char *read, *minus_header; | |
7616 | 658 struct generic_logger_data *data = log->logger_data; |
7457 | 659 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 660 if (!data || !data->path) |
661 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
662 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 663 minus_header = strchr(read, '\n'); |
664 if (!minus_header) | |
665 minus_header = g_strdup(read); | |
666 else | |
667 minus_header = g_strdup(minus_header + 1); | |
668 g_free(read); | |
669 return minus_header; | |
670 } | |
8578 | 671 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7457 | 672 } |
673 | |
674 static GaimLogLogger html_logger = { | |
675 N_("HTML"), "html", | |
9819 | 676 NULL, |
7457 | 677 html_logger_write, |
678 html_logger_finalize, | |
679 html_logger_list, | |
7556 | 680 html_logger_read, |
8096 | 681 log_sizer_common, |
8573 | 682 NULL, |
683 html_logger_list_syslog | |
7457 | 684 }; |
685 | |
686 | |
687 | |
688 | |
689 /**************************** | |
7431 | 690 ** PLAIN TEXT LOGGER ******* |
691 ****************************/ | |
4184 | 692 |
7436 | 693 static void txt_logger_write(GaimLog *log, |
694 GaimMessageFlags type, | |
7431 | 695 const char *from, time_t time, const char *message) |
696 { | |
697 char date[64]; | |
9763 | 698 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
7616 | 699 struct generic_logger_data *data = log->logger_data; |
9763 | 700 char *stripped = NULL; |
701 | |
702 if(!data) { | |
7431 | 703 /* This log is new. We could use the loggers 'new' function, but |
704 * creating a new file there would result in empty files in the case | |
705 * that you open a convo with someone, but don't say anything. | |
706 */ | |
9763 | 707 const char *prpl = |
708 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
9923 | 709 log_writer_common(log, type, time, ".txt"); |
8898 | 710 |
9763 | 711 data = log->logger_data; |
7436 | 712 |
9763 | 713 /* if we can't write to the file, give up before we hurt ourselves */ |
714 if(!data->file) | |
715 return; | |
7616 | 716 |
7453 | 717 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 718 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 719 log->name, date, gaim_account_get_username(log->account), prpl); |
720 } | |
7436 | 721 |
7623 | 722 /* if we can't write to the file, give up before we hurt ourselves */ |
723 if(!data->file) | |
724 return; | |
725 | |
8573 | 726 stripped = gaim_markup_strip_html(message); |
727 | |
728 if(log->type == GAIM_LOG_SYSTEM){ | |
9592 | 729 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8573 | 730 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
731 } else { | |
732 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
733 if (type & GAIM_MESSAGE_SEND || | |
734 type & GAIM_MESSAGE_RECV) { | |
735 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
736 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
737 from, stripped); | |
738 } else { | |
739 if(gaim_message_meify(stripped, -1)) | |
740 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
741 stripped); | |
742 else | |
743 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
744 stripped); | |
745 } | |
746 } else if (type & GAIM_MESSAGE_SYSTEM) | |
747 fprintf(data->file, "(%s) %s\n", date, stripped); | |
748 else if (type & GAIM_MESSAGE_NO_LOG) { | |
749 /* This shouldn't happen */ | |
750 g_free(stripped); | |
751 return; | |
752 } else if (type & GAIM_MESSAGE_WHISPER) | |
753 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
754 else | |
755 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
756 from ? ":" : "", stripped); | |
757 } | |
758 | |
759 fflush(data->file); | |
760 g_free(stripped); | |
7431 | 761 } |
762 | |
763 static void txt_logger_finalize(GaimLog *log) | |
764 { | |
7616 | 765 struct generic_logger_data *data = log->logger_data; |
766 if (data) { | |
767 if(data->file) | |
768 fclose(data->file); | |
769 if(data->path) | |
770 g_free(data->path); | |
7752 | 771 g_free(data); |
7616 | 772 } |
7431 | 773 } |
774 | |
8898 | 775 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 776 { |
8898 | 777 return log_lister_common(type, sn, account, ".txt", &txt_logger); |
7431 | 778 } |
779 | |
8573 | 780 static GList *txt_logger_list_syslog(GaimAccount *account) |
781 { | |
8898 | 782 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); |
8573 | 783 } |
784 | |
7431 | 785 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
786 { | |
8517 | 787 char *read, *minus_header, *minus_header2; |
7616 | 788 struct generic_logger_data *data = log->logger_data; |
7457 | 789 *flags = 0; |
7616 | 790 if (!data || !data->path) |
791 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
792 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 793 minus_header = strchr(read, '\n'); |
794 if (!minus_header) | |
795 minus_header = g_strdup(read); | |
7436 | 796 else |
7431 | 797 minus_header = g_strdup(minus_header + 1); |
798 g_free(read); | |
8517 | 799 minus_header2 = gaim_escape_html(minus_header); |
800 g_free(minus_header); | |
801 return minus_header2; | |
7431 | 802 } |
8578 | 803 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7436 | 804 } |
7431 | 805 |
806 static GaimLogLogger txt_logger = { | |
807 N_("Plain text"), "txt", | |
9819 | 808 NULL, |
7431 | 809 txt_logger_write, |
810 txt_logger_finalize, | |
811 txt_logger_list, | |
7556 | 812 txt_logger_read, |
8096 | 813 log_sizer_common, |
8573 | 814 NULL, |
815 txt_logger_list_syslog | |
7431 | 816 }; |
817 | |
818 /**************** | |
819 * OLD LOGGER *** | |
820 ****************/ | |
821 | |
822 /* The old logger doesn't write logs, only reads them. This is to include | |
823 * old logs in the log viewer transparently. | |
824 */ | |
825 | |
826 struct old_logger_data { | |
7764 | 827 GaimStringref *pathref; |
7431 | 828 int offset; |
829 int length; | |
830 }; | |
831 | |
8898 | 832 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 833 { |
834 FILE *file; | |
835 char buf[BUF_LONG]; | |
836 struct tm tm; | |
7761 | 837 char month[4]; |
7431 | 838 struct old_logger_data *data = NULL; |
839 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 840 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
841 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 842 char *newlog; |
7761 | 843 int logfound = 0; |
844 int lastoff = 0; | |
845 int newlen; | |
7791 | 846 time_t lasttime = 0; |
7431 | 847 |
848 GaimLog *log = NULL; | |
849 GList *list = NULL; | |
850 | |
7473 | 851 g_free(logfile); |
7764 | 852 g_free(pathstr); |
7473 | 853 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
854 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { |
7764 | 855 gaim_stringref_unref(pathref); |
7431 | 856 return NULL; |
7447 | 857 } |
7436 | 858 |
7431 | 859 while (fgets(buf, BUF_LONG, file)) { |
860 if ((newlog = strstr(buf, "---- New C"))) { | |
861 int length; | |
862 int offset; | |
863 char convostart[32]; | |
864 char *temp = strchr(buf, '@'); | |
7436 | 865 |
7431 | 866 if (temp == NULL || strlen(temp) < 2) |
867 continue; | |
7436 | 868 |
7431 | 869 temp++; |
870 length = strcspn(temp, "-"); | |
871 if (length > 31) length = 31; | |
7436 | 872 |
7431 | 873 offset = ftell(file); |
7436 | 874 |
7761 | 875 if (logfound) { |
876 newlen = offset - lastoff - length; | |
7436 | 877 if(strstr(buf, "----</H3><BR>")) { |
7761 | 878 newlen -= |
879 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
880 sizeof("----</H3><BR>") - 2; | |
7436 | 881 } else { |
7761 | 882 newlen -= |
883 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 884 } |
885 | |
7461 | 886 if(strchr(buf, '\r')) |
7770 | 887 newlen--; |
7461 | 888 |
7761 | 889 if (newlen != 0) { |
890 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
891 log->logger = &old_logger; | |
892 log->time = lasttime; | |
893 data = g_new0(struct old_logger_data, 1); | |
894 data->offset = lastoff; | |
895 data->length = newlen; | |
7764 | 896 data->pathref = gaim_stringref_ref(pathref); |
7761 | 897 log->logger_data = data; |
7431 | 898 list = g_list_append(list, log); |
7761 | 899 } |
7431 | 900 } |
901 | |
7761 | 902 logfound = 1; |
903 lastoff = offset; | |
7436 | 904 |
7431 | 905 g_snprintf(convostart, length, "%s", temp); |
7676 | 906 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
907 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
908 /* Ugly hack, in case current locale is not English */ | |
909 if (strcmp(month, "Jan") == 0) { | |
910 tm.tm_mon= 0; | |
911 } else if (strcmp(month, "Feb") == 0) { | |
912 tm.tm_mon = 1; | |
913 } else if (strcmp(month, "Mar") == 0) { | |
914 tm.tm_mon = 2; | |
915 } else if (strcmp(month, "Apr") == 0) { | |
916 tm.tm_mon = 3; | |
917 } else if (strcmp(month, "May") == 0) { | |
918 tm.tm_mon = 4; | |
919 } else if (strcmp(month, "Jun") == 0) { | |
920 tm.tm_mon = 5; | |
921 } else if (strcmp(month, "Jul") == 0) { | |
922 tm.tm_mon = 6; | |
923 } else if (strcmp(month, "Aug") == 0) { | |
924 tm.tm_mon = 7; | |
925 } else if (strcmp(month, "Sep") == 0) { | |
926 tm.tm_mon = 8; | |
927 } else if (strcmp(month, "Oct") == 0) { | |
928 tm.tm_mon = 9; | |
929 } else if (strcmp(month, "Nov") == 0) { | |
930 tm.tm_mon = 10; | |
931 } else if (strcmp(month, "Dec") == 0) { | |
932 tm.tm_mon = 11; | |
933 } | |
934 tm.tm_year -= 1900; | |
7761 | 935 lasttime = mktime(&tm); |
4184 | 936 } |
937 } | |
7613 | 938 |
7761 | 939 if (logfound) { |
940 if ((newlen = ftell(file) - lastoff) != 0) { | |
941 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
942 log->logger = &old_logger; | |
943 log->time = lasttime; | |
944 data = g_new0(struct old_logger_data, 1); | |
945 data->offset = lastoff; | |
946 data->length = newlen; | |
7764 | 947 data->pathref = gaim_stringref_ref(pathref); |
7761 | 948 log->logger_data = data; |
7613 | 949 list = g_list_append(list, log); |
7761 | 950 } |
7613 | 951 } |
952 | |
7764 | 953 gaim_stringref_unref(pathref); |
7431 | 954 fclose(file); |
955 return list; | |
4184 | 956 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
957 |
8898 | 958 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
8096 | 959 { |
960 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
961 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
962 int size; | |
963 struct stat st; | |
964 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
965 if (g_stat(pathstr, &st)) |
8096 | 966 size = 0; |
967 else | |
968 size = st.st_size; | |
969 | |
970 g_free(logfile); | |
971 g_free(pathstr); | |
972 | |
973 return size; | |
974 } | |
975 | |
7616 | 976 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
977 { |
7431 | 978 struct old_logger_data *data = log->logger_data; |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
979 FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); |
7431 | 980 char *read = g_malloc(data->length + 1); |
981 fseek(file, data->offset, SEEK_SET); | |
982 fread(read, data->length, 1, file); | |
8370 | 983 fclose(file); |
7431 | 984 read[data->length] = '\0'; |
7436 | 985 *flags = 0; |
986 if(strstr(read, "<BR>")) | |
987 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 988 return read; |
989 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
990 |
7616 | 991 static int old_logger_size (GaimLog *log) |
7556 | 992 { |
993 struct old_logger_data *data = log->logger_data; | |
7616 | 994 return data ? data->length : 0; |
995 } | |
996 | |
997 static void old_logger_finalize(GaimLog *log) | |
998 { | |
999 struct old_logger_data *data = log->logger_data; | |
7764 | 1000 gaim_stringref_unref(data->pathref); |
7616 | 1001 g_free(data); |
7556 | 1002 } |
1003 | |
7431 | 1004 static GaimLogLogger old_logger = { |
1005 "old logger", "old", | |
7616 | 1006 NULL, NULL, |
1007 old_logger_finalize, | |
7431 | 1008 old_logger_list, |
7616 | 1009 old_logger_read, |
8096 | 1010 old_logger_size, |
8573 | 1011 old_logger_total_size, |
1012 NULL | |
7431 | 1013 }; |