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