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