Mercurial > pidgin.yaz
annotate src/log.c @ 11415:66fe476474ec
[gaim-migrate @ 13652]
update this slightly
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Thu, 01 Sep 2005 20:48:29 +0000 |
parents | ef9280fdc511 |
children | 4db38b374d3f |
rev | line source |
---|---|
7431 | 1 /** |
2 * @file log.c Logging API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
7436 | 10 * |
7431 | 11 * This program is free software; you can redistribute it and/or modify |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
4184 | 24 */ |
4195 | 25 |
7431 | 26 #include "account.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
27 #include "debug.h" |
7431 | 28 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
29 #include "log.h" |
5548 | 30 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
31 #include "util.h" |
7764 | 32 #include "stringref.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
33 |
8096 | 34 static GSList *loggers = NULL; |
35 | |
7457 | 36 static GaimLogLogger html_logger; |
7431 | 37 static GaimLogLogger txt_logger; |
38 static GaimLogLogger old_logger; | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5839
diff
changeset
|
39 |
8635 | 40 struct _gaim_logsize_user { |
41 char *name; | |
42 GaimAccount *account; | |
43 }; | |
44 static GHashTable *logsize_users = NULL; | |
45 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
46 static void log_get_log_sets_common(GHashTable *sets); |
8635 | 47 |
7431 | 48 /************************************************************************** |
49 * PUBLIC LOGGING FUNCTIONS *********************************************** | |
50 **************************************************************************/ | |
4184 | 51 |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
52 GaimLog *gaim_log_new(GaimLogType type, const char *name, GaimAccount *account, |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
53 GaimConversation *conv, time_t time) |
7431 | 54 { |
55 GaimLog *log = g_new0(GaimLog, 1); | |
8635 | 56 log->name = g_strdup(gaim_normalize(account, name)); |
7431 | 57 log->account = account; |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
58 log->conv = conv; |
7431 | 59 log->time = time; |
8573 | 60 log->type = type; |
8096 | 61 log->logger_data = NULL; |
7431 | 62 log->logger = gaim_log_logger_get(); |
7440 | 63 if (log->logger && log->logger->create) |
64 log->logger->create(log); | |
7431 | 65 return log; |
4184 | 66 } |
67 | |
7431 | 68 void gaim_log_free(GaimLog *log) |
4184 | 69 { |
7431 | 70 g_return_if_fail(log); |
71 if (log->logger && log->logger->finalize) | |
72 log->logger->finalize(log); | |
73 g_free(log->name); | |
74 g_free(log); | |
75 } | |
7436 | 76 |
77 void gaim_log_write(GaimLog *log, GaimMessageFlags type, | |
7431 | 78 const char *from, time_t time, const char *message) |
79 { | |
10173 | 80 struct _gaim_logsize_user *lu; |
81 | |
7431 | 82 g_return_if_fail(log); |
83 g_return_if_fail(log->logger); | |
7442 | 84 g_return_if_fail(log->logger->write); |
7431 | 85 |
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
86 (log->logger->write)(log, type, from, time, message); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
87 |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
88 lu = g_new(struct _gaim_logsize_user, 1); |
9892 | 89 |
10171
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
90 lu->name = g_strdup(gaim_normalize(log->account, log->name)); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
91 lu->account = log->account; |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
92 g_hash_table_remove(logsize_users, lu); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
93 g_free(lu->name); |
829a569993e0
[gaim-migrate @ 11263]
Luke Schierer <lschiere@pidgin.im>
parents:
10087
diff
changeset
|
94 g_free(lu); |
9892 | 95 |
4184 | 96 } |
97 | |
7431 | 98 char *gaim_log_read(GaimLog *log, GaimLogReadFlags *flags) |
4184 | 99 { |
7542 | 100 GaimLogReadFlags mflags; |
7431 | 101 g_return_val_if_fail(log && log->logger, NULL); |
7462 | 102 if (log->logger->read) { |
7535 | 103 char *ret = (log->logger->read)(log, flags ? flags : &mflags); |
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7473
diff
changeset
|
104 gaim_str_strip_cr(ret); |
7462 | 105 return ret; |
106 } | |
7470 | 107 return (_("<b><font color=\"red\">The logger has no read function</font></b>")); |
4184 | 108 } |
7616 | 109 |
7556 | 110 int gaim_log_get_size(GaimLog *log) |
111 { | |
112 g_return_val_if_fail(log && log->logger, 0); | |
8096 | 113 |
7556 | 114 if (log->logger->size) |
115 return log->logger->size(log); | |
116 return 0; | |
117 } | |
118 | |
8635 | 119 static guint _gaim_logsize_user_hash(struct _gaim_logsize_user *lu) |
120 { | |
121 return g_str_hash(lu->name); | |
122 } | |
123 | |
124 static guint _gaim_logsize_user_equal(struct _gaim_logsize_user *lu1, | |
125 struct _gaim_logsize_user *lu2) | |
126 { | |
127 return ((!strcmp(lu1->name, lu2->name)) && lu1->account == lu2->account); | |
128 } | |
129 | |
130 static void _gaim_logsize_user_free_key(struct _gaim_logsize_user *lu) | |
131 { | |
132 g_free(lu->name); | |
133 g_free(lu); | |
134 } | |
135 | |
8898 | 136 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account) |
7556 | 137 { |
9677 | 138 gpointer ptrsize; |
139 int size = 0; | |
8096 | 140 GSList *n; |
8635 | 141 struct _gaim_logsize_user *lu; |
8096 | 142 |
8635 | 143 lu = g_new(struct _gaim_logsize_user, 1); |
144 lu->name = g_strdup(gaim_normalize(account, name)); | |
145 lu->account = account; | |
146 | |
9677 | 147 if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) { |
148 size = GPOINTER_TO_INT(ptrsize); | |
8635 | 149 g_free(lu->name); |
150 g_free(lu); | |
151 } else { | |
152 for (n = loggers; n; n = n->next) { | |
153 GaimLogLogger *logger = n->data; | |
7616 | 154 |
8635 | 155 if(logger->total_size){ |
8898 | 156 size += (logger->total_size)(type, name, account); |
8635 | 157 } else if(logger->list) { |
8898 | 158 GList *logs = (logger->list)(type, name, account); |
8635 | 159 int this_size = 0; |
160 | |
161 while (logs) { | |
162 GList *logs2 = logs->next; | |
163 GaimLog *log = (GaimLog*)(logs->data); | |
164 this_size += gaim_log_get_size(log); | |
165 gaim_log_free(log); | |
166 g_list_free_1(logs); | |
167 logs = logs2; | |
168 } | |
169 | |
170 size += this_size; | |
8096 | 171 } |
8635 | 172 } |
8096 | 173 |
8635 | 174 g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size)); |
7556 | 175 } |
176 return size; | |
177 } | |
4184 | 178 |
10822 | 179 char * |
10577 | 180 gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account) |
181 { | |
182 GaimPlugin *prpl; | |
183 GaimPluginProtocolInfo *prpl_info; | |
184 const char *prpl_name; | |
185 char *acct_name; | |
9926 | 186 const char *target; |
10577 | 187 char *dir; |
9926 | 188 |
10577 | 189 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); |
190 if (!prpl) | |
191 return NULL; | |
192 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
193 prpl_name = prpl_info->list_icon(account, NULL); | |
194 | |
195 acct_name = g_strdup(gaim_escape_filename(gaim_normalize(account, | |
196 gaim_account_get_username(account)))); | |
9923 | 197 |
198 if (type == GAIM_LOG_CHAT) { | |
199 char *temp = g_strdup_printf("%s.chat", gaim_normalize(account, name)); | |
9926 | 200 target = gaim_escape_filename(temp); |
9923 | 201 g_free(temp); |
202 } else if(type == GAIM_LOG_SYSTEM) { | |
9926 | 203 target = ".system"; |
9923 | 204 } else { |
9926 | 205 target = gaim_escape_filename(gaim_normalize(account, name)); |
9923 | 206 } |
207 | |
10577 | 208 dir = g_build_filename(gaim_user_dir(), "logs", prpl_name, acct_name, target, NULL); |
9926 | 209 |
9923 | 210 g_free(acct_name); |
10577 | 211 |
9923 | 212 return dir; |
213 } | |
214 | |
7431 | 215 /**************************************************************************** |
216 * LOGGER FUNCTIONS ********************************************************* | |
217 ****************************************************************************/ | |
4184 | 218 |
7431 | 219 static GaimLogLogger *current_logger = NULL; |
7436 | 220 |
7431 | 221 static void logger_pref_cb(const char *name, GaimPrefType type, |
222 gpointer value, gpointer data) | |
223 { | |
224 GaimLogLogger *logger; | |
225 GSList *l = loggers; | |
226 while (l) { | |
227 logger = l->data; | |
228 if (!strcmp(logger->id, value)) { | |
229 gaim_log_logger_set(logger); | |
230 return; | |
4184 | 231 } |
7431 | 232 l = l->next; |
233 } | |
234 gaim_log_logger_set(&txt_logger); | |
235 } | |
4184 | 236 |
237 | |
8898 | 238 GaimLogLogger *gaim_log_logger_new( |
239 void(*create)(GaimLog *), | |
240 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *), | |
241 void(*finalize)(GaimLog *), | |
242 GList*(*list)(GaimLogType type, const char*, GaimAccount*), | |
243 char*(*read)(GaimLog*, GaimLogReadFlags*), | |
11025 | 244 int(*size)(GaimLog*), |
245 int(*total_size)(GaimLogType type, const char *name, GaimAccount *account), | |
246 GList*(*list_syslog)(GaimAccount *account), | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
247 void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets)) |
7431 | 248 { |
249 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); | |
11025 | 250 |
7440 | 251 logger->create = create; |
7431 | 252 logger->write = write; |
253 logger->finalize = finalize; | |
254 logger->list = list; | |
255 logger->read = read; | |
7556 | 256 logger->size = size; |
11025 | 257 logger->total_size = total_size; |
258 logger->list_syslog = list_syslog; | |
259 logger->get_log_sets = get_log_sets; | |
260 | |
7431 | 261 return logger; |
4184 | 262 } |
263 | |
7431 | 264 void gaim_log_logger_free(GaimLogLogger *logger) |
4184 | 265 { |
7431 | 266 g_free(logger); |
267 } | |
4184 | 268 |
7431 | 269 void gaim_log_logger_add (GaimLogLogger *logger) |
270 { | |
271 g_return_if_fail(logger); | |
272 if (g_slist_find(loggers, logger)) | |
273 return; | |
274 loggers = g_slist_append(loggers, logger); | |
275 } | |
276 | |
277 void gaim_log_logger_remove (GaimLogLogger *logger) | |
278 { | |
279 g_return_if_fail(logger); | |
280 g_slist_remove(loggers, logger); | |
4184 | 281 } |
282 | |
7431 | 283 void gaim_log_logger_set (GaimLogLogger *logger) |
4184 | 284 { |
7431 | 285 g_return_if_fail(logger); |
286 current_logger = logger; | |
7436 | 287 } |
4184 | 288 |
7431 | 289 GaimLogLogger *gaim_log_logger_get() |
290 { | |
291 return current_logger; | |
292 } | |
4184 | 293 |
7431 | 294 GList *gaim_log_logger_get_options(void) |
295 { | |
296 GSList *n; | |
297 GList *list = NULL; | |
298 GaimLogLogger *data; | |
4184 | 299 |
7431 | 300 for (n = loggers; n; n = n->next) { |
301 data = n->data; | |
302 if (!data->write) | |
303 continue; | |
7494 | 304 list = g_list_append(list, _(data->name)); |
7431 | 305 list = g_list_append(list, data->id); |
4184 | 306 } |
307 | |
7431 | 308 return list; |
309 } | |
310 | |
8573 | 311 gint gaim_log_compare(gconstpointer y, gconstpointer z) |
7431 | 312 { |
7436 | 313 const GaimLog *a = y; |
314 const GaimLog *b = z; | |
315 | |
7431 | 316 return b->time - a->time; |
317 } | |
318 | |
8898 | 319 GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account) |
7431 | 320 { |
321 GList *logs = NULL; | |
322 GSList *n; | |
323 for (n = loggers; n; n = n->next) { | |
324 GaimLogLogger *logger = n->data; | |
325 if (!logger->list) | |
326 continue; | |
8898 | 327 logs = g_list_concat(logs, logger->list(type, name, account)); |
7431 | 328 } |
7436 | 329 |
8573 | 330 return g_list_sort(logs, gaim_log_compare); |
331 } | |
332 | |
11025 | 333 gint gaim_log_set_compare(gconstpointer y, gconstpointer z) |
334 { | |
335 const GaimLogSet *a = y; | |
336 const GaimLogSet *b = z; | |
337 gint ret = 0; | |
338 | |
339 /* This logic seems weird at first... | |
340 * If either account is NULL, we pretend the accounts are | |
341 * equal. This allows us to detect duplicates that will | |
342 * exist if one logger knows the account and another | |
343 * doesn't. */ | |
344 if (a->account != NULL && b->account != NULL) { | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
345 ret = strcmp(gaim_account_get_username(a->account), gaim_account_get_username(b->account)); |
11025 | 346 if (ret != 0) |
347 return ret; | |
348 } | |
349 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
350 ret = strcmp(a->normalized_name, b->normalized_name); |
11025 | 351 if (ret != 0) |
352 return ret; | |
353 | |
354 return (gint)b->type - (gint)a->type; | |
355 } | |
356 | |
357 guint log_set_hash(gconstpointer key) | |
358 { | |
359 const GaimLogSet *set = key; | |
360 | |
361 /* The account isn't hashed because we need GaimLogSets with NULL accounts | |
362 * to be found when we search by a GaimLogSet that has a non-NULL account | |
363 * but the same type and name. */ | |
364 return g_int_hash((gint *)&set->type) + g_str_hash(set->name); | |
365 } | |
366 | |
367 gboolean log_set_equal(gconstpointer a, gconstpointer b) | |
368 { | |
369 /* I realize that the choices made for GList and GHashTable | |
370 * make sense for those data types, but I wish the comparison | |
371 * functions were compatible. */ | |
372 return !gaim_log_set_compare(a, b); | |
373 } | |
374 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
375 void log_add_log_set_to_hash(GHashTable *sets, GaimLogSet *set) |
11025 | 376 { |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
377 GaimLogSet *existing_set = g_hash_table_lookup(sets, set); |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
378 |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
379 if (existing_set == NULL) |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
380 g_hash_table_insert(sets, set, set); |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
381 else if (existing_set->account == NULL && set->account != NULL) |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
382 g_hash_table_replace(sets, set, set); |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
383 else |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
384 gaim_log_set_free(set); |
11025 | 385 } |
386 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
387 GHashTable *gaim_log_get_log_sets(void) |
11025 | 388 { |
389 GSList *n; | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
390 GHashTable *sets = g_hash_table_new_full(log_set_hash, log_set_equal, |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
391 (GDestroyNotify)gaim_log_set_free, NULL); |
11025 | 392 |
393 /* Get the log sets from all the loggers. */ | |
394 for (n = loggers; n; n = n->next) { | |
395 GaimLogLogger *logger = n->data; | |
396 | |
397 if (!logger->get_log_sets) | |
398 continue; | |
399 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
400 logger->get_log_sets(log_add_log_set_to_hash, sets); |
11025 | 401 } |
402 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
403 log_get_log_sets_common(sets); |
11025 | 404 |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
405 /* Return the GHashTable of unique GaimLogSets. */ |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
406 return sets; |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
407 } |
11025 | 408 |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
409 void gaim_log_set_free(GaimLogSet *set) |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
410 { |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
411 g_return_if_fail(set != NULL); |
11025 | 412 |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
413 g_free(set->name); |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
414 if (set->normalized_name != set->name) |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
415 g_free(set->normalized_name); |
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
416 g_free(set); |
11025 | 417 } |
418 | |
8573 | 419 GList *gaim_log_get_system_logs(GaimAccount *account) |
420 { | |
421 GList *logs = NULL; | |
422 GSList *n; | |
423 for (n = loggers; n; n = n->next) { | |
424 GaimLogLogger *logger = n->data; | |
425 if (!logger->list_syslog) | |
426 continue; | |
427 logs = g_list_concat(logs, logger->list_syslog(account)); | |
428 } | |
429 | |
430 return g_list_sort(logs, gaim_log_compare); | |
7431 | 431 } |
432 | |
433 void gaim_log_init(void) | |
7436 | 434 { |
7431 | 435 gaim_prefs_add_none("/core/logging"); |
7555 | 436 gaim_prefs_add_bool("/core/logging/log_ims", FALSE); |
437 gaim_prefs_add_bool("/core/logging/log_chats", FALSE); | |
8573 | 438 gaim_prefs_add_bool("/core/logging/log_system", FALSE); |
439 gaim_prefs_add_bool("/core/logging/log_signon_signoff", FALSE); | |
440 gaim_prefs_add_bool("/core/logging/log_idle_state", FALSE); | |
441 gaim_prefs_add_bool("/core/logging/log_away_state", FALSE); | |
442 gaim_prefs_add_bool("/core/logging/log_own_states", FALSE); | |
443 | |
7431 | 444 gaim_prefs_add_string("/core/logging/format", "txt"); |
7457 | 445 gaim_log_logger_add(&html_logger); |
7431 | 446 gaim_log_logger_add(&txt_logger); |
447 gaim_log_logger_add(&old_logger); | |
10087 | 448 gaim_prefs_connect_callback(NULL, "/core/logging/format", |
7431 | 449 logger_pref_cb, NULL); |
450 gaim_prefs_trigger_callback("/core/logging/format"); | |
8635 | 451 |
452 logsize_users = g_hash_table_new_full((GHashFunc)_gaim_logsize_user_hash, | |
453 (GEqualFunc)_gaim_logsize_user_equal, | |
454 (GDestroyNotify)_gaim_logsize_user_free_key, NULL); | |
7431 | 455 } |
456 | |
457 /**************************************************************************** | |
458 * LOGGERS ****************************************************************** | |
459 ****************************************************************************/ | |
460 | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
461 void gaim_log_common_writer(GaimLog *log, const char *ext) |
9763 | 462 { |
463 char date[64]; | |
10822 | 464 GaimLogCommonLoggerData *data = log->logger_data; |
9763 | 465 |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
466 if (data == NULL) |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
467 { |
9763 | 468 /* This log is new */ |
9923 | 469 char *dir, *filename, *path; |
10577 | 470 |
9923 | 471 dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
10577 | 472 if (dir == NULL) |
473 return; | |
474 | |
9923 | 475 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
9763 | 476 |
477 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S", localtime(&log->time)); | |
478 | |
479 filename = g_strdup_printf("%s%s", date, ext ? ext : ""); | |
480 | |
481 path = g_build_filename(dir, filename, NULL); | |
482 g_free(dir); | |
483 g_free(filename); | |
484 | |
10822 | 485 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
9763 | 486 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
487 data->file = g_fopen(path, "a"); |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
488 if (data->file == NULL) |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
489 { |
9763 | 490 gaim_debug(GAIM_DEBUG_ERROR, "log", |
9892 | 491 "Could not create log file %s\n", path); |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
492 |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
493 if (log->conv != NULL) |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
494 gaim_conversation_write(log->conv, NULL, _("Logging of this conversation failed."), |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
495 GAIM_MESSAGE_ERROR, time(NULL)); |
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
496 |
9763 | 497 g_free(path); |
498 return; | |
499 } | |
500 g_free(path); | |
10577 | 501 } |
9763 | 502 } |
503 | |
10822 | 504 GList *gaim_log_common_lister(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger) |
7431 | 505 { |
506 GDir *dir; | |
507 GList *list = NULL; | |
7628 | 508 const char *filename; |
8111 | 509 char *path; |
510 | |
511 if(!account) | |
512 return NULL; | |
513 | |
9923 | 514 path = gaim_log_get_log_dir(type, name, account); |
10577 | 515 if (path == NULL) |
516 return NULL; | |
7447 | 517 |
7431 | 518 if (!(dir = g_dir_open(path, 0, NULL))) { |
519 g_free(path); | |
520 return NULL; | |
521 } | |
8898 | 522 |
7431 | 523 while ((filename = g_dir_read_name(dir))) { |
8577 | 524 if (gaim_str_has_suffix(filename, ext) && |
525 strlen(filename) == 17 + strlen(ext)) { | |
7431 | 526 GaimLog *log; |
10822 | 527 GaimLogCommonLoggerData *data; |
8577 | 528 time_t stamp = gaim_str_to_time(filename, FALSE); |
7431 | 529 |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
530 log = gaim_log_new(type, name, account, NULL, stamp); |
7431 | 531 log->logger = logger; |
10822 | 532 log->logger_data = data = g_new0(GaimLogCommonLoggerData, 1); |
7616 | 533 data->path = g_build_filename(path, filename, NULL); |
7431 | 534 list = g_list_append(list, log); |
4184 | 535 } |
536 } | |
7431 | 537 g_dir_close(dir); |
7447 | 538 g_free(path); |
7431 | 539 return list; |
540 } | |
4184 | 541 |
10822 | 542 int gaim_log_common_sizer(GaimLog *log) |
7556 | 543 { |
544 struct stat st; | |
10822 | 545 GaimLogCommonLoggerData *data = log->logger_data; |
7556 | 546 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
547 if (!data->path || g_stat(data->path, &st)) |
7556 | 548 st.st_size = 0; |
549 | |
550 return st.st_size; | |
551 } | |
552 | |
11025 | 553 /* This will build log sets for all loggers that use the common logger |
554 * functions because they use the same directory structure. */ | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
555 static void log_get_log_sets_common(GHashTable *sets) |
11025 | 556 { |
557 gchar *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
558 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
559 const gchar *protocol; | |
560 | |
561 if (log_dir == NULL) { | |
562 g_free(log_path); | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
563 return; |
11025 | 564 } |
565 | |
566 while ((protocol = g_dir_read_name(log_dir)) != NULL) { | |
567 gchar *protocol_path = g_build_filename(log_path, protocol, NULL); | |
568 GDir *protocol_dir; | |
569 const gchar *username; | |
570 gchar *protocol_unescaped; | |
571 GList *account_iter; | |
572 GList *accounts = NULL; | |
573 | |
574 if ((protocol_dir = g_dir_open(protocol_path, 0, NULL)) == NULL) { | |
575 g_free(protocol_path); | |
576 continue; | |
577 } | |
578 | |
579 /* Using g_strdup() to cover the one-in-a-million chance that a | |
580 * prpl's list_icon function uses gaim_unescape_filename(). */ | |
581 protocol_unescaped = g_strdup(gaim_unescape_filename(protocol)); | |
582 | |
583 /* Find all the accounts for protocol. */ | |
584 for (account_iter = gaim_accounts_get_all() ; account_iter != NULL ; account_iter = account_iter->next) { | |
585 GaimPlugin *prpl; | |
586 GaimPluginProtocolInfo *prpl_info; | |
587 | |
588 prpl = gaim_find_prpl(gaim_account_get_protocol_id((GaimAccount *)account_iter->data)); | |
589 if (!prpl) | |
590 continue; | |
591 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
592 | |
593 if (!strcmp(protocol_unescaped, prpl_info->list_icon((GaimAccount *)account_iter->data, NULL))) | |
594 accounts = g_list_append(accounts, account_iter->data); | |
595 } | |
596 g_free(protocol_unescaped); | |
597 | |
598 while ((username = g_dir_read_name(protocol_dir)) != NULL) { | |
599 gchar *username_path = g_build_filename(protocol_path, username, NULL); | |
600 GDir *username_dir; | |
601 const gchar *username_unescaped; | |
602 GaimAccount *account = NULL; | |
603 gchar *name; | |
604 | |
605 if ((username_dir = g_dir_open(username_path, 0, NULL)) == NULL) { | |
606 g_free(username_path); | |
607 continue; | |
608 } | |
609 | |
610 /* Find the account for username in the list of accounts for protocol. */ | |
611 username_unescaped = gaim_unescape_filename(username); | |
612 for (account_iter = g_list_first(accounts) ; account_iter != NULL ; account_iter = account_iter->next) { | |
613 if (!strcmp(((GaimAccount *)account_iter->data)->username, username_unescaped)) { | |
614 account = account_iter->data; | |
615 break; | |
616 } | |
617 } | |
618 | |
619 /* Don't worry about the cast, name will point to dynamically allocated memory shortly. */ | |
620 while ((name = (gchar *)g_dir_read_name(username_dir)) != NULL) { | |
621 size_t len; | |
622 GaimLogSet *set = g_new0(GaimLogSet, 1); | |
623 | |
624 /* Unescape the filename. */ | |
625 name = g_strdup(gaim_unescape_filename(name)); | |
626 | |
627 /* Get the (possibly new) length of name. */ | |
628 len = strlen(name); | |
629 | |
630 set->account = account; | |
631 set->name = name; | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
632 set->normalized_name = g_strdup(gaim_normalize(account, name)); |
11025 | 633 |
634 /* Chat for .chat or .system at the end of the name to determine the type. */ | |
635 set->type = GAIM_LOG_IM; | |
636 if (len > 7) { | |
637 gchar *tmp = &name[len - 7]; | |
638 if (!strcmp(tmp, ".system")) { | |
639 set->type = GAIM_LOG_SYSTEM; | |
640 *tmp = '\0'; | |
641 } | |
642 } | |
643 if (len > 5) { | |
644 gchar *tmp = &name[len - 5]; | |
645 if (!strcmp(tmp, ".chat")) { | |
646 set->type = GAIM_LOG_CHAT; | |
647 *tmp = '\0'; | |
648 } | |
649 } | |
650 | |
651 /* Determine if this (account, name) combination exists as a buddy. */ | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
652 set->buddy = (gaim_find_buddy(account, name) != NULL); |
11025 | 653 |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
654 log_add_log_set_to_hash(sets, set); |
11025 | 655 } |
656 g_free(username_path); | |
657 g_dir_close(username_dir); | |
658 } | |
659 g_free(protocol_path); | |
660 g_dir_close(protocol_dir); | |
661 } | |
662 g_free(log_path); | |
663 g_dir_close(log_dir); | |
664 } | |
665 | |
7431 | 666 #if 0 /* Maybe some other time. */ |
7443 | 667 /**************** |
7431 | 668 ** XML LOGGER ** |
669 ****************/ | |
670 | |
671 static const char *str_from_msg_type (GaimMessageFlags type) | |
672 { | |
7443 | 673 |
7431 | 674 return ""; |
7443 | 675 |
7431 | 676 } |
677 | |
7443 | 678 static void xml_logger_write(GaimLog *log, |
679 GaimMessageFlags type, | |
7431 | 680 const char *from, time_t time, const char *message) |
681 { | |
682 char date[64]; | |
683 char *xhtml = NULL; | |
10577 | 684 |
7431 | 685 if (!log->logger_data) { |
686 /* This log is new. We could use the loggers 'new' function, but | |
687 * creating a new file there would result in empty files in the case | |
688 * that you open a convo with someone, but don't say anything. | |
689 */ | |
9923 | 690 char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
7431 | 691 FILE *file; |
10577 | 692 |
693 if (dir == NULL) | |
694 return; | |
695 | |
7453 | 696 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 697 |
7612 | 698 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7443 | 699 |
7431 | 700 char *filename = g_build_filename(dir, date, NULL); |
701 g_free(dir); | |
7443 | 702 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
703 log->logger_data = g_fopen(filename, "a"); |
7431 | 704 if (!log->logger_data) { |
705 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 706 g_free(filename); |
7431 | 707 return; |
708 } | |
7564 | 709 g_free(filename); |
7431 | 710 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
711 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 712 |
7453 | 713 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 714 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
715 date, log->name, prpl); | |
716 } | |
7443 | 717 |
9923 | 718 /* if we can't write to the file, give up before we hurt ourselves */ |
719 if(!data->file) | |
720 return; | |
721 | |
7453 | 722 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 723 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
724 if (from) | |
7443 | 725 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
726 str_from_msg_type(type), | |
7431 | 727 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
728 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
729 from, date, xhtml); | |
730 else | |
7443 | 731 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
732 str_from_msg_type(type), | |
7431 | 733 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
734 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 735 date, xhtml): |
7431 | 736 fflush(log->logger_data); |
737 g_free(xhtml); | |
7443 | 738 } |
739 | |
7431 | 740 static void xml_logger_finalize(GaimLog *log) |
741 { | |
742 if (log->logger_data) { | |
743 fprintf(log->logger_data, "</conversation>\n"); | |
744 fclose(log->logger_data); | |
745 log->logger_data = NULL; | |
746 } | |
747 } | |
7443 | 748 |
8898 | 749 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 750 { |
10822 | 751 return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger); |
4184 | 752 } |
753 | |
7431 | 754 static GaimLogLogger xml_logger = { |
755 N_("XML"), "xml", | |
756 NULL, | |
757 xml_logger_write, | |
758 xml_logger_finalize, | |
759 xml_logger_list, | |
8096 | 760 NULL, |
11025 | 761 NULL, |
7431 | 762 NULL |
763 }; | |
764 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
765 |
7431 | 766 /**************************** |
7457 | 767 ** HTML LOGGER ************* |
768 ****************************/ | |
769 | |
770 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
771 const char *from, time_t time, const char *message) | |
772 { | |
9763 | 773 char *msg_fixed; |
7457 | 774 char date[64]; |
9613 | 775 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 776 GaimLogCommonLoggerData *data = log->logger_data; |
9613 | 777 |
7618 | 778 if(!data) { |
9763 | 779 const char *prpl = |
780 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
781 gaim_log_common_writer(log, ".html"); |
7457 | 782 |
9763 | 783 data = log->logger_data; |
7457 | 784 |
9763 | 785 /* if we can't write to the file, give up before we hurt ourselves */ |
786 if(!data->file) | |
787 return; | |
7616 | 788 |
7457 | 789 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
11092
68652f4ad6a7
[gaim-migrate @ 13115]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
790 fprintf(data->file, "<html><head>"); |
11094
59a1ff5a4bae
[gaim-migrate @ 13119]
Richard Laager <rlaager@wiktel.com>
parents:
11092
diff
changeset
|
791 fprintf(data->file, "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"); |
11092
68652f4ad6a7
[gaim-migrate @ 13115]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
792 fprintf(data->file, "<title>"); |
7616 | 793 fprintf(data->file, "Conversation with %s at %s on %s (%s)", |
7457 | 794 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 795 fprintf(data->file, "</title></head><body>"); |
796 fprintf(data->file, | |
7457 | 797 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
798 log->name, date, gaim_account_get_username(log->account), prpl); | |
9763 | 799 |
7457 | 800 } |
7623 | 801 |
9892 | 802 /* if we can't write to the file, give up before we hurt ourselves */ |
803 if(!data->file) | |
804 return; | |
805 | |
7882 | 806 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
807 | |
8577 | 808 if(log->type == GAIM_LOG_SYSTEM){ |
9592 | 809 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8577 | 810 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
811 } else { | |
812 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
813 if (type & GAIM_MESSAGE_SYSTEM) | |
814 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
815 else if (type & GAIM_MESSAGE_WHISPER) | |
816 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
817 date, from, msg_fixed); | |
818 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
819 if (type & GAIM_MESSAGE_SEND) | |
820 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
821 else if (type & GAIM_MESSAGE_RECV) | |
822 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
823 } else if (type & GAIM_MESSAGE_RECV) { | |
824 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
825 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 826 date, from, msg_fixed); |
8577 | 827 else |
10645 | 828 fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
829 date, from, msg_fixed); | |
8577 | 830 } else if (type & GAIM_MESSAGE_SEND) { |
831 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
832 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 833 date, from, msg_fixed); |
8577 | 834 else |
10645 | 835 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
836 date, from, msg_fixed); | |
8577 | 837 } |
7564 | 838 } |
8573 | 839 |
7882 | 840 g_free(msg_fixed); |
7616 | 841 fflush(data->file); |
7457 | 842 } |
843 | |
844 static void html_logger_finalize(GaimLog *log) | |
845 { | |
10822 | 846 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 847 if (data) { |
848 if(data->file) { | |
849 fprintf(data->file, "</body></html>"); | |
850 fclose(data->file); | |
851 } | |
852 g_free(data->path); | |
7752 | 853 g_free(data); |
7463 | 854 } |
7457 | 855 } |
856 | |
8898 | 857 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7457 | 858 { |
10822 | 859 return gaim_log_common_lister(type, sn, account, ".html", &html_logger); |
7457 | 860 } |
861 | |
8573 | 862 static GList *html_logger_list_syslog(GaimAccount *account) |
863 { | |
10822 | 864 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); |
8573 | 865 } |
866 | |
7457 | 867 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
868 { | |
869 char *read, *minus_header; | |
10822 | 870 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 871 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 872 if (!data || !data->path) |
873 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
874 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 875 minus_header = strchr(read, '\n'); |
876 if (!minus_header) | |
877 minus_header = g_strdup(read); | |
878 else | |
879 minus_header = g_strdup(minus_header + 1); | |
880 g_free(read); | |
881 return minus_header; | |
882 } | |
8578 | 883 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7457 | 884 } |
885 | |
886 static GaimLogLogger html_logger = { | |
887 N_("HTML"), "html", | |
9819 | 888 NULL, |
7457 | 889 html_logger_write, |
890 html_logger_finalize, | |
891 html_logger_list, | |
7556 | 892 html_logger_read, |
10822 | 893 gaim_log_common_sizer, |
8573 | 894 NULL, |
11025 | 895 html_logger_list_syslog, |
896 NULL | |
7457 | 897 }; |
898 | |
899 | |
900 | |
901 | |
902 /**************************** | |
7431 | 903 ** PLAIN TEXT LOGGER ******* |
904 ****************************/ | |
4184 | 905 |
7436 | 906 static void txt_logger_write(GaimLog *log, |
907 GaimMessageFlags type, | |
7431 | 908 const char *from, time_t time, const char *message) |
909 { | |
910 char date[64]; | |
9763 | 911 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 912 GaimLogCommonLoggerData *data = log->logger_data; |
9763 | 913 char *stripped = NULL; |
914 | |
915 if(!data) { | |
7431 | 916 /* This log is new. We could use the loggers 'new' function, but |
917 * creating a new file there would result in empty files in the case | |
918 * that you open a convo with someone, but don't say anything. | |
919 */ | |
9763 | 920 const char *prpl = |
921 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
922 gaim_log_common_writer(log, ".txt"); |
8898 | 923 |
9763 | 924 data = log->logger_data; |
7436 | 925 |
9763 | 926 /* if we can't write to the file, give up before we hurt ourselves */ |
927 if(!data->file) | |
928 return; | |
7616 | 929 |
7453 | 930 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 931 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 932 log->name, date, gaim_account_get_username(log->account), prpl); |
933 } | |
7436 | 934 |
7623 | 935 /* if we can't write to the file, give up before we hurt ourselves */ |
936 if(!data->file) | |
937 return; | |
938 | |
8573 | 939 stripped = gaim_markup_strip_html(message); |
940 | |
941 if(log->type == GAIM_LOG_SYSTEM){ | |
9592 | 942 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8573 | 943 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
944 } else { | |
945 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
946 if (type & GAIM_MESSAGE_SEND || | |
947 type & GAIM_MESSAGE_RECV) { | |
948 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
949 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
950 from, stripped); | |
951 } else { | |
952 if(gaim_message_meify(stripped, -1)) | |
953 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
954 stripped); | |
955 else | |
956 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
957 stripped); | |
958 } | |
959 } else if (type & GAIM_MESSAGE_SYSTEM) | |
960 fprintf(data->file, "(%s) %s\n", date, stripped); | |
961 else if (type & GAIM_MESSAGE_NO_LOG) { | |
962 /* This shouldn't happen */ | |
963 g_free(stripped); | |
964 return; | |
965 } else if (type & GAIM_MESSAGE_WHISPER) | |
966 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
967 else | |
968 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
969 from ? ":" : "", stripped); | |
970 } | |
971 | |
972 fflush(data->file); | |
973 g_free(stripped); | |
7431 | 974 } |
975 | |
976 static void txt_logger_finalize(GaimLog *log) | |
977 { | |
10822 | 978 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 979 if (data) { |
980 if(data->file) | |
981 fclose(data->file); | |
982 if(data->path) | |
983 g_free(data->path); | |
7752 | 984 g_free(data); |
7616 | 985 } |
7431 | 986 } |
987 | |
8898 | 988 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 989 { |
10822 | 990 return gaim_log_common_lister(type, sn, account, ".txt", &txt_logger); |
7431 | 991 } |
992 | |
8573 | 993 static GList *txt_logger_list_syslog(GaimAccount *account) |
994 { | |
10822 | 995 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); |
8573 | 996 } |
997 | |
7431 | 998 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
999 { | |
8517 | 1000 char *read, *minus_header, *minus_header2; |
10822 | 1001 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 1002 *flags = 0; |
7616 | 1003 if (!data || !data->path) |
1004 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
1005 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 1006 minus_header = strchr(read, '\n'); |
1007 if (!minus_header) | |
1008 minus_header = g_strdup(read); | |
7436 | 1009 else |
7431 | 1010 minus_header = g_strdup(minus_header + 1); |
1011 g_free(read); | |
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10645
diff
changeset
|
1012 minus_header2 = g_markup_escape_text(minus_header, -1); |
8517 | 1013 g_free(minus_header); |
1014 return minus_header2; | |
7431 | 1015 } |
8578 | 1016 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7436 | 1017 } |
7431 | 1018 |
1019 static GaimLogLogger txt_logger = { | |
1020 N_("Plain text"), "txt", | |
9819 | 1021 NULL, |
7431 | 1022 txt_logger_write, |
1023 txt_logger_finalize, | |
1024 txt_logger_list, | |
7556 | 1025 txt_logger_read, |
10822 | 1026 gaim_log_common_sizer, |
8573 | 1027 NULL, |
11025 | 1028 txt_logger_list_syslog, |
1029 NULL | |
7431 | 1030 }; |
1031 | |
1032 /**************** | |
1033 * OLD LOGGER *** | |
1034 ****************/ | |
1035 | |
1036 /* The old logger doesn't write logs, only reads them. This is to include | |
1037 * old logs in the log viewer transparently. | |
1038 */ | |
1039 | |
1040 struct old_logger_data { | |
7764 | 1041 GaimStringref *pathref; |
7431 | 1042 int offset; |
1043 int length; | |
1044 }; | |
1045 | |
8898 | 1046 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 1047 { |
1048 FILE *file; | |
1049 char buf[BUF_LONG]; | |
1050 struct tm tm; | |
7761 | 1051 char month[4]; |
7431 | 1052 struct old_logger_data *data = NULL; |
1053 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 1054 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
1055 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 1056 char *newlog; |
7761 | 1057 int logfound = 0; |
1058 int lastoff = 0; | |
1059 int newlen; | |
7791 | 1060 time_t lasttime = 0; |
7431 | 1061 |
1062 GaimLog *log = NULL; | |
1063 GList *list = NULL; | |
1064 | |
7473 | 1065 g_free(logfile); |
7764 | 1066 g_free(pathstr); |
7473 | 1067 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1068 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { |
7764 | 1069 gaim_stringref_unref(pathref); |
7431 | 1070 return NULL; |
7447 | 1071 } |
7436 | 1072 |
7431 | 1073 while (fgets(buf, BUF_LONG, file)) { |
1074 if ((newlog = strstr(buf, "---- New C"))) { | |
1075 int length; | |
1076 int offset; | |
1077 char convostart[32]; | |
1078 char *temp = strchr(buf, '@'); | |
7436 | 1079 |
7431 | 1080 if (temp == NULL || strlen(temp) < 2) |
1081 continue; | |
7436 | 1082 |
7431 | 1083 temp++; |
1084 length = strcspn(temp, "-"); | |
1085 if (length > 31) length = 31; | |
7436 | 1086 |
7431 | 1087 offset = ftell(file); |
7436 | 1088 |
7761 | 1089 if (logfound) { |
1090 newlen = offset - lastoff - length; | |
7436 | 1091 if(strstr(buf, "----</H3><BR>")) { |
7761 | 1092 newlen -= |
1093 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
1094 sizeof("----</H3><BR>") - 2; | |
7436 | 1095 } else { |
7761 | 1096 newlen -= |
1097 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 1098 } |
1099 | |
7461 | 1100 if(strchr(buf, '\r')) |
7770 | 1101 newlen--; |
7461 | 1102 |
7761 | 1103 if (newlen != 0) { |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1104 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
7761 | 1105 log->logger = &old_logger; |
1106 log->time = lasttime; | |
1107 data = g_new0(struct old_logger_data, 1); | |
1108 data->offset = lastoff; | |
1109 data->length = newlen; | |
7764 | 1110 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1111 log->logger_data = data; |
7431 | 1112 list = g_list_append(list, log); |
7761 | 1113 } |
7431 | 1114 } |
1115 | |
7761 | 1116 logfound = 1; |
1117 lastoff = offset; | |
7436 | 1118 |
7431 | 1119 g_snprintf(convostart, length, "%s", temp); |
7676 | 1120 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
1121 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
1122 /* Ugly hack, in case current locale is not English */ | |
1123 if (strcmp(month, "Jan") == 0) { | |
1124 tm.tm_mon= 0; | |
1125 } else if (strcmp(month, "Feb") == 0) { | |
1126 tm.tm_mon = 1; | |
1127 } else if (strcmp(month, "Mar") == 0) { | |
1128 tm.tm_mon = 2; | |
1129 } else if (strcmp(month, "Apr") == 0) { | |
1130 tm.tm_mon = 3; | |
1131 } else if (strcmp(month, "May") == 0) { | |
1132 tm.tm_mon = 4; | |
1133 } else if (strcmp(month, "Jun") == 0) { | |
1134 tm.tm_mon = 5; | |
1135 } else if (strcmp(month, "Jul") == 0) { | |
1136 tm.tm_mon = 6; | |
1137 } else if (strcmp(month, "Aug") == 0) { | |
1138 tm.tm_mon = 7; | |
1139 } else if (strcmp(month, "Sep") == 0) { | |
1140 tm.tm_mon = 8; | |
1141 } else if (strcmp(month, "Oct") == 0) { | |
1142 tm.tm_mon = 9; | |
1143 } else if (strcmp(month, "Nov") == 0) { | |
1144 tm.tm_mon = 10; | |
1145 } else if (strcmp(month, "Dec") == 0) { | |
1146 tm.tm_mon = 11; | |
1147 } | |
1148 tm.tm_year -= 1900; | |
7761 | 1149 lasttime = mktime(&tm); |
4184 | 1150 } |
1151 } | |
7613 | 1152 |
7761 | 1153 if (logfound) { |
1154 if ((newlen = ftell(file) - lastoff) != 0) { | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1155 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
7761 | 1156 log->logger = &old_logger; |
1157 log->time = lasttime; | |
1158 data = g_new0(struct old_logger_data, 1); | |
1159 data->offset = lastoff; | |
1160 data->length = newlen; | |
7764 | 1161 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1162 log->logger_data = data; |
7613 | 1163 list = g_list_append(list, log); |
7761 | 1164 } |
7613 | 1165 } |
1166 | |
7764 | 1167 gaim_stringref_unref(pathref); |
7431 | 1168 fclose(file); |
1169 return list; | |
4184 | 1170 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1171 |
8898 | 1172 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
8096 | 1173 { |
1174 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
1175 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
1176 int size; | |
1177 struct stat st; | |
1178 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1179 if (g_stat(pathstr, &st)) |
8096 | 1180 size = 0; |
1181 else | |
1182 size = st.st_size; | |
1183 | |
1184 g_free(logfile); | |
1185 g_free(pathstr); | |
1186 | |
1187 return size; | |
1188 } | |
1189 | |
7616 | 1190 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1191 { |
7431 | 1192 struct old_logger_data *data = log->logger_data; |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1193 FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); |
10906 | 1194 char *tmp, *read = g_malloc(data->length + 1); |
7431 | 1195 fseek(file, data->offset, SEEK_SET); |
1196 fread(read, data->length, 1, file); | |
8370 | 1197 fclose(file); |
7431 | 1198 read[data->length] = '\0'; |
7436 | 1199 *flags = 0; |
1200 if(strstr(read, "<BR>")) | |
1201 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
10906 | 1202 else { |
1203 tmp = g_markup_escape_text(read, -1); | |
1204 g_free(read); | |
1205 read = tmp; | |
1206 } | |
7431 | 1207 return read; |
1208 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1209 |
7616 | 1210 static int old_logger_size (GaimLog *log) |
7556 | 1211 { |
1212 struct old_logger_data *data = log->logger_data; | |
7616 | 1213 return data ? data->length : 0; |
1214 } | |
1215 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1216 static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets) |
11025 | 1217 { |
1218 char *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
1219 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
1220 gchar *name; | |
1221 GaimBlistNode *gnode, *cnode, *bnode; | |
1222 | |
1223 g_free(log_path); | |
1224 if (log_dir == NULL) | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1225 return; |
11025 | 1226 |
1227 /* Don't worry about the cast, name will be filled with a dynamically allocated data shortly. */ | |
1228 while ((name = (gchar *)g_dir_read_name(log_dir)) != NULL) { | |
1229 size_t len; | |
1230 gchar *ext; | |
1231 GaimLogSet *set; | |
1232 gboolean found = FALSE; | |
1233 | |
1234 /* Unescape the filename. */ | |
1235 name = g_strdup(gaim_unescape_filename(name)); | |
1236 | |
1237 /* Get the (possibly new) length of name. */ | |
1238 len = strlen(name); | |
1239 | |
1240 if (len < 5) { | |
1241 g_free(name); | |
1242 continue; | |
1243 } | |
1244 | |
1245 /* Make sure we're dealing with a log file. */ | |
1246 ext = &name[len - 4]; | |
1247 if (strcmp(ext, ".log")) { | |
1248 g_free(name); | |
1249 continue; | |
1250 } | |
1251 | |
1252 set = g_new0(GaimLogSet, 1); | |
1253 | |
1254 /* Chat for .chat at the end of the name to determine the type. */ | |
1255 *ext = '\0'; | |
1256 set->type = GAIM_LOG_IM; | |
1257 if (len > 9) { | |
1258 char *tmp = &name[len - 9]; | |
1259 if (!strcmp(tmp, ".chat")) { | |
1260 set->type = GAIM_LOG_CHAT; | |
1261 *tmp = '\0'; | |
1262 } | |
1263 } | |
1264 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1265 set->name = set->normalized_name = name; |
11025 | 1266 |
1267 /* Search the buddy list to find the account and to determine if this is a buddy. */ | |
1268 for (gnode = gaim_get_blist()->root; !found && gnode != NULL; gnode = gnode->next) | |
1269 { | |
1270 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
1271 continue; | |
1272 | |
1273 for (cnode = gnode->child; !found && cnode != NULL; cnode = cnode->next) | |
1274 { | |
1275 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
1276 continue; | |
1277 | |
1278 for (bnode = cnode->child; !found && bnode != NULL; bnode = bnode->next) | |
1279 { | |
1280 GaimBuddy *buddy = (GaimBuddy *)bnode; | |
1281 | |
1282 if (!strcmp(buddy->name, name)) { | |
1283 set->account = buddy->account; | |
1284 set->buddy = TRUE; | |
1285 found = TRUE; | |
1286 } | |
1287 } | |
1288 } | |
1289 } | |
1290 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1291 cb(sets, set); |
11025 | 1292 } |
1293 g_dir_close(log_dir); | |
1294 } | |
1295 | |
7616 | 1296 static void old_logger_finalize(GaimLog *log) |
1297 { | |
1298 struct old_logger_data *data = log->logger_data; | |
7764 | 1299 gaim_stringref_unref(data->pathref); |
7616 | 1300 g_free(data); |
7556 | 1301 } |
1302 | |
7431 | 1303 static GaimLogLogger old_logger = { |
1304 "old logger", "old", | |
7616 | 1305 NULL, NULL, |
1306 old_logger_finalize, | |
7431 | 1307 old_logger_list, |
7616 | 1308 old_logger_read, |
8096 | 1309 old_logger_size, |
8573 | 1310 old_logger_total_size, |
11025 | 1311 NULL, |
1312 old_logger_get_log_sets | |
7431 | 1313 }; |