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