Mercurial > pidgin
comparison src/log.c @ 7436:7cdbd2eb7546
[gaim-migrate @ 8041]
clean up whitespace, and make it not crash on old logs w/o HTML
committer: Tailor Script <tailor@pidgin.im>
| author | Nathan Walp <nwalp@pidgin.im> |
|---|---|
| date | Wed, 05 Nov 2003 22:12:55 +0000 |
| parents | 643cbc9a6035 |
| children | 8c0527c91a92 |
comparison
equal
deleted
inserted
replaced
| 7435:be5a715b71c8 | 7436:7cdbd2eb7546 |
|---|---|
| 3 * @ingroup core | 3 * @ingroup core |
| 4 * | 4 * |
| 5 * gaim | 5 * gaim |
| 6 * | 6 * |
| 7 * Copyright (C) 2003 Buzz Lightyear | 7 * Copyright (C) 2003 Buzz Lightyear |
| 8 * | 8 * |
| 9 * This program is free software; you can redistribute it and/or modify | 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 | 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 | 11 * the Free Software Foundation; either version 2 of the License, or |
| 12 * (at your option) any later version. | 12 * (at your option) any later version. |
| 13 * | 13 * |
| 53 if (log->logger && log->logger->finalize) | 53 if (log->logger && log->logger->finalize) |
| 54 log->logger->finalize(log); | 54 log->logger->finalize(log); |
| 55 g_free(log->name); | 55 g_free(log->name); |
| 56 g_free(log); | 56 g_free(log); |
| 57 } | 57 } |
| 58 | 58 |
| 59 | 59 |
| 60 void gaim_log_write(GaimLog *log, GaimMessageFlags type, | 60 void gaim_log_write(GaimLog *log, GaimMessageFlags type, |
| 61 const char *from, time_t time, const char *message) | 61 const char *from, time_t time, const char *message) |
| 62 { | 62 { |
| 63 g_return_if_fail(log); | 63 g_return_if_fail(log); |
| 64 g_return_if_fail(log->logger); | 64 g_return_if_fail(log->logger); |
| 65 g_return_if_fail(log->logger->write); | 65 g_return_if_fail(log->logger->write); |
| 79 * LOGGER FUNCTIONS ********************************************************* | 79 * LOGGER FUNCTIONS ********************************************************* |
| 80 ****************************************************************************/ | 80 ****************************************************************************/ |
| 81 | 81 |
| 82 static GaimLogLogger *current_logger = NULL; | 82 static GaimLogLogger *current_logger = NULL; |
| 83 static GSList *loggers = NULL; | 83 static GSList *loggers = NULL; |
| 84 | 84 |
| 85 static void logger_pref_cb(const char *name, GaimPrefType type, | 85 static void logger_pref_cb(const char *name, GaimPrefType type, |
| 86 gpointer value, gpointer data) | 86 gpointer value, gpointer data) |
| 87 { | 87 { |
| 88 GaimLogLogger *logger; | 88 GaimLogLogger *logger; |
| 89 GSList *l = loggers; | 89 GSList *l = loggers; |
| 98 gaim_log_logger_set(&txt_logger); | 98 gaim_log_logger_set(&txt_logger); |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 GaimLogLogger *gaim_log_logger_new(void(*new)(GaimLog *), | 102 GaimLogLogger *gaim_log_logger_new(void(*new)(GaimLog *), |
| 103 void(*write)(GaimLog *, GaimMessageFlags, const char *, | 103 void(*write)(GaimLog *, GaimMessageFlags, const char *, |
| 104 time_t, const char *), | 104 time_t, const char *), |
| 105 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), | 105 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), |
| 106 char*(*read)(GaimLog*, GaimLogReadFlags*)) | 106 char*(*read)(GaimLog*, GaimLogReadFlags*)) |
| 107 { | 107 { |
| 108 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | 108 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); |
| 135 | 135 |
| 136 void gaim_log_logger_set (GaimLogLogger *logger) | 136 void gaim_log_logger_set (GaimLogLogger *logger) |
| 137 { | 137 { |
| 138 g_return_if_fail(logger); | 138 g_return_if_fail(logger); |
| 139 current_logger = logger; | 139 current_logger = logger; |
| 140 } | 140 } |
| 141 | 141 |
| 142 GaimLogLogger *gaim_log_logger_get() | 142 GaimLogLogger *gaim_log_logger_get() |
| 143 { | 143 { |
| 144 return current_logger; | 144 return current_logger; |
| 145 } | 145 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 return list; | 161 return list; |
| 162 } | 162 } |
| 163 | 163 |
| 164 static gint log_compare(GaimLog *a, GaimLog *b) | 164 static gint log_compare(gconstpointer y, gconstpointer z) |
| 165 { | 165 { |
| 166 const GaimLog *a = y; | |
| 167 const GaimLog *b = z; | |
| 168 | |
| 166 return b->time - a->time; | 169 return b->time - a->time; |
| 167 } | 170 } |
| 168 | 171 |
| 169 GList *gaim_log_get_logs(const char *name, GaimAccount *account) | 172 GList *gaim_log_get_logs(const char *name, GaimAccount *account) |
| 170 { | 173 { |
| 174 GaimLogLogger *logger = n->data; | 177 GaimLogLogger *logger = n->data; |
| 175 if (!logger->list) | 178 if (!logger->list) |
| 176 continue; | 179 continue; |
| 177 logs = g_list_concat(logs, logger->list(name, account)); | 180 logs = g_list_concat(logs, logger->list(name, account)); |
| 178 } | 181 } |
| 179 | 182 |
| 180 return g_list_sort(logs, log_compare); | 183 return g_list_sort(logs, log_compare); |
| 181 } | 184 } |
| 182 | 185 |
| 183 void gaim_log_init(void) | 186 void gaim_log_init(void) |
| 184 { | 187 { |
| 185 gaim_prefs_add_none("/core/logging"); | 188 gaim_prefs_add_none("/core/logging"); |
| 186 gaim_prefs_add_string("/core/logging/format", "txt"); | 189 gaim_prefs_add_string("/core/logging/format", "txt"); |
| 187 gaim_log_logger_add(&txt_logger); | 190 gaim_log_logger_add(&txt_logger); |
| 188 gaim_log_logger_add(&old_logger); | 191 gaim_log_logger_add(&old_logger); |
| 189 gaim_prefs_connect_callback("/core/logging/format", | 192 gaim_prefs_connect_callback("/core/logging/format", |
| 215 if (g_str_has_suffix(filename, ext)) { | 218 if (g_str_has_suffix(filename, ext)) { |
| 216 const char *l = filename; | 219 const char *l = filename; |
| 217 struct tm time; | 220 struct tm time; |
| 218 GaimLog *log; | 221 GaimLog *log; |
| 219 char d[5]; | 222 char d[5]; |
| 220 | 223 |
| 221 strncpy(d, l, 4); | 224 strncpy(d, l, 4); |
| 222 d[4] = '\0'; | 225 d[4] = '\0'; |
| 223 time.tm_year = atoi(d) - 1900; | 226 time.tm_year = atoi(d) - 1900; |
| 224 l = l + 5; | 227 l = l + 5; |
| 225 | 228 |
| 233 l = l + 3; | 236 l = l + 3; |
| 234 | 237 |
| 235 strncpy(d, l, 2); | 238 strncpy(d, l, 2); |
| 236 time.tm_hour = atoi(d); | 239 time.tm_hour = atoi(d); |
| 237 l = l + 2; | 240 l = l + 2; |
| 238 | 241 |
| 239 strncpy(d, l, 2); | 242 strncpy(d, l, 2); |
| 240 time.tm_min = atoi(d); | 243 time.tm_min = atoi(d); |
| 241 l = l + 2; | 244 l = l + 2; |
| 242 | 245 |
| 243 strncpy(d, l, 2); | 246 strncpy(d, l, 2); |
| 366 | 369 |
| 367 /**************************** | 370 /**************************** |
| 368 ** PLAIN TEXT LOGGER ******* | 371 ** PLAIN TEXT LOGGER ******* |
| 369 ****************************/ | 372 ****************************/ |
| 370 | 373 |
| 371 static void txt_logger_write(GaimLog *log, | 374 static void txt_logger_write(GaimLog *log, |
| 372 GaimMessageFlags type, | 375 GaimMessageFlags type, |
| 373 const char *from, time_t time, const char *message) | 376 const char *from, time_t time, const char *message) |
| 374 { | 377 { |
| 375 char date[64]; | 378 char date[64]; |
| 376 char *stripped = NULL; | 379 char *stripped = NULL; |
| 377 if (!log->logger_data) { | 380 if (!log->logger_data) { |
| 383 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | 386 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
| 384 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | 387 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO |
| 385 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | 388 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); |
| 386 char *dir; | 389 char *dir; |
| 387 FILE *file; | 390 FILE *file; |
| 388 | 391 |
| 389 strftime(date, sizeof(date), "%F.%H%M%S.txt", localtime(&log->time)); | 392 strftime(date, sizeof(date), "%F.%H%M%S.txt", localtime(&log->time)); |
| 390 | 393 |
| 391 dir = g_build_filename(ud, "logs", NULL); | 394 dir = g_build_filename(ud, "logs", NULL); |
| 392 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | 395 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 393 g_free(dir); | 396 g_free(dir); |
| 394 dir = g_build_filename(ud, "logs", | 397 dir = g_build_filename(ud, "logs", |
| 395 prpl, NULL); | 398 prpl, NULL); |
| 396 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | 399 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 397 g_free(dir); | 400 g_free(dir); |
| 398 dir = g_build_filename(ud, "logs", | 401 dir = g_build_filename(ud, "logs", |
| 399 prpl, guy, NULL); | 402 prpl, guy, NULL); |
| 400 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | 403 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 401 g_free(dir); | 404 g_free(dir); |
| 402 dir = g_build_filename(ud, "logs", | 405 dir = g_build_filename(ud, "logs", |
| 403 prpl, guy, gaim_normalize(log->account, log->name), NULL); | 406 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
| 404 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | 407 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 405 | 408 |
| 406 char *filename = g_build_filename(dir, date, NULL); | 409 char *filename = g_build_filename(dir, date, NULL); |
| 407 g_free(dir); | 410 g_free(dir); |
| 408 | 411 |
| 409 file = fopen(dir, "r"); | 412 file = fopen(dir, "r"); |
| 410 if(!file) | 413 if(!file) |
| 411 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | 414 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| 412 else | 415 else |
| 413 fclose(file); | 416 fclose(file); |
| 414 | 417 |
| 415 log->logger_data = fopen(filename, "a"); | 418 log->logger_data = fopen(filename, "a"); |
| 416 if (!log->logger_data) { | 419 if (!log->logger_data) { |
| 417 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | 420 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); |
| 418 return; | 421 return; |
| 419 } | 422 } |
| 420 strftime(date, sizeof(date), "%F %T", localtime(&log->time)); | 423 strftime(date, sizeof(date), "%F %T", localtime(&log->time)); |
| 421 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", | 424 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", |
| 422 log->name, date, gaim_account_get_username(log->account), prpl); | 425 log->name, date, gaim_account_get_username(log->account), prpl); |
| 423 } | 426 } |
| 424 | 427 |
| 425 strftime(date, sizeof(date), "%T", localtime(&time)); | 428 strftime(date, sizeof(date), "%T", localtime(&time)); |
| 426 stripped = gaim_markup_strip_html(message); | 429 stripped = gaim_markup_strip_html(message); |
| 427 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); | 430 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); |
| 428 fflush(log->logger_data); | 431 fflush(log->logger_data); |
| 429 g_free(stripped); | 432 g_free(stripped); |
| 447 return g_strdup("<font color='red'><b>log->logger_data was NULL!</b></font>"); | 450 return g_strdup("<font color='red'><b>log->logger_data was NULL!</b></font>"); |
| 448 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { | 451 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
| 449 minus_header = strchr(read, '\n'); | 452 minus_header = strchr(read, '\n'); |
| 450 if (!minus_header) | 453 if (!minus_header) |
| 451 minus_header = g_strdup(read); | 454 minus_header = g_strdup(read); |
| 452 else | 455 else |
| 453 minus_header = g_strdup(minus_header + 1); | 456 minus_header = g_strdup(minus_header + 1); |
| 454 g_free(read); | 457 g_free(read); |
| 455 return minus_header; | 458 return minus_header; |
| 456 } | 459 } |
| 457 return g_strdup(_("<font color='red'><b>Could not read file: %s</b></font>")); | 460 return g_strdup(_("<font color='red'><b>Could not read file: %s</b></font>")); |
| 458 } | 461 } |
| 459 | 462 |
| 460 static GaimLogLogger txt_logger = { | 463 static GaimLogLogger txt_logger = { |
| 461 N_("Plain text"), "txt", | 464 N_("Plain text"), "txt", |
| 462 NULL, | 465 NULL, |
| 463 txt_logger_write, | 466 txt_logger_write, |
| 478 char *path; | 481 char *path; |
| 479 int offset; | 482 int offset; |
| 480 int length; | 483 int length; |
| 481 }; | 484 }; |
| 482 | 485 |
| 483 static GList *old_logger_list(const char *sn, GaimAccount *account) | 486 static GList *old_logger_list(const char *sn, GaimAccount *account) |
| 484 { | 487 { |
| 485 FILE *file; | 488 FILE *file; |
| 486 char buf[BUF_LONG]; | 489 char buf[BUF_LONG]; |
| 487 struct tm tm; | 490 struct tm tm; |
| 488 struct old_logger_data *data = NULL; | 491 struct old_logger_data *data = NULL; |
| 495 GaimLog *log = NULL; | 498 GaimLog *log = NULL; |
| 496 GList *list = NULL; | 499 GList *list = NULL; |
| 497 | 500 |
| 498 if (!(file = fopen(path, "r"))) | 501 if (!(file = fopen(path, "r"))) |
| 499 return NULL; | 502 return NULL; |
| 500 | 503 |
| 501 while (fgets(buf, BUF_LONG, file)) { | 504 while (fgets(buf, BUF_LONG, file)) { |
| 502 if ((newlog = strstr(buf, "---- New C"))) { | 505 if ((newlog = strstr(buf, "---- New C"))) { |
| 503 int length; | 506 int length; |
| 504 int offset; | 507 int offset; |
| 505 GDate gdate; | 508 GDate gdate; |
| 506 char convostart[32]; | 509 char convostart[32]; |
| 507 char *temp = strchr(buf, '@'); | 510 char *temp = strchr(buf, '@'); |
| 508 | 511 |
| 509 if (temp == NULL || strlen(temp) < 2) | 512 if (temp == NULL || strlen(temp) < 2) |
| 510 continue; | 513 continue; |
| 511 | 514 |
| 512 temp++; | 515 temp++; |
| 513 length = strcspn(temp, "-"); | 516 length = strcspn(temp, "-"); |
| 514 if (length > 31) length = 31; | 517 if (length > 31) length = 31; |
| 515 | 518 |
| 516 offset = ftell(file); | 519 offset = ftell(file); |
| 517 | 520 |
| 518 if (data) { | 521 if (data) { |
| 519 data->length = offset - data->offset - length - | 522 data->length = offset - data->offset - length; |
| 520 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") - | 523 if(strstr(buf, "----</H3><BR>")) { |
| 521 strlen("----</H3><BR>"); | 524 data->length -= |
| 525 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
| 526 strlen("----</H3><BR>"); | |
| 527 } else { | |
| 528 data->length -= | |
| 529 strlen("---- New Conversation @ ") + strlen("----"); | |
| 530 } | |
| 531 | |
| 522 if (data->length != 0) | 532 if (data->length != 0) |
| 523 list = g_list_append(list, log); | 533 list = g_list_append(list, log); |
| 524 else | 534 else |
| 525 gaim_log_free(log); | 535 gaim_log_free(log); |
| 526 } | 536 } |
| 527 | 537 |
| 528 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | 538 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); |
| 529 log->logger = &old_logger; | 539 log->logger = &old_logger; |
| 530 | 540 |
| 531 data = g_malloc(sizeof(struct old_logger_data)); | 541 data = g_new0(struct old_logger_data, 1); |
| 532 data->offset = offset; | 542 data->offset = offset; |
| 533 data->path = path; | 543 data->path = path; |
| 534 log->logger_data = data; | 544 log->logger_data = data; |
| 535 | 545 |
| 536 | 546 |
| 537 g_snprintf(convostart, length, "%s", temp); | 547 g_snprintf(convostart, length, "%s", temp); |
| 538 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | 548 sscanf(convostart, "%*s %s %s %d:%d:%d %s", |
| 539 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | 549 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); |
| 540 date = g_strdup_printf("%s %s %s", month, day, year); | 550 date = g_strdup_printf("%s %s %s", month, day, year); |
| 541 g_date_set_parse(&gdate, date); | 551 g_date_set_parse(&gdate, date); |
| 550 return list; | 560 return list; |
| 551 } | 561 } |
| 552 | 562 |
| 553 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) | 563 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
| 554 { | 564 { |
| 555 *flags = GAIM_LOG_READ_NO_NEWLINE; | |
| 556 struct old_logger_data *data = log->logger_data; | 565 struct old_logger_data *data = log->logger_data; |
| 557 FILE *file = fopen(data->path, "r"); | 566 FILE *file = fopen(data->path, "r"); |
| 558 char *read = g_malloc(data->length + 1); | 567 char *read = g_malloc(data->length + 1); |
| 559 fseek(file, data->offset, SEEK_SET); | 568 fseek(file, data->offset, SEEK_SET); |
| 560 fread(read, data->length, 1, file); | 569 fread(read, data->length, 1, file); |
| 561 read[data->length] = '\0'; | 570 read[data->length] = '\0'; |
| 571 *flags = 0; | |
| 572 if(strstr(read, "<BR>")) | |
| 573 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
| 562 return read; | 574 return read; |
| 563 } | 575 } |
| 564 | 576 |
| 565 static GaimLogLogger old_logger = { | 577 static GaimLogLogger old_logger = { |
| 566 "old logger", "old", | 578 "old logger", "old", |
