Mercurial > pidgin
annotate src/log.c @ 7489:8f05bdc08faf
[gaim-migrate @ 8102]
This will produce better logs. I'm going to try to get even *better* logs
committer: Tailor Script <tailor@pidgin.im>
| author | Sean Egan <seanegan@gmail.com> |
|---|---|
| date | Thu, 13 Nov 2003 00:48:05 +0000 |
| parents | 3c21f3084ff0 |
| children | 9b537f41bd55 |
| 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); | |
|
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
76 gaim_str_strip_cr(ret); |
| 7462 | 77 return ret; |
| 78 } | |
| 7470 | 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 { | |
| 7489 | 385 GaimConnection *gc = gaim_account_get_connection(log->account); |
| 7457 | 386 char date[64]; |
| 387 if(!log->logger_data) { | |
| 388 /* This log is new */ | |
| 389 char *ud = gaim_user_dir(); | |
| 390 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); | |
| 391 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
| 392 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
| 393 char *dir; | |
| 394 char *filename; | |
| 395 FILE *file; | |
| 396 | |
| 397 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time)); | |
| 398 | |
| 399 dir = g_build_filename(ud, "logs", NULL); | |
| 400 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 401 g_free(dir); | |
| 402 dir = g_build_filename(ud, "logs", | |
| 403 prpl, NULL); | |
| 404 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 405 g_free(dir); | |
| 406 dir = g_build_filename(ud, "logs", | |
| 407 prpl, guy, NULL); | |
| 408 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 409 g_free(dir); | |
| 410 dir = g_build_filename(ud, "logs", | |
| 411 prpl, guy, gaim_normalize(log->account, log->name), NULL); | |
| 412 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 413 g_free(guy); | |
| 414 | |
| 415 filename = g_build_filename(dir, date, NULL); | |
| 416 g_free(dir); | |
| 417 | |
| 418 file = fopen(dir, "r"); | |
| 419 if(!file) | |
| 420 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 421 else | |
| 422 fclose(file); | |
| 423 | |
| 424 log->logger_data = fopen(filename, "a"); | |
| 425 if (!log->logger_data) { | |
| 426 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
| 427 return; | |
| 428 } | |
| 429 g_free(filename); | |
| 430 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); | |
| 431 fprintf(log->logger_data, "<html><head><title>"); | |
| 432 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)", | |
| 433 log->name, date, gaim_account_get_username(log->account), prpl); | |
| 434 fprintf(log->logger_data, "</title></head><body>"); | |
| 435 fprintf(log->logger_data, | |
| 436 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", | |
| 437 log->name, date, gaim_account_get_username(log->account), prpl); | |
| 438 } | |
| 439 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
| 7489 | 440 if (type & GAIM_MESSAGE_SYSTEM) |
| 441 fprintf(log->logger_data, "(%s)<b> %s</b><br/>\n", date, message); | |
| 442 else if (type & GAIM_MESSAGE_WHISPER) | |
| 443 fprintf(log->logger_data, "<font color=\"#6C2585\">(%s)<b> %s:</b></font> %s<br/>\n", | |
| 444 date, from, message); | |
| 445 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
| 446 if (type & GAIM_MESSAGE_SEND) | |
| 447 fprintf(log->logger_data, _("<font color=\"#16569E\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); | |
| 448 else if (type & GAIM_MESSAGE_RECV) | |
| 449 fprintf(log->logger_data, _("<font color=\"#A82F2F\">(%s) <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, message); | |
| 450 } else if (type & GAIM_MESSAGE_RECV) | |
| 451 fprintf(log->logger_data, "<font color=\"#A82F2F\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
| 452 date, from, gc->prpl->info->name, message); | |
| 453 else if (type & GAIM_MESSAGE_RECV) | |
| 454 fprintf(log->logger_data, "<font color=\"#16569E\">(%s) <b>%s:</b></font> <font sml=\"%s\">%s</font><br/>\n", | |
| 455 date, from, gc->prpl->info->name, message); | |
| 7457 | 456 fflush(log->logger_data); |
| 457 } | |
| 458 | |
| 459 static void html_logger_finalize(GaimLog *log) | |
| 460 { | |
| 7463 | 461 if (log->logger_data) { |
| 462 fprintf(log->logger_data, "</body></html>"); | |
| 7457 | 463 fclose(log->logger_data); |
| 7463 | 464 } |
| 7457 | 465 } |
| 466 | |
| 467 static GList *html_logger_list(const char *sn, GaimAccount *account) | |
| 468 { | |
| 469 return log_lister_common(sn, account, ".html", &html_logger); | |
| 470 } | |
| 471 | |
| 472 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
| 473 { | |
| 474 char *read, *minus_header; | |
| 475 *flags = GAIM_LOG_READ_NO_NEWLINE; | |
| 476 if (!log->logger_data) | |
| 7472 | 477 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
| 7457 | 478 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
| 479 minus_header = strchr(read, '\n'); | |
| 480 if (!minus_header) | |
| 481 minus_header = g_strdup(read); | |
| 482 else | |
| 483 minus_header = g_strdup(minus_header + 1); | |
| 484 g_free(read); | |
| 485 return minus_header; | |
| 486 } | |
| 7471 | 487 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
| 7457 | 488 } |
| 489 | |
| 490 static GaimLogLogger html_logger = { | |
| 491 N_("HTML"), "html", | |
| 492 NULL, | |
| 493 html_logger_write, | |
| 494 html_logger_finalize, | |
| 495 html_logger_list, | |
| 496 html_logger_read | |
| 497 }; | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 /**************************** | |
| 7431 | 503 ** PLAIN TEXT LOGGER ******* |
| 504 ****************************/ | |
| 4184 | 505 |
| 7436 | 506 static void txt_logger_write(GaimLog *log, |
| 507 GaimMessageFlags type, | |
| 7431 | 508 const char *from, time_t time, const char *message) |
| 509 { | |
| 510 char date[64]; | |
| 511 char *stripped = NULL; | |
| 512 if (!log->logger_data) { | |
| 513 /* This log is new. We could use the loggers 'new' function, but | |
| 514 * creating a new file there would result in empty files in the case | |
| 515 * that you open a convo with someone, but don't say anything. | |
| 516 */ | |
| 517 char *ud = gaim_user_dir(); | |
| 7473 | 518 char *filename; |
| 7431 | 519 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); |
| 520 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO | |
| 521 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); | |
| 522 char *dir; | |
| 523 FILE *file; | |
| 7436 | 524 |
| 7453 | 525 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); |
| 7436 | 526 |
| 7431 | 527 dir = g_build_filename(ud, "logs", NULL); |
| 528 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 529 g_free(dir); | |
| 7436 | 530 dir = g_build_filename(ud, "logs", |
| 7431 | 531 prpl, NULL); |
| 532 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 533 g_free(dir); | |
| 7436 | 534 dir = g_build_filename(ud, "logs", |
| 7431 | 535 prpl, guy, NULL); |
| 536 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 7436 | 537 g_free(dir); |
| 538 dir = g_build_filename(ud, "logs", | |
| 7431 | 539 prpl, guy, gaim_normalize(log->account, log->name), NULL); |
| 540 mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 7447 | 541 g_free(guy); |
| 7436 | 542 |
| 7473 | 543 filename = g_build_filename(dir, date, NULL); |
| 7431 | 544 g_free(dir); |
| 7436 | 545 |
| 7431 | 546 file = fopen(dir, "r"); |
| 547 if(!file) | |
| 548 mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 549 else | |
| 550 fclose(file); | |
| 7436 | 551 |
| 7431 | 552 log->logger_data = fopen(filename, "a"); |
| 553 if (!log->logger_data) { | |
| 554 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
| 555 return; | |
| 4184 | 556 } |
| 7447 | 557 g_free(filename); |
| 7453 | 558 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
| 7431 | 559 fprintf(log->logger_data, "Conversation with %s at %s on %s (%s)\n", |
| 560 log->name, date, gaim_account_get_username(log->account), prpl); | |
| 561 } | |
| 7436 | 562 |
| 7453 | 563 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
| 7431 | 564 stripped = gaim_markup_strip_html(message); |
| 7489 | 565 if (type & GAIM_MESSAGE_SEND || |
| 566 type & GAIM_MESSAGE_RECV) | |
| 567 fprintf(log->logger_data, "(%s) %s: %s\n", date, from, stripped); | |
| 568 else if (type & GAIM_MESSAGE_SYSTEM) | |
| 569 fprintf(log->logger_data, "(%s) %s\n", date, stripped); | |
| 570 else if (type & GAIM_MESSAGE_AUTO_RESP) | |
| 571 fprintf(log->logger_data, _("(%s) %s <AUTO-REPLY>: %s\n"), date, from, stripped); | |
| 572 else if (type & GAIM_MESSAGE_NO_LOG) { | |
| 573 /* This shouldn't happen */ | |
| 574 g_free(stripped); | |
| 575 return; | |
| 576 } else if (type & GAIM_MESSAGE_WHISPER) | |
| 577 fprintf(log->logger_data, "(%s) *%s* %s", date, from, stripped); | |
| 578 else | |
| 579 fprintf(log->logger_data, "(%s) %s%s %s\n", date, from ? from : "", from ? ":" : "", stripped); | |
| 580 | |
| 7431 | 581 fflush(log->logger_data); |
| 582 g_free(stripped); | |
| 583 } | |
| 584 | |
| 585 static void txt_logger_finalize(GaimLog *log) | |
| 586 { | |
| 587 if (log->logger_data) | |
| 588 fclose(log->logger_data); | |
| 589 } | |
| 590 | |
| 591 static GList *txt_logger_list(const char *sn, GaimAccount *account) | |
| 592 { | |
| 593 return log_lister_common(sn, account, ".txt", &txt_logger); | |
| 594 } | |
| 595 | |
| 596 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) | |
| 597 { | |
| 598 char *read, *minus_header; | |
| 7457 | 599 *flags = 0; |
| 7431 | 600 if (!log->logger_data) |
| 7472 | 601 return g_strdup(_("<font color=\"red\"><b>log->logger_data was NULL!</b></font>")); |
| 7431 | 602 if (g_file_get_contents((char *)log->logger_data, &read, NULL, NULL)) { |
| 603 minus_header = strchr(read, '\n'); | |
| 604 if (!minus_header) | |
| 605 minus_header = g_strdup(read); | |
| 7436 | 606 else |
| 7431 | 607 minus_header = g_strdup(minus_header + 1); |
| 608 g_free(read); | |
| 609 return minus_header; | |
| 610 } | |
| 7471 | 611 return g_strdup(_("<font color=\"red\"><b>Could not read file: %s</b></font>")); |
| 7436 | 612 } |
| 7431 | 613 |
| 614 static GaimLogLogger txt_logger = { | |
| 615 N_("Plain text"), "txt", | |
| 616 NULL, | |
| 617 txt_logger_write, | |
| 618 txt_logger_finalize, | |
| 619 txt_logger_list, | |
| 620 txt_logger_read | |
| 621 }; | |
| 622 | |
| 623 /**************** | |
| 624 * OLD LOGGER *** | |
| 625 ****************/ | |
| 626 | |
| 627 /* The old logger doesn't write logs, only reads them. This is to include | |
| 628 * old logs in the log viewer transparently. | |
| 629 */ | |
| 630 | |
| 631 struct old_logger_data { | |
| 632 char *path; | |
| 633 int offset; | |
| 634 int length; | |
| 635 }; | |
| 636 | |
| 7436 | 637 static GList *old_logger_list(const char *sn, GaimAccount *account) |
| 7431 | 638 { |
| 639 FILE *file; | |
| 640 char buf[BUF_LONG]; | |
| 641 struct tm tm; | |
| 642 struct old_logger_data *data = NULL; | |
| 643 char day[4], month[4], year[5]; | |
| 644 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
| 645 char *date; | |
| 646 char *path = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
| 647 char *newlog; | |
| 648 | |
| 649 GaimLog *log = NULL; | |
| 650 GList *list = NULL; | |
| 651 | |
| 7473 | 652 g_free(logfile); |
| 653 | |
| 7461 | 654 if (!(file = fopen(path, "rb"))) { |
| 7447 | 655 g_free(path); |
| 7431 | 656 return NULL; |
| 7447 | 657 } |
| 7436 | 658 |
| 7431 | 659 while (fgets(buf, BUF_LONG, file)) { |
| 660 if ((newlog = strstr(buf, "---- New C"))) { | |
| 661 int length; | |
| 662 int offset; | |
| 663 GDate gdate; | |
| 664 char convostart[32]; | |
| 665 char *temp = strchr(buf, '@'); | |
| 7436 | 666 |
| 7431 | 667 if (temp == NULL || strlen(temp) < 2) |
| 668 continue; | |
| 7436 | 669 |
| 7431 | 670 temp++; |
| 671 length = strcspn(temp, "-"); | |
| 672 if (length > 31) length = 31; | |
| 7436 | 673 |
| 7431 | 674 offset = ftell(file); |
| 7436 | 675 |
| 7431 | 676 if (data) { |
| 7436 | 677 data->length = offset - data->offset - length; |
| 678 if(strstr(buf, "----</H3><BR>")) { | |
| 679 data->length -= | |
| 680 strlen("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
| 681 strlen("----</H3><BR>"); | |
| 682 } else { | |
| 683 data->length -= | |
| 684 strlen("---- New Conversation @ ") + strlen("----"); | |
| 685 } | |
| 686 | |
| 7461 | 687 if(strchr(buf, '\r')) |
| 688 data->length--; | |
| 689 | |
| 7431 | 690 if (data->length != 0) |
| 691 list = g_list_append(list, log); | |
| 692 else | |
| 693 gaim_log_free(log); | |
| 694 } | |
| 695 | |
| 696 log = gaim_log_new(GAIM_LOG_IM, sn, account, -1); | |
| 697 log->logger = &old_logger; | |
| 698 | |
| 7436 | 699 data = g_new0(struct old_logger_data, 1); |
| 7431 | 700 data->offset = offset; |
| 701 data->path = path; | |
| 702 log->logger_data = data; | |
| 703 | |
| 7436 | 704 |
| 7431 | 705 g_snprintf(convostart, length, "%s", temp); |
| 706 sscanf(convostart, "%*s %s %s %d:%d:%d %s", | |
| 707 month, day, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, year); | |
| 708 date = g_strdup_printf("%s %s %s", month, day, year); | |
| 709 g_date_set_parse(&gdate, date); | |
| 710 tm.tm_mday = g_date_get_day(&gdate); | |
| 711 tm.tm_mon = g_date_get_month(&gdate) - 1; | |
| 712 tm.tm_year = g_date_get_year(&gdate) - 1900; | |
| 713 log->time = mktime(&tm); | |
| 714 | |
| 4184 | 715 } |
| 716 } | |
| 7431 | 717 fclose(file); |
| 718 return list; | |
| 4184 | 719 } |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
720 |
| 7431 | 721 char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
722 { |
| 7431 | 723 struct old_logger_data *data = log->logger_data; |
| 7461 | 724 FILE *file = fopen(data->path, "rb"); |
| 7431 | 725 char *read = g_malloc(data->length + 1); |
| 726 fseek(file, data->offset, SEEK_SET); | |
| 727 fread(read, data->length, 1, file); | |
| 728 read[data->length] = '\0'; | |
| 7436 | 729 *flags = 0; |
| 730 if(strstr(read, "<BR>")) | |
| 731 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
| 7431 | 732 return read; |
| 733 } | |
|
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
734 |
| 7431 | 735 static GaimLogLogger old_logger = { |
| 736 "old logger", "old", | |
| 737 NULL, NULL, NULL, | |
| 738 old_logger_list, | |
| 739 old_logger_read | |
| 740 }; |
