From eb8ffd4fd8982a7c422aa4b730c360f183af9753 Mon Sep 17 00:00:00 2001 From: rbbrnc Date: Wed, 6 Apr 2011 14:28:58 +0200 Subject: [PATCH] Add maildir scan option --- src/utils/test_view_mail.c | 100 +++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 30 deletions(-) diff --git a/src/utils/test_view_mail.c b/src/utils/test_view_mail.c index c70477b..c305959 100644 --- a/src/utils/test_view_mail.c +++ b/src/utils/test_view_mail.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -19,6 +20,8 @@ #include "mime.h" #include "mail.h" +static int hdr_only = 0; + static void plain_body(char *line) { printf("%s", line); @@ -102,28 +105,69 @@ static void init_mail_header(struct mail_header *hdr) void usage(const char *appname) { - fprintf(stderr, "Usage:\n%s [-h] -f \n", appname); - fprintf(stderr, "Parameters:\n"); + fprintf(stderr, "Usage:\n%s options\n", appname); + fprintf(stderr, "Options:\n"); fprintf(stderr, " -h Parse header only\n" - " -f Parse file\n"); + " -f Parse file\n" + " -d Parse maildir\n"); } -int main(int argc, char **argv) +static void parse_file(const char *file) { FILE *fh; struct mail_header mail_hdr; + if (file_type(file)) + return; + + fh = fopen(file, "r"); + if (!fh) { + return; + } + + init_mail_header(&mail_hdr); + + parse_mail_header(fh, &mail_hdr); + + fprintf(stderr, "-------------------------------------------\n"); + fprintf(stderr, "Test file: %s\n", file); + fprintf(stderr, "-------------------------------------------\n"); + fprintf(stderr, "Date : %s\n", rfc822_mkdt(mail_hdr.date)); + fprintf(stderr, "From : %s\n", mail_hdr.from); + fprintf(stderr, "Subject: %s\n", mail_hdr.subject); + + debug_mime_header(&mail_hdr.mime); + + fprintf(stderr, "-------------------------------------------\n\n"); + + if (!hdr_only) { + /* Message Body */ + parse_mail_body(fh, &mail_hdr); + } + + fclose(fh); +} + + +int main(int argc, char **argv) +{ int opt; char *file = NULL; - int hdr_only = 0; + char *maildir_path = NULL; + char old_path[PATH_MAX]; + int rc; - while ((opt = getopt(argc, argv, "f:h")) != -1) { + while ((opt = getopt(argc, argv, "d:f:h")) != -1) { switch (opt) { + case 'd': + maildir_path = optarg; + break; case 'f': file = optarg; break; + case 'h': hdr_only = 1; break; @@ -134,41 +178,37 @@ int main(int argc, char **argv) } } - if (file == NULL) { + if ((file == NULL) && (maildir_path == NULL)) { usage(argv[0]); return -1; } + if ((file != NULL) && (maildir_path != NULL)) { + fprintf(stderr, "-f and -d are mutually esclusive!.\n"); + usage(argv[0]); + } - if (file_type(file)) - return -1; + if (file != NULL) { + parse_file(file); + return 0; + } - fh = fopen(file, "r"); - if (!fh) { + /* Parse all maildir */ + if (!getcwd(old_path, PATH_MAX)) { + fprintf(stderr, "%s - GETCWD: %s\n", __func__, strerror(errno)); return -1; } - init_mail_header(&mail_hdr); - - parse_mail_header(fh, &mail_hdr); - - fprintf(stderr, "-------------------------------------------\n"); - fprintf(stderr, "Test file: %s\n", file); - fprintf(stderr, "-------------------------------------------\n"); - fprintf(stderr, "Date : %s\n", rfc822_mkdt(mail_hdr.date)); - fprintf(stderr, "From : %s\n", mail_hdr.from); - fprintf(stderr, "Subject: %s\n", mail_hdr.subject); - - debug_mime_header(&mail_hdr.mime); + if (chdir(maildir_path) < 0) { + fprintf(stderr, "%s - CHDIR: %s\n", __func__, strerror(errno)); + return -1; + } - fprintf(stderr, "-------------------------------------------\n\n"); + rc = scan_dir(maildir_path, parse_file); - if (!hdr_only) { - /* Message Body */ - parse_mail_body(fh, &mail_hdr); + if (chdir(old_path) < 0) { + fprintf(stderr, "%s - Cannot restore path %s!\n", __func__, strerror(errno)); } - fclose(fh); - - return 0; + return rc; } -- 2.11.4.GIT