Move common functions to mail.c
[rmail.git] / src / utils / mail.c
blobee41339fe63b06a538b3fec58626951c6936137d
1 #include <string.h>
2 #include <time.h>
4 #include "rfc822.h"
5 #include "rfc5322.h"
7 #include "md5_utils.h"
9 #include "mime.h"
11 #include "mail.h"
13 void init_mail_header(struct mail_header *hdr)
15 memset(hdr->from, 0, sizeof(hdr->from));
16 memset(hdr->subject, 0, sizeof(hdr->subject));
18 hdr->body_offset = 0L;
20 /* MIME */
21 /* [RFC2045]
22 * If no Content-Type header field is specified, default to:
23 * Content-type: text/plain; charset=us-ascii
25 hdr->mime.type = MIME_TYPE_TEXT;
26 hdr->mime.type_charset = MIME_TYPE_CHARSET_US_ASCII;
27 hdr->mime.subtype = MIME_SUBTYPE_PLAIN;
28 hdr->mime.disposition = MIME_DISPOSITION_UNKNOWN;
30 /* [RFC2045] "Content-Transfer-Encoding: 7BIT" is assumed if the
31 * Content-Transfer-Encoding header field is not present.
32 * ... If a Content-Transfer-Encoding header field appears as part
33 * of a message header, it applies to the entire body of that message.
34 * If a Content-Transfer-Encoding header field appears as part of an
35 * entity's headers, it applies only to the body of that entity.
36 * If an entity is of type "multipart" the Content-Transfer-Encoding
37 * is not permitted to have any value other than "7bit", "8bit" or
38 * "binary".
40 hdr->mime.transfer_encoding = MIME_ENCODING_7BIT;
43 void parse_mail_header(FILE *fh, struct mail_header *hdr)
45 char buf[1024];
46 char *crlf;
47 unsigned int fld_cont; /* RFC822 Header field folding flag */
48 unsigned int fa_valid; /* Valid From Address flag */
50 crlf = NULL;
51 fld_cont = 0;
52 fa_valid = 0;
54 /* Scan all lines */
55 do {
57 * Don't get a new line if we are coming from a folding
58 * field
60 if (!fld_cont) {
61 if (fgets(buf, sizeof(buf), fh) == NULL) {
62 break;
66 fld_cont = 0;
68 if (!strncasecmp(buf, "From:", 5)) {
69 rfc5322_extract_address(hdr->from, buf, sizeof(hdr->from));
70 fa_valid = 1;
71 } else if (!strncasecmp(buf, "Reply-to:", 9)) {
72 if (fa_valid)
73 continue;
75 if (!rfc5322_extract_address(hdr->from, buf, sizeof(hdr->from))) {
76 fa_valid = 1;
79 } else if (!strncasecmp(buf, "Subject:", 8)) {
80 rfc5322_extract_subject(hdr->subject, buf, sizeof(hdr->subject));
81 } else if (!strncasecmp(buf, "Date:", 5)) {
82 hdr->date = rfc822_parsedt(buf);
83 } else if (!strncasecmp(buf, "Content-Type:", 13)) {
84 mime_content_type(buf + 14, &hdr->mime);
86 while (fgets(buf, sizeof(buf), fh)) {
87 if (rfc822_check_field_folding(buf)) {
88 /* Decode folded line */
89 mime_content_type_parameter(buf, &hdr->mime);
90 } else {
91 fld_cont = 1;
92 break;
95 } else if (!strncasecmp(buf, "Content-Disposition:", 20)) {
96 mime_content_disposition(buf + 21, &hdr->mime);
97 } else if (!strncasecmp(buf, "Content-Transfer-Encoding:", 26)) {
98 mime_content_transfer_encoding(buf + 27, &hdr->mime);
99 } else {
100 /*printf("[SKIP] ------- %s", buf);*/
102 } while (!rfc822_check_header_end(buf));
104 /* If a valid 'from' address was found calc the MD5 of this address */
105 if (fa_valid) {
106 crlf = strrchr(hdr->from, '\n');
107 if (crlf)
108 *crlf = '\0';
110 md5_calc_sum(hdr->from, hdr->addr_md5);
113 /* Get file offset of the message body */
114 hdr->body_offset = ftell(fh);