Mercurial > pidgin.yaz
annotate src/log.c @ 8096:81079e3eda47
[gaim-migrate @ 8795]
i tend to agree with javabsp that even though this doesn't affect the
O(n^2)-ness of the sort, that its still better than before since reading
the whole file over is huge in and of itself
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Tue, 13 Jan 2004 17:16:08 +0000 |
parents | 3c4313120c63 |
children | 869d5a5495b8 |
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 |
7431 | 40 /************************************************************************** |
41 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
42 **************************************************************************/ | |
4184 | 43 |
7431 | 44 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, time_t time) |
45 { | |
46 GaimLog *log = g_new0(GaimLog, 1); | |
47 log->name = g_strdup(name); | |
48 log->account = account; | |
49 log->time = time; | |
8096 | 50 log->logger_data = NULL; |
7431 | 51 log->logger = gaim_log_logger_get(); |
7440 | 52 if (log->logger && log->logger->create) |
53 log->logger->create(log); | |
7431 | 54 return log; |
4184 | 55 } |
56 | |
7431 | 57 void gaim_log_free(GaimLog *log) |
4184 | 58 { |
7431 | 59 g_return_if_fail(log); |
60 if (log->logger && log->logger->finalize) | |
61 log->logger->finalize(log); | |
62 g_free(log->name); | |
63 g_free(log); | |
64 } | |
7436 | 65 |
4184 | 66 |
7436 | 67 void gaim_log_write(GaimLog *log, GaimMessageFlags type, |
7431 | 68 const char *from, time_t time, const char *message) |
69 { | |
70 g_return_if_fail(log); | |
71 g_return_if_fail(log->logger); | |
7442 | 72 g_return_if_fail(log->logger->write); |
7431 | 73 |
7555 | 74 if ((log->type == GAIM_LOG_IM && gaim_prefs_get_bool("/core/logging/log_ims")) || |
75 (log->type == GAIM_LOG_CHAT && gaim_prefs_get_bool("/core/logging/log_chats"))) | |
7553 | 76 (log->logger->write)(log, type, from, time, message); |
4184 | 77 } |
78 | |
7431 | 79 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 80 { |
7542 | 81 GaimLogReadFlags mflags; |
7431 | 82 g_return_val_if_fail(log && log->logger, NULL); |
7462 | 83 if (log->logger->read) { |
7535 | 84 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
85 gaim_str_strip_cr(ret); |
7462 | 86 return ret; |
87 } | |
7470 | 88 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
4184 | 89 } |
7616 | 90 |
7556 | 91 int gaim_log_get_size(GaimLog *log) |
92 { | |
93 g_return_val_if_fail(log && log->logger, 0); | |
8096 | 94 |
7556 | 95 if (log->logger->size) |
96 return log->logger->size(log); | |
97 return 0; | |
98 } | |
99 | |
100 int gaim_log_get_total_size(const char *name, GaimAccount *account) | |
101 { | |
102 int size = 0; | |
8096 | 103 GSList *n; |
7616 | 104 |
8096 | 105 for (n = loggers; n; n = n->next) { |
106 GaimLogLogger *logger = n->data; | |
107 | |
108 if(logger->total_size){ | |
109 size += (logger->total_size)(name, account); | |
110 } else if(logger->list) { | |
111 GList *logs = (logger->list)(name, account); | |
112 int this_size = 0; | |
7616 | 113 |
8096 | 114 while (logs) { |
115 GList *logs2 = logs->next; | |
116 GaimLog *log = (GaimLog*)(logs->data); | |
117 this_size += gaim_log_get_size(log); | |
118 gaim_log_free(log); | |
119 g_list_free_1(logs); | |
120 logs = logs2; | |
121 } | |
122 | |
123 size += this_size; | |
124 } | |
7556 | 125 } |
7616 | 126 |
7556 | 127 return size; |
128 } | |
4184 | 129 |
7431 | 130 /**************************************************************************** |
131 * LOGGER FUNCTIONS ********************************************************* | |
132 ****************************************************************************/ | |
4184 | 133 |
7431 | 134 static GaimLogLogger *current_logger = NULL; |
7436 | 135 |
7431 | 136 static void logger_pref_cb(const char *name, GaimPrefType type, |
137 gpointer value, gpointer data) | |
138 { | |
139 GaimLogLogger *logger; | |
140 GSList *l = loggers; | |
141 while (l) { | |
142 logger = l->data; | |
143 if (!strcmp(logger->id, value)) { | |
144 gaim_log_logger_set(logger); | |
145 return; | |
4184 | 146 } |
7431 | 147 l = l->next; |
148 } | |
149 gaim_log_logger_set(&txt_logger); | |
150 } | |
4184 | 151 |
152 | |
7440 | 153 GaimLogLogger *gaim_log_logger_new(void(*create)(GaimLog *), |
7436 | 154 void(*write)(GaimLog *, GaimMessageFlags, const char *, |
7431 | 155 time_t, const char *), |
156 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), | |
7556 | 157 char*(*read)(GaimLog*, GaimLogReadFlags*), |
158 int(*size)(GaimLog*)) | |
7431 | 159 { |
160 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
7440 | 161 logger->create = create; |
7431 | 162 logger->write = write; |
163 logger->finalize = finalize; | |
164 logger->list = list; | |
165 logger->read = read; | |
7556 | 166 logger->size = size; |
7431 | 167 return logger; |
4184 | 168 } |
169 | |
7431 | 170 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 171 { |
7431 | 172 g_free(logger); |
173 } | |
4184 | 174 |
7431 | 175 void gaim_log_logger_add (GaimLogLogger *logger) |
176 { | |
177 g_return_if_fail(logger); | |
178 if (g_slist_find(loggers, logger)) | |
179 return; | |
180 loggers = g_slist_append(loggers, logger); | |
181 } | |
182 | |
183 void gaim_log_logger_remove (GaimLogLogger *logger) | |
184 { | |
185 g_return_if_fail(logger); | |
186 g_slist_remove(loggers, logger); | |
4184 | 187 } |
188 | |
7431 | 189 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 190 { |
7431 | 191 g_return_if_fail(logger); |
192 current_logger = logger; | |
7436 | 193 } |
4184 | 194 |
7431 | 195 GaimLogLogger *gaim_log_logger_get() |
196 { | |
197 return current_logger; | |
198 } | |
4184 | 199 |
7431 | 200 GList *gaim_log_logger_get_options(void) |
201 { | |
202 GSList *n; | |
203 GList *list = NULL; | |
204 GaimLogLogger *data; | |
4184 | 205 |
7431 | 206 for (n = loggers; n; n = n->next) { |
207 data = n->data; | |
208 if (!data->write) | |
209 continue; | |
7494 | 210 list = g_list_append(list, _(data->name)); |
7431 | 211 list = g_list_append(list, data->id); |
4184 | 212 } |
213 | |
7431 | 214 return list; |
215 } | |
216 | |
7436 | 217 static gint log_compare(gconstpointer y, gconstpointer z) |
7431 | 218 { |
7436 | 219 const GaimLog *a = y; |
220 const GaimLog *b = z; | |
221 | |
7431 | 222 return b->time - a->time; |
223 } | |
224 | |
225 GList *gaim_log_get_logs(const char *name, GaimAccount *account) | |
226 { | |
227 GList *logs = NULL; | |
228 GSList *n; | |
229 for (n = loggers; n; n = n->next) { | |
230 GaimLogLogger *logger = n->data; | |
231 if (!logger->list) | |
232 continue; | |
233 logs = g_list_concat(logs, logger->list(name, account)); | |
234 } | |
7436 | 235 |
7431 | 236 return g_list_sort(logs, log_compare); |
237 } | |
238 | |
239 void gaim_log_init(void) | |
7436 | 240 { |
7431 | 241 gaim_prefs_add_none("/core/logging"); |
7555 | 242 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
243 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
7431 | 244 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 245 gaim_log_logger_add(&html_logger); |
7431 | 246 gaim_log_logger_add(&txt_logger); |
247 gaim_log_logger_add(&old_logger); | |
248 gaim_prefs_connect_callback("/core/logging/format", | |
249 logger_pref_cb, NULL); | |
250 gaim_prefs_trigger_callback("/core/logging/format"); | |
251 } | |
252 | |
253 /**************************************************************************** | |
254 * LOGGERS ****************************************************************** | |
255 ****************************************************************************/ | |
256 | |
7616 | 257 struct generic_logger_data { |
258 char *path; | |
259 FILE *file; | |
260 }; | |
261 | |
7431 | 262 static GList *log_lister_common(const char *screenname, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
263 { | |
264 GDir *dir; | |
265 GList *list = NULL; | |
7628 | 266 const char *filename; |
7431 | 267 char *me = g_strdup(gaim_normalize(account, gaim_account_get_username(account))); |
4184 | 268 |
7956 | 269 /* does this seem like a bad way to get this component of the path to anyone else? --Nathan */ |
7431 | 270 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
7956 | 271 (gaim_find_prpl(gaim_account_get_protocol_id(account)))->list_icon(account, NULL); |
7431 | 272 char *path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, screenname), NULL); |
273 | |
7447 | 274 g_free(me); |
275 | |
7431 | 276 if (!(dir = g_dir_open(path, 0, NULL))) { |
277 g_free(path); | |
278 return NULL; | |
279 } | |
280 while ((filename = g_dir_read_name(dir))) { | |
7628 | 281 if (gaim_str_has_suffix(filename, ext)) { |
7431 | 282 const char *l = filename; |
283 struct tm time; | |
284 GaimLog *log; | |
7616 | 285 struct generic_logger_data *data; |
7431 | 286 char d[5]; |
7436 | 287 |
7431 | 288 strncpy(d, l, 4); |
289 d[4] = '\0'; | |
290 time.tm_year = atoi(d) - 1900; | |
291 l = l + 5; | |
292 | |
293 strncpy(d, l, 2); | |
294 d[2] = '\0'; | |
295 time.tm_mon = atoi(d) - 1; | |
296 l = l + 3; | |
297 | |
298 strncpy(d, l, 2); | |
299 time.tm_mday = atoi(d); | |
300 l = l + 3; | |
301 | |
302 strncpy(d, l, 2); | |
303 time.tm_hour = atoi(d); | |
304 l = l + 2; | |
7436 | 305 |
7431 | 306 strncpy(d, l, 2); |
307 time.tm_min = atoi(d); | |
308 l = l + 2; | |
309 | |
310 strncpy(d, l, 2); | |
311 time.tm_sec = atoi(d); | |
312 l = l + 2; | |
313 log = gaim_log_new(GAIM_LOG_IM, screenname, account, mktime(&time)); | |
314 log->logger = logger; | |
7616 | 315 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
316 data->path = g_build_filename(path, filename, NULL); | |
7431 | 317 list = g_list_append(list, log); |
4184 | 318 } |
319 } | |
7431 | 320 g_dir_close(dir); |
7447 | 321 g_free(path); |
7431 | 322 return list; |
323 } | |
4184 | 324 |
7556 | 325 /* Only to be used with logs listed from log_lister_common */ |
7616 | 326 int log_sizer_common(GaimLog *log) |
7556 | 327 { |
328 struct stat st; | |
7616 | 329 struct generic_logger_data *data = log->logger_data; |
7556 | 330 |
7616 | 331 if (!data->path || stat(data->path, &st)) |
7556 | 332 st.st_size = 0; |
333 | |
334 return st.st_size; | |
335 } | |
336 | |
7431 | 337 #if 0 /* Maybe some other time. */ |
7443 | 338 /**************** |
7431 | 339 ** XML LOGGER ** |
340 ****************/ | |
341 | |
342 static const char *str_from_msg_type (GaimMessageFlags type) | |
343 { | |
7443 | 344 |
7431 | 345 return ""; |
7443 | 346 |
7431 | 347 } |
348 | |
7443 | 349 static void xml_logger_write(GaimLog *log, |
350 GaimMessageFlags type, | |
7431 | 351 const char *from, time_t time, const char *message) |
352 { | |
353 char date[64]; | |
354 char *xhtml = NULL; | |
355 if (!log->logger_data) { | |
356 /* This log is new. We could use the loggers 'new' function, but | |
357 * creating a new file there would result in empty files in the case | |
358 * that you open a convo with someone, but don't say anything. | |
359 */ | |
360 char *ud = gaim_user_dir(); | |
361 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
362 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
363 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
364 char *dir; | |
365 FILE *file; | |
366 | |
7453 | 367 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 368 |
369 dir = g_build_filename(ud, "logs", | |
7431 | 370 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
7612 | 371 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7447 | 372 g_free(guy); |
7443 | 373 |
7431 | 374 char *filename = g_build_filename(dir, date, NULL); |
375 g_free(dir); | |
7443 | 376 |
7431 | 377 log->logger_data = fopen(filename, "a"); |
378 if (!log->logger_data) { | |
379 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 380 g_free(filename); |
7431 | 381 return; |
382 } | |
7564 | 383 g_free(filename); |
7431 | 384 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
385 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 386 |
7453 | 387 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 388 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
389 date, log->name, prpl); | |
390 } | |
7443 | 391 |
7453 | 392 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 393 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
394 if (from) | |
7443 | 395 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
396 str_from_msg_type(type), | |
7431 | 397 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
398 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
399 from, date, xhtml); | |
400 else | |
7443 | 401 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
402 str_from_msg_type(type), | |
7431 | 403 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
404 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 405 date, xhtml): |
7431 | 406 fflush(log->logger_data); |
407 g_free(xhtml); | |
7443 | 408 } |
409 | |
7431 | 410 static void xml_logger_finalize(GaimLog *log) |
411 { | |
412 if (log->logger_data) { | |
413 fprintf(log->logger_data, "</conversation>\n"); | |
414 fclose(log->logger_data); | |
415 log->logger_data = NULL; | |
416 } | |
417 } | |
7443 | 418 |
7431 | 419 static GList *xml_logger_list(const char *sn, GaimAccount *account) |
420 { | |
421 return log_lister_common(sn, account, ".xml", &xml_logger); | |
4184 | 422 } |
423 | |
7431 | 424 static GaimLogLogger xml_logger = { |
425 N_("XML"), "xml", | |
426 NULL, | |
427 xml_logger_write, | |
428 xml_logger_finalize, | |
429 xml_logger_list, | |
8096 | 430 NULL, |
7431 | 431 NULL |
432 }; | |
433 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
434 |
7431 | 435 /**************************** |
7457 | 436 ** HTML LOGGER ************* |
437 ****************************/ | |
438 | |
439 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
440 const char *from, time_t time, const char *message) | |
441 { | |
7489 | 442 GaimConnection *gc = gaim_account_get_connection(log->account); |
7457 | 443 char date[64]; |
7882 | 444 char *msg_fixed; |
7616 | 445 struct generic_logger_data *data = log->logger_data; |
7618 | 446 if(!data) { |
7457 | 447 /* This log is new */ |
448 char *ud = gaim_user_dir(); | |
449 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
7553 | 450 char *chat; |
7457 | 451 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
7956 | 452 (gaim_find_prpl(gaim_account_get_protocol_id(log->account)))->list_icon(log->account, NULL); |
7457 | 453 char *dir; |
454 char *filename; | |
455 | |
7553 | 456 if (log->type == GAIM_LOG_CHAT) { |
457 chat = g_strdup_printf("%s.chat", guy); | |
458 g_free(guy); | |
459 guy = chat; | |
460 } | |
461 | |
7457 | 462 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time)); |
463 | |
464 dir = g_build_filename(ud, "logs", | |
465 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
7612 | 466 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7457 | 467 g_free(guy); |
468 | |
469 filename = g_build_filename(dir, date, NULL); | |
470 g_free(dir); | |
471 | |
7616 | 472 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
473 | |
474 data->file = fopen(filename, "a"); | |
475 if (!data->file) { | |
7623 | 476 gaim_debug(GAIM_DEBUG_ERROR, "log", |
477 "Could not create log file %s\n", filename); | |
7564 | 478 g_free(filename); |
7457 | 479 return; |
480 } | |
481 g_free(filename); | |
482 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
7616 | 483 fprintf(data->file, "<html><head><title>"); |
484 fprintf(data->file, "Conversation with %s at %s on %s (%s)", | |
7457 | 485 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 486 fprintf(data->file, "</title></head><body>"); |
487 fprintf(data->file, | |
7457 | 488 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
489 log->name, date, gaim_account_get_username(log->account), prpl); | |
490 } | |
7623 | 491 |
492 /* if we can't write to the file, give up before we hurt ourselves */ | |
493 if(!data->file) | |
494 return; | |
495 | |
7882 | 496 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
497 | |
7457 | 498 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7489 | 499 if (type & GAIM_MESSAGE_SYSTEM) |
7882 | 500 fprintf(data->file, "(%s)<b> %s</b><br/>\n", date, msg_fixed); |
7489 | 501 else if (type & GAIM_MESSAGE_WHISPER) |
7616 | 502 fprintf(data->file, "<font color=\"#6C2585\">(%s)<b> %s:</b></font> %s<br/>\n", |
7882 | 503 date, from, msg_fixed); |
7489 | 504 else if (type & GAIM_MESSAGE_AUTO_RESP) { |
505 if (type & GAIM_MESSAGE_SEND) | |
8062 | 506 fprintf(data->file, _("<font color=\"#16569E\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); |
7489 | 507 else if (type & GAIM_MESSAGE_RECV) |
8062 | 508 fprintf(data->file, _("<font color=\"#A82F2F\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); |
7564 | 509 } else if (type & GAIM_MESSAGE_RECV) { |
7882 | 510 if(gaim_message_meify(msg_fixed, -1)) |
7616 | 511 fprintf(data->file, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7882 | 512 date, from, gc->prpl->info->name, msg_fixed); |
7564 | 513 else |
7616 | 514 fprintf(data->file, "<font color=\"#A82F2F\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7882 | 515 date, from, gc->prpl->info->name, msg_fixed); |
7564 | 516 } else if (type & GAIM_MESSAGE_SEND) { |
7882 | 517 if(gaim_message_meify(msg_fixed, -1)) |
7616 | 518 fprintf(data->file, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7882 | 519 date, from, gc->prpl->info->name, msg_fixed); |
7564 | 520 else |
7616 | 521 fprintf(data->file, "<font color=\"#16569E\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7882 | 522 date, from, gc->prpl->info->name, msg_fixed); |
7564 | 523 } |
7882 | 524 g_free(msg_fixed); |
7616 | 525 fflush(data->file); |
7457 | 526 } |
527 | |
528 static void html_logger_finalize(GaimLog *log) | |
529 { | |
7616 | 530 struct generic_logger_data *data = log->logger_data; |
531 if (data) { | |
532 if(data->file) { | |
533 fprintf(data->file, "</body></html>"); | |
534 fclose(data->file); | |
535 } | |
536 g_free(data->path); | |
7752 | 537 g_free(data); |
7463 | 538 } |
7457 | 539 } |
540 | |
541 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
542 { | |
543 return log_lister_common(sn, account, ".html", &html_logger); | |
544 } | |
545 | |
546 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
547 { | |
548 char *read, *minus_header; | |
7616 | 549 struct generic_logger_data *data = log->logger_data; |
7457 | 550 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 551 if (!data || !data->path) |
552 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
553 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 554 minus_header = strchr(read, '\n'); |
555 if (!minus_header) | |
556 minus_header = g_strdup(read); | |
557 else | |
558 minus_header = g_strdup(minus_header + 1); | |
559 g_free(read); | |
560 return minus_header; | |
561 } | |
7471 | 562 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7457 | 563 } |
564 | |
565 static GaimLogLogger html_logger = { | |
566 N_("HTML"), "html", | |
567 NULL, | |
568 html_logger_write, | |
569 html_logger_finalize, | |
570 html_logger_list, | |
7556 | 571 html_logger_read, |
8096 | 572 log_sizer_common, |
573 NULL | |
7457 | 574 }; |
575 | |
576 | |
577 | |
578 | |
579 /**************************** | |
7431 | 580 ** PLAIN TEXT LOGGER ******* |
581 ****************************/ | |
4184 | 582 |
7436 | 583 static void txt_logger_write(GaimLog *log, |
584 GaimMessageFlags type, | |
7431 | 585 const char *from, time_t time, const char *message) |
586 { | |
587 char date[64]; | |
588 char *stripped = NULL; | |
7616 | 589 struct generic_logger_data *data = log->logger_data; |
590 if (!data) { | |
7431 | 591 /* This log is new. We could use the loggers 'new' function, but |
592 * creating a new file there would result in empty files in the case | |
593 * that you open a convo with someone, but don't say anything. | |
594 */ | |
595 char *ud = gaim_user_dir(); | |
7473 | 596 char *filename; |
7431 | 597 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
7553 | 598 char *chat; |
7431 | 599 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
7956 | 600 (gaim_find_prpl(gaim_account_get_protocol_id(log->account)))->list_icon(log->account, NULL); |
7431 | 601 char *dir; |
7436 | 602 |
7553 | 603 if (log->type == GAIM_LOG_CHAT) { |
604 chat = g_strdup_printf("%s.chat", guy); | |
605 g_free(guy); | |
606 guy = chat; | |
607 } | |
7453 | 608 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
7436 | 609 |
610 dir = g_build_filename(ud, "logs", | |
7431 | 611 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
7612 | 612 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7447 | 613 g_free(guy); |
7436 | 614 |
7473 | 615 filename = g_build_filename(dir, date, NULL); |
7431 | 616 g_free(dir); |
7436 | 617 |
7616 | 618 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
619 | |
620 data->file = fopen(filename, "a"); | |
621 if (!data->file) { | |
7431 | 622 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); |
7564 | 623 g_free(filename); |
7431 | 624 return; |
4184 | 625 } |
7447 | 626 g_free(filename); |
7453 | 627 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 628 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 629 log->name, date, gaim_account_get_username(log->account), prpl); |
630 } | |
7436 | 631 |
7623 | 632 /* if we can't write to the file, give up before we hurt ourselves */ |
633 if(!data->file) | |
634 return; | |
635 | |
7453 | 636 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 637 stripped = gaim_markup_strip_html(message); |
7489 | 638 if (type & GAIM_MESSAGE_SEND || |
7541 | 639 type & GAIM_MESSAGE_RECV) { |
7564 | 640 if (type & GAIM_MESSAGE_AUTO_RESP) { |
7616 | 641 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, from, stripped); |
7564 | 642 } else { |
643 if(gaim_message_meify(stripped, -1)) | |
7616 | 644 fprintf(data->file, "(%s) ***%s %s\n", date, from, |
7564 | 645 stripped); |
646 else | |
7616 | 647 fprintf(data->file, "(%s) %s: %s\n", date, from, |
7564 | 648 stripped); |
649 } | |
650 } else if (type & GAIM_MESSAGE_SYSTEM) | |
7616 | 651 fprintf(data->file, "(%s) %s\n", date, stripped); |
7489 | 652 else if (type & GAIM_MESSAGE_NO_LOG) { |
653 /* This shouldn't happen */ | |
654 g_free(stripped); | |
655 return; | |
656 } else if (type & GAIM_MESSAGE_WHISPER) | |
7616 | 657 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); |
7489 | 658 else |
7616 | 659 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); |
7489 | 660 |
7616 | 661 fflush(data->file); |
7431 | 662 g_free(stripped); |
663 } | |
664 | |
665 static void txt_logger_finalize(GaimLog *log) | |
666 { | |
7616 | 667 struct generic_logger_data *data = log->logger_data; |
668 if (data) { | |
669 if(data->file) | |
670 fclose(data->file); | |
671 if(data->path) | |
672 g_free(data->path); | |
7752 | 673 g_free(data); |
7616 | 674 } |
7431 | 675 } |
676 | |
677 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
678 { | |
679 return log_lister_common(sn, account, ".txt", &txt_logger); | |
680 } | |
681 | |
682 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
683 { | |
684 char *read, *minus_header; | |
7616 | 685 struct generic_logger_data *data = log->logger_data; |
7457 | 686 *flags = 0; |
7616 | 687 if (!data || !data->path) |
688 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
689 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 690 minus_header = strchr(read, '\n'); |
691 if (!minus_header) | |
692 minus_header = g_strdup(read); | |
7436 | 693 else |
7431 | 694 minus_header = g_strdup(minus_header + 1); |
695 g_free(read); | |
696 return minus_header; | |
697 } | |
7471 | 698 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7436 | 699 } |
7431 | 700 |
701 static GaimLogLogger txt_logger = { | |
702 N_("Plain text"), "txt", | |
703 NULL, | |
704 txt_logger_write, | |
705 txt_logger_finalize, | |
706 txt_logger_list, | |
7556 | 707 txt_logger_read, |
8096 | 708 log_sizer_common, |
709 NULL | |
7431 | 710 }; |
711 | |
712 /**************** | |
713 * OLD LOGGER *** | |
714 ****************/ | |
715 | |
716 /* The old logger doesn't write logs, only reads them. This is to include | |
717 * old logs in the log viewer transparently. | |
718 */ | |
719 | |
720 struct old_logger_data { | |
7764 | 721 GaimStringref *pathref; |
7431 | 722 int offset; |
723 int length; | |
724 }; | |
725 | |
7436 | 726 static GList *old_logger_list(const char *sn, GaimAccount *account) |
7431 | 727 { |
728 FILE *file; | |
729 char buf[BUF_LONG]; | |
730 struct tm tm; | |
7761 | 731 char month[4]; |
7431 | 732 struct old_logger_data *data = NULL; |
733 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 734 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
735 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 736 char *newlog; |
7761 | 737 int logfound = 0; |
738 int lastoff = 0; | |
739 int newlen; | |
7791 | 740 time_t lasttime = 0; |
7431 | 741 |
742 GaimLog *log = NULL; | |
743 GList *list = NULL; | |
744 | |
7473 | 745 g_free(logfile); |
7764 | 746 g_free(pathstr); |
7473 | 747 |
7764 | 748 if (!(file = fopen(gaim_stringref_value(pathref), "rb"))) { |
749 gaim_stringref_unref(pathref); | |
7431 | 750 return NULL; |
7447 | 751 } |
7436 | 752 |
7431 | 753 while (fgets(buf, BUF_LONG, file)) { |
754 if ((newlog = strstr(buf, "---- New C"))) { | |
755 int length; | |
756 int offset; | |
757 char convostart[32]; | |
758 char *temp = strchr(buf, '@'); | |
7436 | 759 |
7431 | 760 if (temp == NULL || strlen(temp) < 2) |
761 continue; | |
7436 | 762 |
7431 | 763 temp++; |
764 length = strcspn(temp, "-"); | |
765 if (length > 31) length = 31; | |
7436 | 766 |
7431 | 767 offset = ftell(file); |
7436 | 768 |
7761 | 769 if (logfound) { |
770 newlen = offset - lastoff - length; | |
7436 | 771 if(strstr(buf, "----</H3><BR>")) { |
7761 | 772 newlen -= |
773 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
774 sizeof("----</H3><BR>") - 2; | |
7436 | 775 } else { |
7761 | 776 newlen -= |
777 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 778 } |
779 | |
7461 | 780 if(strchr(buf, '\r')) |
7770 | 781 newlen--; |
7461 | 782 |
7761 | 783 if (newlen != 0) { |
784 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
785 log->logger = &old_logger; | |
786 log->time = lasttime; | |
787 data = g_new0(struct old_logger_data, 1); | |
788 data->offset = lastoff; | |
789 data->length = newlen; | |
7764 | 790 data->pathref = gaim_stringref_ref(pathref); |
7761 | 791 log->logger_data = data; |
7431 | 792 list = g_list_append(list, log); |
7761 | 793 } |
7431 | 794 } |
795 | |
7761 | 796 logfound = 1; |
797 lastoff = offset; | |
7436 | 798 |
7431 | 799 g_snprintf(convostart, length, "%s", temp); |
7676 | 800 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
801 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
802 /* Ugly hack, in case current locale is not English */ | |
803 if (strcmp(month, "Jan") == 0) { | |
804 tm.tm_mon= 0; | |
805 } else if (strcmp(month, "Feb") == 0) { | |
806 tm.tm_mon = 1; | |
807 } else if (strcmp(month, "Mar") == 0) { | |
808 tm.tm_mon = 2; | |
809 } else if (strcmp(month, "Apr") == 0) { | |
810 tm.tm_mon = 3; | |
811 } else if (strcmp(month, "May") == 0) { | |
812 tm.tm_mon = 4; | |
813 } else if (strcmp(month, "Jun") == 0) { | |
814 tm.tm_mon = 5; | |
815 } else if (strcmp(month, "Jul") == 0) { | |
816 tm.tm_mon = 6; | |
817 } else if (strcmp(month, "Aug") == 0) { | |
818 tm.tm_mon = 7; | |
819 } else if (strcmp(month, "Sep") == 0) { | |
820 tm.tm_mon = 8; | |
821 } else if (strcmp(month, "Oct") == 0) { | |
822 tm.tm_mon = 9; | |
823 } else if (strcmp(month, "Nov") == 0) { | |
824 tm.tm_mon = 10; | |
825 } else if (strcmp(month, "Dec") == 0) { | |
826 tm.tm_mon = 11; | |
827 } | |
828 tm.tm_year -= 1900; | |
7761 | 829 lasttime = mktime(&tm); |
4184 | 830 } |
831 } | |
7613 | 832 |
7761 | 833 if (logfound) { |
834 if ((newlen = ftell(file) - lastoff) != 0) { | |
835 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
836 log->logger = &old_logger; | |
837 log->time = lasttime; | |
838 data = g_new0(struct old_logger_data, 1); | |
839 data->offset = lastoff; | |
840 data->length = newlen; | |
7764 | 841 data->pathref = gaim_stringref_ref(pathref); |
7761 | 842 log->logger_data = data; |
7613 | 843 list = g_list_append(list, log); |
7761 | 844 } |
7613 | 845 } |
846 | |
7764 | 847 gaim_stringref_unref(pathref); |
7431 | 848 fclose(file); |
849 return list; | |
4184 | 850 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
851 |
8096 | 852 static int old_logger_total_size(const char *name, GaimAccount *account) |
853 { | |
854 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
855 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
856 int size; | |
857 struct stat st; | |
858 | |
859 if (stat(pathstr, &st)) | |
860 size = 0; | |
861 else | |
862 size = st.st_size; | |
863 | |
864 g_free(logfile); | |
865 g_free(pathstr); | |
866 | |
867 return size; | |
868 } | |
869 | |
7616 | 870 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
871 { |
7431 | 872 struct old_logger_data *data = log->logger_data; |
7764 | 873 FILE *file = fopen(gaim_stringref_value(data->pathref), "rb"); |
7431 | 874 char *read = g_malloc(data->length + 1); |
875 fseek(file, data->offset, SEEK_SET); | |
876 fread(read, data->length, 1, file); | |
877 read[data->length] = '\0'; | |
7436 | 878 *flags = 0; |
879 if(strstr(read, "<BR>")) | |
880 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 881 return read; |
882 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
883 |
7616 | 884 static int old_logger_size (GaimLog *log) |
7556 | 885 { |
886 struct old_logger_data *data = log->logger_data; | |
7616 | 887 return data ? data->length : 0; |
888 } | |
889 | |
890 static void old_logger_finalize(GaimLog *log) | |
891 { | |
892 struct old_logger_data *data = log->logger_data; | |
7764 | 893 gaim_stringref_unref(data->pathref); |
7616 | 894 g_free(data); |
7556 | 895 } |
896 | |
7431 | 897 static GaimLogLogger old_logger = { |
898 "old logger", "old", | |
7616 | 899 NULL, NULL, |
900 old_logger_finalize, | |
7431 | 901 old_logger_list, |
7616 | 902 old_logger_read, |
8096 | 903 old_logger_size, |
904 old_logger_total_size | |
7431 | 905 }; |