Mercurial > pidgin.yaz
annotate src/log.c @ 11025:8d2007d738d5
[gaim-migrate @ 12899]
sf patch #1180490, from Richard Laager (who else?)
A pretty peach of a patch that allows you to auto-complete screen names
based on log file names.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 23 Jun 2005 03:04:52 +0000 |
parents | 5e41c817dfa2 |
children | 50224ac8184d |
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 | |
11025 | 46 static GList *log_get_log_sets_common(); |
8635 | 47 |
7431 | 48 /************************************************************************** |
49 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
50 **************************************************************************/ | |
4184 | 51 |
7431 | 52 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, time_t time) |
53 { | |
54 GaimLog *log = g_new0(GaimLog, 1); | |
8635 | 55 log->name = g_strdup(gaim_normalize(account, name)); |
7431 | 56 log->account = account; |
57 log->time = time; | |
8573 | 58 log->type = type; |
8096 | 59 log->logger_data = NULL; |
7431 | 60 log->logger = gaim_log_logger_get(); |
7440 | 61 if (log->logger && log->logger->create) |
62 log->logger->create(log); | |
7431 | 63 return log; |
4184 | 64 } |
65 | |
7431 | 66 void gaim_log_free(GaimLog *log) |
4184 | 67 { |
7431 | 68 g_return_if_fail(log); |
69 if (log->logger && log->logger->finalize) | |
70 log->logger->finalize(log); | |
71 g_free(log->name); | |
72 g_free(log); | |
73 } | |
7436 | 74 |
75 void gaim_log_write(GaimLog *log, GaimMessageFlags type, | |
7431 | 76 const char *from, time_t time, const char *message) |
77 { | |
10173 | 78 struct _gaim_logsize_user *lu; |
79 | |
7431 | 80 g_return_if_fail(log); |
81 g_return_if_fail(log->logger); | |
7442 | 82 g_return_if_fail(log->logger->write); |
7431 | 83 |
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
84 (log->logger->write)(log, type, from, time, message); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
85 |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
86 lu = g_new(struct _gaim_logsize_user, 1); |
9892 | 87 |
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
88 lu->name = g_strdup(gaim_normalize(log->account, log->name)); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
89 lu->account = log->account; |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
90 g_hash_table_remove(logsize_users, lu); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
91 g_free(lu->name); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
92 g_free(lu); |
9892 | 93 |
4184 | 94 } |
95 | |
7431 | 96 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 97 { |
7542 | 98 GaimLogReadFlags mflags; |
7431 | 99 g_return_val_if_fail(log && log->logger, NULL); |
7462 | 100 if (log->logger->read) { |
7535 | 101 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
102 gaim_str_strip_cr(ret); |
7462 | 103 return ret; |
104 } | |
7470 | 105 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
4184 | 106 } |
7616 | 107 |
7556 | 108 int gaim_log_get_size(GaimLog *log) |
109 { | |
110 g_return_val_if_fail(log && log->logger, 0); | |
8096 | 111 |
7556 | 112 if (log->logger->size) |
113 return log->logger->size(log); | |
114 return 0; | |
115 } | |
116 | |
8635 | 117 static guint _gaim_logsize_user_hash(struct _gaim_logsize_user *lu) |
118 { | |
119 return g_str_hash(lu->name); | |
120 } | |
121 | |
122 static guint _gaim_logsize_user_equal(struct _gaim_logsize_user *lu1, | |
123 struct _gaim_logsize_user *lu2) | |
124 { | |
125 return ((!strcmp(lu1->name, lu2->name)) && lu1->account == lu2->account); | |
126 } | |
127 | |
128 static void _gaim_logsize_user_free_key(struct _gaim_logsize_user *lu) | |
129 { | |
130 g_free(lu->name); | |
131 g_free(lu); | |
132 } | |
133 | |
8898 | 134 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account) |
7556 | 135 { |
9677 | 136 gpointer ptrsize; |
137 int size = 0; | |
8096 | 138 GSList *n; |
8635 | 139 struct _gaim_logsize_user *lu; |
8096 | 140 |
8635 | 141 lu = g_new(struct _gaim_logsize_user, 1); |
142 lu->name = g_strdup(gaim_normalize(account, name)); | |
143 lu->account = account; | |
144 | |
9677 | 145 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { |
146 size = GPOINTER_TO_INT(ptrsize); | |
8635 | 147 g_free(lu->name); |
148 g_free(lu); | |
149 } else { | |
150 for (n = loggers; n; n = n->next) { | |
151 GaimLogLogger *logger = n->data; | |
7616 | 152 |
8635 | 153 if(logger->total_size){ |
8898 | 154 size += (logger->total_size)(type, name, account); |
8635 | 155 } else if(logger->list) { |
8898 | 156 GList *logs = (logger->list)(type, name, account); |
8635 | 157 int this_size = 0; |
158 | |
159 while (logs) { | |
160 GList *logs2 = logs->next; | |
161 GaimLog *log = (GaimLog*)(logs->data); | |
162 this_size += gaim_log_get_size(log); | |
163 gaim_log_free(log); | |
164 g_list_free_1(logs); | |
165 logs = logs2; | |
166 } | |
167 | |
168 size += this_size; | |
8096 | 169 } |
8635 | 170 } |
8096 | 171 |
8635 | 172 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); |
7556 | 173 } |
174 return size; | |
175 } | |
4184 | 176 |
10822 | 177 char * |
10577 | 178 gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account) |
179 { | |
180 GaimPlugin *prpl; | |
181 GaimPluginProtocolInfo *prpl_info; | |
182 const char *prpl_name; | |
183 char *acct_name; | |
9926 | 184 const char *target; |
10577 | 185 char *dir; |
9926 | 186 |
10577 | 187 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); |
188 if (!prpl) | |
189 return NULL; | |
190 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
191 prpl_name = prpl_info->list_icon(account, NULL); | |
192 | |
193 acct_name = g_strdup(gaim_escape_filename(gaim_normalize(account, | |
194 gaim_account_get_username(account)))); | |
9923 | 195 |
196 if (type == GAIM_LOG_CHAT) { | |
197 char *temp = g_strdup_printf("%s.chat", gaim_normalize(account, name)); | |
9926 | 198 target = gaim_escape_filename(temp); |
9923 | 199 g_free(temp); |
200 } else if(type == GAIM_LOG_SYSTEM) { | |
9926 | 201 target = ".system"; |
9923 | 202 } else { |
9926 | 203 target = gaim_escape_filename(gaim_normalize(account, name)); |
9923 | 204 } |
205 | |
10577 | 206 dir = g_build_filename(gaim_user_dir(), "logs", prpl_name, acct_name, target, NULL); |
9926 | 207 |
9923 | 208 g_free(acct_name); |
10577 | 209 |
9923 | 210 return dir; |
211 } | |
212 | |
7431 | 213 /**************************************************************************** |
214 * LOGGER FUNCTIONS ********************************************************* | |
215 ****************************************************************************/ | |
4184 | 216 |
7431 | 217 static GaimLogLogger *current_logger = NULL; |
7436 | 218 |
7431 | 219 static void logger_pref_cb(const char *name, GaimPrefType type, |
220 gpointer value, gpointer data) | |
221 { | |
222 GaimLogLogger *logger; | |
223 GSList *l = loggers; | |
224 while (l) { | |
225 logger = l->data; | |
226 if (!strcmp(logger->id, value)) { | |
227 gaim_log_logger_set(logger); | |
228 return; | |
4184 | 229 } |
7431 | 230 l = l->next; |
231 } | |
232 gaim_log_logger_set(&txt_logger); | |
233 } | |
4184 | 234 |
235 | |
8898 | 236 GaimLogLogger *gaim_log_logger_new( |
237 void(*create)(GaimLog *), | |
238 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), | |
239 void(*finalize)(GaimLog *), | |
240 GList*(*list)(GaimLogType type, const char*, GaimAccount*), | |
241 char*(*read)(GaimLog*, GaimLogReadFlags*), | |
11025 | 242 int(*size)(GaimLog*), |
243 int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), | |
244 GList*(*list_syslog)(GaimAccount *account), | |
245 GList*(*get_log_sets)(void)) | |
7431 | 246 { |
247 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
11025 | 248 |
7440 | 249 logger->create = create; |
7431 | 250 logger->write = write; |
251 logger->finalize = finalize; | |
252 logger->list = list; | |
253 logger->read = read; | |
7556 | 254 logger->size = size; |
11025 | 255 logger->total_size = total_size; |
256 logger->list_syslog = list_syslog; | |
257 logger->get_log_sets = get_log_sets; | |
258 | |
7431 | 259 return logger; |
4184 | 260 } |
261 | |
7431 | 262 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 263 { |
7431 | 264 g_free(logger); |
265 } | |
4184 | 266 |
7431 | 267 void gaim_log_logger_add (GaimLogLogger *logger) |
268 { | |
269 g_return_if_fail(logger); | |
270 if (g_slist_find(loggers, logger)) | |
271 return; | |
272 loggers = g_slist_append(loggers, logger); | |
273 } | |
274 | |
275 void gaim_log_logger_remove (GaimLogLogger *logger) | |
276 { | |
277 g_return_if_fail(logger); | |
278 g_slist_remove(loggers, logger); | |
4184 | 279 } |
280 | |
7431 | 281 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 282 { |
7431 | 283 g_return_if_fail(logger); |
284 current_logger = logger; | |
7436 | 285 } |
4184 | 286 |
7431 | 287 GaimLogLogger *gaim_log_logger_get() |
288 { | |
289 return current_logger; | |
290 } | |
4184 | 291 |
7431 | 292 GList *gaim_log_logger_get_options(void) |
293 { | |
294 GSList *n; | |
295 GList *list = NULL; | |
296 GaimLogLogger *data; | |
4184 | 297 |
7431 | 298 for (n = loggers; n; n = n->next) { |
299 data = n->data; | |
300 if (!data->write) | |
301 continue; | |
7494 | 302 list = g_list_append(list, _(data->name)); |
7431 | 303 list = g_list_append(list, data->id); |
4184 | 304 } |
305 | |
7431 | 306 return list; |
307 } | |
308 | |
8573 | 309 gint gaim_log_compare(gconstpointer y, gconstpointer z) |
7431 | 310 { |
7436 | 311 const GaimLog *a = y; |
312 const GaimLog *b = z; | |
313 | |
7431 | 314 return b->time - a->time; |
315 } | |
316 | |
8898 | 317 GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account) |
7431 | 318 { |
319 GList *logs = NULL; | |
320 GSList *n; | |
321 for (n = loggers; n; n = n->next) { | |
322 GaimLogLogger *logger = n->data; | |
323 if (!logger->list) | |
324 continue; | |
8898 | 325 logs = g_list_concat(logs, logger->list(type, name, account)); |
7431 | 326 } |
7436 | 327 |
8573 | 328 return g_list_sort(logs, gaim_log_compare); |
329 } | |
330 | |
11025 | 331 gint gaim_log_set_compare(gconstpointer y, gconstpointer z) |
332 { | |
333 const GaimLogSet *a = y; | |
334 const GaimLogSet *b = z; | |
335 gint ret = 0; | |
336 char *tmp; | |
337 | |
338 /* This logic seems weird at first... | |
339 * If either account is NULL, we pretend the accounts are | |
340 * equal. This allows us to detect duplicates that will | |
341 * exist if one logger knows the account and another | |
342 * doesn't. */ | |
343 if (a->account != NULL && b->account != NULL) { | |
344 ret = gaim_utf8_strcasecmp(gaim_account_get_username(a->account), gaim_account_get_username(b->account)); | |
345 if (ret != 0) | |
346 return ret; | |
347 } | |
348 | |
349 tmp = g_strdup(gaim_normalize(a->account, a->name)); | |
350 ret = gaim_utf8_strcasecmp(tmp, gaim_normalize(b->account, b->name)); | |
351 g_free(tmp); | |
352 if (ret != 0) | |
353 return ret; | |
354 | |
355 return (gint)b->type - (gint)a->type; | |
356 } | |
357 | |
358 guint log_set_hash(gconstpointer key) | |
359 { | |
360 const GaimLogSet *set = key; | |
361 | |
362 /* The account isn't hashed because we need GaimLogSets with NULL accounts | |
363 * to be found when we search by a GaimLogSet that has a non-NULL account | |
364 * but the same type and name. */ | |
365 return g_int_hash((gint *)&set->type) + g_str_hash(set->name); | |
366 } | |
367 | |
368 gboolean log_set_equal(gconstpointer a, gconstpointer b) | |
369 { | |
370 /* I realize that the choices made for GList and GHashTable | |
371 * make sense for those data types, but I wish the comparison | |
372 * functions were compatible. */ | |
373 return !gaim_log_set_compare(a, b); | |
374 } | |
375 | |
376 void log_set_build_list(gpointer key, gpointer value, gpointer user_data) | |
377 { | |
378 *((GList **)user_data) = g_list_append(*((GList **)user_data), key); | |
379 } | |
380 | |
381 GList *gaim_log_get_log_sets() | |
382 { | |
383 GSList *n; | |
384 GList *sets = NULL; | |
385 GList *set; | |
386 GHashTable *sets_ht = g_hash_table_new(log_set_hash, log_set_equal); | |
387 | |
388 /* Get the log sets from all the loggers. */ | |
389 for (n = loggers; n; n = n->next) { | |
390 GaimLogLogger *logger = n->data; | |
391 | |
392 if (!logger->get_log_sets) | |
393 continue; | |
394 | |
395 sets = g_list_concat(sets, logger->get_log_sets()); | |
396 } | |
397 | |
398 /* Get the log sets for loggers that use the common logger functions. */ | |
399 sets = g_list_concat(sets, log_get_log_sets_common()); | |
400 | |
401 for (set = sets; set != NULL ; set = set->next) { | |
402 GaimLogSet *existing_set = g_hash_table_lookup(sets_ht, set->data); | |
403 | |
404 if (existing_set == NULL) { | |
405 g_hash_table_insert(sets_ht, set->data, set->data); | |
406 } else if (existing_set->account == NULL && ((GaimLogSet *)set->data)->account != NULL) { | |
407 /* The existing entry in the hash table has no account. | |
408 * This one does. We'll delete the old one and keep this one. */ | |
409 g_hash_table_replace(sets_ht, set->data, set->data); | |
410 g_free(existing_set->name); | |
411 g_free(existing_set); | |
412 } else { | |
413 g_free(((GaimLogSet *)set->data)->name); | |
414 g_free(set->data); | |
415 } | |
416 } | |
417 g_list_free(sets); | |
418 | |
419 /* At this point, we've built a GHashTable of unique GaimLogSets. | |
420 * So, we build a list of those keys and destroy the GHashTable. */ | |
421 sets = NULL; | |
422 g_hash_table_foreach(sets_ht, log_set_build_list, &sets); | |
423 g_hash_table_destroy(sets_ht); | |
424 | |
425 return g_list_sort(sets, gaim_log_set_compare); | |
426 } | |
427 | |
8573 | 428 GList *gaim_log_get_system_logs(GaimAccount *account) |
429 { | |
430 GList *logs = NULL; | |
431 GSList *n; | |
432 for (n = loggers; n; n = n->next) { | |
433 GaimLogLogger *logger = n->data; | |
434 if (!logger->list_syslog) | |
435 continue; | |
436 logs = g_list_concat(logs, logger->list_syslog(account)); | |
437 } | |
438 | |
439 return g_list_sort(logs, gaim_log_compare); | |
7431 | 440 } |
441 | |
442 void gaim_log_init(void) | |
7436 | 443 { |
7431 | 444 gaim_prefs_add_none("/core/logging"); |
7555 | 445 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
446 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
8573 | 447 gaim_prefs_add_bool("/core/logging/log_system", FALSE); |
448 gaim_prefs_add_bool("/core/logging/log_signon_signoff", FALSE); | |
449 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); | |
450 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); | |
451 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); | |
452 | |
7431 | 453 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 454 gaim_log_logger_add(&html_logger); |
7431 | 455 gaim_log_logger_add(&txt_logger); |
456 gaim_log_logger_add(&old_logger); | |
10087 | 457 gaim_prefs_connect_callback(NULL, "/core/logging/format", |
7431 | 458 logger_pref_cb, NULL); |
459 gaim_prefs_trigger_callback("/core/logging/format"); | |
8635 | 460 |
461 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, | |
462 (GEqualFunc)_gaim_logsize_user_equal, | |
463 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); | |
7431 | 464 } |
465 | |
466 /**************************************************************************** | |
467 * LOGGERS ****************************************************************** | |
468 ****************************************************************************/ | |
469 | |
10822 | 470 void gaim_log_common_writer(GaimLog *log, time_t time, const char *ext) |
9763 | 471 { |
472 char date[64]; | |
10822 | 473 GaimLogCommonLoggerData *data = log->logger_data; |
9763 | 474 |
475 if(!data) { | |
476 /* This log is new */ | |
9923 | 477 char *dir, *filename, *path; |
10577 | 478 |
9923 | 479 dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
10577 | 480 if (dir == NULL) |
481 return; | |
482 | |
9923 | 483 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
9763 | 484 |
485 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S", localtime(&log->time)); | |
486 | |
487 filename = g_strdup_printf("%s%s", date, ext ? ext : ""); | |
488 | |
489 path = g_build_filename(dir, filename, NULL); | |
490 g_free(dir); | |
491 g_free(filename); | |
492 | |
10822 | 493 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
9763 | 494 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
495 data->file = g_fopen(path, "a"); |
9763 | 496 if (!data->file) { |
497 gaim_debug(GAIM_DEBUG_ERROR, "log", | |
9892 | 498 "Could not create log file %s\n", path); |
9763 | 499 g_free(path); |
500 return; | |
501 } | |
502 g_free(path); | |
10577 | 503 } |
9763 | 504 } |
505 | |
10822 | 506 GList *gaim_log_common_lister(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
7431 | 507 { |
508 GDir *dir; | |
509 GList *list = NULL; | |
7628 | 510 const char *filename; |
8111 | 511 char *path; |
512 | |
513 if(!account) | |
514 return NULL; | |
515 | |
9923 | 516 path = gaim_log_get_log_dir(type, name, account); |
10577 | 517 if (path == NULL) |
518 return NULL; | |
7447 | 519 |
7431 | 520 if (!(dir = g_dir_open(path, 0, NULL))) { |
521 g_free(path); | |
522 return NULL; | |
523 } | |
8898 | 524 |
7431 | 525 while ((filename = g_dir_read_name(dir))) { |
8577 | 526 if (gaim_str_has_suffix(filename, ext) && |
527 strlen(filename) == 17 + strlen(ext)) { | |
7431 | 528 GaimLog *log; |
10822 | 529 GaimLogCommonLoggerData *data; |
8577 | 530 time_t stamp = gaim_str_to_time(filename, FALSE); |
7431 | 531 |
8898 | 532 log = gaim_log_new(type, name, account, stamp); |
7431 | 533 log->logger = logger; |
10822 | 534 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
7616 | 535 data->path = g_build_filename(path, filename, NULL); |
7431 | 536 list = g_list_append(list, log); |
4184 | 537 } |
538 } | |
7431 | 539 g_dir_close(dir); |
7447 | 540 g_free(path); |
7431 | 541 return list; |
542 } | |
4184 | 543 |
10822 | 544 int gaim_log_common_sizer(GaimLog *log) |
7556 | 545 { |
546 struct stat st; | |
10822 | 547 GaimLogCommonLoggerData *data = log->logger_data; |
7556 | 548 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
549 if (!data->path || g_stat(data->path, &st)) |
7556 | 550 st.st_size = 0; |
551 | |
552 return st.st_size; | |
553 } | |
554 | |
11025 | 555 /* This will build log sets for all loggers that use the common logger |
556 * functions because they use the same directory structure. */ | |
557 static GList *log_get_log_sets_common() | |
558 { | |
559 gchar *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
560 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
561 const gchar *protocol; | |
562 GList *sets = NULL; | |
563 | |
564 if (log_dir == NULL) { | |
565 g_free(log_path); | |
566 return NULL; | |
567 } | |
568 | |
569 while ((protocol = g_dir_read_name(log_dir)) != NULL) { | |
570 gchar *protocol_path = g_build_filename(log_path, protocol, NULL); | |
571 GDir *protocol_dir; | |
572 const gchar *username; | |
573 gchar *protocol_unescaped; | |
574 GList *account_iter; | |
575 GList *accounts = NULL; | |
576 | |
577 if ((protocol_dir = g_dir_open(protocol_path, 0, NULL)) == NULL) { | |
578 g_free(protocol_path); | |
579 continue; | |
580 } | |
581 | |
582 /* Using g_strdup() to cover the one-in-a-million chance that a | |
583 * prpl's list_icon function uses gaim_unescape_filename(). */ | |
584 protocol_unescaped = g_strdup(gaim_unescape_filename(protocol)); | |
585 | |
586 /* Find all the accounts for protocol. */ | |
587 for (account_iter = gaim_accounts_get_all() ; account_iter != NULL ; account_iter = account_iter->next) { | |
588 GaimPlugin *prpl; | |
589 GaimPluginProtocolInfo *prpl_info; | |
590 | |
591 prpl = gaim_find_prpl(gaim_account_get_protocol_id((GaimAccount *)account_iter->data)); | |
592 if (!prpl) | |
593 continue; | |
594 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
595 | |
596 if (!strcmp(protocol_unescaped, prpl_info->list_icon((GaimAccount *)account_iter->data, NULL))) | |
597 accounts = g_list_append(accounts, account_iter->data); | |
598 } | |
599 g_free(protocol_unescaped); | |
600 | |
601 while ((username = g_dir_read_name(protocol_dir)) != NULL) { | |
602 gchar *username_path = g_build_filename(protocol_path, username, NULL); | |
603 GDir *username_dir; | |
604 const gchar *username_unescaped; | |
605 GaimAccount *account = NULL; | |
606 gchar *name; | |
607 | |
608 if ((username_dir = g_dir_open(username_path, 0, NULL)) == NULL) { | |
609 g_free(username_path); | |
610 continue; | |
611 } | |
612 | |
613 /* Find the account for username in the list of accounts for protocol. */ | |
614 username_unescaped = gaim_unescape_filename(username); | |
615 for (account_iter = g_list_first(accounts) ; account_iter != NULL ; account_iter = account_iter->next) { | |
616 if (!strcmp(((GaimAccount *)account_iter->data)->username, username_unescaped)) { | |
617 account = account_iter->data; | |
618 break; | |
619 } | |
620 } | |
621 | |
622 /* Don't worry about the cast, name will point to dynamically allocated memory shortly. */ | |
623 while ((name = (gchar *)g_dir_read_name(username_dir)) != NULL) { | |
624 size_t len; | |
625 GaimLogSet *set = g_new0(GaimLogSet, 1); | |
626 | |
627 /* Unescape the filename. */ | |
628 name = g_strdup(gaim_unescape_filename(name)); | |
629 | |
630 /* Get the (possibly new) length of name. */ | |
631 len = strlen(name); | |
632 | |
633 set->account = account; | |
634 set->name = name; | |
635 | |
636 /* Chat for .chat or .system at the end of the name to determine the type. */ | |
637 set->type = GAIM_LOG_IM; | |
638 if (len > 7) { | |
639 gchar *tmp = &name[len - 7]; | |
640 if (!strcmp(tmp, ".system")) { | |
641 set->type = GAIM_LOG_SYSTEM; | |
642 *tmp = '\0'; | |
643 } | |
644 } | |
645 if (len > 5) { | |
646 gchar *tmp = &name[len - 5]; | |
647 if (!strcmp(tmp, ".chat")) { | |
648 set->type = GAIM_LOG_CHAT; | |
649 *tmp = '\0'; | |
650 } | |
651 } | |
652 | |
653 /* Determine if this (account, name) combination exists as a buddy. */ | |
654 if (gaim_find_buddy(account, name) != NULL) | |
655 set->buddy = TRUE; | |
656 else | |
657 set->buddy = FALSE; | |
658 | |
659 sets = g_list_append(sets, set); | |
660 } | |
661 g_free(username_path); | |
662 g_dir_close(username_dir); | |
663 } | |
664 g_free(protocol_path); | |
665 g_dir_close(protocol_dir); | |
666 } | |
667 g_free(log_path); | |
668 g_dir_close(log_dir); | |
669 | |
670 return sets; | |
671 } | |
672 | |
7431 | 673 #if 0 /* Maybe some other time. */ |
7443 | 674 /**************** |
7431 | 675 ** XML LOGGER ** |
676 ****************/ | |
677 | |
678 static const char *str_from_msg_type (GaimMessageFlags type) | |
679 { | |
7443 | 680 |
7431 | 681 return ""; |
7443 | 682 |
7431 | 683 } |
684 | |
7443 | 685 static void xml_logger_write(GaimLog *log, |
686 GaimMessageFlags type, | |
7431 | 687 const char *from, time_t time, const char *message) |
688 { | |
689 char date[64]; | |
690 char *xhtml = NULL; | |
10577 | 691 |
7431 | 692 if (!log->logger_data) { |
693 /* This log is new. We could use the loggers 'new' function, but | |
694 * creating a new file there would result in empty files in the case | |
695 * that you open a convo with someone, but don't say anything. | |
696 */ | |
9923 | 697 char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
7431 | 698 FILE *file; |
10577 | 699 |
700 if (dir == NULL) | |
701 return; | |
702 | |
7453 | 703 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 704 |
7612 | 705 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7443 | 706 |
7431 | 707 char *filename = g_build_filename(dir, date, NULL); |
708 g_free(dir); | |
7443 | 709 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
710 log->logger_data = g_fopen(filename, "a"); |
7431 | 711 if (!log->logger_data) { |
712 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 713 g_free(filename); |
7431 | 714 return; |
715 } | |
7564 | 716 g_free(filename); |
7431 | 717 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
718 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 719 |
7453 | 720 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 721 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
722 date, log->name, prpl); | |
723 } | |
7443 | 724 |
9923 | 725 /* if we can't write to the file, give up before we hurt ourselves */ |
726 if(!data->file) | |
727 return; | |
728 | |
7453 | 729 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 730 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
731 if (from) | |
7443 | 732 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
733 str_from_msg_type(type), | |
7431 | 734 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
735 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
736 from, date, xhtml); | |
737 else | |
7443 | 738 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
739 str_from_msg_type(type), | |
7431 | 740 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
741 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 742 date, xhtml): |
7431 | 743 fflush(log->logger_data); |
744 g_free(xhtml); | |
7443 | 745 } |
746 | |
7431 | 747 static void xml_logger_finalize(GaimLog *log) |
748 { | |
749 if (log->logger_data) { | |
750 fprintf(log->logger_data, "</conversation>\n"); | |
751 fclose(log->logger_data); | |
752 log->logger_data = NULL; | |
753 } | |
754 } | |
7443 | 755 |
8898 | 756 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 757 { |
10822 | 758 return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger); |
4184 | 759 } |
760 | |
7431 | 761 static GaimLogLogger xml_logger = { |
762 N_("XML"), "xml", | |
763 NULL, | |
764 xml_logger_write, | |
765 xml_logger_finalize, | |
766 xml_logger_list, | |
8096 | 767 NULL, |
11025 | 768 NULL, |
7431 | 769 NULL |
770 }; | |
771 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
772 |
7431 | 773 /**************************** |
7457 | 774 ** HTML LOGGER ************* |
775 ****************************/ | |
776 | |
777 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
778 const char *from, time_t time, const char *message) | |
779 { | |
9763 | 780 char *msg_fixed; |
7457 | 781 char date[64]; |
9613 | 782 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 783 GaimLogCommonLoggerData *data = log->logger_data; |
9613 | 784 |
7618 | 785 if(!data) { |
9763 | 786 const char *prpl = |
787 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
10822 | 788 gaim_log_common_writer(log, time, ".html"); |
7457 | 789 |
9763 | 790 data = log->logger_data; |
7457 | 791 |
9763 | 792 /* if we can't write to the file, give up before we hurt ourselves */ |
793 if(!data->file) | |
794 return; | |
7616 | 795 |
7457 | 796 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 797 fprintf(data->file, "<html><head><title>"); |
798 fprintf(data->file, "Conversation with %s at %s on %s (%s)", | |
7457 | 799 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 800 fprintf(data->file, "</title></head><body>"); |
801 fprintf(data->file, | |
7457 | 802 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
803 log->name, date, gaim_account_get_username(log->account), prpl); | |
9763 | 804 |
7457 | 805 } |
7623 | 806 |
9892 | 807 /* if we can't write to the file, give up before we hurt ourselves */ |
808 if(!data->file) | |
809 return; | |
810 | |
7882 | 811 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
812 | |
8577 | 813 if(log->type == GAIM_LOG_SYSTEM){ |
9592 | 814 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8577 | 815 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
816 } else { | |
817 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
818 if (type & GAIM_MESSAGE_SYSTEM) | |
819 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
820 else if (type & GAIM_MESSAGE_WHISPER) | |
821 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
822 date, from, msg_fixed); | |
823 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
824 if (type & GAIM_MESSAGE_SEND) | |
825 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
826 else if (type & GAIM_MESSAGE_RECV) | |
827 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
828 } else if (type & GAIM_MESSAGE_RECV) { | |
829 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
830 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 831 date, from, msg_fixed); |
8577 | 832 else |
10645 | 833 fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
834 date, from, msg_fixed); | |
8577 | 835 } else if (type & GAIM_MESSAGE_SEND) { |
836 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
837 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 838 date, from, msg_fixed); |
8577 | 839 else |
10645 | 840 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
841 date, from, msg_fixed); | |
8577 | 842 } |
7564 | 843 } |
8573 | 844 |
7882 | 845 g_free(msg_fixed); |
7616 | 846 fflush(data->file); |
7457 | 847 } |
848 | |
849 static void html_logger_finalize(GaimLog *log) | |
850 { | |
10822 | 851 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 852 if (data) { |
853 if(data->file) { | |
854 fprintf(data->file, "</body></html>"); | |
855 fclose(data->file); | |
856 } | |
857 g_free(data->path); | |
7752 | 858 g_free(data); |
7463 | 859 } |
7457 | 860 } |
861 | |
8898 | 862 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7457 | 863 { |
10822 | 864 return gaim_log_common_lister(type, sn, account, ".html", &html_logger); |
7457 | 865 } |
866 | |
8573 | 867 static GList *html_logger_list_syslog(GaimAccount *account) |
868 { | |
10822 | 869 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); |
8573 | 870 } |
871 | |
7457 | 872 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
873 { | |
874 char *read, *minus_header; | |
10822 | 875 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 876 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 877 if (!data || !data->path) |
878 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
879 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 880 minus_header = strchr(read, '\n'); |
881 if (!minus_header) | |
882 minus_header = g_strdup(read); | |
883 else | |
884 minus_header = g_strdup(minus_header + 1); | |
885 g_free(read); | |
886 return minus_header; | |
887 } | |
8578 | 888 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7457 | 889 } |
890 | |
891 static GaimLogLogger html_logger = { | |
892 N_("HTML"), "html", | |
9819 | 893 NULL, |
7457 | 894 html_logger_write, |
895 html_logger_finalize, | |
896 html_logger_list, | |
7556 | 897 html_logger_read, |
10822 | 898 gaim_log_common_sizer, |
8573 | 899 NULL, |
11025 | 900 html_logger_list_syslog, |
901 NULL | |
7457 | 902 }; |
903 | |
904 | |
905 | |
906 | |
907 /**************************** | |
7431 | 908 ** PLAIN TEXT LOGGER ******* |
909 ****************************/ | |
4184 | 910 |
7436 | 911 static void txt_logger_write(GaimLog *log, |
912 GaimMessageFlags type, | |
7431 | 913 const char *from, time_t time, const char *message) |
914 { | |
915 char date[64]; | |
9763 | 916 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 917 GaimLogCommonLoggerData *data = log->logger_data; |
9763 | 918 char *stripped = NULL; |
919 | |
920 if(!data) { | |
7431 | 921 /* This log is new. We could use the loggers 'new' function, but |
922 * creating a new file there would result in empty files in the case | |
923 * that you open a convo with someone, but don't say anything. | |
924 */ | |
9763 | 925 const char *prpl = |
926 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
10822 | 927 gaim_log_common_writer(log, time, ".txt"); |
8898 | 928 |
9763 | 929 data = log->logger_data; |
7436 | 930 |
9763 | 931 /* if we can't write to the file, give up before we hurt ourselves */ |
932 if(!data->file) | |
933 return; | |
7616 | 934 |
7453 | 935 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 936 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 937 log->name, date, gaim_account_get_username(log->account), prpl); |
938 } | |
7436 | 939 |
7623 | 940 /* if we can't write to the file, give up before we hurt ourselves */ |
941 if(!data->file) | |
942 return; | |
943 | |
8573 | 944 stripped = gaim_markup_strip_html(message); |
945 | |
946 if(log->type == GAIM_LOG_SYSTEM){ | |
9592 | 947 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8573 | 948 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
949 } else { | |
950 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
951 if (type & GAIM_MESSAGE_SEND || | |
952 type & GAIM_MESSAGE_RECV) { | |
953 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
954 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
955 from, stripped); | |
956 } else { | |
957 if(gaim_message_meify(stripped, -1)) | |
958 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
959 stripped); | |
960 else | |
961 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
962 stripped); | |
963 } | |
964 } else if (type & GAIM_MESSAGE_SYSTEM) | |
965 fprintf(data->file, "(%s) %s\n", date, stripped); | |
966 else if (type & GAIM_MESSAGE_NO_LOG) { | |
967 /* This shouldn't happen */ | |
968 g_free(stripped); | |
969 return; | |
970 } else if (type & GAIM_MESSAGE_WHISPER) | |
971 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
972 else | |
973 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
974 from ? ":" : "", stripped); | |
975 } | |
976 | |
977 fflush(data->file); | |
978 g_free(stripped); | |
7431 | 979 } |
980 | |
981 static void txt_logger_finalize(GaimLog *log) | |
982 { | |
10822 | 983 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 984 if (data) { |
985 if(data->file) | |
986 fclose(data->file); | |
987 if(data->path) | |
988 g_free(data->path); | |
7752 | 989 g_free(data); |
7616 | 990 } |
7431 | 991 } |
992 | |
8898 | 993 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 994 { |
10822 | 995 return gaim_log_common_lister(type, sn, account, ".txt", &txt_logger); |
7431 | 996 } |
997 | |
8573 | 998 static GList *txt_logger_list_syslog(GaimAccount *account) |
999 { | |
10822 | 1000 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); |
8573 | 1001 } |
1002 | |
7431 | 1003 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
1004 { | |
8517 | 1005 char *read, *minus_header, *minus_header2; |
10822 | 1006 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 1007 *flags = 0; |
7616 | 1008 if (!data || !data->path) |
1009 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
1010 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 1011 minus_header = strchr(read, '\n'); |
1012 if (!minus_header) | |
1013 minus_header = g_strdup(read); | |
7436 | 1014 else |
7431 | 1015 minus_header = g_strdup(minus_header + 1); |
1016 g_free(read); | |
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10645
diff
changeset
|
1017 minus_header2 = g_markup_escape_text(minus_header, -1); |
8517 | 1018 g_free(minus_header); |
1019 return minus_header2; | |
7431 | 1020 } |
8578 | 1021 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7436 | 1022 } |
7431 | 1023 |
1024 static GaimLogLogger txt_logger = { | |
1025 N_("Plain text"), "txt", | |
9819 | 1026 NULL, |
7431 | 1027 txt_logger_write, |
1028 txt_logger_finalize, | |
1029 txt_logger_list, | |
7556 | 1030 txt_logger_read, |
10822 | 1031 gaim_log_common_sizer, |
8573 | 1032 NULL, |
11025 | 1033 txt_logger_list_syslog, |
1034 NULL | |
7431 | 1035 }; |
1036 | |
1037 /**************** | |
1038 * OLD LOGGER *** | |
1039 ****************/ | |
1040 | |
1041 /* The old logger doesn't write logs, only reads them. This is to include | |
1042 * old logs in the log viewer transparently. | |
1043 */ | |
1044 | |
1045 struct old_logger_data { | |
7764 | 1046 GaimStringref *pathref; |
7431 | 1047 int offset; |
1048 int length; | |
1049 }; | |
1050 | |
8898 | 1051 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 1052 { |
1053 FILE *file; | |
1054 char buf[BUF_LONG]; | |
1055 struct tm tm; | |
7761 | 1056 char month[4]; |
7431 | 1057 struct old_logger_data *data = NULL; |
1058 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 1059 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
1060 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 1061 char *newlog; |
7761 | 1062 int logfound = 0; |
1063 int lastoff = 0; | |
1064 int newlen; | |
7791 | 1065 time_t lasttime = 0; |
7431 | 1066 |
1067 GaimLog *log = NULL; | |
1068 GList *list = NULL; | |
1069 | |
7473 | 1070 g_free(logfile); |
7764 | 1071 g_free(pathstr); |
7473 | 1072 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1073 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { |
7764 | 1074 gaim_stringref_unref(pathref); |
7431 | 1075 return NULL; |
7447 | 1076 } |
7436 | 1077 |
7431 | 1078 while (fgets(buf, BUF_LONG, file)) { |
1079 if ((newlog = strstr(buf, "---- New C"))) { | |
1080 int length; | |
1081 int offset; | |
1082 char convostart[32]; | |
1083 char *temp = strchr(buf, '@'); | |
7436 | 1084 |
7431 | 1085 if (temp == NULL || strlen(temp) < 2) |
1086 continue; | |
7436 | 1087 |
7431 | 1088 temp++; |
1089 length = strcspn(temp, "-"); | |
1090 if (length > 31) length = 31; | |
7436 | 1091 |
7431 | 1092 offset = ftell(file); |
7436 | 1093 |
7761 | 1094 if (logfound) { |
1095 newlen = offset - lastoff - length; | |
7436 | 1096 if(strstr(buf, "----</H3><BR>")) { |
7761 | 1097 newlen -= |
1098 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
1099 sizeof("----</H3><BR>") - 2; | |
7436 | 1100 } else { |
7761 | 1101 newlen -= |
1102 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 1103 } |
1104 | |
7461 | 1105 if(strchr(buf, '\r')) |
7770 | 1106 newlen--; |
7461 | 1107 |
7761 | 1108 if (newlen != 0) { |
1109 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
1110 log->logger = &old_logger; | |
1111 log->time = lasttime; | |
1112 data = g_new0(struct old_logger_data, 1); | |
1113 data->offset = lastoff; | |
1114 data->length = newlen; | |
7764 | 1115 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1116 log->logger_data = data; |
7431 | 1117 list = g_list_append(list, log); |
7761 | 1118 } |
7431 | 1119 } |
1120 | |
7761 | 1121 logfound = 1; |
1122 lastoff = offset; | |
7436 | 1123 |
7431 | 1124 g_snprintf(convostart, length, "%s", temp); |
7676 | 1125 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
1126 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
1127 /* Ugly hack, in case current locale is not English */ | |
1128 if (strcmp(month, "Jan") == 0) { | |
1129 tm.tm_mon= 0; | |
1130 } else if (strcmp(month, "Feb") == 0) { | |
1131 tm.tm_mon = 1; | |
1132 } else if (strcmp(month, "Mar") == 0) { | |
1133 tm.tm_mon = 2; | |
1134 } else if (strcmp(month, "Apr") == 0) { | |
1135 tm.tm_mon = 3; | |
1136 } else if (strcmp(month, "May") == 0) { | |
1137 tm.tm_mon = 4; | |
1138 } else if (strcmp(month, "Jun") == 0) { | |
1139 tm.tm_mon = 5; | |
1140 } else if (strcmp(month, "Jul") == 0) { | |
1141 tm.tm_mon = 6; | |
1142 } else if (strcmp(month, "Aug") == 0) { | |
1143 tm.tm_mon = 7; | |
1144 } else if (strcmp(month, "Sep") == 0) { | |
1145 tm.tm_mon = 8; | |
1146 } else if (strcmp(month, "Oct") == 0) { | |
1147 tm.tm_mon = 9; | |
1148 } else if (strcmp(month, "Nov") == 0) { | |
1149 tm.tm_mon = 10; | |
1150 } else if (strcmp(month, "Dec") == 0) { | |
1151 tm.tm_mon = 11; | |
1152 } | |
1153 tm.tm_year -= 1900; | |
7761 | 1154 lasttime = mktime(&tm); |
4184 | 1155 } |
1156 } | |
7613 | 1157 |
7761 | 1158 if (logfound) { |
1159 if ((newlen = ftell(file) - lastoff) != 0) { | |
1160 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
1161 log->logger = &old_logger; | |
1162 log->time = lasttime; | |
1163 data = g_new0(struct old_logger_data, 1); | |
1164 data->offset = lastoff; | |
1165 data->length = newlen; | |
7764 | 1166 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1167 log->logger_data = data; |
7613 | 1168 list = g_list_append(list, log); |
7761 | 1169 } |
7613 | 1170 } |
1171 | |
7764 | 1172 gaim_stringref_unref(pathref); |
7431 | 1173 fclose(file); |
1174 return list; | |
4184 | 1175 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1176 |
8898 | 1177 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
8096 | 1178 { |
1179 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
1180 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
1181 int size; | |
1182 struct stat st; | |
1183 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1184 if (g_stat(pathstr, &st)) |
8096 | 1185 size = 0; |
1186 else | |
1187 size = st.st_size; | |
1188 | |
1189 g_free(logfile); | |
1190 g_free(pathstr); | |
1191 | |
1192 return size; | |
1193 } | |
1194 | |
7616 | 1195 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1196 { |
7431 | 1197 struct old_logger_data *data = log->logger_data; |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1198 FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); |
10906 | 1199 char *tmp, *read = g_malloc(data->length + 1); |
7431 | 1200 fseek(file, data->offset, SEEK_SET); |
1201 fread(read, data->length, 1, file); | |
8370 | 1202 fclose(file); |
7431 | 1203 read[data->length] = '\0'; |
7436 | 1204 *flags = 0; |
1205 if(strstr(read, "<BR>")) | |
1206 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
10906 | 1207 else { |
1208 tmp = g_markup_escape_text(read, -1); | |
1209 g_free(read); | |
1210 read = tmp; | |
1211 } | |
7431 | 1212 return read; |
1213 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1214 |
7616 | 1215 static int old_logger_size (GaimLog *log) |
7556 | 1216 { |
1217 struct old_logger_data *data = log->logger_data; | |
7616 | 1218 return data ? data->length : 0; |
1219 } | |
1220 | |
11025 | 1221 static GList *old_logger_get_log_sets() |
1222 { | |
1223 char *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
1224 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
1225 gchar *name; | |
1226 GList *sets = NULL; | |
1227 GaimBlistNode *gnode, *cnode, *bnode; | |
1228 | |
1229 g_free(log_path); | |
1230 if (log_dir == NULL) | |
1231 return NULL; | |
1232 | |
1233 /* Don't worry about the cast, name will be filled with a dynamically allocated data shortly. */ | |
1234 while ((name = (gchar *)g_dir_read_name(log_dir)) != NULL) { | |
1235 size_t len; | |
1236 gchar *ext; | |
1237 GaimLogSet *set; | |
1238 gboolean found = FALSE; | |
1239 | |
1240 /* Unescape the filename. */ | |
1241 name = g_strdup(gaim_unescape_filename(name)); | |
1242 | |
1243 /* Get the (possibly new) length of name. */ | |
1244 len = strlen(name); | |
1245 | |
1246 if (len < 5) { | |
1247 g_free(name); | |
1248 continue; | |
1249 } | |
1250 | |
1251 /* Make sure we're dealing with a log file. */ | |
1252 ext = &name[len - 4]; | |
1253 if (strcmp(ext, ".log")) { | |
1254 g_free(name); | |
1255 continue; | |
1256 } | |
1257 | |
1258 set = g_new0(GaimLogSet, 1); | |
1259 | |
1260 /* Chat for .chat at the end of the name to determine the type. */ | |
1261 *ext = '\0'; | |
1262 set->type = GAIM_LOG_IM; | |
1263 if (len > 9) { | |
1264 char *tmp = &name[len - 9]; | |
1265 if (!strcmp(tmp, ".chat")) { | |
1266 set->type = GAIM_LOG_CHAT; | |
1267 *tmp = '\0'; | |
1268 } | |
1269 } | |
1270 | |
1271 set->name = name; | |
1272 | |
1273 /* Search the buddy list to find the account and to determine if this is a buddy. */ | |
1274 for (gnode = gaim_get_blist()->root; !found && gnode != NULL; gnode = gnode->next) | |
1275 { | |
1276 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
1277 continue; | |
1278 | |
1279 for (cnode = gnode->child; !found && cnode != NULL; cnode = cnode->next) | |
1280 { | |
1281 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
1282 continue; | |
1283 | |
1284 for (bnode = cnode->child; !found && bnode != NULL; bnode = bnode->next) | |
1285 { | |
1286 GaimBuddy *buddy = (GaimBuddy *)bnode; | |
1287 | |
1288 if (!strcmp(buddy->name, name)) { | |
1289 set->account = buddy->account; | |
1290 set->buddy = TRUE; | |
1291 found = TRUE; | |
1292 } | |
1293 } | |
1294 } | |
1295 } | |
1296 | |
1297 sets = g_list_append(sets, set); | |
1298 } | |
1299 g_dir_close(log_dir); | |
1300 | |
1301 return sets; | |
1302 } | |
1303 | |
7616 | 1304 static void old_logger_finalize(GaimLog *log) |
1305 { | |
1306 struct old_logger_data *data = log->logger_data; | |
7764 | 1307 gaim_stringref_unref(data->pathref); |
7616 | 1308 g_free(data); |
7556 | 1309 } |
1310 | |
7431 | 1311 static GaimLogLogger old_logger = { |
1312 "old logger", "old", | |
7616 | 1313 NULL, NULL, |
1314 old_logger_finalize, | |
7431 | 1315 old_logger_list, |
7616 | 1316 old_logger_read, |
8096 | 1317 old_logger_size, |
8573 | 1318 old_logger_total_size, |
11025 | 1319 NULL, |
1320 old_logger_get_log_sets | |
7431 | 1321 }; |