Mercurial > pidgin.yaz
annotate src/log.c @ 11484:4539174a88bd
[gaim-migrate @ 13726]
Fix a warning.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Fri, 09 Sep 2005 20:06:13 +0000 |
parents | 4db38b374d3f |
children | cd0c8830d881 |
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. */ | |
11458
4db38b374d3f
[gaim-migrate @ 13697]
Richard Laager <rlaager@wiktel.com>
parents:
11292
diff
changeset
|
652 if (account != NULL) |
4db38b374d3f
[gaim-migrate @ 13697]
Richard Laager <rlaager@wiktel.com>
parents:
11292
diff
changeset
|
653 set->buddy = (gaim_find_buddy(account, name) != NULL); |
11025 | 654 |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
655 log_add_log_set_to_hash(sets, set); |
11025 | 656 } |
657 g_free(username_path); | |
658 g_dir_close(username_dir); | |
659 } | |
660 g_free(protocol_path); | |
661 g_dir_close(protocol_dir); | |
662 } | |
663 g_free(log_path); | |
664 g_dir_close(log_dir); | |
665 } | |
666 | |
7431 | 667 #if 0 /* Maybe some other time. */ |
7443 | 668 /**************** |
7431 | 669 ** XML LOGGER ** |
670 ****************/ | |
671 | |
672 static const char *str_from_msg_type (GaimMessageFlags type) | |
673 { | |
7443 | 674 |
7431 | 675 return ""; |
7443 | 676 |
7431 | 677 } |
678 | |
7443 | 679 static void xml_logger_write(GaimLog *log, |
680 GaimMessageFlags type, | |
7431 | 681 const char *from, time_t time, const char *message) |
682 { | |
683 char date[64]; | |
684 char *xhtml = NULL; | |
10577 | 685 |
7431 | 686 if (!log->logger_data) { |
687 /* This log is new. We could use the loggers 'new' function, but | |
688 * creating a new file there would result in empty files in the case | |
689 * that you open a convo with someone, but don't say anything. | |
690 */ | |
9923 | 691 char *dir = gaim_log_get_log_dir(log->type, log->name, log->account); |
7431 | 692 FILE *file; |
10577 | 693 |
694 if (dir == NULL) | |
695 return; | |
696 | |
7453 | 697 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); |
7443 | 698 |
7612 | 699 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); |
7443 | 700 |
7431 | 701 char *filename = g_build_filename(dir, date, NULL); |
702 g_free(dir); | |
7443 | 703 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
704 log->logger_data = g_fopen(filename, "a"); |
7431 | 705 if (!log->logger_data) { |
706 gaim_debug(GAIM_DEBUG_ERROR, "log", "Could not create log file %s\n", filename); | |
7564 | 707 g_free(filename); |
7431 | 708 return; |
709 } | |
7564 | 710 g_free(filename); |
7431 | 711 fprintf(log->logger_data, "<?xml version='1.0' encoding='UTF-8' ?>\n" |
712 "<?xml-stylesheet href='file:///usr/src/web/htdocs/log-stylesheet.xsl' type='text/xml' ?>\n"); | |
7443 | 713 |
7453 | 714 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7431 | 715 fprintf(log->logger_data, "<conversation time='%s' screenname='%s' protocol='%s'>\n", |
716 date, log->name, prpl); | |
717 } | |
7443 | 718 |
9923 | 719 /* if we can't write to the file, give up before we hurt ourselves */ |
720 if(!data->file) | |
721 return; | |
722 | |
7453 | 723 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); |
7431 | 724 gaim_markup_html_to_xhtml(message, &xhtml, NULL); |
725 if (from) | |
7443 | 726 fprintf(log->logger_data, "<message %s %s from='%s' time='%s'>%s</message>\n", |
727 str_from_msg_type(type), | |
7431 | 728 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
729 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
730 from, date, xhtml); | |
731 else | |
7443 | 732 fprintf(log->logger_data, "<message %s %s time='%s'>%s</message>\n", |
733 str_from_msg_type(type), | |
7431 | 734 type & GAIM_MESSAGE_SEND ? "direction='sent'" : |
735 type & GAIM_MESSAGE_RECV ? "direction='received'" : "", | |
7443 | 736 date, xhtml): |
7431 | 737 fflush(log->logger_data); |
738 g_free(xhtml); | |
7443 | 739 } |
740 | |
7431 | 741 static void xml_logger_finalize(GaimLog *log) |
742 { | |
743 if (log->logger_data) { | |
744 fprintf(log->logger_data, "</conversation>\n"); | |
745 fclose(log->logger_data); | |
746 log->logger_data = NULL; | |
747 } | |
748 } | |
7443 | 749 |
8898 | 750 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 751 { |
10822 | 752 return gaim_log_common_lister(type, sn, account, ".xml", &xml_logger); |
4184 | 753 } |
754 | |
7431 | 755 static GaimLogLogger xml_logger = { |
756 N_("XML"), "xml", | |
757 NULL, | |
758 xml_logger_write, | |
759 xml_logger_finalize, | |
760 xml_logger_list, | |
8096 | 761 NULL, |
11025 | 762 NULL, |
7431 | 763 NULL |
764 }; | |
765 #endif | |
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
766 |
7431 | 767 /**************************** |
7457 | 768 ** HTML LOGGER ************* |
769 ****************************/ | |
770 | |
771 static void html_logger_write(GaimLog *log, GaimMessageFlags type, | |
772 const char *from, time_t time, const char *message) | |
773 { | |
9763 | 774 char *msg_fixed; |
7457 | 775 char date[64]; |
9613 | 776 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 777 GaimLogCommonLoggerData *data = log->logger_data; |
9613 | 778 |
7618 | 779 if(!data) { |
9763 | 780 const char *prpl = |
781 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
782 gaim_log_common_writer(log, ".html"); |
7457 | 783 |
9763 | 784 data = log->logger_data; |
7457 | 785 |
9763 | 786 /* if we can't write to the file, give up before we hurt ourselves */ |
787 if(!data->file) | |
788 return; | |
7616 | 789 |
7457 | 790 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
|
791 fprintf(data->file, "<html><head>"); |
11094
59a1ff5a4bae
[gaim-migrate @ 13119]
Richard Laager <rlaager@wiktel.com>
parents:
11092
diff
changeset
|
792 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
|
793 fprintf(data->file, "<title>"); |
7616 | 794 fprintf(data->file, "Conversation with %s at %s on %s (%s)", |
7457 | 795 log->name, date, gaim_account_get_username(log->account), prpl); |
7616 | 796 fprintf(data->file, "</title></head><body>"); |
797 fprintf(data->file, | |
7457 | 798 "<h3>Conversation with %s at %s on %s (%s)</h3>\n", |
799 log->name, date, gaim_account_get_username(log->account), prpl); | |
9763 | 800 |
7457 | 801 } |
7623 | 802 |
9892 | 803 /* if we can't write to the file, give up before we hurt ourselves */ |
804 if(!data->file) | |
805 return; | |
806 | |
7882 | 807 gaim_markup_html_to_xhtml(message, &msg_fixed, NULL); |
808 | |
8577 | 809 if(log->type == GAIM_LOG_SYSTEM){ |
9592 | 810 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8577 | 811 fprintf(data->file, "---- %s @ %s ----<br/>\n", msg_fixed, date); |
812 } else { | |
813 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
814 if (type & GAIM_MESSAGE_SYSTEM) | |
815 fprintf(data->file, "<font size=\"2\">(%s)</font><b> %s</b><br/>\n", date, msg_fixed); | |
816 else if (type & GAIM_MESSAGE_WHISPER) | |
817 fprintf(data->file, "<font color=\"#6C2585\"><font size=\"2\">(%s)</font><b> %s:</b></font> %s<br/>\n", | |
818 date, from, msg_fixed); | |
819 else if (type & GAIM_MESSAGE_AUTO_RESP) { | |
820 if (type & GAIM_MESSAGE_SEND) | |
821 fprintf(data->file, _("<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
822 else if (type & GAIM_MESSAGE_RECV) | |
823 fprintf(data->file, _("<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s <AUTO-REPLY>:</b></font> %s<br/>\n"), date, from, msg_fixed); | |
824 } else if (type & GAIM_MESSAGE_RECV) { | |
825 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
826 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 827 date, from, msg_fixed); |
8577 | 828 else |
10645 | 829 fprintf(data->file, "<font color=\"#A82F2F\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
830 date, from, msg_fixed); | |
8577 | 831 } else if (type & GAIM_MESSAGE_SEND) { |
832 if(gaim_message_meify(msg_fixed, -1)) | |
10735
a0edd89ddb83
[gaim-migrate @ 12337]
Luke Schierer <lschiere@pidgin.im>
parents:
10732
diff
changeset
|
833 fprintf(data->file, "<font color=\"#062585\"><font size=\"2\">(%s)</font> <b>***%s</b></font> %s<br/>\n", |
10645 | 834 date, from, msg_fixed); |
8577 | 835 else |
10645 | 836 fprintf(data->file, "<font color=\"#16569E\"><font size=\"2\">(%s)</font> <b>%s:</b></font> %s<br/>\n", |
837 date, from, msg_fixed); | |
8577 | 838 } |
7564 | 839 } |
8573 | 840 |
7882 | 841 g_free(msg_fixed); |
7616 | 842 fflush(data->file); |
7457 | 843 } |
844 | |
845 static void html_logger_finalize(GaimLog *log) | |
846 { | |
10822 | 847 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 848 if (data) { |
849 if(data->file) { | |
850 fprintf(data->file, "</body></html>"); | |
851 fclose(data->file); | |
852 } | |
853 g_free(data->path); | |
7752 | 854 g_free(data); |
7463 | 855 } |
7457 | 856 } |
857 | |
8898 | 858 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7457 | 859 { |
10822 | 860 return gaim_log_common_lister(type, sn, account, ".html", &html_logger); |
7457 | 861 } |
862 | |
8573 | 863 static GList *html_logger_list_syslog(GaimAccount *account) |
864 { | |
10822 | 865 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger); |
8573 | 866 } |
867 | |
7457 | 868 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
869 { | |
870 char *read, *minus_header; | |
10822 | 871 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 872 *flags = GAIM_LOG_READ_NO_NEWLINE; |
7616 | 873 if (!data || !data->path) |
874 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
875 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7457 | 876 minus_header = strchr(read, '\n'); |
877 if (!minus_header) | |
878 minus_header = g_strdup(read); | |
879 else | |
880 minus_header = g_strdup(minus_header + 1); | |
881 g_free(read); | |
882 return minus_header; | |
883 } | |
8578 | 884 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7457 | 885 } |
886 | |
887 static GaimLogLogger html_logger = { | |
888 N_("HTML"), "html", | |
9819 | 889 NULL, |
7457 | 890 html_logger_write, |
891 html_logger_finalize, | |
892 html_logger_list, | |
7556 | 893 html_logger_read, |
10822 | 894 gaim_log_common_sizer, |
8573 | 895 NULL, |
11025 | 896 html_logger_list_syslog, |
897 NULL | |
7457 | 898 }; |
899 | |
900 | |
901 | |
902 | |
903 /**************************** | |
7431 | 904 ** PLAIN TEXT LOGGER ******* |
905 ****************************/ | |
4184 | 906 |
7436 | 907 static void txt_logger_write(GaimLog *log, |
908 GaimMessageFlags type, | |
7431 | 909 const char *from, time_t time, const char *message) |
910 { | |
911 char date[64]; | |
9763 | 912 GaimPlugin *plugin = gaim_find_prpl(gaim_account_get_protocol_id(log->account)); |
10822 | 913 GaimLogCommonLoggerData *data = log->logger_data; |
9763 | 914 char *stripped = NULL; |
915 | |
916 if(!data) { | |
7431 | 917 /* This log is new. We could use the loggers 'new' function, but |
918 * creating a new file there would result in empty files in the case | |
919 * that you open a convo with someone, but don't say anything. | |
920 */ | |
9763 | 921 const char *prpl = |
922 GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon(log->account, NULL); | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
923 gaim_log_common_writer(log, ".txt"); |
8898 | 924 |
9763 | 925 data = log->logger_data; |
7436 | 926 |
9763 | 927 /* if we can't write to the file, give up before we hurt ourselves */ |
928 if(!data->file) | |
929 return; | |
7616 | 930 |
7453 | 931 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&log->time)); |
7616 | 932 fprintf(data->file, "Conversation with %s at %s on %s (%s)\n", |
7431 | 933 log->name, date, gaim_account_get_username(log->account), prpl); |
934 } | |
7436 | 935 |
7623 | 936 /* if we can't write to the file, give up before we hurt ourselves */ |
937 if(!data->file) | |
938 return; | |
939 | |
8573 | 940 stripped = gaim_markup_strip_html(message); |
941 | |
942 if(log->type == GAIM_LOG_SYSTEM){ | |
9592 | 943 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&time)); |
8573 | 944 fprintf(data->file, "---- %s @ %s ----\n", stripped, date); |
945 } else { | |
946 strftime(date, sizeof(date), "%H:%M:%S", localtime(&time)); | |
947 if (type & GAIM_MESSAGE_SEND || | |
948 type & GAIM_MESSAGE_RECV) { | |
949 if (type & GAIM_MESSAGE_AUTO_RESP) { | |
950 fprintf(data->file, _("(%s) %s <AUTO-REPLY>: %s\n"), date, | |
951 from, stripped); | |
952 } else { | |
953 if(gaim_message_meify(stripped, -1)) | |
954 fprintf(data->file, "(%s) ***%s %s\n", date, from, | |
955 stripped); | |
956 else | |
957 fprintf(data->file, "(%s) %s: %s\n", date, from, | |
958 stripped); | |
959 } | |
960 } else if (type & GAIM_MESSAGE_SYSTEM) | |
961 fprintf(data->file, "(%s) %s\n", date, stripped); | |
962 else if (type & GAIM_MESSAGE_NO_LOG) { | |
963 /* This shouldn't happen */ | |
964 g_free(stripped); | |
965 return; | |
966 } else if (type & GAIM_MESSAGE_WHISPER) | |
967 fprintf(data->file, "(%s) *%s* %s", date, from, stripped); | |
968 else | |
969 fprintf(data->file, "(%s) %s%s %s\n", date, from ? from : "", | |
970 from ? ":" : "", stripped); | |
971 } | |
972 | |
973 fflush(data->file); | |
974 g_free(stripped); | |
7431 | 975 } |
976 | |
977 static void txt_logger_finalize(GaimLog *log) | |
978 { | |
10822 | 979 GaimLogCommonLoggerData *data = log->logger_data; |
7616 | 980 if (data) { |
981 if(data->file) | |
982 fclose(data->file); | |
983 if(data->path) | |
984 g_free(data->path); | |
7752 | 985 g_free(data); |
7616 | 986 } |
7431 | 987 } |
988 | |
8898 | 989 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 990 { |
10822 | 991 return gaim_log_common_lister(type, sn, account, ".txt", &txt_logger); |
7431 | 992 } |
993 | |
8573 | 994 static GList *txt_logger_list_syslog(GaimAccount *account) |
995 { | |
10822 | 996 return gaim_log_common_lister(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger); |
8573 | 997 } |
998 | |
7431 | 999 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) |
1000 { | |
8517 | 1001 char *read, *minus_header, *minus_header2; |
10822 | 1002 GaimLogCommonLoggerData *data = log->logger_data; |
7457 | 1003 *flags = 0; |
7616 | 1004 if (!data || !data->path) |
1005 return g_strdup(_("<font color=\"red\"><b>Unable to find log path!</b></font>")); | |
1006 if (g_file_get_contents(data->path, &read, NULL, NULL)) { | |
7431 | 1007 minus_header = strchr(read, '\n'); |
1008 if (!minus_header) | |
1009 minus_header = g_strdup(read); | |
7436 | 1010 else |
7431 | 1011 minus_header = g_strdup(minus_header + 1); |
1012 g_free(read); | |
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10645
diff
changeset
|
1013 minus_header2 = g_markup_escape_text(minus_header, -1); |
8517 | 1014 g_free(minus_header); |
1015 return minus_header2; | |
7431 | 1016 } |
8578 | 1017 return g_strdup_printf(_("<font color=\"red\"><b>Could not read file: %s</b></font>"), data->path); |
7436 | 1018 } |
7431 | 1019 |
1020 static GaimLogLogger txt_logger = { | |
1021 N_("Plain text"), "txt", | |
9819 | 1022 NULL, |
7431 | 1023 txt_logger_write, |
1024 txt_logger_finalize, | |
1025 txt_logger_list, | |
7556 | 1026 txt_logger_read, |
10822 | 1027 gaim_log_common_sizer, |
8573 | 1028 NULL, |
11025 | 1029 txt_logger_list_syslog, |
1030 NULL | |
7431 | 1031 }; |
1032 | |
1033 /**************** | |
1034 * OLD LOGGER *** | |
1035 ****************/ | |
1036 | |
1037 /* The old logger doesn't write logs, only reads them. This is to include | |
1038 * old logs in the log viewer transparently. | |
1039 */ | |
1040 | |
1041 struct old_logger_data { | |
7764 | 1042 GaimStringref *pathref; |
7431 | 1043 int offset; |
1044 int length; | |
1045 }; | |
1046 | |
8898 | 1047 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) |
7431 | 1048 { |
1049 FILE *file; | |
1050 char buf[BUF_LONG]; | |
1051 struct tm tm; | |
7761 | 1052 char month[4]; |
7431 | 1053 struct old_logger_data *data = NULL; |
1054 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn)); | |
7764 | 1055 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); |
1056 GaimStringref *pathref = gaim_stringref_new(pathstr); | |
7431 | 1057 char *newlog; |
7761 | 1058 int logfound = 0; |
1059 int lastoff = 0; | |
1060 int newlen; | |
7791 | 1061 time_t lasttime = 0; |
7431 | 1062 |
1063 GaimLog *log = NULL; | |
1064 GList *list = NULL; | |
1065 | |
7473 | 1066 g_free(logfile); |
7764 | 1067 g_free(pathstr); |
7473 | 1068 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1069 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { |
7764 | 1070 gaim_stringref_unref(pathref); |
7431 | 1071 return NULL; |
7447 | 1072 } |
7436 | 1073 |
7431 | 1074 while (fgets(buf, BUF_LONG, file)) { |
1075 if ((newlog = strstr(buf, "---- New C"))) { | |
1076 int length; | |
1077 int offset; | |
1078 char convostart[32]; | |
1079 char *temp = strchr(buf, '@'); | |
7436 | 1080 |
7431 | 1081 if (temp == NULL || strlen(temp) < 2) |
1082 continue; | |
7436 | 1083 |
7431 | 1084 temp++; |
1085 length = strcspn(temp, "-"); | |
1086 if (length > 31) length = 31; | |
7436 | 1087 |
7431 | 1088 offset = ftell(file); |
7436 | 1089 |
7761 | 1090 if (logfound) { |
1091 newlen = offset - lastoff - length; | |
7436 | 1092 if(strstr(buf, "----</H3><BR>")) { |
7761 | 1093 newlen -= |
1094 sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") + | |
1095 sizeof("----</H3><BR>") - 2; | |
7436 | 1096 } else { |
7761 | 1097 newlen -= |
1098 sizeof("---- New Conversation @ ") + sizeof("----") - 2; | |
7436 | 1099 } |
1100 | |
7461 | 1101 if(strchr(buf, '\r')) |
7770 | 1102 newlen--; |
7461 | 1103 |
7761 | 1104 if (newlen != 0) { |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1105 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
7761 | 1106 log->logger = &old_logger; |
1107 log->time = lasttime; | |
1108 data = g_new0(struct old_logger_data, 1); | |
1109 data->offset = lastoff; | |
1110 data->length = newlen; | |
7764 | 1111 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1112 log->logger_data = data; |
7431 | 1113 list = g_list_append(list, log); |
7761 | 1114 } |
7431 | 1115 } |
1116 | |
7761 | 1117 logfound = 1; |
1118 lastoff = offset; | |
7436 | 1119 |
7431 | 1120 g_snprintf(convostart, length, "%s", temp); |
7676 | 1121 sscanf(convostart, "%*s %s %d %d:%d:%d %d", |
1122 month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year); | |
1123 /* Ugly hack, in case current locale is not English */ | |
1124 if (strcmp(month, "Jan") == 0) { | |
1125 tm.tm_mon= 0; | |
1126 } else if (strcmp(month, "Feb") == 0) { | |
1127 tm.tm_mon = 1; | |
1128 } else if (strcmp(month, "Mar") == 0) { | |
1129 tm.tm_mon = 2; | |
1130 } else if (strcmp(month, "Apr") == 0) { | |
1131 tm.tm_mon = 3; | |
1132 } else if (strcmp(month, "May") == 0) { | |
1133 tm.tm_mon = 4; | |
1134 } else if (strcmp(month, "Jun") == 0) { | |
1135 tm.tm_mon = 5; | |
1136 } else if (strcmp(month, "Jul") == 0) { | |
1137 tm.tm_mon = 6; | |
1138 } else if (strcmp(month, "Aug") == 0) { | |
1139 tm.tm_mon = 7; | |
1140 } else if (strcmp(month, "Sep") == 0) { | |
1141 tm.tm_mon = 8; | |
1142 } else if (strcmp(month, "Oct") == 0) { | |
1143 tm.tm_mon = 9; | |
1144 } else if (strcmp(month, "Nov") == 0) { | |
1145 tm.tm_mon = 10; | |
1146 } else if (strcmp(month, "Dec") == 0) { | |
1147 tm.tm_mon = 11; | |
1148 } | |
1149 tm.tm_year -= 1900; | |
7761 | 1150 lasttime = mktime(&tm); |
4184 | 1151 } |
1152 } | |
7613 | 1153 |
7761 | 1154 if (logfound) { |
1155 if ((newlen = ftell(file) - lastoff) != 0) { | |
11292
ef9280fdc511
[gaim-migrate @ 13492]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
1156 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1); |
7761 | 1157 log->logger = &old_logger; |
1158 log->time = lasttime; | |
1159 data = g_new0(struct old_logger_data, 1); | |
1160 data->offset = lastoff; | |
1161 data->length = newlen; | |
7764 | 1162 data->pathref = gaim_stringref_ref(pathref); |
7761 | 1163 log->logger_data = data; |
7613 | 1164 list = g_list_append(list, log); |
7761 | 1165 } |
7613 | 1166 } |
1167 | |
7764 | 1168 gaim_stringref_unref(pathref); |
7431 | 1169 fclose(file); |
1170 return list; | |
4184 | 1171 } |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1172 |
8898 | 1173 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) |
8096 | 1174 { |
1175 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); | |
1176 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); | |
1177 int size; | |
1178 struct stat st; | |
1179 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1180 if (g_stat(pathstr, &st)) |
8096 | 1181 size = 0; |
1182 else | |
1183 size = st.st_size; | |
1184 | |
1185 g_free(logfile); | |
1186 g_free(pathstr); | |
1187 | |
1188 return size; | |
1189 } | |
1190 | |
7616 | 1191 static char * old_logger_read (GaimLog *log, GaimLogReadFlags *flags) |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1192 { |
7431 | 1193 struct old_logger_data *data = log->logger_data; |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10577
diff
changeset
|
1194 FILE *file = g_fopen(gaim_stringref_value(data->pathref), "rb"); |
10906 | 1195 char *tmp, *read = g_malloc(data->length + 1); |
7431 | 1196 fseek(file, data->offset, SEEK_SET); |
1197 fread(read, data->length, 1, file); | |
8370 | 1198 fclose(file); |
7431 | 1199 read[data->length] = '\0'; |
7436 | 1200 *flags = 0; |
1201 if(strstr(read, "<BR>")) | |
1202 *flags |= GAIM_LOG_READ_NO_NEWLINE; | |
10906 | 1203 else { |
1204 tmp = g_markup_escape_text(read, -1); | |
1205 g_free(read); | |
1206 read = tmp; | |
1207 } | |
7431 | 1208 return read; |
1209 } | |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4227
diff
changeset
|
1210 |
7616 | 1211 static int old_logger_size (GaimLog *log) |
7556 | 1212 { |
1213 struct old_logger_data *data = log->logger_data; | |
7616 | 1214 return data ? data->length : 0; |
1215 } | |
1216 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1217 static void old_logger_get_log_sets(GaimLogSetCallback cb, GHashTable *sets) |
11025 | 1218 { |
1219 char *log_path = g_build_filename(gaim_user_dir(), "logs", NULL); | |
1220 GDir *log_dir = g_dir_open(log_path, 0, NULL); | |
1221 gchar *name; | |
1222 GaimBlistNode *gnode, *cnode, *bnode; | |
1223 | |
1224 g_free(log_path); | |
1225 if (log_dir == NULL) | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1226 return; |
11025 | 1227 |
1228 /* Don't worry about the cast, name will be filled with a dynamically allocated data shortly. */ | |
1229 while ((name = (gchar *)g_dir_read_name(log_dir)) != NULL) { | |
1230 size_t len; | |
1231 gchar *ext; | |
1232 GaimLogSet *set; | |
1233 gboolean found = FALSE; | |
1234 | |
1235 /* Unescape the filename. */ | |
1236 name = g_strdup(gaim_unescape_filename(name)); | |
1237 | |
1238 /* Get the (possibly new) length of name. */ | |
1239 len = strlen(name); | |
1240 | |
1241 if (len < 5) { | |
1242 g_free(name); | |
1243 continue; | |
1244 } | |
1245 | |
1246 /* Make sure we're dealing with a log file. */ | |
1247 ext = &name[len - 4]; | |
1248 if (strcmp(ext, ".log")) { | |
1249 g_free(name); | |
1250 continue; | |
1251 } | |
1252 | |
1253 set = g_new0(GaimLogSet, 1); | |
1254 | |
1255 /* Chat for .chat at the end of the name to determine the type. */ | |
1256 *ext = '\0'; | |
1257 set->type = GAIM_LOG_IM; | |
1258 if (len > 9) { | |
1259 char *tmp = &name[len - 9]; | |
1260 if (!strcmp(tmp, ".chat")) { | |
1261 set->type = GAIM_LOG_CHAT; | |
1262 *tmp = '\0'; | |
1263 } | |
1264 } | |
1265 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1266 set->name = set->normalized_name = name; |
11025 | 1267 |
1268 /* Search the buddy list to find the account and to determine if this is a buddy. */ | |
1269 for (gnode = gaim_get_blist()->root; !found && gnode != NULL; gnode = gnode->next) | |
1270 { | |
1271 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
1272 continue; | |
1273 | |
1274 for (cnode = gnode->child; !found && cnode != NULL; cnode = cnode->next) | |
1275 { | |
1276 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
1277 continue; | |
1278 | |
1279 for (bnode = cnode->child; !found && bnode != NULL; bnode = bnode->next) | |
1280 { | |
1281 GaimBuddy *buddy = (GaimBuddy *)bnode; | |
1282 | |
1283 if (!strcmp(buddy->name, name)) { | |
1284 set->account = buddy->account; | |
1285 set->buddy = TRUE; | |
1286 found = TRUE; | |
1287 } | |
1288 } | |
1289 } | |
1290 } | |
1291 | |
11177
3924db2b1ca8
[gaim-migrate @ 13285]
Richard Laager <rlaager@wiktel.com>
parents:
11094
diff
changeset
|
1292 cb(sets, set); |
11025 | 1293 } |
1294 g_dir_close(log_dir); | |
1295 } | |
1296 | |
7616 | 1297 static void old_logger_finalize(GaimLog *log) |
1298 { | |
1299 struct old_logger_data *data = log->logger_data; | |
7764 | 1300 gaim_stringref_unref(data->pathref); |
7616 | 1301 g_free(data); |
7556 | 1302 } |
1303 | |
7431 | 1304 static GaimLogLogger old_logger = { |
1305 "old logger", "old", | |
7616 | 1306 NULL, NULL, |
1307 old_logger_finalize, | |
7431 | 1308 old_logger_list, |
7616 | 1309 old_logger_read, |
8096 | 1310 old_logger_size, |
8573 | 1311 old_logger_total_size, |
11025 | 1312 NULL, |
1313 old_logger_get_log_sets | |
7431 | 1314 }; |