12 #include <sys/types.h>
15 #include "string_utils.h"
16 #include "file_utils.h"
23 static int hdr_only
= 0;
25 static void plain_body(char *line
)
31 static void qp_body(char *line
)
35 rfc2047_decode_quoted_printable(buf
, line
, sizeof(buf
) - 1);
39 static void parse_mail_body(FILE *fh
, struct mail_header
*hdr
)
42 void (*decode_body
)(char *line
);
44 switch (hdr
->mime
.transfer_encoding
) {
45 case MIME_ENCODING_QUOTED_PRINTABLE
:
46 decode_body
= qp_body
;
49 decode_body
= plain_body
;
53 (void) fseek(fh
, hdr
->body_offset
, SEEK_SET
);
55 while (fgets(buf
, sizeof(buf
), fh
)) {
60 static void parse_mail_header(FILE *fh
, struct mail_header
*hdr
)
66 while (fgets(buf
, sizeof(buf
), fh
)) {
67 if (!strncmp(buf
, "From:", 5)) {
68 rfc5322_extract_address(hdr
->from
, buf
, sizeof(hdr
->from
));
69 crlf
= strrchr(buf
, '\n');
72 } else if (!strncmp(buf
, "Subject:", 8)) {
73 rfc5322_extract_subject(hdr
->subject
, buf
, sizeof(hdr
->subject
));
74 } else if (!strncmp(buf
, "Date:", 5)) {
75 hdr
->date
= rfc822_parsedt(buf
);
76 } else if (!strncmp(buf
, "Content-Type:", 13)) {
77 mime_content_type(buf
, &hdr
->mime
);
78 } else if (!strncmp(buf
, "Content-Disposition:", 20)) {
79 hdr
->mime
.disposition
= mime_content_disposition(buf
);
80 } else if (!strncmp(buf
, "Content-Transfer-Encoding:", 26)) {
81 hdr
->mime
.transfer_encoding
= mime_content_transfer_encoding(buf
);
83 if (rfc822_check_header_end(buf
)) {
86 /*printf("[SKIP] ------- %s", buf); */
90 hdr
->body_offset
= ftell(fh
);
93 static void init_mail_header(struct mail_header
*hdr
)
95 memset(hdr
->from
, 0, sizeof(hdr
->from
));
96 memset(hdr
->subject
, 0, sizeof(hdr
->subject
));
98 hdr
->body_offset
= 0L;
101 hdr
->mime
.type
= MIME_TYPE_UNKNOWN
;
102 hdr
->mime
.disposition
= MIME_DISPOSITION_UNKNOWN
;
103 hdr
->mime
.transfer_encoding
= MIME_ENCODING_UNKNOWN
;
106 void usage(const char *appname
)
108 fprintf(stderr
, "Usage:\n%s options\n", appname
);
109 fprintf(stderr
, "Options:\n");
110 fprintf(stderr
, " -h Parse header only\n"
111 " -f <file> Parse file\n"
112 " -d <maildir> Parse maildir\n");
115 static void parse_file(const char *file
)
118 struct mail_header mail_hdr
;
123 fh
= fopen(file
, "r");
128 init_mail_header(&mail_hdr
);
130 parse_mail_header(fh
, &mail_hdr
);
132 fprintf(stderr
, "-------------------------------------------\n");
133 fprintf(stderr
, "Test file: %s\n", file
);
134 fprintf(stderr
, "-------------------------------------------\n");
135 fprintf(stderr
, "Date : %s\n", rfc822_mkdt(mail_hdr
.date
));
136 fprintf(stderr
, "From : %s\n", mail_hdr
.from
);
137 fprintf(stderr
, "Subject: %s\n", mail_hdr
.subject
);
139 debug_mime_header(&mail_hdr
.mime
);
141 fprintf(stderr
, "-------------------------------------------\n\n");
145 parse_mail_body(fh
, &mail_hdr
);
152 int main(int argc
, char **argv
)
157 char *maildir_path
= NULL
;
159 char old_path
[PATH_MAX
];
162 while ((opt
= getopt(argc
, argv
, "d:f:h")) != -1) {
165 maildir_path
= optarg
;
175 fprintf(stderr
, "Unknown parameter.\n");
181 if ((file
== NULL
) && (maildir_path
== NULL
)) {
185 if ((file
!= NULL
) && (maildir_path
!= NULL
)) {
186 fprintf(stderr
, "-f and -d are mutually esclusive!.\n");
195 /* Parse all maildir */
196 if (!getcwd(old_path
, PATH_MAX
)) {
197 fprintf(stderr
, "%s - GETCWD: %s\n", __func__
, strerror(errno
));
201 if (chdir(maildir_path
) < 0) {
202 fprintf(stderr
, "%s - CHDIR: %s\n", __func__
, strerror(errno
));
206 rc
= scan_dir(maildir_path
, parse_file
);
208 if (chdir(old_path
) < 0) {
209 fprintf(stderr
, "%s - Cannot restore path %s!\n", __func__
, strerror(errno
));