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