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