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