forked from sba1/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.c
3837 lines (3347 loc) · 101 KB
/
mail.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 mail.c
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "account.h"
#include "addressbook.h"
#include "codecs.h"
#include "configuration.h"
#include "debug.h"
#include "folder.h" /* for mail_compose_new() */
#include "mail.h"
#include "parse.h"
#include "pgp.h"
#include "phrase.h"
#include "signature.h"
#include "simplemail.h" /* for the callbacks() */
#include "SimpleMail_rev.h"
#include "smintl.h"
#include "support.h"
#include "support_indep.h"
#include "trans.h" /* for mail_upload_single() */
#include "arch.h"
char *stradd(char *src, const char *str1);
char *strnadd(char *src, const char *str1, int n);
/* porototypes */
//static char *mail_find_content_parameter_value(struct mail_complete *mail, char *attribute);
/*static struct header *mail_find_header(struct mail *mail, char *name);*/
static int mail_read_structure(struct mail_complete *mail);
/* the mime preample used in mime multipart messages */
static const char mime_preample[] =
{
"Warning: This is a message in MIME format. Your mail reader does not\n"
"support MIME. Some parts of this message will be readable as plain text.\n"
"To see the rest, you will need to upgrade your mail reader. Following are\n"
"some URLs where you can find MIME-capable mail programs for common\n"
"platforms:\n"
"\n"
" Amiga............: SimpleMail http://simplemail.sourceforge.net/\n"
" Unix.............: Metamail ftp://ftp.bellcore.com/nsb/\n"
" Windows/Macintosh: Eudora http://www.qualcomm.com/\n"
"\n"
"General info about MIME can be found at:\n"
"\n"
"http://www.cis.ohio-state.edu/hypertext/faq/usenet/mail/mime-faq/top.html\n"
};
static const char pgp_text[] =
{
"Content-Type: application/pgp-encrypted\n"
"\n"
"Version: 1\n"
"The following body part contains a PGP encrypted message. Either your\n"
"mail reader doesn't support MIME/PGP as specified in RFC 2015, or\n"
"the message was encrypted for someone else. To read the encrypted\n"
"message, run the next body part through Pretty Good Privacy.\n"
};
/*
* Index into the table below with the first byte of a UTF-8 sequence to
* get the number of trailing bytes that are supposed to follow it.
*/
static const char trailingBytesForUTF8[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};
/**************************************************************************
like strncpy() but for mail headers, returns the length of the string
**************************************************************************/
static int mailncpy(char *dest, const char *src, int n)
{
int i;
int len = 0;
char c;
char *dest_ptr = dest;
/* skip spaces */
while(n && isspace((unsigned char)(*src)))
{
src++;
n--;
}
for (i=0;i<n;i++)
{
c = *src++;
if (c==10 || c == 13 || c == 27) continue;
if (c=='\t') c=' ';
len++;
*dest_ptr++ = c;
}
return len;
}
/**
* @brief Determines the length (in bytes) from now until a wordend.
*
* @param buf defines the input which must be UTF8
* @return
*/
static int word_length(const char *buf)
{
unsigned char c;
int len = 0;
/* if word length counting starts on a space we count every space to the the word */
while ((c = *buf))
{
if (isspace(c))
{
if (c == 10 || c == 13) return 0;
len++;
} else break;
buf++;
}
while ((c = *buf))
{
if (isspace(c) || c == 0) break;
len++;
buf += trailingBytesForUTF8[c] + 1;
}
return len;
}
/**************************************************************************
Copies to quoting chars in to the buffer. len is the length of the
buffer to avoid overflow
**************************************************************************/
static void quoting_chars(char *buf, int len, char *text)
{
unsigned char c;
int new_color = 0;
int i=0;
int last_bracket = 0;
while ((c = *text++) && i<len-1)
{
if (c == '>')
{
last_bracket = i+1;
if (new_color == 1) new_color = 2;
else new_color = 1;
} else
{
if (c==10) break;
if ((new_color == 1 || new_color == 2) && c != ' ') break;
if (c==' ' && new_color == 0) break;
}
buf[i++] = c;
}
buf[last_bracket]=0;
}
/**
* Quotes the given text. The preferences entry
* user.config.write_reply_citeemptyl is respected.
*
* @param src
* @param len
* @return
*/
static char *quote_text(char *src, int len)
{
static char temp_buf[128];
int temp_len = 0;
string cited;
if (string_initialize(&cited,len))
{
int newline = 1;
int wrapped = 0; /* needed for user.config.write_reply_quote */
int line_len = 0;
if (user.config.write_reply_quote)
{
quoting_chars(temp_buf,sizeof(temp_buf),src);
temp_len = strlen(temp_buf);
}
while (len)
{
unsigned char c = *src;
int char_len; /* utf8 characters are not always one byte in size */
if (c==13)
{
src++;
len--;
continue;
}
if (c==10)
{
src++;
len--;
/* This actually means "wrap before quoting" */
if (user.config.write_reply_quote)
{
quoting_chars(temp_buf,sizeof(temp_buf),src);
if (temp_len == strlen(temp_buf) && wrapped)
{
/* the text has been wrapped previously and the quoting chars
are the same like the previous line, so the following text
probably belongs to the same paragraph */
len -= temp_len; /* skip the quoting chars */
src += temp_len;
wrapped = 0;
/* add a space to if this was the first quoting */
if (!temp_len)
{
string_append_char(&cited,' ');
line_len++;
}
continue;
}
temp_len = strlen(temp_buf);
wrapped = 0;
}
/* If newline is already true this is a empty line */
if (newline && user.config.write_reply_citeemptyl)
string_append_char(&cited,'>');
string_append_char(&cited,'\n');
newline = 1;
line_len = 0;
/* Strip the signature */
if (len >= 4 && user.config.write_reply_stripsig && !strncmp(src,"-- \n",4))
break;
continue;
}
if (newline)
{
if (user.config.write_wrap)
{
if (!strlen(temp_buf)) { string_append(&cited,"> "); line_len+=2;}
else {string_append_char(&cited,'>'); line_len++;}
} else
{
if (c=='>') { string_append_char(&cited,'>'); line_len++;}
else { string_append(&cited,"> "); line_len+=2;}
}
newline = 0;
}
/* This actually means "wrap before quoting" */
if (user.config.write_reply_quote)
{
if (isspace(c) && line_len + word_length(src) >= user.config.write_wrap)
{
src++;
string_append(&cited,"\n>");
string_append(&cited,temp_buf);
string_append_char(&cited,' ');
line_len=strlen(temp_buf)+2;
wrapped = 1; /* indicates that a word has been wrapped manually */
continue;
}
}
/* write out next character, it's an utf8 one */
char_len = trailingBytesForUTF8[c];
len -= char_len + 1;
if (len < 0) break; /* input not valid */
do
{
c = *src++; /* small overhead because for the first time we already have c */
string_append_char(&cited,c);
} while (char_len--);
line_len++;
}
return cited.str;
}
return NULL;
}
/**************************************************************************
Allocate an header and insert it into the header list. If
Avoid_duplicates is set to one only one header with the same
name will exists
**************************************************************************/
static int mail_complete_add_header(struct mail_complete *mail, char *name, int name_len,
char *contents, int contents_len, int avoid_duplicates)
{
struct header *header;
if (avoid_duplicates)
{
/* Look for an existing header with this name */
header = (struct header*)list_first(&mail->header_list);
while (header)
{
if (!mystrnicmp(header->name, name, name_len))
{
if (!header->name[name_len])
break;
}
header = (struct header*)node_next(&header->node);
}
} else header = NULL;
if (!header)
{
header = (struct header*)malloc(sizeof(struct header));
} else
{
free(header->contents);
free(header->name);
}
if (header)
{
char *new_name = (char*)malloc(name_len+1);
char *new_contents = (char*)malloc(contents_len+1);
if (new_name && new_contents)
{
new_name[mailncpy(new_name,name,name_len)] = 0;
new_contents[mailncpy(new_contents,contents,contents_len)] = 0;
header->name = new_name;
header->contents = new_contents;
list_insert_tail(&mail->header_list,&header->node);
return 1;
}
if (name) free(name);
if (contents) free(contents);
free(header);
}
return 0;
}
/**************************************************************************
Prepares the mail scanning
**************************************************************************/
void mail_scan_buffer_start(struct mail_scan *ms, struct mail_complete *mail, int avoid_duplicates)
{
memset(ms,0,sizeof(struct mail_scan));
ms->mail = mail;
ms->avoid_duplicates = avoid_duplicates;
}
/**************************************************************************
Finish the mail scanning and free's all memory which has been allocated
**************************************************************************/
void mail_scan_buffer_end(struct mail_scan *ms)
{
if (ms->line) free(ms->line);
}
/**************************************************************************
save the current header line in line, sets name_size and contents_size
return 0 if an error happened
**************************************************************************/
static int mail_scan_buffer_save_line(struct mail_scan *ms, char *name_start, int name_size, char *contents_start, int contents_size)
{
if (name_size + contents_size) /* else nothing has changed */
{
char *line;
if ((line = (char*)malloc(ms->name_size + ms->contents_size + name_size + contents_size)))
{
int pos = 0;
if (ms->line)
{
/* restore the old line */
pos = ms->name_size + ms->contents_size;
strncpy(line,ms->line,pos);
free(ms->line);
}
if (name_start)
{
strncpy(&line[pos],name_start,name_size);
pos += name_size;
}
if (contents_start)
{
strncpy(&line[pos],contents_start,contents_size);
}
ms->line = line;
ms->name_size += name_size;
ms->contents_size += contents_size;
} else return 0;
}
return 1;
}
/**************************************************************************
scans a buffer and fill the given mail instance. If more info is needed
1 is returned, else 0 (error handling not supported yet, but it's safe).
This function could now be replaced by a line version, since we now have
tcp_readln()
**************************************************************************/
int mail_scan_buffer(struct mail_scan *ms, char *mail_buf, int size)
{
unsigned char c;
char *name_start = NULL; /* start of the header */
int name_size = 0; /* size of the header's name (without colon) */
char *contents_start = NULL; /* start of the headers's contents */
int contents_size = 0; /* size of the contents */
int mode = ms->mode; /* 0 search name, 1 read name, 2 search contents, 3 read contents, 4 a LF is expected */
char *buf = mail_buf;
char *mail_buf_end = mail_buf + size; /* the end of the buffer */
struct mail_complete *mail = ms->mail;
if (mode == 1) name_start = mail_buf;
else if (mode == 3 || mode == 4) contents_start = mail_buf;
while (buf < mail_buf_end)
{
c = *buf;
if (mode == 4)
{
if (c != 10) mode = 3; /* the expected LF weren't there, so it's an error, once we returned here, but now we ignore it */
else
{
mode = 0;
ms->position++;
buf++;
continue;
}
}
if (mode == 0)
{
if (c != '\t' && c != ' ')
{
if (name_size || ms->name_size)
{
if (contents_start && !contents_size) contents_size = buf - contents_start - 1;
if (ms->line)
{
if (!mail_scan_buffer_save_line(ms,name_start,name_size,contents_start,contents_size))
return 0;
mail_complete_add_header(mail, ms->line, ms->name_size, ms->line + ms->name_size, ms->contents_size,ms->avoid_duplicates);
/* a previous call to this function saved a line */
free(ms->line);
ms->line = NULL;
ms->name_size = ms->contents_size = 0;
} else
{
/* no line has saved */
mail_complete_add_header(mail,name_start,name_size,contents_start,contents_size,ms->avoid_duplicates);
}
name_start = contents_start = NULL;
name_size = contents_size = 0;
}
if (c==10 || c==13)
{
mail->text_begin = ms->position+((c==13)?2:1);
mail->text_len = mail->info->size - mail->text_begin;
return 0; /* all headers have been read */
}
name_start = buf;
mode = 1;
} else
{
mode = 3; /* the header continues on the next line */
}
}
if (mode == 1 && c == ':')
{
name_size = buf - name_start;
mode = 2;
} else
{
if (mode == 2)
{
contents_start = buf;
if (!isspace(c))
{
mode = 3;
} else
{
if (c==10 || c == 13) /* the headers contents was empty */
{
mode = 3;
}
}
}
if (mode == 3 && (c == 10 || c == 13))
{
if (c==13) mode = 4; /* a LF (10) should follow now */
else
{
mode = 0;
}
}
}
buf++;
ms->position++;
}
/* if we are here the buffersize was too small */
{
if (/*name_start && !name_size &&*/mode == 1) name_size = buf - name_start;
if (contents_start && !contents_size && (mode == 3 || mode ==4 || mode == 0)) contents_size = buf - contents_start;
if (!mail_scan_buffer_save_line(ms,name_start,name_size,contents_start,contents_size))
return 0;
ms->mode = mode;
}
return 1;
}
/**************************************************************************
Find an compound object of a multipart/related mail (RFC2387)
(eighter by Content-ID or Content-Location). NULL if object. m is a mail
in the multipart/related mail.
**************************************************************************/
struct mail_complete *mail_find_compound_object(struct mail_complete *m, char *id)
{
int content_id = !mystrnicmp("cid:",id,4);
if (content_id)
{
unsigned char c;
id += 4;
while ((c=*id))
{
if (!isspace(c)) break;
id++;
}
}
while ((m = m->parent_mail))
{
if (!mystricmp(m->content_type,"multipart") && !mystricmp(m->content_subtype,"related"))
{
int i;
for (i=0;i<m->num_multiparts;i++)
{
if (content_id)
{
if (!mystricmp(id,m->multipart_array[i]->content_id)) return m->multipart_array[i];
}
}
return NULL;
}
}
return NULL;
}
/**************************************************************************
returns the first mail with the given mime type/subtype
(recursive). Return NULL if it doesn't exists.
**************************************************************************/
struct mail_complete *mail_find_content_type(struct mail_complete *m, char *type, char *subtype)
{
int i;
if (!mystricmp(m->content_type, type) && !mystricmp(m->content_subtype,subtype))
return m;
for (i=0;i < m->num_multiparts; i++)
{
struct mail_complete *rm = mail_find_content_type(m->multipart_array[i],type,subtype);
if (rm) return rm;
}
return NULL;
}
/**************************************************************************
Finds the initial mail which should be displayed. This is always the
first non multipart mail. For multipart/alternative mails it returns the
prefered one (depending on what the GUI prefers and how SimpleMail is
configured).
**************************************************************************/
struct mail_complete *mail_find_initial(struct mail_complete *m)
{
struct mail_complete *pref = NULL;
int alter = 0;
int i = 0;
while(m)
{
if (m->multipart_array)
{
if (!mystricmp(m->content_type, "multipart") &&
!mystricmp(m->content_subtype, "alternative")) alter = 1;
i = 0;
m = m->multipart_array[0];
} else
{
if (!alter) return m;
if (!pref) pref = m;
else
{
/* Currently we prefer always text/plain which is on the beginning anywhy */
if (!mystricmp(m->content_type, "text") && !mystricmp(m->content_subtype, "plain"))
return m;
}
i++;
if (i >= m->parent_mail->num_multiparts) return pref;
m = m->parent_mail->multipart_array[i];
}
}
return NULL;
}
/**************************************************************************
Returns the root of the mail
**************************************************************************/
struct mail_complete *mail_get_root(struct mail_complete *m)
{
while (m->parent_mail) m = m->parent_mail;
return m;
}
/**************************************************************************
Returns the next part of the mail (excluding multiparts)
**************************************************************************/
struct mail_complete *mail_get_next(struct mail_complete *m)
{
if (!m->multipart_array)
{
struct mail_complete *parent = m->parent_mail;
if (!parent) return NULL;
while (parent->multipart_array)
{
int i;
for (i=0;i < parent->num_multiparts;i++)
{
if (parent->multipart_array[i] == m)
{
i++;
if (i == parent->num_multiparts)
{
m = parent;
parent = m->parent_mail;
if (!parent) return NULL;
break;
}
m = parent->multipart_array[i];
while (m->multipart_array) m = m->multipart_array[0];
return m;
}
}
}
} else
{
while (m->multipart_array) m = m->multipart_array[0];
}
return m;
}
/**************************************************************************
Converts a number to base 18 character sign
**************************************************************************/
static char get_char_18(int val)
{
if (val >= 0 && val <= 9) return (char)('0' + val);
return (char)('a' + val-10);
}
/* a table with all filename extensions */
/* they are mapped 1 to 1 */
static char status_extensions[] =
{
0,'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','$','-'
};
/**************************************************************************
Returns a unique filename for a new mail
**************************************************************************/
char *mail_get_new_name(int status)
{
struct tm tm;
unsigned short day_secs;
unsigned int secs;
short i;
char dummy[8];
char status_buf[4];
char buf[64];
secs = sm_get_current_seconds();
sm_convert_seconds(secs, &tm);
day_secs = (tm.tm_min * 60) + tm.tm_sec;
dummy[4] = 0;
dummy[3] = get_char_18(day_secs % 18);
dummy[2] = get_char_18((day_secs / 18)%18);
dummy[1] = get_char_18((day_secs / 18 / 18)%18);
dummy[0] = get_char_18(day_secs / 18 / 18 / 18);
if (status == MAIL_STATUS_UNREAD) status_buf[0] = 0;
else {
status_buf[0] = '.';
status_buf[1] = status_extensions[status&0x1f];
status_buf[2] = 0;
}
for (i=0;;i++)
{
FILE *fp;
sm_snprintf(buf,sizeof(buf),"%02d%02d%04d%s.%03x%s",tm.tm_mday,tm.tm_mon,tm.tm_year,dummy,i,status_buf);
if ((fp = fopen(buf, "r"))) fclose(fp);
else break;
}
return mystrdup(buf);
}
/**************************************************************************
Returns wheather a mail is marked as deleted (on IMAP folders)
**************************************************************************/
int mail_is_marked_as_deleted(struct mail_info *mail)
{
return (*mail->filename == 'd') || (*mail->filename == 'D');
}
/**************************************************************************
Returns a new filename for the mail which matches the given status.
String is allocated with malloc
**************************************************************************/
char *mail_get_status_filename(char *oldfilename, int status_new)
{
int len = strlen(oldfilename);
char *filename = (char*)malloc(len+6);
if (filename)
{
char *suffix;
int new_suffix;
strcpy(filename,oldfilename);
suffix = strrchr(filename,'.');
if (!suffix) suffix = filename + len;
else
{
if (suffix[2])
{
/* the point is not the status point, so it must be added */
suffix = filename + len;
}
}
if (status_new < 0 || status_new >= 32)
{
*suffix = 0;
return filename;
}
new_suffix = status_extensions[status_new];
if (!new_suffix) *suffix = 0;
else
{
*suffix++ = '.';
*suffix++ = new_suffix;
*suffix = 0;
}
}
return filename;
}
/**************************************************************************
Identifies the status of the mail (using the filename)
**************************************************************************/
void mail_identify_status(struct mail_info *m)
{
char *suffix;
int i;
if (!m->filename) return;
suffix = strrchr(m->filename,'.');
if (!suffix || suffix[2])
{
m->status = MAIL_STATUS_UNREAD;
return;
}
/* decode the status information */
for (i=0;i<sizeof(status_extensions);i++)
{
if (suffix[1] == status_extensions[i])
m->status = i;
}
}
/**************************************************************************
creates a mail, initialize it to deault values
**************************************************************************/
struct mail_info *mail_info_create(void)
{
struct mail_info *m;
if ((m = (struct mail_info*)malloc(sizeof(struct mail_info))))
memset(m,0,sizeof(*m));
return m;
}
/**************************************************************************
creates a mail, initialize it to deault values
**************************************************************************/
struct mail_complete *mail_complete_create(void)
{
struct mail_complete *m;
if ((m = (struct mail_complete*)malloc(sizeof(struct mail_complete))))
{
memset(m,0,sizeof(*m));
if ((m->info = mail_info_create()))
{
list_init(&m->content_parameter_list);
list_init(&m->header_list);
return m;
}
mail_complete_free(m);
}
return NULL;
}
/**************************************************************************
This function fills in the header list if it is empty.
Return 1 on success
**************************************************************************/
int mail_read_header_list_if_empty(struct mail_complete *m)
{
char *buf;
FILE *fh;
if (list_first(&m->header_list)) return 1;
if (!m->info->filename) return 0;
if (!(fh = fopen(m->info->filename,"rb"))) return 0;
if ((buf = (char*)malloc(2048)))
{
struct mail_scan ms;
unsigned int bytes_read = 0;
mail_scan_buffer_start(&ms,m,0);
while ((bytes_read = fread(buf, 1, 2048, fh)))
{
if (!mail_scan_buffer(&ms,buf,bytes_read))
break; /* we have enough */
}
mail_scan_buffer_end(&ms);
}
free(buf);
fclose(fh);
return 1;
}
/**************************************************************************
scans a mail file and returns a filled (malloced) mail instance, NULL
if an error happened.
**************************************************************************/
struct mail_complete *mail_complete_create_from_file(char *filename)
{
struct mail_complete *m;
FILE *fh;
if ((m = mail_complete_create()))
{
unsigned int size = ~0;
if ((fh = fopen(filename,"rb")))
{
char *buf;
size = myfsize(fh); /* get the size of the file */
if (size) /* empty mails are no mails */
{
if ((buf = (char*)malloc(2048))) /* a small buffer to test the the new functions */
{
if ((m->info->filename = mystrdup(filename))) /* Not ANSI C */
{
struct mail_scan ms;
unsigned int bytes_read = 0;
m->info->size = size;
mail_scan_buffer_start(&ms,m,0);
while ((bytes_read = fread(buf, 1, 2048/*buf_size*/, fh)))
{
if (!mail_scan_buffer(&ms,buf,bytes_read))
break; /* we have enough */
}
mail_scan_buffer_end(&ms);
mail_process_headers(m);
mail_identify_status(m->info);
}
free(buf);
}
}
fclose(fh);
} else
{
mail_complete_free(m);
return NULL;
}
if (!size)
{
/* we remove files with a size of 0 */
/* remove(filename);*/
mail_complete_free(m);
return NULL;
}
}
return m;
}
/**************************************************************************
scans a mail file and returns a filled (malloced) mail_info instance, NULL
if an error happened.
**************************************************************************/
struct mail_info *mail_info_create_from_file(char *filename)
{
struct mail_complete *mail;
if ((mail = mail_complete_create_from_file(filename)))
{
struct mail_info *info;
info = mail->info;
mail->info = NULL;
mail_complete_free(mail);
return info;
}
return NULL;
}
/**
* Creates a mail to be send to a given address (fills out the to field
* and the body).
*
* @param from
* @param to_str_unexpanded
* @param replyto
* @param subject