Mercurial > pidgin
annotate src/log.c @ 7623:5381f96fc185
[gaim-migrate @ 8247]
more logging tweaks. this should let the last </body></html> get written
to all the open logs when you quit gaim.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Mon, 24 Nov 2003 07:00:39 +0000 |
parents | 53e38b1ce00a |
children | e293d0c42ccb |
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 } |
7616 | 84 |
7556 | 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; | |
7616 | 97 |
98 | |
7556 | 99 while (logs) { |
100 GList *logs2 = logs->next; | |
101 GaimLog *log = (GaimLog*)(logs->data); | |
102 size += gaim_log_get_size(log); | |
103 g_free(log->name); | |
104 g_free(log); | |
105 g_list_free_1(logs); | |
106 logs = logs2; | |
107 } | |
7616 | 108 |
7556 | 109 return size; |
110 } | |
4184 | 111 |
7431 | 112 /**************************************************************************** |
113 * LOGGER FUNCTIONS ********************************************************* | |
114 ****************************************************************************/ | |
4184 | 115 |
7431 | 116 static GaimLogLogger *current_logger = NULL; |
117 static GSList *loggers = NULL; | |
7436 | 118 |
7431 | 119 static void logger_pref_cb(const char *name, GaimPrefType type, |
120 gpointer value, gpointer data) | |
121 { | |
122 GaimLogLogger *logger; | |
123 GSList *l = loggers; | |
124 while (l) { | |
125 logger = l->data; | |
126 if (!strcmp(logger->id, value)) { | |
127 gaim_log_logger_set(logger); | |
128 return; | |
4184 | 129 } |
7431 | 130 l = l->next; |
131 } | |
132 gaim_log_logger_set(&txt_logger); | |
133 } | |
4184 | 134 |
135 | |
7440 | 136 GaimLogLogger *gaim_log_logger_new(void(*create)(GaimLog *), |
7436 | 137 void(*write)(GaimLog *, GaimMessageFlags, const char *, |
7431 | 138 time_t, const char *), |
139 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), | |
7556 | 140 char*(*read)(GaimLog*, GaimLogReadFlags*), |
141 int(*size)(GaimLog*)) | |
7431 | 142 { |
143 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
7440 | 144 logger->create = create; |
7431 | 145 logger->write = write; |
146 logger->finalize = finalize; | |
147 logger->list = list; | |
148 logger->read = read; | |
7556 | 149 logger->size = size; |
7431 | 150 return logger; |
4184 | 151 } |
152 | |
7431 | 153 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 154 { |
7431 | 155 g_free(logger); |
156 } | |
4184 | 157 |
7431 | 158 void gaim_log_logger_add (GaimLogLogger *logger) |
159 { | |
160 g_return_if_fail(logger); | |
161 if (g_slist_find(loggers, logger)) | |
162 return; | |
163 loggers = g_slist_append(loggers, logger); | |
164 } | |
165 | |
166 void gaim_log_logger_remove (GaimLogLogger *logger) | |
167 { | |
168 g_return_if_fail(logger); | |
169 g_slist_remove(loggers, logger); | |
4184 | 170 } |
171 | |
7431 | 172 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 173 { |
7431 | 174 g_return_if_fail(logger); |
175 current_logger = logger; | |
7436 | 176 } |
4184 | 177 |
7431 | 178 GaimLogLogger *gaim_log_logger_get() |
179 { | |
180 return current_logger; | |
181 } | |
4184 | 182 |
7431 | 183 GList *gaim_log_logger_get_options(void) |
184 { | |
185 GSList *n; | |
186 GList *list = NULL; | |
187 GaimLogLogger *data; | |
4184 | 188 |
7431 | 189 for (n = loggers; n; n = n->next) { |
190 data = n->data; | |
191 if (!data->write) | |
192 continue; | |
7494 | 193 list = g_list_append(list, _(data->name)); |
7431 | 194 list = g_list_append(list, data->id); |
4184 | 195 } |
196 | |
7431 | 197 return list; |
198 } | |
199 | |
7436 | 200 static gint log_compare(gconstpointer y, gconstpointer z) |
7431 | 201 { |
7436 | 202 const GaimLog *a = y; |
203 const GaimLog *b = z; | |
204 | |
7431 | 205 return b->time - a->time; |
206 } | |
207 | |
208 GList *gaim_log_get_logs(const char *name, GaimAccount *account) | |
209 { | |
210 GList *logs = NULL; | |
211 GSList *n; | |
212 for (n = loggers; n; n = n->next) { | |
213 GaimLogLogger *logger = n->data; | |
214 if (!logger->list) | |
215 continue; | |
216 logs = g_list_concat(logs, logger->list(name, account)); | |
217 } | |
7436 | 218 |
7431 | 219 return g_list_sort(logs, log_compare); |
220 } | |
221 | |
222 void gaim_log_init(void) | |
7436 | 223 { |
7431 | 224 gaim_prefs_add_none("/core/logging"); |
7555 | 225 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
226 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
7431 | 227 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 228 gaim_log_logger_add(&html_logger); |
7431 | 229 gaim_log_logger_add(&txt_logger); |
230 gaim_log_logger_add(&old_logger); | |
231 gaim_prefs_connect_callback("/core/logging/format", | |
232 logger_pref_cb, NULL); | |
233 gaim_prefs_trigger_callback("/core/logging/format"); | |
234 } | |
235 | |
236 /**************************************************************************** | |
237 * LOGGERS ****************************************************************** | |
238 ****************************************************************************/ | |
239 | |
7616 | 240 struct generic_logger_data { |
241 char *path; | |
242 FILE *file; | |
243 }; | |
244 | |
7431 | 245 static GList *log_lister_common(const char *screenname, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
246 { | |
247 GDir *dir; | |
248 GList *list = NULL; | |
7501 | 249 const char *filename, *tmp; |
7431 | 250 char *me = g_strdup(gaim_normalize(account, gaim_account_get_username(account))); |
4184 | 251 |
7431 | 252 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
253 (gaim_find_prpl(gaim_account_get_protocol(account)))->list_icon(account, NULL); | |
254 char *path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, screenname), NULL); | |
255 | |
7447 | 256 g_free(me); |
257 | |
7431 | 258 if (!(dir = g_dir_open(path, 0, NULL))) { |
259 g_free(path); | |
260 return NULL; | |
261 } | |
262 while ((filename = g_dir_read_name(dir))) { | |
7501 | 263 tmp = filename + (strlen(filename) - strlen(ext)); |
264 if (tmp > filename && !strcmp(tmp, ext)) { | |
7431 | 265 const char *l = filename; |
266 struct tm time; | |
267 GaimLog *log; | |
7616 | 268 struct generic_logger_data *data; |
7431 | 269 char d[5]; |
7436 | 270 |
7431 | 271 strncpy(d, l, 4); |
272 d[4] = '\0'; | |
273 time.tm_year = atoi(d) - 1900; | |
274 l = l + 5; | |
275 | |
276 strncpy(d, l, 2); | |
277 d[2] = '\0'; | |
278 time.tm_mon = atoi(d) - 1; | |
279 l = l + 3; | |
280 | |
281 strncpy(d, l, 2); | |
282 time.tm_mday = atoi(d); | |
283 l = l + 3; | |
284 | |
285 strncpy(d, l, 2); | |
286 time.tm_hour = atoi(d); | |
287 l = l + 2; | |
7436 | 288 |
7431 | 289 strncpy(d, l, 2); |
290 time.tm_min = atoi(d); | |
291 l = l + 2; | |
292 | |
293 strncpy(d, l, 2); | |
294 time.tm_sec = atoi(d); | |
295 l = l + 2; | |
296 log = gaim_log_new(GAIM_LOG_IM, screenname, account, mktime(&time)); | |
297 log->logger = logger; | |
7616 | 298 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
299 data->path = g_build_filename(path, filename, NULL); | |
7431 | 300 list = g_list_append(list, log); |
4184 | 301 } |
302 } | |
7431 | 303 g_dir_close(dir); |
7447 | 304 g_free(path); |
7431 | 305 return list; |
306 } | |
4184 | 307 |
7556 | 308 /* Only to be used with logs listed from log_lister_common */ |
7616 | 309 int log_sizer_common(GaimLog *log) |
7556 | 310 { |
311 struct stat st; | |
7616 | 312 struct generic_logger_data *data = log->logger_data; |
7556 | 313 |
7616 | 314 if (!data->path || stat(data->path, &st)) |
7556 | 315 st.st_size = 0; |
316 | |
317 return st.st_size; | |
318 } | |
319 | |
7431 | 320 #if 0 /* Maybe some other time. */ |
7443 | 321 /**************** |
7431 | 322 ** XML LOGGER ** |
323 ****************/ | |
324 | |
325 static const char *str_from_msg_type (GaimMessageFlags type) | |
326 { | |
7443 | 327 |
7431 | 328 return ""; |
7443 | 329 |
7431 | 330 } |
331 | |
7443 | 332 static void xml_logger_write(GaimLog *log, |
333 GaimMessageFlags type, | |
7431 | 334 const char *from, time_t time, const char *message) |
335 { | |
336 char date[64]; | |
337 char *xhtml = NULL; | |
338 if (!log->logger_data) { | |
339 /* This log is new. We could use the loggers 'new' function, but | |
340 * creating a new file there would result in empty files in the case | |
341 * that you open a convo with someone, but don't say anything. | |
342 */ | |
343 char *ud = gaim_user_dir(); | |
344 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
345 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
346 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
347 char *dir; | |
348 FILE *file; | |
349 | |
7453 | 350 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 351 |
352 dir = g_build_filename(ud, "logs", | |
7431 | 353 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
7612 | 354 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7447 | 355 g_free(guy); |
7443 | 356 |
7431 | 357 char *filename = g_build_filename(dir, date, NULL); |
358 g_free(dir); | |
7443 | 359 |
7431 | 360 log->logger_data = fopen(filename, "a"); |
361 if (!log->logger_data) { | |
362 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 363 g_free(filename); |
7431 | 364 return; |
365 } | |
7564 | 366 g_free(filename); |
7431 | 367 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
368 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 369 |
7453 | 370 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 371 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
372 date, log->name, prpl); | |
373 } | |
7443 | 374 |
7453 | 375 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 376 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
377 if (from) | |
7443 | 378 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
379 str_from_msg_type(type), | |
7431 | 380 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
381 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
382 from, date, xhtml); | |
383 else | |
7443 | 384 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
385 str_from_msg_type(type), | |
7431 | 386 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
387 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 388 date, xhtml): |
7431 | 389 fflush(log->logger_data); |
390 g_free(xhtml); | |
7443 | 391 } |
392 | |
7431 | 393 static void xml_logger_finalize(GaimLog *log) |
394 { | |
395 if (log->logger_data) { | |
396 fprintf(log->logger_data, "</conversation>\n"); | |
397 fclose(log->logger_data); | |
398 log->logger_data = NULL; | |
399 } | |
400 } | |
7443 | 401 |
7431 | 402 static GList *xml_logger_list(const char *sn, GaimAccount *account) |
403 { | |
404 return log_lister_common(sn, account, ".xml", &xml_logger); | |
4184 | 405 } |
406 | |
7431 | 407 static GaimLogLogger xml_logger = { |
408 N_("XML"), "xml", | |
409 NULL, | |
410 xml_logger_write, | |
411 xml_logger_finalize, | |
412 xml_logger_list, | |
413 NULL | |
414 }; | |
415 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
416 |
7431 | 417 /**************************** |
7457 | 418 ** HTML LOGGER ************* |
419 ****************************/ | |
420 | |
421 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
422 const char *from, time_t time, const char *message) | |
423 { | |
7489 | 424 GaimConnection *gc = gaim_account_get_connection(log->account); |
7457 | 425 char date[64]; |
7616 | 426 struct generic_logger_data *data = log->logger_data; |
7618 | 427 if(!data) { |
7457 | 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", | |
446 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
7612 | 447 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7457 | 448 g_free(guy); |
449 | |
450 filename = g_build_filename(dir, date, NULL); | |
451 g_free(dir); | |
452 | |
7616 | 453 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
454 | |
455 data->file = fopen(filename, "a"); | |
456 if (!data->file) { | |
7623 | 457 gaim_debug(GAIM_DEBUG_ERROR, "log", |
458 "Could not create log file %s\n", filename); | |
7564 | 459 g_free(filename); |
7457 | 460 return; |
461 } | |
462 g_free(filename); | |
463 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
7616 | 464 fprintf(data->file, "<html><head><title>"); |
465 fprintf(data->file, "Conversation with %s at %s on %s (%s)", | |
7457 | 466 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 467 fprintf(data->file, "</title></head><body>"); |
468 fprintf(data->file, | |
7457 | 469 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
470 log->name, date, gaim_account_get_username(log->account), prpl); | |
471 } | |
7623 | 472 |
473 /* if we can't write to the file, give up before we hurt ourselves */ | |
474 if(!data->file) | |
475 return; | |
476 | |
7457 | 477 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7489 | 478 if (type & GAIM_MESSAGE_SYSTEM) |
7616 | 479 fprintf(data->file, "(%s)<b> %s</b><br/>\n", date, message); |
7489 | 480 else if (type & GAIM_MESSAGE_WHISPER) |
7616 | 481 fprintf(data->file, "<font color=\"#6C2585\">(%s)<b> %s:</b></font> %s<br/>\n", |
7489 | 482 date, from, message); |
483 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
484 if (type & GAIM_MESSAGE_SEND) | |
7616 | 485 fprintf(data->file, _("<font color=\"#16569E\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7489 | 486 else if (type & GAIM_MESSAGE_RECV) |
7616 | 487 fprintf(data->file, _("<font color=\"#A82F2F\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); |
7564 | 488 } else if (type & GAIM_MESSAGE_RECV) { |
489 char *msg = g_strdup(message); | |
490 if(gaim_message_meify(msg, -1)) | |
7616 | 491 fprintf(data->file, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7564 | 492 date, from, gc->prpl->info->name, msg); |
493 else | |
7616 | 494 fprintf(data->file, "<font color=\"#A82F2F\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7564 | 495 date, from, gc->prpl->info->name, msg); |
496 g_free(msg); | |
497 } else if (type & GAIM_MESSAGE_SEND) { | |
498 char *msg = g_strdup(message); | |
499 if(gaim_message_meify(msg, -1)) | |
7616 | 500 fprintf(data->file, "<font color=\"#6C2585\">(%s) <b>***%s</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7564 | 501 date, from, gc->prpl->info->name, msg); |
502 else | |
7616 | 503 fprintf(data->file, "<font color=\"#16569E\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", |
7564 | 504 date, from, gc->prpl->info->name, msg); |
505 g_free(msg); | |
506 } | |
7616 | 507 fflush(data->file); |
7457 | 508 } |
509 | |
510 static void html_logger_finalize(GaimLog *log) | |
511 { | |
7616 | 512 struct generic_logger_data *data = log->logger_data; |
513 if (data) { | |
514 if(data->file) { | |
515 fprintf(data->file, "</body></html>"); | |
516 fclose(data->file); | |
517 } | |
518 g_free(data->path); | |
7463 | 519 } |
7457 | 520 } |
521 | |
522 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
523 { | |
524 return log_lister_common(sn, account, ".html", &html_logger); | |
525 } | |
526 | |
527 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
528 { | |
529 char *read, *minus_header; | |
7616 | 530 struct generic_logger_data *data = log->logger_data; |
7457 | 531 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 532 if (!data || !data->path) |
533 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
534 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 535 minus_header = strchr(read, '\n'); |
536 if (!minus_header) | |
537 minus_header = g_strdup(read); | |
538 else | |
539 minus_header = g_strdup(minus_header + 1); | |
540 g_free(read); | |
541 return minus_header; | |
542 } | |
7471 | 543 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7457 | 544 } |
545 | |
546 static GaimLogLogger html_logger = { | |
547 N_("HTML"), "html", | |
548 NULL, | |
549 html_logger_write, | |
550 html_logger_finalize, | |
551 html_logger_list, | |
7556 | 552 html_logger_read, |
553 log_sizer_common | |
7457 | 554 }; |
555 | |
556 | |
557 | |
558 | |
559 /**************************** | |
7431 | 560 ** PLAIN TEXT LOGGER ******* |
561 ****************************/ | |
4184 | 562 |
7436 | 563 static void txt_logger_write(GaimLog *log, |
564 GaimMessageFlags type, | |
7431 | 565 const char *from, time_t time, const char *message) |
566 { | |
567 char date[64]; | |
568 char *stripped = NULL; | |
7616 | 569 struct generic_logger_data *data = log->logger_data; |
570 if (!data) { | |
7431 | 571 /* This log is new. We could use the loggers 'new' function, but |
572 * creating a new file there would result in empty files in the case | |
573 * that you open a convo with someone, but don't say anything. | |
574 */ | |
575 char *ud = gaim_user_dir(); | |
7473 | 576 char *filename; |
7431 | 577 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
7553 | 578 char *chat; |
7431 | 579 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
580 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
581 char *dir; | |
7436 | 582 |
7553 | 583 if (log->type == GAIM_LOG_CHAT) { |
584 chat = g_strdup_printf("%s.chat", guy); | |
585 g_free(guy); | |
586 guy = chat; | |
587 } | |
7453 | 588 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
7436 | 589 |
590 dir = g_build_filename(ud, "logs", | |
7431 | 591 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
7612 | 592 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7447 | 593 g_free(guy); |
7436 | 594 |
7473 | 595 filename = g_build_filename(dir, date, NULL); |
7431 | 596 g_free(dir); |
7436 | 597 |
7616 | 598 log->logger_data = data = g_new0(struct generic_logger_data, 1); |
599 | |
600 data->file = fopen(filename, "a"); | |
601 if (!data->file) { | |
7431 | 602 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); |
7564 | 603 g_free(filename); |
7431 | 604 return; |
4184 | 605 } |
7447 | 606 g_free(filename); |
7453 | 607 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 608 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 609 log->name, date, gaim_account_get_username(log->account), prpl); |
610 } | |
7436 | 611 |
7623 | 612 /* if we can't write to the file, give up before we hurt ourselves */ |
613 if(!data->file) | |
614 return; | |
615 | |
7453 | 616 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 617 stripped = gaim_markup_strip_html(message); |
7489 | 618 if (type & GAIM_MESSAGE_SEND || |
7541 | 619 type & GAIM_MESSAGE_RECV) { |
7564 | 620 if (type & GAIM_MESSAGE_AUTO_RESP) { |
7616 | 621 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, from, stripped); |
7564 | 622 } else { |
623 if(gaim_message_meify(stripped, -1)) | |
7616 | 624 fprintf(data->file, "(%s) ***%s %s\n", date, from, |
7564 | 625 stripped); |
626 else | |
7616 | 627 fprintf(data->file, "(%s) %s: %s\n", date, from, |
7564 | 628 stripped); |
629 } | |
630 } else if (type & GAIM_MESSAGE_SYSTEM) | |
7616 | 631 fprintf(data->file, "(%s) %s\n", date, stripped); |
7489 | 632 else if (type & GAIM_MESSAGE_NO_LOG) { |
633 /* This shouldn't happen */ | |
634 g_free(stripped); | |
635 return; | |
636 } else if (type & GAIM_MESSAGE_WHISPER) | |
7616 | 637 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); |
7489 | 638 else |
7616 | 639 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); |
7489 | 640 |
7616 | 641 fflush(data->file); |
7431 | 642 g_free(stripped); |
643 } | |
644 | |
645 static void txt_logger_finalize(GaimLog *log) | |
646 { | |
7616 | 647 struct generic_logger_data *data = log->logger_data; |
648 if (data) { | |
649 if(data->file) | |
650 fclose(data->file); | |
651 if(data->path) | |
652 g_free(data->path); | |
653 } | |
7431 | 654 } |
655 | |
656 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
657 { | |
658 return log_lister_common(sn, account, ".txt", &txt_logger); | |
659 } | |
660 | |
661 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
662 { | |
663 char *read, *minus_header; | |
7616 | 664 struct generic_logger_data *data = log->logger_data; |
7457 | 665 *flags = 0; |
7616 | 666 if (!data || !data->path) |
667 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
668 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 669 minus_header = strchr(read, '\n'); |
670 if (!minus_header) | |
671 minus_header = g_strdup(read); | |
7436 | 672 else |
7431 | 673 minus_header = g_strdup(minus_header + 1); |
674 g_free(read); | |
675 return minus_header; | |
676 } | |
7471 | 677 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
7436 | 678 } |
7431 | 679 |
680 static GaimLogLogger txt_logger = { | |
681 N_("Plain text"), "txt", | |
682 NULL, | |
683 txt_logger_write, | |
684 txt_logger_finalize, | |
685 txt_logger_list, | |
7556 | 686 txt_logger_read, |
687 log_sizer_common | |
7431 | 688 }; |
689 | |
690 /**************** | |
691 * OLD LOGGER *** | |
692 ****************/ | |
693 | |
694 /* The old logger doesn't write logs, only reads them. This is to include | |
695 * old logs in the log viewer transparently. | |
696 */ | |
697 | |
698 struct old_logger_data { | |
699 char *path; | |
700 int offset; | |
701 int length; | |
702 }; | |
703 | |
7436 | 704 static GList *old_logger_list(const char *sn, GaimAccount *account) |
7431 | 705 { |
706 FILE *file; | |
707 char buf[BUF_LONG]; | |
708 struct tm tm; | |
709 struct old_logger_data *data = NULL; | |
710 char day[4], month[4], year[5]; | |
711 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
712 char *date; | |
713 char *path = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
714 char *newlog; | |
715 | |
716 GaimLog *log = NULL; | |
717 GList *list = NULL; | |
718 | |
7473 | 719 g_free(logfile); |
720 | |
7461 | 721 if (!(file = fopen(path, "rb"))) { |
7447 | 722 g_free(path); |
7431 | 723 return NULL; |
7447 | 724 } |
7436 | 725 |
7431 | 726 while (fgets(buf, BUF_LONG, file)) { |
727 if ((newlog = strstr(buf, "---- New C"))) { | |
728 int length; | |
729 int offset; | |
730 GDate gdate; | |
731 char convostart[32]; | |
732 char *temp = strchr(buf, '@'); | |
7436 | 733 |
7431 | 734 if (temp == NULL || strlen(temp) < 2) |
735 continue; | |
7436 | 736 |
7431 | 737 temp++; |
738 length = strcspn(temp, "-"); | |
739 if (length > 31) length = 31; | |
7436 | 740 |
7431 | 741 offset = ftell(file); |
7436 | 742 |
7431 | 743 if (data) { |
7436 | 744 data->length = offset - data->offset - length; |
745 if(strstr(buf, "----</H3><BR>")) { | |
746 data->length -= | |
747 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
748 strlen("----</H3><BR>"); | |
749 } else { | |
750 data->length -= | |
751 strlen("---- New Conversation @ ") + strlen("----"); | |
752 } | |
753 | |
7461 | 754 if(strchr(buf, '\r')) |
755 data->length--; | |
756 | |
7431 | 757 if (data->length != 0) |
758 list = g_list_append(list, log); | |
759 else | |
760 gaim_log_free(log); | |
761 } | |
762 | |
763 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
764 log->logger = &old_logger; | |
765 | |
7436 | 766 data = g_new0(struct old_logger_data, 1); |
7431 | 767 data->offset = offset; |
7616 | 768 data->path = g_strdup(path); |
7431 | 769 log->logger_data = data; |
770 | |
7436 | 771 |
7431 | 772 g_snprintf(convostart, length, "%s", temp); |
773 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | |
774 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | |
775 date = g_strdup_printf("%s %s %s", month, day, year); | |
776 g_date_set_parse(&gdate, date); | |
777 tm.tm_mday = g_date_get_day(&gdate); | |
778 tm.tm_mon = g_date_get_month(&gdate) - 1; | |
779 tm.tm_year = g_date_get_year(&gdate) - 1900; | |
780 log->time = mktime(&tm); | |
7616 | 781 g_free(date); |
7431 | 782 |
4184 | 783 } |
784 } | |
7613 | 785 |
786 if (data) { | |
787 data->length = ftell(file) - data->offset; | |
788 if (data->length != 0) | |
789 list = g_list_append(list, log); | |
790 else | |
791 gaim_log_free(log); | |
792 } | |
793 | |
7616 | 794 g_free(path); |
7431 | 795 fclose(file); |
796 return list; | |
4184 | 797 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
798 |
7616 | 799 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
800 { |
7431 | 801 struct old_logger_data *data = log->logger_data; |
7461 | 802 FILE *file = fopen(data->path, "rb"); |
7431 | 803 char *read = g_malloc(data->length + 1); |
804 fseek(file, data->offset, SEEK_SET); | |
805 fread(read, data->length, 1, file); | |
806 read[data->length] = '\0'; | |
7436 | 807 *flags = 0; |
808 if(strstr(read, "<BR>")) | |
809 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
7431 | 810 return read; |
811 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
812 |
7616 | 813 static int old_logger_size (GaimLog *log) |
7556 | 814 { |
815 struct old_logger_data *data = log->logger_data; | |
7616 | 816 return data ? data->length : 0; |
817 } | |
818 | |
819 static void old_logger_finalize(GaimLog *log) | |
820 { | |
821 struct old_logger_data *data = log->logger_data; | |
822 g_free(data->path); | |
823 g_free(data); | |
7556 | 824 } |
825 | |
7431 | 826 static GaimLogLogger old_logger = { |
827 "old logger", "old", | |
7616 | 828 NULL, NULL, |
829 old_logger_finalize, | |
7431 | 830 old_logger_list, |
7616 | 831 old_logger_read, |
832 old_logger_size | |
7431 | 833 }; |