12 #include <sys/types.h>
15 #include "string_utils.h"
16 #include "file_utils.h"
21 #include "md5_utils.h"
24 static int hdr_only
= 0;
25 static int mime_only
= 0;
27 static void plain_body(char *line
)
33 static void qp_body(char *line
)
37 rfc2047_decode_quoted_printable(buf
, line
, sizeof(buf
) - 1);
41 static void parse_mail_body(FILE *fh
, struct mail_header
*hdr
)
44 void (*decode_body
)(char *line
);
46 switch (hdr
->mime
.transfer_encoding
) {
47 case MIME_ENCODING_QUOTED_PRINTABLE
:
48 decode_body
= qp_body
;
51 decode_body
= plain_body
;
55 (void) fseek(fh
, hdr
->body_offset
, SEEK_SET
);
57 while (fgets(buf
, sizeof(buf
), fh
)) {
62 void usage(const char *appname
)
64 fprintf(stderr
, "Usage:\n%s options\n", appname
);
65 fprintf(stderr
, "Options:\n");
66 fprintf(stderr
, " -h Parse header only\n"
67 " -m Parse MIME only\n"
68 " -f <file> Parse file\n"
69 " -d <maildir> Parse maildir\n");
72 static void parse_file(const char *file
)
75 struct mail_header mail_hdr
;
80 fh
= fopen(file
, "r");
85 init_mail_header(&mail_hdr
);
87 strncpy(mail_hdr
.filename
, file
, 1024);
89 parse_mail_header(fh
, &mail_hdr
);
91 fprintf(stderr
, "-------------------------------------------\n");
92 fprintf(stderr
, "Test file: %s\n", mail_hdr
.filename
);
93 fprintf(stderr
, "-------------------------------------------\n");
95 fprintf(stderr
, "Date : %s\n", rfc822_mkdt(mail_hdr
.date
));
96 fprintf(stderr
, "From : %s\n", mail_hdr
.from
);
97 fprintf(stderr
, "Subject: %s\n", mail_hdr
.subject
);
100 debug_mime_header(&mail_hdr
.mime
);
104 fprintf(stderr
, "-------------------------------------------\n");
106 parse_mail_body(fh
, &mail_hdr
);
113 int main(int argc
, char **argv
)
118 char *maildir_path
= NULL
;
120 char old_path
[PATH_MAX
];
123 while ((opt
= getopt(argc
, argv
, "d:f:hm")) != -1) {
126 maildir_path
= optarg
;
138 fprintf(stderr
, "Unknown parameter.\n");
144 if ((file
== NULL
) && (maildir_path
== NULL
)) {
148 if ((file
!= NULL
) && (maildir_path
!= NULL
)) {
149 fprintf(stderr
, "-f and -d are mutually esclusive!.\n");
158 /* Parse all maildir */
159 if (!getcwd(old_path
, PATH_MAX
)) {
160 fprintf(stderr
, "%s - GETCWD: %s\n", __func__
, strerror(errno
));
164 if (chdir(maildir_path
) < 0) {
165 fprintf(stderr
, "%s - CHDIR: %s\n", __func__
, strerror(errno
));
169 rc
= scan_dir(maildir_path
, parse_file
);
171 if (chdir(old_path
) < 0) {
172 fprintf(stderr
, "%s - Cannot restore path %s!\n", __func__
, strerror(errno
));