-
Notifications
You must be signed in to change notification settings - Fork 8
/
folder.c
5005 lines (4180 loc) · 121 KB
/
folder.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 folder.c
*/
#include "folder.h"
#include <ctype.h> /* toupper() */
#include <dirent.h> /* unix dir stuff */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h> /* state() */
#include <unistd.h>
#include "account.h"
#include "addresslist.h"
#include "atcleanup.h"
#include "codesets.h"
#include "configuration.h"
#include "debug.h"
#include "filter.h"
#include "imap.h"
#include "imap_helper.h"
#include "lists.h"
#include "mail_context.h"
#include "mail_support.h"
#include "parse.h"
#include "progmon.h"
#include "qsort.h"
#include "simplemail.h"
#include "smintl.h"
#include "status.h"
#include "string_pools.h"
#include "subthreads.h"
#include "support_indep.h"
#include "gui_main.h" /* gui_execute_arexx() */
#include "request.h"
#include "searchwnd.h"
#include "support.h"
#include "timesupport.h"
/*****************************************************************************/
#define FOLDER_INDEX_VERSION 8
struct folder_index
{
FILE *fh;
int pending;
int num_mails;
int unread_mails;
char *string_pool_name;
};
static void folder_index_read_them_all(struct folder_index *fi, struct string_pool *sp, struct mail_info ***out_ptr);
static void folder_remove_mail_info(struct folder *folder, struct mail_info *mail);
static struct folder_index *folder_index_open(struct folder *f);
static void folder_index_close(struct folder_index *fi);
/*****************************************************************************/
/* folder sort stuff to control the compare functions */
static int compare_primary_reverse;
static int (*compare_primary)(const struct mail_info *arg1, const struct mail_info *arg2, int reverse);
static int compare_secondary_reverse;
static int (*compare_secondary)(const struct mail_info *arg1, const struct mail_info *arg2, int reverse);
/* the global folder lock semaphore */
static semaphore_t folders_semaphore;
/* the global mail context for all mails associated to folders */
static mail_context *folder_mail_context;
/**
* Compare two mails with respect to their status.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_status(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc;
if ((arg1->flags & MAIL_FLAGS_NEW) && !(arg2->flags & MAIL_FLAGS_NEW)) rc = -1;
else if ((arg2->flags & MAIL_FLAGS_NEW) && !(arg1->flags & MAIL_FLAGS_NEW)) rc = 1;
else rc = (arg1->status & MAIL_STATUS_MASK) - (arg2->status & MAIL_STATUS_MASK);
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the from field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_from(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = utf8stricmp(mail_info_get_from(arg1),mail_info_get_from(arg2));
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the to field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_to(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = utf8stricmp(mail_info_get_to(arg1),mail_info_get_to(arg2));
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the subject field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_subject(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = utf8stricmp(mail_get_compare_subject(arg1->subject),mail_get_compare_subject(arg2->subject));
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the reply field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_reply(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = utf8stricmp(arg1->reply_addr, arg2->reply_addr);
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the date field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_date(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
if (arg1->seconds > arg2->seconds) return reverse?(-1):1;
else if (arg1->seconds == arg2->seconds) return 0;
return reverse?1:(-1);
}
/**
* Compares two mails with respect to the size field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_size(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
if (arg1->size > arg2->size) return reverse?(-1):1;
else if (arg1->size == arg2->size) return 0;
return reverse?1:(-1);
}
/**
* Compares two mails with respect to the filename field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_filename(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = mystricmp(arg1->filename, arg2->filename);
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the pop3 field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_pop3(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
int rc = mystricmp(mail_get_pop3_server(arg1), mail_get_pop3_server(arg2));
if (reverse) rc *= -1;
return rc;
}
/**
* Compares two mails with respect to the received (time) field.
*
* @param arg1 first mail to be compared
* @param arg2 second mail to be compared
* @param reverse negates the result
* @return > 0 if arg1 is larger than arg2
*/
static int mail_compare_recv(const struct mail_info *arg1, const struct mail_info *arg2, int reverse)
{
if (arg1->received > arg2->received) return reverse?(-1):1;
else if (arg1->received == arg2->received) return 0;
return reverse?1:(-1);
}
/**
* The general sorting function that is usable for qsort().
* It invokes compare_primary() and compare_secondary(). If
* both return 0, mails are sorted according to the date.
*
* @param arg1
* @param arg2
* @return
*/
static int mail_compare(const void *arg1, const void *arg2)
{
int ret = 0;
const struct mail_info *m1, *m2;
m1 = *(const struct mail_info**)arg1;
m2 = *(const struct mail_info**)arg2;
if (compare_primary) ret = compare_primary(m1,m2,compare_primary_reverse);
if (ret == 0 && compare_secondary) ret = compare_secondary(m1,m2,compare_secondary_reverse);
if (ret == 0) ret = mail_compare_date(m1,m2,0);
return ret;
}
/**
* Returns a pointer to a function that correspond to the given sort mode.
*
* @param sort_mode
* @param reverse
* @param folder_type
* @return
*/
static int (*get_compare_function(int sort_mode, int *reverse, int folder_type))(const struct mail_info *, const struct mail_info *, int)
{
if (sort_mode & FOLDER_SORT_REVERSE) *reverse = 1;
else *reverse = 0;
switch (sort_mode & FOLDER_SORT_MODEMASK)
{
case FOLDER_SORT_STATUS: return mail_compare_status;
case FOLDER_SORT_FROMTO: return (folder_type == FOLDER_TYPE_SEND)?mail_compare_to:mail_compare_from;
case FOLDER_SORT_SUBJECT: return mail_compare_subject;
case FOLDER_SORT_REPLY: return mail_compare_reply;
case FOLDER_SORT_DATE: return mail_compare_date;
case FOLDER_SORT_SIZE: return mail_compare_size;
case FOLDER_SORT_FILENAME: return mail_compare_filename;
case FOLDER_SORT_POP3: return mail_compare_pop3;
case FOLDER_SORT_RECV: return mail_compare_recv;
}
return NULL; /* thread */
}
/**
* Set the sort functions for mail_compare in accordance of the current
* settings.
*
* @param folder
*/
static void mail_compare_set_sort_mode(struct folder *folder)
{
compare_primary = get_compare_function(folder->primary_sort, &compare_primary_reverse, folder->type);
compare_secondary = get_compare_function(folder->secondary_sort, &compare_secondary_reverse, folder->type);
if (compare_primary == compare_secondary) compare_secondary = NULL;
}
/**
* Sort the given mail array by date and the secondary sorting_mode currently set.
*
* @param mails
* @param num_mails
*/
static void folder_sort_mails_by_date(struct mail_info **mails, int num_mails)
{
#define mail_date_lt(a,b) (((*a)->seconds < (*b)->seconds)?1:(((*a)->seconds > (*b)->seconds)?0:(compare_secondary?compare_secondary(*a,*b,compare_secondary_reverse)<0:0)))
QSORT(struct mail_info *, mails, num_mails, mail_date_lt);
}
/**
* Sort the given mail array by reverse date and the secondary sorting_mode currently set.
*
* @param mails
* @param num_mails
*/
static void folder_sort_mails_by_date_rev(struct mail_info **mails, int num_mails)
{
#define mail_date_lt_rev(a,b) (((*a)->seconds > (*b)->seconds)?1:(((*a)->seconds < (*b)->seconds)?0:(compare_secondary?compare_secondary(*a,*b,compare_secondary_reverse)<0:0)))
QSORT(struct mail_info *, mails, num_mails, mail_date_lt_rev);
}
static void folder_delete_mails(struct folder *folder);
static int folder_read_mail_infos(struct folder *folder, int only_num_mails);
static struct list folder_list; /**< The global folder list */
struct folder_node
{
struct node node; /**< Embedded node structure */
struct folder folder; /**< The actual folder, most follow */
};
/**
* Returns the folder node of a given plain folder.
*
* @param f the folder data
* @return the entire folder_node
*/
static struct folder_node *find_folder_node_by_folder(struct folder *f)
{
/* TODO: This could be done by pointer arithmetic much faster */
struct folder_node *node = (struct folder_node*)list_first(&folder_list);
while (node)
{
if (&node->folder == f)
{
return node;
}
node = (struct folder_node*)node_next(&node->node);
}
return NULL;
}
/**
* Writes a string into a filehandle. Returns 0 for an error else
* the number of bytes which has been written (at least two).
*
* @param fh
* @param str
* @param sp the optional string pool that can be used to get the string id.
* @return
*/
static int fwrite_str(FILE *fh, char *str, struct string_pool *sp)
{
if (str)
{
int sp_id;
int len;
unsigned char upper, lower;
int strl = strlen(str);
/* We only support up to 32767 characters */
if (strl > (1<<15))
{
return 0;
}
sp_id = sp?string_pool_get_id(sp, str):-1;
if (sp_id != -1)
{
upper = (sp_id >> 24) & 0x7f;
upper |= 1<<7; /* string pool ref mark */
lower = sp_id & 0xff;
if (fputc(upper,fh)==EOF) return 0;
if (fputc((sp_id >> 16) & 0xff, fh)==EOF) return 0;
if (fputc((sp_id >> 8) & 0xff, fh)==EOF) return 0;
if (fputc(lower,fh)==EOF) return 0;
return 4;
} else
{
upper = (strl/256)%256;
lower = strl%256;
if (fputc(upper,fh)==EOF) return 0;
if (fputc(lower,fh)==EOF) return 0;
len = fwrite(str,1,strl,fh);
if (len == strl) return len + 2;
}
}
fputc(0,fh);
fputc(0,fh);
return 2;
}
/**
* Reads a string from a filehandle. It is allocated with malloc(), except
* if sp_id is given and it filled with a meaningful value.
*
* @param fh
* @param sp
* @param zero_is_null if set to 1, NULL is returned for 0 length strings.
* Otherwise, an empty string is allocated if it is necessary.
* @param sp_id_ptr pointer to an integer variable, in which the id of the string
* is stored. If the pointer is non-NULL, no string will be allocated and returned
* if the string was read as an id.
* @return the string or NULL (which may or not may be due to an error)
*/
static char *fread_str(FILE *fh, struct string_pool *sp, int zero_is_null, int *sp_id_ptr)
{
unsigned char upper;
char *txt;
txt = NULL;
if (sp_id_ptr)
{
*sp_id_ptr = -1;
}
upper = fgetc(fh);
if (upper & (1<<7))
{
/* It's a string pool ref */
int sp_id;
unsigned char m2, m1, lower;
char *src_txt;
m2 = fgetc(fh);
m1 = fgetc(fh);
lower = fgetc(fh);
sp_id = ((upper & 0x7f) << 24) | (m2 << 16) | (m1 << 8) | lower;
src_txt = string_pool_get(sp, sp_id);
if (src_txt && sp_id_ptr)
{
*sp_id_ptr = sp_id;
return NULL;
}
if (src_txt && (txt = (char *)malloc(strlen(src_txt) + 1)))
{
strcpy(txt, src_txt);
}
} else
{
int len;
len = upper << 8;
upper = fgetc(fh);
len += upper;
if (zero_is_null && !len)
{
return NULL;
}
if ((txt = (char*)malloc(len+1)))
{
fread(txt,1,len,fh);
txt[len]=0;
} else
{
fseek(fh, len, SEEK_CUR);
}
}
return txt;
}
/**
* Reads a string from a file handle. It is allocated with malloc().
* Returns NULL if the string has an length of 0.
*
* @param fh
* @param sp_id_ptr pointer to an integer variable, in which the id of the string
* is stored. If the pointer is non-NULL, no string will be allocated and returned
* if the string was read as an id.
* @return the string or NULL (which may or not may be due to an error)
*/
static char *fread_str_no_null(FILE *fh, struct string_pool *sp, int *sp_id_ptr)
{
return fread_str(fh, sp, 1, sp_id_ptr);
}
static int folder_config_load(struct folder *f);
/**
* Returns the filename of the string pool that belongs to the index.
*
* @param f
* @return the name of the folder that needs to be freed via free().
*/
static char *folder_get_string_pool_name(struct folder *f)
{
char *sp_name;
if ((sp_name = (char *)malloc(strlen(f->path) + 12)))
{
strcpy(sp_name, f->path);
strcat(sp_name, ".index.sp");
}
return sp_name;
}
/**
* Opens the indexfile of the given folder and return the filehandle.
*
* @param f the folder for which the index file should be opened.
* @param mode the open mode, like fopen().
* @return the filehandle or NULL on error.
*/
static FILE *folder_indexfile_open(struct folder *f, const char *mode)
{
FILE *fh;
char *path;
char *index_name;
char *filename_ptr;
char cpath[256];
if (!f || !f->path) return 0;
if (f->special == FOLDER_SPECIAL_GROUP)
{
SM_DEBUGF(5, ("Folder \"%s\" is a group. No index file support for now.\n"));
return 0;
}
if (!(path = mystrdup(f->path))) return 0;
*sm_path_part(path) = 0;
filename_ptr = sm_file_part(f->path);
index_name = (char*)malloc(strlen(filename_ptr)+16);
if (!index_name)
{
free(path);
return 0;
}
sprintf(index_name,"%s.index",filename_ptr);
getcwd(cpath, sizeof(cpath));
if(chdir(path) == -1)
{
free(index_name);
free(path);
return 0;
}
fh = fopen(index_name,mode);
chdir(cpath);
free(index_name);
free(path);
return fh;
}
/**
* Delete the indexfile of the given folder.
*
* @param f the folder for which the index file should be deleted.
*/
static void folder_indexfile_delete(struct folder *f)
{
char *path;
char *index_name;
char *filename_ptr;
char cpath[256];
if (!f || !f->path) return;
if (!(path = mystrdup(f->path))) return;
*sm_path_part(path) = 0;
filename_ptr = sm_file_part(f->path);
index_name = (char*)malloc(strlen(filename_ptr)+16);
if (!index_name)
{
free(path);
return;
}
sprintf(index_name,"%s.index",filename_ptr);
getcwd(cpath, sizeof(cpath));
if(chdir(path) == -1)
{
free(index_name);
free(path);
return;
}
remove(index_name);
chdir(cpath);
free(index_name);
free(path);
}
/**
* @brief invalidates (i.e., deletes) the indexfile of the given folder.
*
* @param folder specifies the folder of which the index should
* be made invalid.
*/
static void folder_indexfile_invalidate(struct folder *folder)
{
if (folder->index_uptodate)
{
folder_indexfile_delete(folder);
folder->index_uptodate = 0;
}
}
/*****************************************************************************/
void folder_delete_all_indexfiles(void)
{
struct folder *f = folder_first();
while (f)
{
folder_indexfile_delete(f);
f->index_uptodate = 0;
f = folder_next(f);
}
}
/**
* Prepare the folder to contain the given amount of mails.
*
*
* @param folder the folder to be prepared
* @param num_mails number of mails that the folder shall contain at least
* @return 0 on failure, else 1
*/
static int folder_prepare_for_additional_mails(struct folder *folder, int num_mails)
{
struct mail_info **new_mail_array;
int new_mail_array_allocated;
if (num_mails + folder->num_mails < folder->mail_info_array_allocated)
{
return 1;
}
new_mail_array_allocated = folder->mail_info_array_allocated + num_mails + 5;
new_mail_array = (struct mail_info**)realloc(folder->mail_info_array, new_mail_array_allocated*sizeof(struct mail_info*));
if (!new_mail_array) return 0;
folder->mail_info_array_allocated = new_mail_array_allocated;
folder->mail_info_array = new_mail_array;
return 1;
}
struct folder_index_magic
{
char magic[4];
int ver;
};
/**
* Resets the indexuptodate field within the folders indexfile.
*
* This flag is an indication that the indexfile is not uptodate
* (does not reflect the contents of the folder)
*
* The use of this flags is, if if this flag is set and there are
* no pending mails the indexfile mustn't be read instead the whole
* directory must be rescanned. This should only happen if SimpleMail
* has not been shut down properly.
*
* @param folder the folder whose pending flag should be set.
* @return 0 on failure, else 1.
*/
static int folder_set_pending_flag_in_indexfile(struct folder *folder)
{
int rc = 0;
FILE *fh;
SM_ENTER;
if (folder->special == FOLDER_SPECIAL_GROUP) SM_RETURN(0,"%ld");
if ((fh = folder_indexfile_open(folder,"rb+")))
{
/* Move at the position of the field */
if (fseek(fh,sizeof(struct folder_index_magic),SEEK_SET) == 0)
{
int pending = 1;
if (fwrite(&pending,1,4,fh) == 4)
rc = 1;
}
fclose(fh);
}
if (rc)
{
SM_DEBUGF(10,("Set pending flag in index file of folder %s\n",folder->name));
}
SM_RETURN(rc,"%ld");
}
/*****************************************************************************/
int folder_add_mail(struct folder *folder, struct mail_info *mail, int sort)
{
int i,pos;
if (!folder->mail_infos_loaded)
{
/* No mail infos has been read. We could load them now but
instead we add the new mail to the pending mail array. This
makes this operation a lot of faster and the overall operation
of SimpleMail as well if this folder is not viewed (which is
often the case for the spam folder) */
/* Add the mail to the pending array, this is not necessary if the
there is no usable indexfile for this folder (num_index_mails == -1
in such case) */
if (folder->num_index_mails == -1)
{
SM_DEBUGF(10,("Mail added not to pending mails because number of mails is not known (indexfile not available)\n"));
return -1;
}
if (folder->num_pending_mails == folder->pending_mail_info_array_allocated)
{
struct mail_info **array = (struct mail_info**)realloc(folder->pending_mail_info_array,(folder->pending_mail_info_array_allocated + 16)*sizeof(struct mail_info*));
if (!array) return 0;
folder->pending_mail_info_array_allocated += 16;
folder->pending_mail_info_array = array;
}
/* set the pending flag within the indexfile if there were no pending mails before */
if (folder->num_pending_mails == 0)
{
if (!folder_set_pending_flag_in_indexfile(folder)) return 0;
}
folder->pending_mail_info_array[folder->num_pending_mails++] = mail;
folder->num_index_mails++;
if (mail_info_get_status_type(mail) == MAIL_STATUS_UNREAD) folder->unread_mails++;
if (mail->flags & MAIL_FLAGS_NEW) folder->new_mails++;
SM_DEBUGF(10,("Mail has been successfully added as a pending mail\n"));
return -1;
}
/* free the sorted mail array */
if (folder->sorted_mail_info_array && !sort)
{
free(folder->sorted_mail_info_array);
folder->sorted_mail_info_array = NULL;
} else if (!folder->sorted_mail_info_array && sort)
{
void *handle = NULL;
/* this should sort the folder */
folder_next_mail_info(folder, &handle);
}
/* delete the indexfile if not already done */
folder_indexfile_invalidate(folder);
if (folder->mail_info_array_allocated == folder->num_mails)
{
folder->mail_info_array_allocated += 50;
folder->mail_info_array = (struct mail_info **)realloc(folder->mail_info_array,folder->mail_info_array_allocated*sizeof(struct mail_info *));
if (folder->sorted_mail_info_array)
folder->sorted_mail_info_array = (struct mail_info **)realloc(folder->sorted_mail_info_array,folder->mail_info_array_allocated*sizeof(struct mail_info *));
}
if (!folder->mail_info_array)
{
folder->mail_info_array_allocated = 0;
return -1;
}
if (mail->message_id)
{
/* check if there is already an mail with the same message id, this would cause problems */
for (i=0;i<folder->num_mails;i++)
{
struct mail_info *fm = folder->mail_info_array[i];
if (!(mystricmp(mail->message_id,fm->message_id)))
{
if (mail->message_id)
{
free(mail->message_id);
mail->message_id = NULL;
}
}
}
}
if (folder->sorted_mail_info_array)
{
mail_compare_set_sort_mode(folder);
#if 0
/* this search routine has O(n) but should be improved to O(log n) with binary serach */
for (pos=0;pos<folder->num_mails;pos++)
{
if (mail_compare(&folder->sorted_mail_info_array[pos],&mail) > 0) break;
}
#else
/* here comes the binary search. because this is my (= bgol) first try in such an
algorythmus I left the old code in. */
{
int low=0, high=folder->num_mails+1;
/* For the beginning, low must be (start-1) and high must be (end+1).
As we are in C the array goes from start=0 to end=(n-1) but this code
doesn't work with low < 0. So, I'm calculating the pos counter one to
high and use for the mail_compare() [pos-1]. This has the advantage
that the pos counter is allready correct after the calculation.
The search also doesn't stop at a match (mail_compare==0) because the
mail must be placed at the end of the list of same mails. */
pos = (low + high) / 2;
while (pos > low)
{
if (mail_compare(&folder->sorted_mail_info_array[pos-1],&mail) <= 0)
low = pos;
else
high = pos;
pos = (low + high) / 2;
}
}
#endif
/* G++ didn't compile the non-C++ (gcc 8.2) so workaround this problem */
#ifdef __cplusplus
struct mail_info *mi = mail;
#endif
memmove(&folder->sorted_mail_info_array[pos+1],&folder->sorted_mail_info_array[pos],(folder->num_mails - pos)*sizeof(struct mail*));
#ifdef __cplusplus
folder->sorted_mail_info_array[pos] = mi;
#else
folder->sorted_mail_info_array[pos] = mail;
#endif
} else pos = folder->num_mails;
folder->mail_info_array[folder->num_mails++] = mail;
if (folder->num_mails > folder->num_index_mails) folder->num_index_mails = folder->num_mails;
if (mail_info_get_status_type(mail) == MAIL_STATUS_UNREAD) folder->unread_mails++;
if (mail->flags & MAIL_FLAGS_NEW) folder->new_mails++;
if (mail->flags & MAIL_FLAGS_PARTIAL) folder->partial_mails++;
/* Disabled because slow, buggy, and not really used */
#if 0
/* sort the mails for threads */
if (mail->message_id)
{
for (i=0;i<folder->num_mails-1;i++) /* ignore the last mail since it is this mail */
{
struct mail_info *fm = folder->mail_info_array[i];
if (!(mystricmp(mail->message_id,fm->message_reply_id)))
{
if (!mail->sub_thread_mail)
{
mail->sub_thread_mail = fm;
} else
{
struct mail_info *m = mail->sub_thread_mail;
struct mail_info *next = m->next_thread_mail;
while (next)
{
m = next;
next = next->next_thread_mail;
}
m->next_thread_mail = fm;
}
fm->child_mail = 1;
}
}
}
if (mail->message_reply_id)
{
for (i=0;i<folder->num_mails-1;i++) /* ignore the last mail since it is this mail */
{
struct mail_info *fm = folder->mail_info_array[i];
if (!(mystricmp(mail->message_reply_id,fm->message_id)))
{
if (!fm->sub_thread_mail)
{
fm->sub_thread_mail = mail;
} else
{
struct mail_info *m = fm->sub_thread_mail;
struct mail_info *next = m->next_thread_mail;
while (next)
{
m = next;
next = next->next_thread_mail;
}
m->next_thread_mail = mail;
}
mail->child_mail = 1;
break;
}
}
}
#endif
return pos;
}
/*****************************************************************************/
static int folder_add_mails(struct folder *f, struct mail_info **mails, int num)
{
int i;
if (!mails)
{
return 0;
}
folder_prepare_for_additional_mails(f, num);
for (i = 0; i < num; i++)
{
if (!mails[i]) continue;
if (folder_add_mail(f, mails[i], 0) == -1)
{
return 0;
}
}
return 1;
}
/**
* Remove the given mail from the given folder. It does not free the mail.
*
* @param folder the folder in which the mail resides.
* @param mail the mail to be removed.
*/
static void folder_remove_mail_info(struct folder *folder, struct mail_info *mail)
{
int i;
#if 0
struct mail_info *submail;
#endif
/* lock the folder, because we are going to remove something */
folder_lock(folder);
/* If mails info is not read_yet, the mail cannot be associated to this folder */
if (!folder->mail_infos_loaded)
{
return;
}
/* free the sorted mail array */
if (folder->sorted_mail_info_array)
{
free(folder->sorted_mail_info_array);
folder->sorted_mail_info_array = NULL;
}
/* delete the indexfile if not already done */
folder_indexfile_invalidate(folder);
#if 0
for (i=0; i < folder->num_mails; i++)
{
if (folder->mail_info_array[i]->sub_thread_mail == mail)
{