Mercurial > pidgin
annotate src/log.c @ 7556:219903d29401
[gaim-migrate @ 8170]
And good night.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Tue, 18 Nov 2003 06:26:01 +0000 |
parents | 450b25d8d953 |
children | 54b370f7d9bf |
rev | line source |
---|---|
7431 | 1 /** |
2 * @file log.c Logging API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
7 * Copyright (C) 2003 Buzz Lightyear | |
7436 | 8 * |
7431 | 9 * This program is free software; you can redistribute it and/or modify |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
4184 | 22 */ |
4195 | 23 |
7431 | 24 #include "account.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
25 #include "debug.h" |
7431 | 26 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
27 #include "log.h" |
5548 | 28 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
29 #include "util.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
30 |
7457 | 31 static GaimLogLogger html_logger; |
7431 | 32 static GaimLogLogger txt_logger; |
33 static GaimLogLogger old_logger; | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
34 |
7431 | 35 /************************************************************************** |
36 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
37 **************************************************************************/ | |
4184 | 38 |
7431 | 39 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, time_t time) |
40 { | |
41 GaimLog *log = g_new0(GaimLog, 1); | |
42 log->name = g_strdup(name); | |
43 log->account = account; | |
44 log->time = time; | |
45 log->logger = gaim_log_logger_get(); | |
7440 | 46 if (log->logger && log->logger->create) |
47 log->logger->create(log); | |
7431 | 48 return log; |
4184 | 49 } |
50 | |
7431 | 51 void gaim_log_free(GaimLog *log) |
4184 | 52 { |
7431 | 53 g_return_if_fail(log); |
54 if (log->logger && log->logger->finalize) | |
55 log->logger->finalize(log); | |
56 g_free(log->name); | |
57 g_free(log); | |
58 } | |
7436 | 59 |
4184 | 60 |
7436 | 61 void gaim_log_write(GaimLog *log, GaimMessageFlags type, |
7431 | 62 const char *from, time_t time, const char *message) |
63 { | |
64 g_return_if_fail(log); | |
65 g_return_if_fail(log->logger); | |
7442 | 66 g_return_if_fail(log->logger->write); |
7431 | 67 |
7555 | 68 if ((log->type == GAIM_LOG_IM && gaim_prefs_get_bool("/core/logging/log_ims")) || |
69 (log->type == GAIM_LOG_CHAT && gaim_prefs_get_bool("/core/logging/log_chats"))) | |
7553 | 70 (log->logger->write)(log, type, from, time, message); |
4184 | 71 } |
72 | |
7431 | 73 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 74 { |
7542 | 75 GaimLogReadFlags mflags; |
7431 | 76 g_return_val_if_fail(log && log->logger, NULL); |
7462 | 77 if (log->logger->read) { |
7535 | 78 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
79 gaim_str_strip_cr(ret); |
7462 | 80 return ret; |
81 } | |
7470 | 82 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
4184 | 83 } |
7556 | 84 |
85 int gaim_log_get_size(GaimLog *log) | |
86 { | |
87 g_return_val_if_fail(log && log->logger, 0); | |
88 if (log->logger->size) | |
89 return log->logger->size(log); | |
90 return 0; | |
91 } | |
92 | |
93 int gaim_log_get_total_size(const char *name, GaimAccount *account) | |
94 { | |
95 GList *logs = gaim_log_get_logs(name, account); | |
96 int size = 0; | |
97 | |
98 while (logs) { | |
99 GList *logs2 = logs->next; | |
100 GaimLog *log = (GaimLog*)(logs->data); | |
101 size += gaim_log_get_size(log); | |
102 g_free(log->name); | |
103 g_free(log); | |
104 g_list_free_1(logs); | |
105 logs = logs2; | |
106 } | |
107 return size; | |
108 } | |
4184 | 109 |
7431 | 110 /**************************************************************************** |
111 * LOGGER FUNCTIONS ********************************************************* | |
112 ****************************************************************************/ | |
4184 | 113 |
7431 | 114 static GaimLogLogger *current_logger = NULL; |
115 static GSList *loggers = NULL; | |
7436 | 116 |
7431 | 117 static void logger_pref_cb(const char *name, GaimPrefType type, |
118 gpointer value, gpointer data) | |
119 { | |
120 GaimLogLogger *logger; | |
121 GSList *l = loggers; | |
122 while (l) { | |
123 logger = l->data; | |
124 if (!strcmp(logger->id, value)) { | |
125 gaim_log_logger_set(logger); | |
126 return; | |
4184 | 127 } |
7431 | 128 l = l->next; |
129 } | |
130 gaim_log_logger_set(&txt_logger); | |
131 } | |
4184 | 132 |
133 | |
7440 | 134 GaimLogLogger *gaim_log_logger_new(void(*create)(GaimLog *), |
7436 | 135 void(*write)(GaimLog *, GaimMessageFlags, const char *, |
7431 | 136 time_t, const char *), |
137 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), | |
7556 | 138 char*(*read)(GaimLog*, GaimLogReadFlags*), |
139 int(*size)(GaimLog*)) | |
7431 | 140 { |
141 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
7440 | 142 logger->create = create; |
7431 | 143 logger->write = write; |
144 logger->finalize = finalize; | |
145 logger->list = list; | |
146 logger->read = read; | |
7556 | 147 logger->size = size; |
7431 | 148 return logger; |
4184 | 149 } |
150 | |
7431 | 151 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 152 { |
7431 | 153 g_free(logger); |
154 } | |
4184 | 155 |
7431 | 156 void gaim_log_logger_add (GaimLogLogger *logger) |
157 { | |
158 g_return_if_fail(logger); | |
159 if (g_slist_find(loggers, logger)) | |
160 return; | |
161 loggers = g_slist_append(loggers, logger); | |
162 } | |
163 | |
164 void gaim_log_logger_remove (GaimLogLogger *logger) | |
165 { | |
166 g_return_if_fail(logger); | |
167 g_slist_remove(loggers, logger); | |
4184 | 168 } |
169 | |
7431 | 170 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 171 { |
7431 | 172 g_return_if_fail(logger); |
173 current_logger = logger; | |
7436 | 174 } |
4184 | 175 |
7431 | 176 GaimLogLogger *gaim_log_logger_get() |
177 { | |
178 return current_logger; | |
179 } | |
4184 | 180 |
7431 | 181 GList *gaim_log_logger_get_options(void) |
182 { | |
183 GSList *n; | |
184 GList *list = NULL; | |
185 GaimLogLogger *data; | |
4184 | 186 |
7431 | 187 for (n = loggers; n; n = n->next) { |
188 data = n->data; | |
189 if (!data->write) | |
190 continue; | |
7494 | 191 list = g_list_append(list, _(data->name)); |
7431 | 192 list = g_list_append(list, data->id); |
4184 | 193 } |
194 | |
7431 | 195 return list; |
196 } | |
197 | |
7436 | 198 static gint log_compare(gconstpointer y, gconstpointer z) |
7431 | 199 { |
7436 | 200 const GaimLog *a = y; |
201 const GaimLog *b = z; | |
202 | |
7431 | 203 return b->time - a->time; |
204 } | |
205 | |
206 GList *gaim_log_get_logs(const char *name, GaimAccount *account) | |
207 { | |
208 GList *logs = NULL; | |
209 GSList *n; | |
210 for (n = loggers; n; n = n->next) { | |
211 GaimLogLogger *logger = n->data; | |
212 if (!logger->list) | |
213 continue; | |
214 logs = g_list_concat(logs, logger->list(name, account)); | |
215 } | |
7436 | 216 |
7431 | 217 return g_list_sort(logs, log_compare); |
218 } | |
219 | |
220 void gaim_log_init(void) | |
7436 | 221 { |
7431 | 222 gaim_prefs_add_none("/core/logging"); |
7555 | 223 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
224 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
7431 | 225 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 226 gaim_log_logger_add(&html_logger); |
7431 | 227 gaim_log_logger_add(&txt_logger); |
228 gaim_log_logger_add(&old_logger); | |
229 gaim_prefs_connect_callback("/core/logging/format", | |
230 logger_pref_cb, NULL); | |
231 gaim_prefs_trigger_callback("/core/logging/format"); | |
232 } | |
233 | |
234 /**************************************************************************** | |
235 * LOGGERS ****************************************************************** | |
236 ****************************************************************************/ | |
237 | |
238 static GList *log_lister_common(const char *screenname, GaimAccount *account, const char *ext, GaimLogLogger *logger) | |
239 { | |
240 GDir *dir; | |
241 GList *list = NULL; | |
7501 | 242 const char *filename, *tmp; |
7431 | 243 char *me = g_strdup(gaim_normalize(account, gaim_account_get_username(account))); |
4184 | 244 |
7431 | 245 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
246 (gaim_find_prpl(gaim_account_get_protocol(account)))->list_icon(account, NULL); | |
247 char *path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, screenname), NULL); | |
248 | |
7447 | 249 g_free(me); |
250 | |
7431 | 251 if (!(dir = g_dir_open(path, 0, NULL))) { |
252 g_free(path); | |
253 return NULL; | |
254 } | |
255 while ((filename = g_dir_read_name(dir))) { | |
7501 | 256 tmp = filename + (strlen(filename) - strlen(ext)); |
257 if (tmp > filename && !strcmp(tmp, ext)) { | |
7431 | 258 const char *l = filename; |
259 struct tm time; | |
260 GaimLog *log; | |
261 char d[5]; | |
7436 | 262 |
7431 | 263 strncpy(d, l, 4); |
264 d[4] = '\0'; | |
265 time.tm_year = atoi(d) - 1900; | |
266 l = l + 5; | |
267 | |
268 strncpy(d, l, 2); | |
269 d[2] = '\0'; | |
270 time.tm_mon = atoi(d) - 1; | |
271 l = l + 3; | |
272 | |
273 strncpy(d, l, 2); | |
274 time.tm_mday = atoi(d); | |
275 l = l + 3; | |
276 | |
277 strncpy(d, l, 2); | |
278 time.tm_hour = atoi(d); | |
279 l = l + 2; | |
7436 | 280 |
7431 | 281 strncpy(d, l, 2); |
282 time.tm_min = atoi(d); | |
283 l = l + 2; | |
284 | |
285 strncpy(d, l, 2); | |
286 time.tm_sec = atoi(d); | |
287 l = l + 2; | |
288 log = gaim_log_new(GAIM_LOG_IM, screenname, account, mktime(&time)); | |
289 log->logger = logger; | |
290 log->logger_data = g_build_filename(path, filename, NULL); | |
291 list = g_list_append(list, log); | |
4184 | 292 } |
293 } | |
7431 | 294 g_dir_close(dir); |
7447 | 295 g_free(path); |
7431 | 296 return list; |
297 } | |
4184 | 298 |
7556 | 299 /* Only to be used with logs listed from log_lister_common */ |
300 int log_sizer_common(GaimLog *log) | |
301 { | |
302 struct stat st; | |
303 | |
304 if (stat((char*)(log->logger_data), &st)) | |
305 st.st_size = 0; | |
306 | |
307 return st.st_size; | |
308 } | |
309 | |
7431 | 310 #if 0 /* Maybe some other time. */ |
7443 | 311 /**************** |
7431 | 312 ** XML LOGGER ** |
313 ****************/ | |
314 | |
315 static const char *str_from_msg_type (GaimMessageFlags type) | |
316 { | |
7443 | 317 |
7431 | 318 return ""; |
7443 | 319 |
7431 | 320 } |
321 | |
7443 | 322 static void xml_logger_write(GaimLog *log, |
323 GaimMessageFlags type, | |
7431 | 324 const char *from, time_t time, const char *message) |
325 { | |
326 char date[64]; | |
327 char *xhtml = NULL; | |
328 if (!log->logger_data) { | |
329 /* This log is new. We could use the loggers 'new' function, but | |
330 * creating a new file there would result in empty files in the case | |
331 * that you open a convo with someone, but don't say anything. | |
332 */ | |
333 char *ud = gaim_user_dir(); | |
334 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
335 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
336 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
337 char *dir; | |
338 FILE *file; | |
339 | |
7453 | 340 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 341 |
7431 | 342 dir = g_build_filename(ud, "logs", NULL); |
343 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
344 g_free(dir); | |
7443 | 345 dir = g_build_filename(ud, "logs", |
7431 | 346 prpl, NULL); |
347 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
348 g_free(dir); | |
7443 | 349 dir = g_build_filename(ud, "logs", |
7431 | 350 prpl, guy, NULL); |
351 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7443 | 352 g_free(dir); |
353 dir = g_build_filename(ud, "logs", | |
7431 | 354 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
355 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7447 | 356 g_free(guy); |
7443 | 357 |
7431 | 358 char *filename = g_build_filename(dir, date, NULL); |
359 g_free(dir); | |
7443 | 360 |
7431 | 361 file = fopen(dir, "r"); |
362 if(!file) | |
363 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
364 else | |
365 fclose(file); | |
7443 | 366 |
7431 | 367 log->logger_data = fopen(filename, "a"); |
368 if (!log->logger_data) { | |
369 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
370 return; | |
371 } | |
372 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" | |
373 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 374 |
7453 | 375 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 376 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
377 date, log->name, prpl); | |
378 } | |
7443 | 379 |
7453 | 380 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 381 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
382 if (from) | |
7443 | 383 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
384 str_from_msg_type(type), | |
7431 | 385 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
386 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
387 from, date, xhtml); | |
388 else | |
7443 | 389 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
390 str_from_msg_type(type), | |
7431 | 391 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
392 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 393 date, xhtml): |
7431 | 394 fflush(log->logger_data); |
395 g_free(xhtml); | |
7443 | 396 } |
397 | |
7431 | 398 static void xml_logger_finalize(GaimLog *log) |
399 { | |
400 if (log->logger_data) { | |
401 fprintf(log->logger_data, "</conversation>\n"); | |
402 fclose(log->logger_data); | |
403 log->logger_data = NULL; | |
404 } | |
405 } | |
7443 | 406 |
7431 | 407 static GList *xml_logger_list(const char *sn, GaimAccount *account) |
408 { | |
409 return log_lister_common(sn, account, ".xml", &xml_logger); | |
4184 | 410 } |
411 | |
7431 | 412 static GaimLogLogger xml_logger = { |
413 N_("XML"), "xml", | |
414 NULL, | |
415 xml_logger_write, | |
416 xml_logger_finalize, | |
417 xml_logger_list, | |
418 NULL | |
419 }; | |
420 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
421 |
7431 | 422 /**************************** |
7457 | 423 ** HTML LOGGER ************* |
424 ****************************/ | |
425 | |
426 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
427 const char *from, time_t time, const char *message) | |
428 { | |
7489 | 429 GaimConnection *gc = gaim_account_get_connection(log->account); |
7457 | 430 char date[64]; |
431 if(!log->logger_data) { | |
432 /* This log is new */ | |
433 char *ud = gaim_user_dir(); | |
434 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
7553 | 435 char *chat; |
7457 | 436 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
437 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
438 char *dir; | |
439 char *filename; | |
440 FILE *file; | |
441 | |
7553 | 442 if (log->type == GAIM_LOG_CHAT) { |
443 chat = g_strdup_printf("%s.chat", guy); | |
444 g_free(guy); | |
445 guy = chat; | |
446 } | |
447 | |
7457 | 448 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time)); |
449 | |
450 dir = g_build_filename(ud, "logs", NULL); | |
451 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
452 g_free(dir); | |
453 dir = g_build_filename(ud, "logs", | |
454 prpl, NULL); | |
455 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
456 g_free(dir); | |
457 dir = g_build_filename(ud, "logs", | |
458 prpl, guy, NULL); | |
459 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
460 g_free(dir); | |
461 dir = g_build_filename(ud, "logs", | |
462 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
463 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
464 g_free(guy); | |
465 | |
466 filename = g_build_filename(dir, date, NULL); | |
467 g_free(dir); | |
468 | |
469 file = fopen(dir, "r"); | |
470 if(!file) | |
471 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
472 else | |
473 fclose(file); | |
474 | |
475 log->logger_data = fopen(filename, "a"); | |
476 if (!log->logger_data) { | |
477 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
478 return; | |
479 } | |
480 g_free(filename); | |
481 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
482 fprintf(log->logger_data, "<html><head><title>"); | |
483 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)", | |
484 log->name, date, gaim_account_get_username(log->account), prpl); | |
485 fprintf(log->logger_data, "</title></head><body>"); | |
486 fprintf(log->logger_data, | |
487 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", | |
488 log->name, date, gaim_account_get_username(log->account), prpl); | |
489 } | |
490 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
7489 | 491 if (type & GAIM_MESSAGE_SYSTEM) |
492 fprintf(log->logger_data, "(%s)<b> %s</b><br/>\n", date, message); | |
493 else if (type & GAIM_MESSAGE_WHISPER) | |
494 fprintf(log->logger_data, "<font color=\"#6C2585\">(%s)<b> %s:</b></font> %s<br/>\n", | |
495 date, from, message); | |
496 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
497 if (type & GAIM_MESSAGE_SEND) | |
7540 | 498 fprintf(log->logger_data, _("<font color=\"#16569E\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7489 | 499 else if (type & GAIM_MESSAGE_RECV) |
7540 | 500 fprintf(log->logger_data, _("<font color=\"#A82F2F\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7489 | 501 } else if (type & GAIM_MESSAGE_RECV) |
502 fprintf(log->logger_data, "<font color=\"#A82F2F\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
503 date, from, gc->prpl->info->name, message); | |
7491 | 504 else if (type & GAIM_MESSAGE_SEND) |
7489 | 505 fprintf(log->logger_data, "<font color=\"#16569E\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", |
506 date, from, gc->prpl->info->name, message); | |
7457 | 507 fflush(log->logger_data); |
508 } | |
509 | |
510 static void html_logger_finalize(GaimLog *log) | |
511 { | |
7463 | 512 if (log->logger_data) { |
513 fprintf(log->logger_data, "</body></html>"); | |
7457 | 514 fclose(log->logger_data); |
7463 | 515 } |
7457 | 516 } |
517 | |
518 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
519 { | |
520 return log_lister_common(sn, account, ".html", &html_logger); | |
521 } | |
522 | |
523 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
524 { | |
525 char *read, *minus_header; | |
526 *flags = GAIM_LOG_READ_NO_NEWLINE; | |
527 if (!log->logger_data) | |
7472 | 528 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
7457 | 529 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
530 minus_header = strchr(read, '\n'); | |
531 if (!minus_header) | |
532 minus_header = g_strdup(read); | |
533 else | |
534 minus_header = g_strdup(minus_header + 1); | |
535 g_free(read); | |
536 return minus_header; | |
537 } | |
7471 | 538 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7457 | 539 } |
540 | |
541 static GaimLogLogger html_logger = { | |
542 N_("HTML"), "html", | |
543 NULL, | |
544 html_logger_write, | |
545 html_logger_finalize, | |
546 html_logger_list, | |
7556 | 547 html_logger_read, |
548 log_sizer_common | |
7457 | 549 }; |
550 | |
551 | |
552 | |
553 | |
554 /**************************** | |
7431 | 555 ** PLAIN TEXT LOGGER ******* |
556 ****************************/ | |
4184 | 557 |
7436 | 558 static void txt_logger_write(GaimLog *log, |
559 GaimMessageFlags type, | |
7431 | 560 const char *from, time_t time, const char *message) |
561 { | |
562 char date[64]; | |
563 char *stripped = NULL; | |
564 if (!log->logger_data) { | |
565 /* This log is new. We could use the loggers 'new' function, but | |
566 * creating a new file there would result in empty files in the case | |
567 * that you open a convo with someone, but don't say anything. | |
568 */ | |
569 char *ud = gaim_user_dir(); | |
7473 | 570 char *filename; |
7431 | 571 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
7553 | 572 char *chat; |
7431 | 573 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
574 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
575 char *dir; | |
576 FILE *file; | |
7436 | 577 |
7553 | 578 if (log->type == GAIM_LOG_CHAT) { |
579 chat = g_strdup_printf("%s.chat", guy); | |
580 g_free(guy); | |
581 guy = chat; | |
582 } | |
7453 | 583 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
7436 | 584 |
7431 | 585 dir = g_build_filename(ud, "logs", NULL); |
586 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
587 g_free(dir); | |
7436 | 588 dir = g_build_filename(ud, "logs", |
7431 | 589 prpl, NULL); |
590 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
591 g_free(dir); | |
7436 | 592 dir = g_build_filename(ud, "logs", |
7431 | 593 prpl, guy, NULL); |
594 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7436 | 595 g_free(dir); |
596 dir = g_build_filename(ud, "logs", | |
7431 | 597 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
598 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7447 | 599 g_free(guy); |
7436 | 600 |
7473 | 601 filename = g_build_filename(dir, date, NULL); |
7431 | 602 g_free(dir); |
7436 | 603 |
7431 | 604 file = fopen(dir, "r"); |
605 if(!file) | |
606 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
607 else | |
608 fclose(file); | |
7436 | 609 |
7431 | 610 log->logger_data = fopen(filename, "a"); |
611 if (!log->logger_data) { | |
612 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
613 return; | |
4184 | 614 } |
7447 | 615 g_free(filename); |
7453 | 616 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 617 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", |
618 log->name, date, gaim_account_get_username(log->account), prpl); | |
619 } | |
7436 | 620 |
7453 | 621 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 622 stripped = gaim_markup_strip_html(message); |
7489 | 623 if (type & GAIM_MESSAGE_SEND || |
7541 | 624 type & GAIM_MESSAGE_RECV) { |
625 if (type & GAIM_MESSAGE_AUTO_RESP) | |
626 fprintf(log->logger_data, _("(%s) %s <AUTO-REPLY>: %s\n"), date, from, stripped); | |
627 else | |
628 fprintf(log->logger_data, "(%s) %s: %s\n", date, from, stripped); | |
629 } else if (type & GAIM_MESSAGE_SYSTEM) | |
7489 | 630 fprintf(log->logger_data, "(%s) %s\n", date, stripped); |
631 else if (type & GAIM_MESSAGE_NO_LOG) { | |
632 /* This shouldn't happen */ | |
633 g_free(stripped); | |
634 return; | |
635 } else if (type & GAIM_MESSAGE_WHISPER) | |
636 fprintf(log->logger_data, "(%s) *%s* %s", date, from, stripped); | |
637 else | |
638 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); | |
639 | |
7431 | 640 fflush(log->logger_data); |
641 g_free(stripped); | |
642 } | |
643 | |
644 static void txt_logger_finalize(GaimLog *log) | |
645 { | |
646 if (log->logger_data) | |
647 fclose(log->logger_data); | |
648 } | |
649 | |
650 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
651 { | |
652 return log_lister_common(sn, account, ".txt", &txt_logger); | |
653 } | |
654 | |
655 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
656 { | |
657 char *read, *minus_header; | |
7457 | 658 *flags = 0; |
7431 | 659 if (!log->logger_data) |
7472 | 660 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
7431 | 661 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
662 minus_header = strchr(read, '\n'); | |
663 if (!minus_header) | |
664 minus_header = g_strdup(read); | |
7436 | 665 else |
7431 | 666 minus_header = g_strdup(minus_header + 1); |
667 g_free(read); | |
668 return minus_header; | |
669 } | |
7471 | 670 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7436 | 671 } |
7431 | 672 |
673 static GaimLogLogger txt_logger = { | |
674 N_("Plain text"), "txt", | |
675 NULL, | |
676 txt_logger_write, | |
677 txt_logger_finalize, | |
678 txt_logger_list, | |
7556 | 679 txt_logger_read, |
680 log_sizer_common | |
7431 | 681 }; |
682 | |
683 /**************** | |
684 * OLD LOGGER *** | |
685 ****************/ | |
686 | |
687 /* The old logger doesn't write logs, only reads them. This is to include | |
688 * old logs in the log viewer transparently. | |
689 */ | |
690 | |
691 struct old_logger_data { | |
692 char *path; | |
693 int offset; | |
694 int length; | |
695 }; | |
696 | |
7436 | 697 static GList *old_logger_list(const char *sn, GaimAccount *account) |
7431 | 698 { |
699 FILE *file; | |
700 char buf[BUF_LONG]; | |
701 struct tm tm; | |
702 struct old_logger_data *data = NULL; | |
703 char day[4], month[4], year[5]; | |
704 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
705 char *date; | |
706 char *path = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
707 char *newlog; | |
708 | |
709 GaimLog *log = NULL; | |
710 GList *list = NULL; | |
711 | |
7473 | 712 g_free(logfile); |
713 | |
7461 | 714 if (!(file = fopen(path, "rb"))) { |
7447 | 715 g_free(path); |
7431 | 716 return NULL; |
7447 | 717 } |
7436 | 718 |
7431 | 719 while (fgets(buf, BUF_LONG, file)) { |
720 if ((newlog = strstr(buf, "---- New C"))) { | |
721 int length; | |
722 int offset; | |
723 GDate gdate; | |
724 char convostart[32]; | |
725 char *temp = strchr(buf, '@'); | |
7436 | 726 |
7431 | 727 if (temp == NULL || strlen(temp) < 2) |
728 continue; | |
7436 | 729 |
7431 | 730 temp++; |
731 length = strcspn(temp, "-"); | |
732 if (length > 31) length = 31; | |
7436 | 733 |
7431 | 734 offset = ftell(file); |
7436 | 735 |
7431 | 736 if (data) { |
7436 | 737 data->length = offset - data->offset - length; |
738 if(strstr(buf, "----</H3><BR>")) { | |
739 data->length -= | |
740 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
741 strlen("----</H3><BR>"); | |
742 } else { | |
743 data->length -= | |
744 strlen("---- New Conversation @ ") + strlen("----"); | |
745 } | |
746 | |
7461 | 747 if(strchr(buf, '\r')) |
748 data->length--; | |
749 | |
7431 | 750 if (data->length != 0) |
751 list = g_list_append(list, log); | |
752 else | |
753 gaim_log_free(log); | |
754 } | |
755 | |
756 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
757 log->logger = &old_logger; | |
758 | |
7436 | 759 data = g_new0(struct old_logger_data, 1); |
7431 | 760 data->offset = offset; |
761 data->path = path; | |
762 log->logger_data = data; | |
763 | |
7436 | 764 |
7431 | 765 g_snprintf(convostart, length, "%s", temp); |
766 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | |
767 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | |
768 date = g_strdup_printf("%s %s %s", month, day, year); | |
769 g_date_set_parse(&gdate, date); | |
770 tm.tm_mday = g_date_get_day(&gdate); | |
771 tm.tm_mon = g_date_get_month(&gdate) - 1; | |
772 tm.tm_year = g_date_get_year(&gdate) - 1900; | |
773 log->time = mktime(&tm); | |
774 | |
4184 | 775 } |
776 } | |
7431 | 777 fclose(file); |
778 return list; | |
4184 | 779 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
780 |
7431 | 781 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
782 { |
7431 | 783 struct old_logger_data *data = log->logger_data; |
7461 | 784 FILE *file = fopen(data->path, "rb"); |
7431 | 785 char *read = g_malloc(data->length + 1); |
786 fseek(file, data->offset, SEEK_SET); | |
787 fread(read, data->length, 1, file); | |
788 read[data->length] = '\0'; | |
7436 | 789 *flags = 0; |
790 if(strstr(read, "<BR>")) | |
791 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 792 return read; |
793 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
794 |
7556 | 795 int old_logger_size (GaimLog *log) |
796 { | |
797 struct old_logger_data *data = log->logger_data; | |
798 return data->length; | |
799 } | |
800 | |
7431 | 801 static GaimLogLogger old_logger = { |
802 "old logger", "old", | |
803 NULL, NULL, NULL, | |
804 old_logger_list, | |
805 old_logger_read | |
806 }; |