Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / utils / test_view_mail.c
blobc70477b3548600027bda072733ea93e3c49dda70
1 #define _BSD_SOURCE
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <time.h>
9 #include <ctype.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
14 #include "string_utils.h"
15 #include "file_utils.h"
16 #include "rfc5322.h"
17 #include "rfc2047.h"
18 #include "rfc822.h"
19 #include "mime.h"
20 #include "mail.h"
22 static void plain_body(char *line)
24 printf("%s", line);
28 static void qp_body(char *line)
30 char buf[1024];
32 rfc2047_decode_quoted_printable(buf, line, sizeof(buf) - 1);
33 printf("%s", buf);
36 static void parse_mail_body(FILE *fh, struct mail_header *hdr)
38 char buf[1024];
39 void (*decode_body)(char *line);
41 switch (hdr->mime.transfer_encoding) {
42 case MIME_ENCODING_QUOTED_PRINTABLE:
43 decode_body = qp_body;
44 break;
45 default:
46 decode_body = plain_body;
47 break;
50 (void) fseek(fh, hdr->body_offset, SEEK_SET);
52 while (fgets(buf, sizeof(buf), fh)) {
53 decode_body(buf);
57 static void parse_mail_header(FILE *fh, struct mail_header *hdr)
59 char *crlf = NULL;
60 char buf[1024];
62 /* Scan all lines */
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');
67 if (crlf)
68 *crlf = '\0';
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);
79 } else {
80 if (rfc822_check_header_end(buf)) {
81 break;
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;
97 /* MIME */
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)
113 FILE *fh;
114 struct mail_header mail_hdr;
116 int opt;
118 char *file = NULL;
119 int hdr_only = 0;
122 while ((opt = getopt(argc, argv, "f:h")) != -1) {
123 switch (opt) {
124 case 'f':
125 file = optarg;
126 break;
127 case 'h':
128 hdr_only = 1;
129 break;
130 default:
131 fprintf(stderr, "Unknown parameter.\n");
132 usage(argv[0]);
133 return -1;
137 if (file == NULL) {
138 usage(argv[0]);
139 return -1;
142 if (file_type(file))
143 return -1;
145 fh = fopen(file, "r");
146 if (!fh) {
147 return -1;
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");
165 if (!hdr_only) {
166 /* Message Body */
167 parse_mail_body(fh, &mail_hdr);
170 fclose(fh);
172 return 0;