Mercurial > pidgin.yaz
annotate src/log.c @ 7579:5a942f1b14b2
[gaim-migrate @ 8196]
polish
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Thu, 20 Nov 2003 02:45:44 +0000 |
parents | 54b370f7d9bf |
children | 3ae88e96dde2 |
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 log->logger_data = fopen(filename, "a"); |
362 if (!log->logger_data) { | |
363 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 364 g_free(filename); |
7431 | 365 return; |
366 } | |
7564 | 367 g_free(filename); |
7431 | 368 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
369 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 370 |
7453 | 371 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 372 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
373 date, log->name, prpl); | |
374 } | |
7443 | 375 |
7453 | 376 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 377 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
378 if (from) | |
7443 | 379 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
380 str_from_msg_type(type), | |
7431 | 381 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
382 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
383 from, date, xhtml); | |
384 else | |
7443 | 385 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
386 str_from_msg_type(type), | |
7431 | 387 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
388 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 389 date, xhtml): |
7431 | 390 fflush(log->logger_data); |
391 g_free(xhtml); | |
7443 | 392 } |
393 | |
7431 | 394 static void xml_logger_finalize(GaimLog *log) |
395 { | |
396 if (log->logger_data) { | |
397 fprintf(log->logger_data, "</conversation>\n"); | |
398 fclose(log->logger_data); | |
399 log->logger_data = NULL; | |
400 } | |
401 } | |
7443 | 402 |
7431 | 403 static GList *xml_logger_list(const char *sn, GaimAccount *account) |
404 { | |
405 return log_lister_common(sn, account, ".xml", &xml_logger); | |
4184 | 406 } |
407 | |
7431 | 408 static GaimLogLogger xml_logger = { |
409 N_("XML"), "xml", | |
410 NULL, | |
411 xml_logger_write, | |
412 xml_logger_finalize, | |
413 xml_logger_list, | |
414 NULL | |
415 }; | |
416 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
417 |
7431 | 418 /**************************** |
7457 | 419 ** HTML LOGGER ************* |
420 ****************************/ | |
421 | |
422 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
423 const char *from, time_t time, const char *message) | |
424 { | |
7489 | 425 GaimConnection *gc = gaim_account_get_connection(log->account); |
7457 | 426 char date[64]; |
427 if(!log->logger_data) { | |
428 /* This log is new */ | |
429 char *ud = gaim_user_dir(); | |
430 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
7553 | 431 char *chat; |
7457 | 432 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
433 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
434 char *dir; | |
435 char *filename; | |
436 | |
7553 | 437 if (log->type == GAIM_LOG_CHAT) { |
438 chat = g_strdup_printf("%s.chat", guy); | |
439 g_free(guy); | |
440 guy = chat; | |
441 } | |
442 | |
7457 | 443 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time)); |
444 | |
445 dir = g_build_filename(ud, "logs", NULL); | |
446 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
447 g_free(dir); | |
448 dir = g_build_filename(ud, "logs", | |
449 prpl, NULL); | |
450 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
451 g_free(dir); | |
452 dir = g_build_filename(ud, "logs", | |
453 prpl, guy, NULL); | |
454 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
455 g_free(dir); | |
456 dir = g_build_filename(ud, "logs", | |
457 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
458 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
459 g_free(guy); | |
460 | |
461 filename = g_build_filename(dir, date, NULL); | |
462 g_free(dir); | |
463 | |
464 log->logger_data = fopen(filename, "a"); | |
465 if (!log->logger_data) { | |
466 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 467 g_free(filename); |
7457 | 468 return; |
469 } | |
470 g_free(filename); | |
471 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
472 fprintf(log->logger_data, "<html><head><title>"); | |
473 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)", | |
474 log->name, date, gaim_account_get_username(log->account), prpl); | |
475 fprintf(log->logger_data, "</title></head><body>"); | |
476 fprintf(log->logger_data, | |
477 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", | |
478 log->name, date, gaim_account_get_username(log->account), prpl); | |
479 } | |
480 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
7489 | 481 if (type & GAIM_MESSAGE_SYSTEM) |
482 fprintf(log->logger_data, "(%s)<b> %s</b><br/>\n", date, message); | |
483 else if (type & GAIM_MESSAGE_WHISPER) | |
484 fprintf(log->logger_data, "<font color=\"#6C2585\">(%s)<b> %s:</b></font> %s<br/>\n", | |
485 date, from, message); | |
486 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
487 if (type & GAIM_MESSAGE_SEND) | |
7540 | 488 fprintf(log->logger_data, _("<font color=\"#16569E\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7489 | 489 else if (type & GAIM_MESSAGE_RECV) |
7540 | 490 fprintf(log->logger_data, _("<font color=\"#A82F2F\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7564 | 491 } else if (type & GAIM_MESSAGE_RECV) { |
492 char *msg = g_strdup(message); | |
493 if(gaim_message_meify(msg, -1)) | |
494 fprintf(log->logger_data, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
495 date, from, gc->prpl->info->name, msg); | |
496 else | |
497 fprintf(log->logger_data, "<font color=\"#A82F2F\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
498 date, from, gc->prpl->info->name, msg); | |
499 g_free(msg); | |
500 } else if (type & GAIM_MESSAGE_SEND) { | |
501 char *msg = g_strdup(message); | |
502 if(gaim_message_meify(msg, -1)) | |
503 fprintf(log->logger_data, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
504 date, from, gc->prpl->info->name, msg); | |
505 else | |
506 fprintf(log->logger_data, "<font color=\"#16569E\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
507 date, from, gc->prpl->info->name, msg); | |
508 g_free(msg); | |
509 } | |
7457 | 510 fflush(log->logger_data); |
511 } | |
512 | |
513 static void html_logger_finalize(GaimLog *log) | |
514 { | |
7463 | 515 if (log->logger_data) { |
516 fprintf(log->logger_data, "</body></html>"); | |
7457 | 517 fclose(log->logger_data); |
7463 | 518 } |
7457 | 519 } |
520 | |
521 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
522 { | |
523 return log_lister_common(sn, account, ".html", &html_logger); | |
524 } | |
525 | |
526 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
527 { | |
528 char *read, *minus_header; | |
529 *flags = GAIM_LOG_READ_NO_NEWLINE; | |
530 if (!log->logger_data) | |
7472 | 531 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
7457 | 532 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
533 minus_header = strchr(read, '\n'); | |
534 if (!minus_header) | |
535 minus_header = g_strdup(read); | |
536 else | |
537 minus_header = g_strdup(minus_header + 1); | |
538 g_free(read); | |
539 return minus_header; | |
540 } | |
7471 | 541 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7457 | 542 } |
543 | |
544 static GaimLogLogger html_logger = { | |
545 N_("HTML"), "html", | |
546 NULL, | |
547 html_logger_write, | |
548 html_logger_finalize, | |
549 html_logger_list, | |
7556 | 550 html_logger_read, |
551 log_sizer_common | |
7457 | 552 }; |
553 | |
554 | |
555 | |
556 | |
557 /**************************** | |
7431 | 558 ** PLAIN TEXT LOGGER ******* |
559 ****************************/ | |
4184 | 560 |
7436 | 561 static void txt_logger_write(GaimLog *log, |
562 GaimMessageFlags type, | |
7431 | 563 const char *from, time_t time, const char *message) |
564 { | |
565 char date[64]; | |
566 char *stripped = NULL; | |
567 if (!log->logger_data) { | |
568 /* This log is new. We could use the loggers 'new' function, but | |
569 * creating a new file there would result in empty files in the case | |
570 * that you open a convo with someone, but don't say anything. | |
571 */ | |
572 char *ud = gaim_user_dir(); | |
7473 | 573 char *filename; |
7431 | 574 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
7553 | 575 char *chat; |
7431 | 576 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
577 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
578 char *dir; | |
7436 | 579 |
7553 | 580 if (log->type == GAIM_LOG_CHAT) { |
581 chat = g_strdup_printf("%s.chat", guy); | |
582 g_free(guy); | |
583 guy = chat; | |
584 } | |
7453 | 585 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
7436 | 586 |
7431 | 587 dir = g_build_filename(ud, "logs", NULL); |
588 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
589 g_free(dir); | |
7436 | 590 dir = g_build_filename(ud, "logs", |
7431 | 591 prpl, NULL); |
592 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
593 g_free(dir); | |
7436 | 594 dir = g_build_filename(ud, "logs", |
7431 | 595 prpl, guy, NULL); |
596 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7436 | 597 g_free(dir); |
598 dir = g_build_filename(ud, "logs", | |
7431 | 599 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
600 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
7447 | 601 g_free(guy); |
7436 | 602 |
7473 | 603 filename = g_build_filename(dir, date, NULL); |
7431 | 604 g_free(dir); |
7436 | 605 |
7431 | 606 log->logger_data = fopen(filename, "a"); |
607 if (!log->logger_data) { | |
608 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 609 g_free(filename); |
7431 | 610 return; |
4184 | 611 } |
7447 | 612 g_free(filename); |
7453 | 613 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 614 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", |
615 log->name, date, gaim_account_get_username(log->account), prpl); | |
616 } | |
7436 | 617 |
7453 | 618 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 619 stripped = gaim_markup_strip_html(message); |
7489 | 620 if (type & GAIM_MESSAGE_SEND || |
7541 | 621 type & GAIM_MESSAGE_RECV) { |
7564 | 622 if (type & GAIM_MESSAGE_AUTO_RESP) { |
7541 | 623 fprintf(log->logger_data, _("(%s) %s <AUTO-REPLY>: %s\n"), date, from, stripped); |
7564 | 624 } else { |
625 if(gaim_message_meify(stripped, -1)) | |
626 fprintf(log->logger_data, "(%s) ***%s %s\n", date, from, | |
627 stripped); | |
628 else | |
629 fprintf(log->logger_data, "(%s) %s: %s\n", date, from, | |
630 stripped); | |
631 } | |
632 } else if (type & GAIM_MESSAGE_SYSTEM) | |
7489 | 633 fprintf(log->logger_data, "(%s) %s\n", date, stripped); |
634 else if (type & GAIM_MESSAGE_NO_LOG) { | |
635 /* This shouldn't happen */ | |
636 g_free(stripped); | |
637 return; | |
638 } else if (type & GAIM_MESSAGE_WHISPER) | |
639 fprintf(log->logger_data, "(%s) *%s* %s", date, from, stripped); | |
640 else | |
641 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); | |
642 | |
7431 | 643 fflush(log->logger_data); |
644 g_free(stripped); | |
645 } | |
646 | |
647 static void txt_logger_finalize(GaimLog *log) | |
648 { | |
649 if (log->logger_data) | |
650 fclose(log->logger_data); | |
651 } | |
652 | |
653 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
654 { | |
655 return log_lister_common(sn, account, ".txt", &txt_logger); | |
656 } | |
657 | |
658 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
659 { | |
660 char *read, *minus_header; | |
7457 | 661 *flags = 0; |
7431 | 662 if (!log->logger_data) |
7472 | 663 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
7431 | 664 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
665 minus_header = strchr(read, '\n'); | |
666 if (!minus_header) | |
667 minus_header = g_strdup(read); | |
7436 | 668 else |
7431 | 669 minus_header = g_strdup(minus_header + 1); |
670 g_free(read); | |
671 return minus_header; | |
672 } | |
7471 | 673 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7436 | 674 } |
7431 | 675 |
676 static GaimLogLogger txt_logger = { | |
677 N_("Plain text"), "txt", | |
678 NULL, | |
679 txt_logger_write, | |
680 txt_logger_finalize, | |
681 txt_logger_list, | |
7556 | 682 txt_logger_read, |
683 log_sizer_common | |
7431 | 684 }; |
685 | |
686 /**************** | |
687 * OLD LOGGER *** | |
688 ****************/ | |
689 | |
690 /* The old logger doesn't write logs, only reads them. This is to include | |
691 * old logs in the log viewer transparently. | |
692 */ | |
693 | |
694 struct old_logger_data { | |
695 char *path; | |
696 int offset; | |
697 int length; | |
698 }; | |
699 | |
7436 | 700 static GList *old_logger_list(const char *sn, GaimAccount *account) |
7431 | 701 { |
702 FILE *file; | |
703 char buf[BUF_LONG]; | |
704 struct tm tm; | |
705 struct old_logger_data *data = NULL; | |
706 char day[4], month[4], year[5]; | |
707 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
708 char *date; | |
709 char *path = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
710 char *newlog; | |
711 | |
712 GaimLog *log = NULL; | |
713 GList *list = NULL; | |
714 | |
7473 | 715 g_free(logfile); |
716 | |
7461 | 717 if (!(file = fopen(path, "rb"))) { |
7447 | 718 g_free(path); |
7431 | 719 return NULL; |
7447 | 720 } |
7436 | 721 |
7431 | 722 while (fgets(buf, BUF_LONG, file)) { |
723 if ((newlog = strstr(buf, "---- New C"))) { | |
724 int length; | |
725 int offset; | |
726 GDate gdate; | |
727 char convostart[32]; | |
728 char *temp = strchr(buf, '@'); | |
7436 | 729 |
7431 | 730 if (temp == NULL || strlen(temp) < 2) |
731 continue; | |
7436 | 732 |
7431 | 733 temp++; |
734 length = strcspn(temp, "-"); | |
735 if (length > 31) length = 31; | |
7436 | 736 |
7431 | 737 offset = ftell(file); |
7436 | 738 |
7431 | 739 if (data) { |
7436 | 740 data->length = offset - data->offset - length; |
741 if(strstr(buf, "----</H3><BR>")) { | |
742 data->length -= | |
743 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
744 strlen("----</H3><BR>"); | |
745 } else { | |
746 data->length -= | |
747 strlen("---- New Conversation @ ") + strlen("----"); | |
748 } | |
749 | |
7461 | 750 if(strchr(buf, '\r')) |
751 data->length--; | |
752 | |
7431 | 753 if (data->length != 0) |
754 list = g_list_append(list, log); | |
755 else | |
756 gaim_log_free(log); | |
757 } | |
758 | |
759 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
760 log->logger = &old_logger; | |
761 | |
7436 | 762 data = g_new0(struct old_logger_data, 1); |
7431 | 763 data->offset = offset; |
764 data->path = path; | |
765 log->logger_data = data; | |
766 | |
7436 | 767 |
7431 | 768 g_snprintf(convostart, length, "%s", temp); |
769 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | |
770 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | |
771 date = g_strdup_printf("%s %s %s", month, day, year); | |
772 g_date_set_parse(&gdate, date); | |
773 tm.tm_mday = g_date_get_day(&gdate); | |
774 tm.tm_mon = g_date_get_month(&gdate) - 1; | |
775 tm.tm_year = g_date_get_year(&gdate) - 1900; | |
776 log->time = mktime(&tm); | |
777 | |
4184 | 778 } |
779 } | |
7431 | 780 fclose(file); |
781 return list; | |
4184 | 782 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
783 |
7431 | 784 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
785 { |
7431 | 786 struct old_logger_data *data = log->logger_data; |
7461 | 787 FILE *file = fopen(data->path, "rb"); |
7431 | 788 char *read = g_malloc(data->length + 1); |
789 fseek(file, data->offset, SEEK_SET); | |
790 fread(read, data->length, 1, file); | |
791 read[data->length] = '\0'; | |
7436 | 792 *flags = 0; |
793 if(strstr(read, "<BR>")) | |
794 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 795 return read; |
796 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
797 |
7556 | 798 int old_logger_size (GaimLog *log) |
799 { | |
800 struct old_logger_data *data = log->logger_data; | |
801 return data->length; | |
802 } | |
803 | |
7431 | 804 static GaimLogLogger old_logger = { |
805 "old logger", "old", | |
806 NULL, NULL, NULL, | |
807 old_logger_list, | |
808 old_logger_read | |
809 }; |