11 #include <sys/types.h>
14 #include "string_utils.h"
15 #include "file_utils.h"
22 static void plain_body(char *line
)
28 static void qp_body(char *line
)
32 rfc2047_decode_quoted_printable(buf
, line
, sizeof(buf
) - 1);
36 static void parse_mail_body(FILE *fh
, struct mail_header
*hdr
)
39 void (*decode_body
)(char *line
);
41 switch (hdr
->mime
.transfer_encoding
) {
42 case MIME_ENCODING_QUOTED_PRINTABLE
:
43 decode_body
= qp_body
;
46 decode_body
= plain_body
;
50 (void) fseek(fh
, hdr
->body_offset
, SEEK_SET
);
52 while (fgets(buf
, sizeof(buf
), fh
)) {
57 static void parse_mail_header(FILE *fh
, struct mail_header
*hdr
)
63 while (fgets(buf
, sizeof(buf
), fh
)) {
64 if (!strncmp(buf
, "From:", 5)) {
65 rfc5322_extract_address(hdr
->from
, buf
, sizeof(hdr
->from
));
66 crlf
= strrchr(buf
, '\n');
69 } else if (!strncmp(buf
, "Subject:", 8)) {
70 rfc5322_extract_subject(hdr
->subject
, buf
, sizeof(hdr
->subject
));
71 } else if (!strncmp(buf
, "Date:", 5)) {
72 hdr
->date
= rfc822_parsedt(buf
);
73 } else if (!strncmp(buf
, "Content-Type:", 13)) {
74 mime_content_type(buf
, &hdr
->mime
);
75 } else if (!strncmp(buf
, "Content-Disposition:", 20)) {
76 hdr
->mime
.disposition
= mime_content_disposition(buf
);
77 } else if (!strncmp(buf
, "Content-Transfer-Encoding:", 26)) {
78 hdr
->mime
.transfer_encoding
= mime_content_transfer_encoding(buf
);
80 if (rfc822_check_header_end(buf
)) {
83 /*printf("[SKIP] ------- %s", buf); */
87 hdr
->body_offset
= ftell(fh
);
90 static void init_mail_header(struct mail_header
*hdr
)
92 memset(hdr
->from
, 0, sizeof(hdr
->from
));
93 memset(hdr
->subject
, 0, sizeof(hdr
->subject
));
95 hdr
->body_offset
= 0L;
98 hdr
->mime
.type
= MIME_TYPE_UNKNOWN
;
99 hdr
->mime
.disposition
= MIME_DISPOSITION_UNKNOWN
;
100 hdr
->mime
.transfer_encoding
= MIME_ENCODING_UNKNOWN
;
103 void usage(const char *appname
)
105 fprintf(stderr
, "Usage:\n%s [-h] -f <file>\n", appname
);
106 fprintf(stderr
, "Parameters:\n");
107 fprintf(stderr
, " -h Parse header only\n"
108 " -f <dir> Parse file\n");
111 int main(int argc
, char **argv
)
114 struct mail_header mail_hdr
;
122 while ((opt
= getopt(argc
, argv
, "f:h")) != -1) {
131 fprintf(stderr
, "Unknown parameter.\n");
145 fh
= fopen(file
, "r");
150 init_mail_header(&mail_hdr
);
152 parse_mail_header(fh
, &mail_hdr
);
154 fprintf(stderr
, "-------------------------------------------\n");
155 fprintf(stderr
, "Test file: %s\n", file
);
156 fprintf(stderr
, "-------------------------------------------\n");
157 fprintf(stderr
, "Date : %s\n", rfc822_mkdt(mail_hdr
.date
));
158 fprintf(stderr
, "From : %s\n", mail_hdr
.from
);
159 fprintf(stderr
, "Subject: %s\n", mail_hdr
.subject
);
161 debug_mime_header(&mail_hdr
.mime
);
163 fprintf(stderr
, "-------------------------------------------\n\n");
167 parse_mail_body(fh
, &mail_hdr
);