-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.c
2068 lines (1731 loc) · 56 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file log.c Logging API
* @ingroup core
*/
/* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "account.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "internal.h"
#include "log.h"
#include "prefs.h"
#include "util.h"
#include "stringref.h"
#include "imgstore.h"
#include "time.h"
static GSList *loggers = NULL;
static PurpleLogLogger *html_logger;
static PurpleLogLogger *txt_logger;
static PurpleLogLogger *old_logger;
struct _purple_logsize_user {
char *name;
PurpleAccount *account;
};
static GHashTable *logsize_users = NULL;
static GHashTable *logsize_users_decayed = NULL;
static void log_get_log_sets_common(GHashTable *sets);
static gsize html_logger_write(PurpleLog *log, PurpleMessageFlags type,
const char *from, time_t time, const char *message);
static void html_logger_finalize(PurpleLog *log);
static GList *html_logger_list(PurpleLogType type, const char *sn, PurpleAccount *account);
static GList *html_logger_list_syslog(PurpleAccount *account);
static char *html_logger_read(PurpleLog *log, PurpleLogReadFlags *flags);
static int html_logger_total_size(PurpleLogType type, const char *name, PurpleAccount *account);
static GList *old_logger_list(PurpleLogType type, const char *sn, PurpleAccount *account);
static int old_logger_total_size(PurpleLogType type, const char *name, PurpleAccount *account);
static char * old_logger_read (PurpleLog *log, PurpleLogReadFlags *flags);
static int old_logger_size (PurpleLog *log);
static void old_logger_get_log_sets(PurpleLogSetCallback cb, GHashTable *sets);
static void old_logger_finalize(PurpleLog *log);
static gsize txt_logger_write(PurpleLog *log,
PurpleMessageFlags type,
const char *from, time_t time, const char *message);
static void txt_logger_finalize(PurpleLog *log);
static GList *txt_logger_list(PurpleLogType type, const char *sn, PurpleAccount *account);
static GList *txt_logger_list_syslog(PurpleAccount *account);
static char *txt_logger_read(PurpleLog *log, PurpleLogReadFlags *flags);
static int txt_logger_total_size(PurpleLogType type, const char *name, PurpleAccount *account);
/**************************************************************************
* PUBLIC LOGGING FUNCTIONS ***********************************************
**************************************************************************/
PurpleLog *purple_log_new(PurpleLogType type, const char *name, PurpleAccount *account,
PurpleConversation *conv, time_t time, const struct tm *tm)
{
PurpleLog *log;
/* IMPORTANT: Make sure to initialize all the members of PurpleLog */
log = g_slice_new(PurpleLog);
PURPLE_DBUS_REGISTER_POINTER(log, PurpleLog);
log->type = type;
log->name = g_strdup(purple_normalize(account, name));
log->account = account;
log->conv = conv;
log->time = time;
log->logger = purple_log_logger_get();
log->logger_data = NULL;
if (tm == NULL)
log->tm = NULL;
else
{
/* There's no need to zero this as we immediately do a direct copy. */
log->tm = g_slice_new(struct tm);
*(log->tm) = *tm;
#ifdef HAVE_STRUCT_TM_TM_ZONE
/* XXX: This is so wrong... */
if (log->tm->tm_zone != NULL)
{
char *tmp = g_locale_from_utf8(log->tm->tm_zone, -1, NULL, NULL, NULL);
if (tmp != NULL)
log->tm->tm_zone = tmp;
else
/* Just shove the UTF-8 bytes in and hope... */
log->tm->tm_zone = g_strdup(log->tm->tm_zone);
}
#endif
}
if (log->logger && log->logger->create)
log->logger->create(log);
return log;
}
void purple_log_free(PurpleLog *log)
{
g_return_if_fail(log);
if (log->logger && log->logger->finalize)
log->logger->finalize(log);
g_free(log->name);
if (log->tm != NULL)
{
#ifdef HAVE_STRUCT_TM_TM_ZONE
/* XXX: This is so wrong... */
g_free((char *)log->tm->tm_zone);
#endif
g_slice_free(struct tm, log->tm);
}
PURPLE_DBUS_UNREGISTER_POINTER(log);
g_slice_free(PurpleLog, log);
}
void purple_log_write(PurpleLog *log, PurpleMessageFlags type,
const char *from, time_t time, const char *message)
{
struct _purple_logsize_user *lu;
gsize written, total = 0;
gpointer ptrsize;
g_return_if_fail(log);
g_return_if_fail(log->logger);
g_return_if_fail(log->logger->write);
written = (log->logger->write)(log, type, from, time, message);
lu = g_new(struct _purple_logsize_user, 1);
lu->name = g_strdup(purple_normalize(log->account, log->name));
lu->account = log->account;
if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) {
char *tmp = lu->name;
total = GPOINTER_TO_INT(ptrsize);
total += written;
g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(total));
/* The hash table takes ownership of lu, so create a new one
* for the logsize_users_decayed check below. */
lu = g_new(struct _purple_logsize_user, 1);
lu->name = g_strdup(tmp);
lu->account = log->account;
}
if(g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrsize)) {
total = GPOINTER_TO_INT(ptrsize);
total += written;
g_hash_table_replace(logsize_users_decayed, lu, GINT_TO_POINTER(total));
} else {
g_free(lu->name);
g_free(lu);
}
}
char *purple_log_read(PurpleLog *log, PurpleLogReadFlags *flags)
{
PurpleLogReadFlags mflags;
g_return_val_if_fail(log && log->logger, NULL);
if (log->logger->read) {
char *ret = (log->logger->read)(log, flags ? flags : &mflags);
purple_str_strip_char(ret, '\r');
return ret;
}
return g_strdup(_("<b><font color=\"red\">The logger has no read function</font></b>"));
}
int purple_log_get_size(PurpleLog *log)
{
g_return_val_if_fail(log && log->logger, 0);
if (log->logger->size)
return log->logger->size(log);
return 0;
}
static guint _purple_logsize_user_hash(struct _purple_logsize_user *lu)
{
return g_str_hash(lu->name);
}
static guint _purple_logsize_user_equal(struct _purple_logsize_user *lu1,
struct _purple_logsize_user *lu2)
{
return (lu1->account == lu2->account && purple_strequal(lu1->name, lu2->name));
}
static void _purple_logsize_user_free_key(struct _purple_logsize_user *lu)
{
g_free(lu->name);
g_free(lu);
}
int purple_log_get_total_size(PurpleLogType type, const char *name, PurpleAccount *account)
{
gpointer ptrsize;
int size = 0;
GSList *n;
struct _purple_logsize_user *lu;
lu = g_new(struct _purple_logsize_user, 1);
lu->name = g_strdup(purple_normalize(account, name));
lu->account = account;
if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) {
size = GPOINTER_TO_INT(ptrsize);
g_free(lu->name);
g_free(lu);
} else {
for (n = loggers; n; n = n->next) {
PurpleLogLogger *logger = n->data;
if(logger->total_size){
size += (logger->total_size)(type, name, account);
} else if(logger->list) {
GList *logs = (logger->list)(type, name, account);
int this_size = 0;
while (logs) {
PurpleLog *log = (PurpleLog*)(logs->data);
this_size += purple_log_get_size(log);
purple_log_free(log);
logs = g_list_delete_link(logs, logs);
}
size += this_size;
}
}
g_hash_table_replace(logsize_users, lu, GINT_TO_POINTER(size));
}
return size;
}
gint purple_log_get_activity_score(PurpleLogType type, const char *name, PurpleAccount *account)
{
gpointer ptrscore;
int score;
GSList *n;
struct _purple_logsize_user *lu;
time_t now;
time(&now);
lu = g_new(struct _purple_logsize_user, 1);
lu->name = g_strdup(purple_normalize(account, name));
lu->account = account;
if(g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrscore)) {
score = GPOINTER_TO_INT(ptrscore);
g_free(lu->name);
g_free(lu);
} else {
double score_double = 0.0;
for (n = loggers; n; n = n->next) {
PurpleLogLogger *logger = n->data;
if(logger->list) {
GList *logs = (logger->list)(type, name, account);
while (logs) {
PurpleLog *log = (PurpleLog*)(logs->data);
/* Activity score counts bytes in the log, exponentially
decayed with a half-life of 14 days. */
score_double += purple_log_get_size(log) *
pow(0.5, difftime(now, log->time)/1209600.0);
purple_log_free(log);
logs = g_list_delete_link(logs, logs);
}
}
}
score = (gint) ceil(score_double);
g_hash_table_replace(logsize_users_decayed, lu, GINT_TO_POINTER(score));
}
return score;
}
gboolean purple_log_is_deletable(PurpleLog *log)
{
g_return_val_if_fail(log != NULL, FALSE);
g_return_val_if_fail(log->logger != NULL, FALSE);
if (log->logger->remove == NULL)
return FALSE;
if (log->logger->is_deletable != NULL)
return log->logger->is_deletable(log);
return TRUE;
}
gboolean purple_log_delete(PurpleLog *log)
{
g_return_val_if_fail(log != NULL, FALSE);
g_return_val_if_fail(log->logger != NULL, FALSE);
if (log->logger->remove != NULL)
return log->logger->remove(log);
return FALSE;
}
char *
purple_log_get_log_dir(PurpleLogType type, const char *name, PurpleAccount *account)
{
PurplePlugin *prpl;
PurplePluginProtocolInfo *prpl_info;
const char *prpl_name;
char *acct_name;
const char *target;
char *dir;
prpl = purple_find_prpl(purple_account_get_protocol_id(account));
if (!prpl)
return NULL;
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
prpl_name = prpl_info->list_icon(account, NULL);
acct_name = g_strdup(purple_escape_filename(purple_normalize(account,
purple_account_get_username(account))));
if (type == PURPLE_LOG_CHAT) {
char *temp = g_strdup_printf("%s.chat", purple_normalize(account, name));
target = purple_escape_filename(temp);
g_free(temp);
} else if(type == PURPLE_LOG_SYSTEM) {
target = ".system";
} else {
target = purple_escape_filename(purple_normalize(account, name));
}
dir = g_build_filename(purple_user_dir(), "logs", prpl_name, acct_name, target, NULL);
g_free(acct_name);
return dir;
}
/****************************************************************************
* LOGGER FUNCTIONS *********************************************************
****************************************************************************/
static PurpleLogLogger *current_logger = NULL;
static void logger_pref_cb(const char *name, PurplePrefType type,
gconstpointer value, gpointer data)
{
PurpleLogLogger *logger;
GSList *l = loggers;
while (l) {
logger = l->data;
if (purple_strequal(logger->id, value)) {
purple_log_logger_set(logger);
return;
}
l = l->next;
}
purple_log_logger_set(txt_logger);
}
PurpleLogLogger *purple_log_logger_new(const char *id, const char *name, int functions, ...)
{
#if 0
void(*create)(PurpleLog *),
gsize(*write)(PurpleLog *, PurpleMessageFlags, const char *, time_t, const char *),
void(*finalize)(PurpleLog *),
GList*(*list)(PurpleLogType type, const char*, PurpleAccount*),
char*(*read)(PurpleLog*, PurpleLogReadFlags*),
int(*size)(PurpleLog*),
int(*total_size)(PurpleLogType type, const char *name, PurpleAccount *account),
GList*(*list_syslog)(PurpleAccount *account),
void(*get_log_sets)(PurpleLogSetCallback cb, GHashTable *sets),
gboolean(*remove)(PurpleLog *log),
gboolean(*is_deletable)(PurpleLog *log))
#endif
PurpleLogLogger *logger;
va_list args;
g_return_val_if_fail(id != NULL, NULL);
g_return_val_if_fail(name != NULL, NULL);
g_return_val_if_fail(functions >= 1, NULL);
logger = g_new0(PurpleLogLogger, 1);
logger->id = g_strdup(id);
logger->name = g_strdup(name);
va_start(args, functions);
if (functions >= 1)
logger->create = va_arg(args, void *);
if (functions >= 2)
logger->write = va_arg(args, void *);
if (functions >= 3)
logger->finalize = va_arg(args, void *);
if (functions >= 4)
logger->list = va_arg(args, void *);
if (functions >= 5)
logger->read = va_arg(args, void *);
if (functions >= 6)
logger->size = va_arg(args, void *);
if (functions >= 7)
logger->total_size = va_arg(args, void *);
if (functions >= 8)
logger->list_syslog = va_arg(args, void *);
if (functions >= 9)
logger->get_log_sets = va_arg(args, void *);
if (functions >= 10)
logger->remove = va_arg(args, void *);
if (functions >= 11)
logger->is_deletable = va_arg(args, void *);
if (functions >= 12)
purple_debug_info("log", "Dropping new functions for logger: %s (%s)\n", name, id);
va_end(args);
return logger;
}
void purple_log_logger_free(PurpleLogLogger *logger)
{
g_free(logger->name);
g_free(logger->id);
g_free(logger);
}
void purple_log_logger_add (PurpleLogLogger *logger)
{
g_return_if_fail(logger);
if (g_slist_find(loggers, logger))
return;
loggers = g_slist_append(loggers, logger);
if (purple_strequal(purple_prefs_get_string("/purple/logging/format"), logger->id)) {
purple_prefs_trigger_callback("/purple/logging/format");
}
}
void purple_log_logger_remove (PurpleLogLogger *logger)
{
g_return_if_fail(logger);
loggers = g_slist_remove(loggers, logger);
}
void purple_log_logger_set (PurpleLogLogger *logger)
{
g_return_if_fail(logger);
current_logger = logger;
}
PurpleLogLogger *purple_log_logger_get()
{
return current_logger;
}
GList *purple_log_logger_get_options(void)
{
GSList *n;
GList *list = NULL;
PurpleLogLogger *data;
for (n = loggers; n; n = n->next) {
data = n->data;
if (!data->write)
continue;
list = g_list_append(list, data->name);
list = g_list_append(list, data->id);
}
return list;
}
gint purple_log_compare(gconstpointer y, gconstpointer z)
{
const PurpleLog *a = y;
const PurpleLog *b = z;
return b->time - a->time;
}
GList *purple_log_get_logs(PurpleLogType type, const char *name, PurpleAccount *account)
{
GList *logs = NULL;
GSList *n;
for (n = loggers; n; n = n->next) {
PurpleLogLogger *logger = n->data;
if (!logger->list)
continue;
logs = g_list_concat(logger->list(type, name, account), logs);
}
return g_list_sort(logs, purple_log_compare);
}
gint purple_log_set_compare(gconstpointer y, gconstpointer z)
{
const PurpleLogSet *a = y;
const PurpleLogSet *b = z;
gint ret = 0;
/* This logic seems weird at first...
* If either account is NULL, we pretend the accounts are
* equal. This allows us to detect duplicates that will
* exist if one logger knows the account and another
* doesn't. */
if (a->account != NULL && b->account != NULL) {
ret = strcmp(purple_account_get_username(a->account), purple_account_get_username(b->account));
if (ret != 0)
return ret;
}
ret = strcmp(a->normalized_name, b->normalized_name);
if (ret != 0)
return ret;
return (gint)b->type - (gint)a->type;
}
static guint
log_set_hash(gconstpointer key)
{
const PurpleLogSet *set = key;
/* The account isn't hashed because we need PurpleLogSets with NULL accounts
* to be found when we search by a PurpleLogSet that has a non-NULL account
* but the same type and name. */
return g_int_hash(&set->type) + g_str_hash(set->name);
}
static gboolean
log_set_equal(gconstpointer a, gconstpointer b)
{
/* I realize that the choices made for GList and GHashTable
* make sense for those data types, but I wish the comparison
* functions were compatible. */
return !purple_log_set_compare(a, b);
}
static void
log_add_log_set_to_hash(GHashTable *sets, PurpleLogSet *set)
{
PurpleLogSet *existing_set = g_hash_table_lookup(sets, set);
if (existing_set == NULL)
g_hash_table_insert(sets, set, set);
else if (existing_set->account == NULL && set->account != NULL)
g_hash_table_replace(sets, set, set);
else
purple_log_set_free(set);
}
GHashTable *purple_log_get_log_sets(void)
{
GSList *n;
GHashTable *sets = g_hash_table_new_full(log_set_hash, log_set_equal,
(GDestroyNotify)purple_log_set_free, NULL);
/* Get the log sets from all the loggers. */
for (n = loggers; n; n = n->next) {
PurpleLogLogger *logger = n->data;
if (!logger->get_log_sets)
continue;
logger->get_log_sets(log_add_log_set_to_hash, sets);
}
log_get_log_sets_common(sets);
/* Return the GHashTable of unique PurpleLogSets. */
return sets;
}
void purple_log_set_free(PurpleLogSet *set)
{
g_return_if_fail(set != NULL);
g_free(set->name);
if (set->normalized_name != set->name)
g_free(set->normalized_name);
g_slice_free(PurpleLogSet, set);
}
GList *purple_log_get_system_logs(PurpleAccount *account)
{
GList *logs = NULL;
GSList *n;
for (n = loggers; n; n = n->next) {
PurpleLogLogger *logger = n->data;
if (!logger->list_syslog)
continue;
logs = g_list_concat(logger->list_syslog(account), logs);
}
return g_list_sort(logs, purple_log_compare);
}
/****************************************************************************
* LOG SUBSYSTEM ************************************************************
****************************************************************************/
void *
purple_log_get_handle(void)
{
static int handle;
return &handle;
}
void purple_log_init(void)
{
void *handle = purple_log_get_handle();
purple_prefs_add_none("/purple/logging");
purple_prefs_add_bool("/purple/logging/log_ims", TRUE);
purple_prefs_add_bool("/purple/logging/log_chats", TRUE);
purple_prefs_add_bool("/purple/logging/log_system", FALSE);
purple_prefs_add_string("/purple/logging/format", "html");
html_logger = purple_log_logger_new("html", _("HTML"), 11,
NULL,
html_logger_write,
html_logger_finalize,
html_logger_list,
html_logger_read,
purple_log_common_sizer,
html_logger_total_size,
html_logger_list_syslog,
NULL,
purple_log_common_deleter,
purple_log_common_is_deletable);
purple_log_logger_add(html_logger);
txt_logger = purple_log_logger_new("txt", _("Plain text"), 11,
NULL,
txt_logger_write,
txt_logger_finalize,
txt_logger_list,
txt_logger_read,
purple_log_common_sizer,
txt_logger_total_size,
txt_logger_list_syslog,
NULL,
purple_log_common_deleter,
purple_log_common_is_deletable);
purple_log_logger_add(txt_logger);
old_logger = purple_log_logger_new("old", _("Old flat format"), 9,
NULL,
NULL,
old_logger_finalize,
old_logger_list,
old_logger_read,
old_logger_size,
old_logger_total_size,
NULL,
old_logger_get_log_sets);
purple_log_logger_add(old_logger);
purple_signal_register(handle, "log-timestamp",
#if SIZEOF_TIME_T == 4
purple_marshal_POINTER__POINTER_INT_BOOLEAN,
#elif SIZEOF_TIME_T == 8
purple_marshal_POINTER__POINTER_INT64_BOOLEAN,
#else
#error Unknown size of time_t
#endif
purple_value_new(PURPLE_TYPE_STRING), 3,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_LOG),
#if SIZEOF_TIME_T == 4
purple_value_new(PURPLE_TYPE_INT),
#elif SIZEOF_TIME_T == 8
purple_value_new(PURPLE_TYPE_INT64),
#else
# error Unknown size of time_t
#endif
purple_value_new(PURPLE_TYPE_BOOLEAN));
purple_prefs_connect_callback(NULL, "/purple/logging/format",
logger_pref_cb, NULL);
purple_prefs_trigger_callback("/purple/logging/format");
logsize_users = g_hash_table_new_full((GHashFunc)_purple_logsize_user_hash,
(GEqualFunc)_purple_logsize_user_equal,
(GDestroyNotify)_purple_logsize_user_free_key, NULL);
logsize_users_decayed = g_hash_table_new_full((GHashFunc)_purple_logsize_user_hash,
(GEqualFunc)_purple_logsize_user_equal,
(GDestroyNotify)_purple_logsize_user_free_key, NULL);
}
void
purple_log_uninit(void)
{
purple_signals_unregister_by_instance(purple_log_get_handle());
purple_log_logger_remove(html_logger);
purple_log_logger_free(html_logger);
html_logger = NULL;
purple_log_logger_remove(txt_logger);
purple_log_logger_free(txt_logger);
txt_logger = NULL;
purple_log_logger_remove(old_logger);
purple_log_logger_free(old_logger);
old_logger = NULL;
g_hash_table_destroy(logsize_users);
g_hash_table_destroy(logsize_users_decayed);
}
/****************************************************************************
* LOGGERS ******************************************************************
****************************************************************************/
static char *log_get_timestamp(PurpleLog *log, time_t when)
{
gboolean show_date;
char *date;
struct tm tm;
show_date = (log->type == PURPLE_LOG_SYSTEM) || (time(NULL) > when + 20*60);
date = purple_signal_emit_return_1(purple_log_get_handle(),
"log-timestamp",
log, when, show_date);
if (date != NULL)
return date;
tm = *(localtime(&when));
if (show_date)
return g_strdup(purple_date_format_long(&tm));
else
return g_strdup(purple_time_format(&tm));
}
/* NOTE: This can return msg (which you may or may not want to g_free())
* NOTE: or a newly allocated string which you MUST g_free(). */
static char *
convert_image_tags(const PurpleLog *log, const char *msg)
{
const char *tmp;
const char *start;
const char *end;
GData *attributes;
GString *newmsg = NULL;
tmp = msg;
while (purple_markup_find_tag("img", tmp, &start, &end, &attributes)) {
int imgid = 0;
char *idstr = NULL;
if (newmsg == NULL)
newmsg = g_string_new("");
/* copy any text before the img tag */
if (tmp < start)
g_string_append_len(newmsg, tmp, start - tmp);
if ((idstr = g_datalist_get_data(&attributes, "id")) != NULL)
imgid = atoi(idstr);
if (imgid != 0)
{
FILE *image_file;
char *dir;
PurpleStoredImage *image;
gconstpointer image_data;
char *new_filename = NULL;
char *path = NULL;
size_t image_byte_count;
image = purple_imgstore_find_by_id(imgid);
if (image == NULL)
{
/* This should never happen. */
/* This *does* happen for failed Direct-IMs -DAA */
g_string_free(newmsg, TRUE);
g_return_val_if_reached((char *)msg);
}
image_data = purple_imgstore_get_data(image);
image_byte_count = purple_imgstore_get_size(image);
dir = purple_log_get_log_dir(log->type, log->name, log->account);
new_filename = purple_util_get_image_filename(image_data, image_byte_count);
path = g_build_filename(dir, new_filename, NULL);
/* Only save unique files. */
if (!g_file_test(path, G_FILE_TEST_EXISTS))
{
if ((image_file = g_fopen(path, "wb")) != NULL)
{
if (!fwrite(image_data, image_byte_count, 1, image_file))
{
purple_debug_error("log", "Error writing %s: %s\n",
path, g_strerror(errno));
fclose(image_file);
/* Attempt to not leave half-written files around. */
unlink(path);
}
else
{
purple_debug_info("log", "Wrote image file: %s\n", path);
fclose(image_file);
}
}
else
{
purple_debug_error("log", "Unable to create file %s: %s\n",
path, g_strerror(errno));
}
}
/* Write the new image tag */
g_string_append_printf(newmsg, "<IMG SRC=\"%s\">", new_filename);
g_free(new_filename);
g_free(path);
}
/* Continue from the end of the tag */
tmp = end + 1;
}
if (newmsg == NULL)
{
/* No images were found to change. */
return (char *)msg;
}
/* Append any remaining message data */
g_string_append(newmsg, tmp);
return g_string_free(newmsg, FALSE);
}
void purple_log_common_writer(PurpleLog *log, const char *ext)
{
PurpleLogCommonLoggerData *data = log->logger_data;
if (data == NULL)
{
/* This log is new */
char *dir;
struct tm *tm;
const char *tz;
const char *date;
char *filename;
char *path;
dir = purple_log_get_log_dir(log->type, log->name, log->account);
if (dir == NULL)
return;
purple_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
tm = localtime(&log->time);
tz = purple_escape_filename(purple_utf8_strftime("%Z", tm));
date = purple_utf8_strftime("%Y-%m-%d.%H%M%S%z", tm);
filename = g_strdup_printf("%s%s%s", date, tz, ext ? ext : "");
path = g_build_filename(dir, filename, NULL);
g_free(dir);
g_free(filename);
log->logger_data = data = g_slice_new0(PurpleLogCommonLoggerData);
data->file = g_fopen(path, "a");
if (data->file == NULL)
{
purple_debug(PURPLE_DEBUG_ERROR, "log",
"Could not create log file %s\n", path);
if (log->conv != NULL)
purple_conversation_write(log->conv, NULL, _("Logging of this conversation failed."),
PURPLE_MESSAGE_ERROR, time(NULL));
g_free(path);
return;
}
g_free(path);
}
}
GList *purple_log_common_lister(PurpleLogType type, const char *name, PurpleAccount *account, const char *ext, PurpleLogLogger *logger)
{
GDir *dir;
GList *list = NULL;
const char *filename;
char *path;
if(!account)
return NULL;
path = purple_log_get_log_dir(type, name, account);
if (path == NULL)
return NULL;
if (!(dir = g_dir_open(path, 0, NULL)))
{
g_free(path);
return NULL;
}
while ((filename = g_dir_read_name(dir)))
{
if (purple_str_has_suffix(filename, ext) &&
strlen(filename) >= (17 + strlen(ext)))
{
PurpleLog *log;
PurpleLogCommonLoggerData *data;
struct tm tm;
#if defined (HAVE_TM_GMTOFF) && defined (HAVE_STRUCT_TM_TM_ZONE)
long tz_off;
const char *rest, *end;
time_t stamp = purple_str_to_time(purple_unescape_filename(filename), FALSE, &tm, &tz_off, &rest);
/* As zero is a valid offset, PURPLE_NO_TZ_OFF means no offset was
* provided. See util.h. Yes, it's kinda ugly. */
if (tz_off != PURPLE_NO_TZ_OFF)
tm.tm_gmtoff = tz_off - tm.tm_gmtoff;
if (stamp == 0 || rest == NULL || (end = strchr(rest, '.')) == NULL || strchr(rest, ' ') != NULL)
{
log = purple_log_new(type, name, account, NULL, stamp, NULL);
}
else
{
char *tmp = g_strndup(rest, end - rest);
tm.tm_zone = tmp;
log = purple_log_new(type, name, account, NULL, stamp, &tm);
g_free(tmp);
}
#else
time_t stamp = purple_str_to_time(filename, FALSE, &tm, NULL, NULL);
log = purple_log_new(type, name, account, NULL, stamp, (stamp != 0) ? &tm : NULL);
#endif
log->logger = logger;
log->logger_data = data = g_slice_new0(PurpleLogCommonLoggerData);
data->path = g_build_filename(path, filename, NULL);
list = g_list_prepend(list, log);
}
}
g_dir_close(dir);
g_free(path);
return list;
}
int purple_log_common_total_sizer(PurpleLogType type, const char *name, PurpleAccount *account, const char *ext)
{
GDir *dir;
int size = 0;
const char *filename;
char *path;
if(!account)
return 0;