-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaddressbook.c
1851 lines (1550 loc) · 46.5 KB
/
addressbook.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
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/**
* @file
*/
#include "addressbook.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "expatinc.h" /* IWYU pragma: keep */
#include "configuration.h"
#include "debug.h"
#include "hash.h"
#include "http.h"
#include "index.h"
#include "index_naive.h"
#include "parse.h"
#include "smintl.h"
#include "support_indep.h"
#include "request.h"
#include "support.h"
#include "timesupport.h"
static struct list group_list;
static struct list address_list;
/** Maps email addresses to addressbook entries */
static struct hash_table address_hash;
/** Maps all kind of information */
static struct index *address_index;
static struct list yamimport_group_list;
static void snailphonecpy(struct address_snail_phone *dest, struct address_snail_phone *src);
static void freesnailphone(struct address_snail_phone *dest);
/*****************************************************************************/
struct address_hash_entry
{
struct hash_entry e;
/**
* Number of addressbook entries associated to the email address. Ideally
* this is only one, but we support ambiguous ones.
*/
int num_abe;
int num_abe_allocated;
struct addressbook_entry_new **abe;
};
/******************* Group *****************/
/*****************************************************************************/
struct addressbook_group *addressbook_first_group(void)
{
return (struct addressbook_group*)list_first(&group_list);
}
/*****************************************************************************/
struct addressbook_group *addressbook_next_group(struct addressbook_group *grp)
{
return (struct addressbook_group*)node_next(&grp->node);
}
/*****************************************************************************/
struct addressbook_group *addressbook_duplicate_group(struct addressbook_group *srcgrp)
{
struct addressbook_group *grp = (struct addressbook_group *)malloc(sizeof(*grp));
if (!grp) return 0;
memset(grp,0,sizeof(struct addressbook_group));
if ((grp->name = mystrdup(srcgrp->name)))
{
grp->description = mystrdup(srcgrp->description);
return grp;
}
free(grp);
return NULL;
}
/*****************************************************************************/
void addressbook_free_group(struct addressbook_group *grp)
{
if (!grp) return;
free(grp->name);
free(grp->description);
free(grp);
}
/*****************************************************************************/
struct addressbook_group *addressbook_find_group_by_name(const utf8 *name)
{
struct addressbook_group *grp;
grp = (struct addressbook_group*)list_first(&group_list);
while (grp)
{
if (!mystricmp(name,grp->name)) return grp;
grp = (struct addressbook_group*)node_next(&grp->node);
}
return NULL;
}
/*****************************************************************************/
struct addressbook_group *addressbook_add_group(const utf8 *name)
{
struct addressbook_group *grp = (struct addressbook_group *)malloc(sizeof(*grp));
if (!grp) return 0;
memset(grp,0,sizeof(struct addressbook_group));
if ((grp->name = mystrdup(name)))
{
list_insert_tail(&group_list,&grp->node);
return grp;
}
free(grp);
return NULL;
}
/*****************************************************************************/
struct addressbook_group *addressbook_add_group_duplicate(struct addressbook_group *group)
{
if ((group = addressbook_duplicate_group(group)))
list_insert_tail(&group_list, &group->node);
return group;
}
/***************** Entry ********************/
/**
* Hash the given address book entry.
*
* @param e the entry to be hashed.
*/
static void addressbook_hash_entry(struct addressbook_entry_new *e)
{
int num_email_addresses;
int i;
num_email_addresses = array_length(e->email_array);
for (i=0; i < num_email_addresses; i++)
{
struct address_hash_entry *he;
char *email = e->email_array[i];
if (!(he = (struct address_hash_entry*)hash_table_lookup(&address_hash, email)))
{
char *dup_email = mystrdup(email);
if ((dup_email && (he = (struct address_hash_entry*)hash_table_insert(&address_hash, dup_email, 0))))
{
if (!(he->abe = (struct addressbook_entry_new **)malloc(sizeof(*he->abe)*4)))
goto out;
he->num_abe_allocated = 4;
he->num_abe = 0;
}
}
if (he && he->abe)
{
if (he->num_abe + 1 == he->num_abe_allocated)
{
int new_num_abe_allocated = he->num_abe_allocated*3/2;
struct addressbook_entry_new **new_abe;
if (!(new_abe = (struct addressbook_entry_new **)realloc(he->abe, new_num_abe_allocated*sizeof(*new_abe))))
goto out;
he->abe = new_abe;
he->num_abe_allocated = new_num_abe_allocated;
}
he->abe[he->num_abe++] = e;
}
out:
(void)1;
}
}
/*****************************************************************************/
struct addressbook_entry_new *addressbook_first_entry(void)
{
return (struct addressbook_entry_new*)list_first(&address_list);
}
/*****************************************************************************/
struct addressbook_entry_new *addressbook_next_entry(struct addressbook_entry_new *entry)
{
return (struct addressbook_entry_new*)node_next(&entry->node);
}
/*****************************************************************************/
struct addressbook_entry_new *addressbook_duplicate_entry_new(struct addressbook_entry_new *entry)
{
struct addressbook_entry_new *new_entry = (struct addressbook_entry_new*)malloc(sizeof(*entry));
if (new_entry)
{
new_entry->alias = mystrdup(entry->alias);
new_entry->description = mystrdup(entry->description);
new_entry->realname = mystrdup(entry->realname);
new_entry->notepad = mystrdup(entry->notepad);
new_entry->pgpid = mystrdup(entry->pgpid);
new_entry->homepage = mystrdup(entry->homepage);
new_entry->portrait = mystrdup(entry->portrait);
snailphonecpy(&new_entry->priv, &entry->priv);
snailphonecpy(&new_entry->work, &entry->work);
new_entry->dob_day = entry->dob_day;
new_entry->dob_month = entry->dob_month;
new_entry->dob_year = entry->dob_year;
new_entry->sex = entry->sex;
new_entry->email_array = array_duplicate(entry->email_array);
new_entry->group_array = array_duplicate(entry->group_array);
}
return new_entry;
}
/*****************************************************************************/
struct addressbook_entry_new *addressbook_add_entry_duplicate(struct addressbook_entry_new *entry)
{
if ((entry = addressbook_duplicate_entry_new(entry)))
{
list_insert_tail(&address_list, &entry->node);
addressbook_hash_entry(entry);
}
return entry;
}
/*****************************************************************************/
void addressbook_free_entry_new(struct addressbook_entry_new *entry)
{
free(entry->alias);
free(entry->description);
free(entry->realname);
free(entry->notepad);
free(entry->pgpid);
free(entry->homepage);
free(entry->portrait);
freesnailphone(&entry->priv);
freesnailphone(&entry->work);
array_free(entry->email_array);
array_free(entry->group_array);
free(entry);
}
/*****************************************************************************/
char *addressbook_get_entry_completing_part(struct addressbook_entry_new *entry, char *part, int *type_ptr)
{
int pl;
unsigned int i;
pl = mystrlen(part);
if (!mystrnicmp(part,entry->alias,pl) && entry->alias)
{
if (type_ptr) *type_ptr = 0;
return entry->alias + pl;
}
if (!mystrnicmp(part,entry->realname,pl))
{
if (type_ptr) *type_ptr = 1;
return entry->realname + pl;
}
for (i=0;i<array_length(entry->email_array);i++)
{
if (!mystrnicmp(part,entry->email_array[i],pl))
{
if (type_ptr) *type_ptr = i + 2;
return entry->email_array[i] + pl;
}
}
return NULL;
}
/*****************************************************************************/
struct addressbook_entry_new *addressbook_add_entry(const char *realname)
{
struct addressbook_entry_new *entry = (struct addressbook_entry_new*)malloc(sizeof(*entry));
if (!entry) return NULL;
memset(entry,0,sizeof(*entry));
entry->realname = mystrdup(realname);
list_insert_tail(&address_list,&entry->node);
return entry;
}
/*************** FileIO ******************/
/**
* Put a xml elelemt into a file (if string exists).
*
* @param fh
* @param element
* @param contents
*/
static void put_xml_element_string(FILE *fh, const char *element, const char *contents)
{
const char *src;
char c;
if (!contents) return;
src = contents;
fprintf(fh,"<%s>",element);
while (src && (c = *src))
{
if (((unsigned char)c)>=128)
{
unsigned int code;
src = uft8toucs(src, &code);
fprintf(fh,"&#x%04X;",code);
continue;
}
else if (c == '&') fputs("&",fh);
else fputc(c,fh);
src++;
}
fprintf(fh,"</%s>",element);
}
#ifndef SAVEDS
#ifdef __SASC
#define SAVEDS __saveds
#else
#define SAVEDS
#endif
#endif
static struct {
int newaddressbook_tag;
int newcontact_tag;
int newgroup_tag;
int private_tag;
int work_tag;
char *data_buf;
struct addressbook_entry_new current_entry;
struct addressbook_group current_group;
int is_classic_addressbook;
int contact_tag;
int group_tag;
char *classic_group_name;
} xml_context;
/**
* Start Tag.
*
* @param data
* @param el
* @param attr
*/
SAVEDS void xml_start_tag(void *data, const char *el, const char **attr)
{
/* new addressbook format */
if (!mystricmp("newaddressbook",el)) xml_context.newaddressbook_tag = 1;
else if (!mystricmp("newcontact",el))
{
if (!xml_context.newcontact_tag)
{
memset(&xml_context.current_entry,0,sizeof(xml_context.current_entry));
xml_context.newcontact_tag = 1;
}
}
else if (!mystricmp("newgroup",el))
{
if (!xml_context.newgroup_tag)
{
memset(&xml_context.current_group,0,sizeof(xml_context.current_group));
xml_context.newgroup_tag = 1;
}
}
else if (!mystricmp("private",el))
{
if (!xml_context.private_tag)
xml_context.private_tag = 1;
}
else if (!mystricmp("work",el))
{
if (!xml_context.work_tag)
xml_context.work_tag = 1;
}
else if (!mystricmp("addressbook",el))
{
xml_context.is_classic_addressbook = 1;
}
else if (!mystricmp("contact",el))
{
if (xml_context.is_classic_addressbook) xml_context.contact_tag++;
}
else if (!mystricmp("group",el))
{
if (xml_context.is_classic_addressbook) xml_context.group_tag++;
}
} /* End of start handler */
/**
* End Tag.
*
* @param data
* @param el
*/
SAVEDS void xml_end_tag(void *data, const char *el)
{
struct address_snail_phone *asp = NULL;
char *data_buf = xml_context.data_buf;
if (xml_context.private_tag) asp = &xml_context.current_entry.priv;
else if (xml_context.work_tag) asp = &xml_context.current_entry.work;
/* Remove ending spaces */
if (data_buf)
{
int len = strlen(data_buf);
while (len && isspace((unsigned char)data_buf[len-1]))
{
if (isspace((unsigned char)data_buf[len-1]))
len--;
}
}
if (!mystricmp("newaddressbook",el)) xml_context.newaddressbook_tag = 0;
else if (!mystricmp("addressbook",el)) xml_context.is_classic_addressbook = 0;
else if (!mystricmp("newcontact",el) || !mystricmp("contact",el))
{
if (xml_context.newcontact_tag || xml_context.contact_tag)
{
struct addressbook_entry_new *entry;
xml_context.newcontact_tag = 0;
if ((entry = (struct addressbook_entry_new*)malloc(sizeof(struct addressbook_entry_new))))
{
/* Add the group to the entry, if it is a classical address book */
if (xml_context.is_classic_addressbook && xml_context.classic_group_name)
{
xml_context.current_entry.group_array =
array_add_string(xml_context.current_entry.group_array,xml_context.classic_group_name);
}
*entry = xml_context.current_entry;
memset(&xml_context.current_entry,0,sizeof(xml_context.current_entry));
list_insert_tail(&address_list,&entry->node);
addressbook_hash_entry(entry);
}
}
} else if (!mystricmp("newgroup",el))
{
if (xml_context.newgroup_tag)
{
struct addressbook_group *group;
xml_context.newgroup_tag = 0;
if ((group = (struct addressbook_group*)malloc(sizeof(struct addressbook_group))))
{
*group = xml_context.current_group;
memset(&xml_context.current_group,0,sizeof(xml_context.current_group));
list_insert_tail(&group_list,&group->node);
}
}
}
else if (!mystricmp("group",el))
{
if (xml_context.newcontact_tag) xml_context.current_entry.group_array = array_add_string(xml_context.current_entry.group_array,data_buf);
else if (xml_context.is_classic_addressbook && xml_context.group_tag)
{
/* Old addressbook group */
xml_context.group_tag--;
if (!xml_context.group_tag)
{
free(xml_context.classic_group_name);
xml_context.classic_group_name = NULL;
}
}
}
else if (!mystricmp("private",el)) xml_context.private_tag = 0;
else if (!mystricmp("work",el)) xml_context.work_tag = 0;
else if (!mystricmp("alias",el))
{
if (xml_context.newcontact_tag)
xml_context.current_entry.alias = mystrdup(data_buf);
else if (xml_context.is_classic_addressbook && xml_context.group_tag == 1 && !xml_context.contact_tag)
{
if (!xml_context.classic_group_name)
{
xml_context.classic_group_name = mystrdup(data_buf);
addressbook_add_group(data_buf);
}
}
}
else if (!mystricmp("name",el))
{
if (xml_context.newgroup_tag) xml_context.current_group.name = mystrdup(data_buf);
else if (xml_context.newcontact_tag || xml_context.contact_tag) xml_context.current_entry.realname = mystrdup(data_buf);
}
else if (!mystricmp("description",el))
{
if (xml_context.newgroup_tag) xml_context.current_group.description = mystrdup(data_buf);
else if (xml_context.newcontact_tag || xml_context.contact_tag) xml_context.current_entry.description = mystrdup(data_buf);
}
else if (!mystricmp("email",el)) xml_context.current_entry.email_array = array_add_string(xml_context.current_entry.email_array,data_buf);
else if (!mystricmp("pgpid",el)) xml_context.current_entry.pgpid = mystrdup(data_buf);
else if (!mystricmp("homepage",el)) xml_context.current_entry.homepage = mystrdup(data_buf);
else if (!mystricmp("portrait",el)) xml_context.current_entry.portrait = mystrdup(data_buf);
else if (!mystricmp("note",el)) xml_context.current_entry.notepad = mystrdup(data_buf);
else if (!mystricmp("sex",el))
{
if (!mystricmp(data_buf,"female")) xml_context.current_entry.sex = 1;
else if (!mystricmp(data_buf,"male")) xml_context.current_entry.sex = 2;
}
else if (!mystricmp("birthday",el))
{
char *buf = data_buf;
int i;
i = atoi(buf);
if (i >= 1 && i <= 12)
{
xml_context.current_entry.dob_month = i;
if ((buf = strchr(buf,'/'))) buf++;
if (buf) xml_context.current_entry.dob_day = atoi(buf);
if ((buf = strchr(buf,'/'))) buf++;
if (buf) xml_context.current_entry.dob_year = atoi(buf);
}
} else
{
if (asp)
{
if (!mystricmp("title",el)) asp->title = mystrdup(data_buf);
else if (!mystricmp("organization",el)) asp->organization = mystrdup(data_buf);
else if (!mystricmp("street",el)) asp->street = mystrdup(data_buf);
else if (!mystricmp("city",el)) asp->city = mystrdup(data_buf);
else if (!mystricmp("zip",el)) asp->zip = mystrdup(data_buf);
else if (!mystricmp("state",el)) asp->state = mystrdup(data_buf);
else if (!mystricmp("country",el)) asp->country = mystrdup(data_buf);
else if (!mystricmp("mobil",el)) asp->mobil = mystrdup(data_buf);
else if (!mystricmp("fax",el)) asp->fax = mystrdup(data_buf);
else if (!mystricmp("phone",el))
{
if (!asp->phone1) asp->phone1 = mystrdup(data_buf);
else if (!asp->phone2) asp->phone2 = mystrdup(data_buf);
}
}
}
free(data_buf);
xml_context.data_buf = NULL;
}
/**************************************************************************
Converts a single UFT-8 Chracter to ISO-Latin 1 one, very very
incomplete.
**************************************************************************/
#if 0
static char *uft8toiso(char *chr, char *code)
{
unsigned int unicode;
char *ret = uft8toucs(chr,&unicode);
*code = unicode;
return ret;
}
#endif
/**
* Read the characters.
*
* @param data
* @param s
* @param len
*/
SAVEDS void xml_char_data(void *data, const XML_Char *s, int len)
{
if (xml_context.newcontact_tag || xml_context.newgroup_tag ||
xml_context.contact_tag || xml_context.group_tag)
{
int old_len = 0;
if (xml_context.data_buf)
old_len = strlen(xml_context.data_buf);
if ((xml_context.data_buf = (char*)realloc(xml_context.data_buf,old_len+len+1)))
{
unsigned char *src = (unsigned char*)s;
unsigned char *dest = (unsigned char*)xml_context.data_buf + old_len;
if (!old_len)
{
/* Skip trailing spaces */
while ((isspace(*src) && len)) /* is not really correct */
{
src++;
len--;
}
}
while (src && len)
{
*dest++ = *src++;
len--;
}
*dest = 0;
}
}
}
/*****************************************************************************/
int init_addressbook(void)
{
struct addressbook_entry_new *entry;
list_init(&group_list);
list_init(&address_list);
if (!hash_table_init_with_size(&address_hash, 8, sizeof(struct address_hash_entry), NULL))
{
return 0;
}
if (!(address_index = index_create(&index_naive, NULL)))
{
hash_table_clean(&address_hash);
return 0;
}
addressbook_load();
if (user.config.dont_add_default_addresses)
return 1;
/* Add the very important email addresses, in case they are not yet within the
* addressbook */
if (!addressbook_find_group_by_name("SimpleMail Team"))
addressbook_add_group("SimpleMail Team");
if (!(entry = addressbook_find_entry_by_address("[email protected]")))
{
if ((entry = addressbook_add_entry("Hynek Schlawack")))
{
entry->email_array = array_add_string(entry->email_array,"[email protected]");
entry->email_array = array_add_string(entry->email_array,"[email protected]");
entry->description = mystrdup(_("Original author of SimpleMail"));
entry->group_array = array_add_string(entry->group_array, "SimpleMail Team");
}
} else
{
if (addressbook_find_group_by_name("SimpleMail Team") &&
!array_contains(entry->group_array,"SimpleMail Team"))
{
char **newarray = array_add_string(entry->group_array,"SimpleMail Team");
if (newarray) entry->group_array = newarray;
}
}
if (!addressbook_find_entry_by_address("[email protected]"))
{
if ((entry = addressbook_find_entry_by_address("[email protected]")))
{
char **newarray;
array_free(entry->email_array);
entry->email_array = array_add_string(NULL,"[email protected]");
newarray = array_add_string(entry->group_array,"SimpleMail Team");
if (newarray) entry->group_array = newarray;
} else
{
if ((entry = addressbook_add_entry("Sebastian Bauer")))
{
entry->email_array = array_add_string(entry->email_array,"[email protected]");
entry->description = mystrdup(_("Original author of SimpleMail"));
entry->group_array = array_add_string(entry->group_array, "SimpleMail Team");
}
}
}
if (!addressbook_find_entry_by_address("[email protected]"))
{
if ((entry = addressbook_find_entry_by_address("[email protected]")))
{
array_free(entry->email_array);
entry->email_array = array_add_string(NULL,"[email protected]");
} else
if ((entry = addressbook_find_entry_by_address("[email protected]")))
{
array_free(entry->email_array);
entry->email_array = array_add_string(NULL,"[email protected]");
} else
{
if ((entry = addressbook_add_entry("Bernd Gollesch")))
{
entry->email_array = array_add_string(entry->email_array,"[email protected]");
entry->description = mystrdup(_("Contributor of SimpleMail"));
entry->group_array = array_add_string(entry->group_array, "SimpleMail Team");
}
}
}
if (!addressbook_find_entry_by_address("[email protected]"))
{
if ((entry = addressbook_find_entry_by_address("[email protected]")))
{
int idx;
char **newarray;
/* idx must exist, so no check for -1 is needed */
idx = array_index(entry->email_array,"[email protected]");
newarray = array_replace_idx(entry->email_array,idx,"[email protected]");
if (newarray) entry->email_array = newarray;
if (!array_contains(entry->group_array,"SimpleMail Team"))
{
newarray = array_add_string(entry->group_array,"SimpleMail Team");
if (newarray) entry->group_array = newarray;
}
} else
if ((entry = addressbook_add_entry("Nicolas Sallin")))
{
entry->email_array = array_add_string(entry->email_array,"[email protected]");
entry->description = mystrdup(_("MorphOS maintainer of SimpleMail"));
entry->group_array = array_add_string(entry->group_array, "SimpleMail Team");
entry->alias = mystrdup("Henes");
}
}
return 1;
}
/*****************************************************************************/
static void addressbook_free_hash_entry(struct hash_entry *entry, void *data)
{
struct address_hash_entry *ahe = (struct address_hash_entry*)entry;
free(ahe->abe);
ahe->num_abe = ahe->num_abe_allocated = 0;
}
/*****************************************************************************/
void addressbook_clear(void)
{
struct addressbook_entry_new *entry;
struct addressbook_group *group;
while ((entry = (struct addressbook_entry_new *)list_remove_tail(&address_list)))
addressbook_free_entry_new(entry);
while ((group = (struct addressbook_group*)list_remove_tail(&group_list)))
addressbook_free_group(group);
index_dispose(address_index);
hash_table_call_for_each_entry(&address_hash, addressbook_free_hash_entry, NULL);
hash_table_clear(&address_hash);
}
/*****************************************************************************/
void cleanup_addressbook(void)
{
addressbook_clear();
hash_table_clean(&address_hash);
}
/**
* Load the entries in the current group as XML.
*
* @param fh
*/
static void addressbook_load_entries(FILE *fh)
{
char *buf;
XML_Parser p;
memset(&xml_context,0,sizeof(xml_context));
if (!(buf = (char*)malloc(512))) return;
if (!(p = XML_ParserCreate(NULL)))
{
free(buf);
return;
}
XML_SetElementHandler(p, xml_start_tag, xml_end_tag);
XML_SetCharacterDataHandler(p, xml_char_data);
XML_UseParserAsHandlerArg(p);
for (;;)
{
int len;
len = fread(buf, 1, 512, fh);
if (len <= 0) break;
if (!XML_Parse(p, buf, len, 0))
{
int column = XML_GetErrorColumnNumber(p);
int line = XML_GetErrorLineNumber(p);
enum XML_Error err = XML_GetErrorCode(p);
SM_DEBUGF(5,("Parsing the xml file failed because %s (code %d) at %d/%d\n",
XML_ErrorString(err),err,line,column));
sm_request(NULL,_("Parsing the XML file failed because of\n%s (code %d)\nat line %d column %d"),
_("Ok"),XML_ErrorString(err),err,line,column);
break;
}
}
XML_Parse(p,buf,0,1); /* is_final have to be done */
XML_ParserFree(p);
free(buf);
}
/*****************************************************************************/
int addressbook_import_sm(char *filename)
{
int retval = 0;
FILE *fh;
if ((fh = fopen(filename,"r")))
{
addressbook_load_entries(fh);
fclose(fh);
retval = 1;
}
return retval;
}
static char *striplr(char *string)
{
int len;
char *line = (char *)string;
if (line)
{
if ((len = strlen(line)))
{
if(line[len-1] == '\n')
{
line[len-1] = 0;
}
}
}
return string;
}
/**
* Import addressbook entries from YAM.
*
* @param fp
* @return
*/
static int yam_import_entries(FILE *fp)
{
int rc = 1;
static char line[1024];
char *charset = user.config.default_codeset?user.config.default_codeset->name:NULL;
if (!fgets(line,sizeof(line),fp)) return 0;
while ((!feof(fp)) && (strncmp(line, "@ENDGROUP",9) != 0))
{
if (strncmp(line, "@USER", 5) == 0)
{
struct addressbook_group *grp;
struct addressbook_entry_new *newperson;
if ((newperson = addressbook_add_entry(NULL)))
{
newperson->alias = utf8create(striplr(line) + 6, charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->email_array = array_add_string(newperson->email_array, striplr(line));
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->realname = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->description = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->priv.phone1 = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->priv.street = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->priv.zip = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->priv.country = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->pgpid = utf8create(striplr(line), charset);
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->dob_day = 10*(line[0]-'0') + (line[1]-'0');
newperson->dob_month = 10*(line[2]-'0') + (line[3]-'0');
newperson->dob_year = 1000*(line[4]-'0') + 100*(line[5]-'0') + 10*(line[6]-'0') + (line[7]-'0');
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->portrait = mystrdup(striplr(line));
if (!fgets(line,sizeof(line),fp)) return 0;
newperson->homepage = mystrdup(striplr(line));
while (fgets(line,sizeof(line),fp))
{
if (!strncmp(line, "@ENDUSER", 8)) break;
}
/* Add YAM import groups to the person */
grp = (struct addressbook_group*)list_first(&yamimport_group_list);
while (grp)
{
/* Add YAM import group to the addressbook, if not already done */
if (!addressbook_find_group_by_name(grp->name))
addressbook_add_group_duplicate(grp);
newperson->group_array = array_add_string(newperson->group_array,grp->name);
grp = (struct addressbook_group*)node_next(&grp->node);
}
addressbook_hash_entry(newperson);
}
} else if(strncmp(line, "@GROUP", 6) == 0)
{
struct addressbook_group *newgroup = (struct addressbook_group *)malloc(sizeof(*newgroup));
/* Add the group to the YAM import group list */
memset(newgroup,0,sizeof(struct addressbook_group));
if (newgroup)
{
newgroup->name = striplr(utf8create(line + 7, charset));
if (fgets(line,sizeof(line),fp))
newgroup->description = striplr(utf8create(line, charset));
list_insert_tail(&yamimport_group_list,&newgroup->node);
}
rc = yam_import_entries(fp);
if (newgroup)
{
/* remove the group from the YAM import group list */
list_remove_tail(&yamimport_group_list);
addressbook_free_group(newgroup);