Add maildir scan option
[rmail.git] / src / utils / test_view_mail.c
blobc305959c31efbdc156ac3d6dc7a72b90159c1853
1 #define _BSD_SOURCE
3 #include <stdio.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <time.h>
10 #include <ctype.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
15 #include "string_utils.h"
16 #include "file_utils.h"
17 #include "rfc5322.h"
18 #include "rfc2047.h"
19 #include "rfc822.h"
20 #include "mime.h"
21 #include "mail.h"
23 static int hdr_only = 0;
25 static void plain_body(char *line)
27 printf("%s", line);
31 static void qp_body(char *line)
33 char buf[1024];
35 rfc2047_decode_quoted_printable(buf, line, sizeof(buf) - 1);
36 printf("%s", buf);
39 static void parse_mail_body(FILE *fh, struct mail_header *hdr)
41 char buf[1024];
42 void (*decode_body)(char *line);
44 switch (hdr->mime.transfer_encoding) {
45 case MIME_ENCODING_QUOTED_PRINTABLE:
46 decode_body = qp_body;
47 break;
48 default:
49 decode_body = plain_body;
50 break;
53 (void) fseek(fh, hdr->body_offset, SEEK_SET);
55 while (fgets(buf, sizeof(buf), fh)) {
56 decode_body(buf);
60 static void parse_mail_header(FILE *fh, struct mail_header *hdr)
62 char *crlf = NULL;
63 char buf[1024];
65 /* Scan all lines */
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');
70 if (crlf)
71 *crlf = '\0';
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);
82 } else {
83 if (rfc822_check_header_end(buf)) {
84 break;
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;
100 /* MIME */
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)
117 FILE *fh;
118 struct mail_header mail_hdr;
120 if (file_type(file))
121 return;
123 fh = fopen(file, "r");
124 if (!fh) {
125 return;
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");
143 if (!hdr_only) {
144 /* Message Body */
145 parse_mail_body(fh, &mail_hdr);
148 fclose(fh);
152 int main(int argc, char **argv)
154 int opt;
156 char *file = NULL;
157 char *maildir_path = NULL;
159 char old_path[PATH_MAX];
160 int rc;
162 while ((opt = getopt(argc, argv, "d:f:h")) != -1) {
163 switch (opt) {
164 case 'd':
165 maildir_path = optarg;
166 break;
167 case 'f':
168 file = optarg;
169 break;
171 case 'h':
172 hdr_only = 1;
173 break;
174 default:
175 fprintf(stderr, "Unknown parameter.\n");
176 usage(argv[0]);
177 return -1;
181 if ((file == NULL) && (maildir_path == NULL)) {
182 usage(argv[0]);
183 return -1;
185 if ((file != NULL) && (maildir_path != NULL)) {
186 fprintf(stderr, "-f and -d are mutually esclusive!.\n");
187 usage(argv[0]);
190 if (file != NULL) {
191 parse_file(file);
192 return 0;
195 /* Parse all maildir */
196 if (!getcwd(old_path, PATH_MAX)) {
197 fprintf(stderr, "%s - GETCWD: %s\n", __func__, strerror(errno));
198 return -1;
201 if (chdir(maildir_path) < 0) {
202 fprintf(stderr, "%s - CHDIR: %s\n", __func__, strerror(errno));
203 return -1;
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));
212 return rc;