Commit c887d437 c887d437ec72b5b34ae06238d6c43b7e5dffa193 by Sergey Poznyakoff

Added copyleft header. Changed output format to better suit testing.

1 parent e93af638
...@@ -28,22 +28,32 @@ ...@@ -28,22 +28,32 @@
28 28
29 #include <sys/types.h> 29 #include <sys/types.h>
30 30
31 #include <mailutils/body.h> 31 #include <mailutils/mailutils.h>
32 #include <mailutils/debug.h>
33 #include <mailutils/errno.h>
34 #include <mailutils/filter.h>
35 #include <mailutils/header.h>
36 #include <mailutils/header.h>
37 #include <mailutils/list.h>
38 #include <mailutils/mailbox.h>
39 #include <mailutils/message.h>
40 #include <mailutils/registrar.h>
41 #include <mailutils/stream.h>
42 32
43 void message_display_parts(message_t msg, const char *indent); 33 void message_display_parts(message_t msg, int indent);
44 34
45 char from[256]; 35 char from[256];
46 char subject[256]; 36 char subject[256];
37 int print_attachments;
38 int indent_level = 4;
39
40 void
41 print_file (const char *fname, int indent)
42 {
43 char buf[128];
44 FILE *fp = fopen (fname, "r");
45
46 if (!fp)
47 {
48 fprintf (stderr, "can't open file %s: %s", fname, strerror (errno));
49 return;
50 }
51
52 while (fgets (buf, sizeof buf, fp))
53 printf ("%*.*s%s", indent, indent, "", buf);
54 fclose (fp);
55 unlink (fname);
56 }
47 57
48 int 58 int
49 main (int argc, char **argv) 59 main (int argc, char **argv)
...@@ -52,15 +62,23 @@ main (int argc, char **argv) ...@@ -52,15 +62,23 @@ main (int argc, char **argv)
52 int ret; 62 int ret;
53 size_t i; 63 size_t i;
54 size_t count = 0; 64 size_t count = 0;
55 char *mailbox_name = argv[1]; 65 char *mailbox_name;
56 int debug = 0; 66 int debug = 0;
57 67
58 if (strcmp("-d", mailbox_name) == 0) 68 for (i = 1; i < argc; i++)
59 { 69 {
70 if (strcmp (argv[i], "-d") == 0)
60 debug = 1; 71 debug = 1;
61 mailbox_name = argv[2]; 72 else if (strcmp (argv[i], "-p") == 0)
73 print_attachments = 1;
74 else if (strcmp (argv[i], "-i") == 0)
75 indent_level = strtoul (argv[++i], NULL, 0);
76 else
77 break;
62 } 78 }
63 79
80 mailbox_name = argv[i];
81
64 /* Registration. */ 82 /* Registration. */
65 { 83 {
66 list_t bookie; 84 list_t bookie;
...@@ -121,18 +139,18 @@ main (int argc, char **argv) ...@@ -121,18 +139,18 @@ main (int argc, char **argv)
121 header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); 139 header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
122 header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), 140 header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
123 NULL); 141 NULL);
124 printf ("-- Message: %d\n", i); 142 printf ("Message: %d\n", i);
125 printf ("-- From: %s\n", from); 143 printf ("From: %s\n", from);
126 printf ("-- Subject: %s\n", subject); 144 printf ("Subject: %s\n", subject);
127 145
128 if ((ret = message_get_num_parts (msg, &nparts)) != 0) 146 if ((ret = message_get_num_parts (msg, &nparts)) != 0)
129 { 147 {
130 fprintf (stderr, "message_get_num_parts - %s\n", mu_errstring (ret)); 148 fprintf (stderr, "message_get_num_parts - %s\n", mu_errstring (ret));
131 exit (2); 149 exit (2);
132 } 150 }
133 printf ("-- Number of parts in message - %d\n", nparts); 151 printf ("Number of parts in message - %d\n", nparts);
134 printf ("-- Total message size - %d\n", msize); 152 printf ("Total message size - %d\n", msize);
135 message_display_parts (msg, " "); 153 message_display_parts (msg, 0);
136 } 154 }
137 mailbox_close (mbox); 155 mailbox_close (mbox);
138 mailbox_destroy (&mbox); 156 mailbox_destroy (&mbox);
...@@ -142,7 +160,7 @@ main (int argc, char **argv) ...@@ -142,7 +160,7 @@ main (int argc, char **argv)
142 char buf[2048]; 160 char buf[2048];
143 161
144 void 162 void
145 message_display_parts (message_t msg, const char *indent) 163 message_display_parts (message_t msg, int indent)
146 { 164 {
147 int ret, j; 165 int ret, j;
148 size_t msize, nparts, nsubparts; 166 size_t msize, nparts, nsubparts;
...@@ -184,8 +202,8 @@ message_display_parts (message_t msg, const char *indent) ...@@ -184,8 +202,8 @@ message_display_parts (message_t msg, const char *indent)
184 } 202 }
185 header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type), 203 header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type),
186 NULL); 204 NULL);
187 printf ("%sType of part %d = %s\n", indent, j, type); 205 printf ("%*.*sType of part %d = %s\n", indent, indent, "", j, type);
188 printf ("%sMessage part size - %d\n", indent, msize); 206 printf ("%*.*sMessage part size - %d\n", indent, indent, "", msize);
189 encoding[0] = '\0'; 207 encoding[0] = '\0';
190 header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding, 208 header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding,
191 sizeof (encoding), NULL); 209 sizeof (encoding), NULL);
...@@ -194,8 +212,6 @@ message_display_parts (message_t msg, const char *indent) ...@@ -194,8 +212,6 @@ message_display_parts (message_t msg, const char *indent)
194 && strncasecmp (type, "message/rfc822", strlen (type)) == 0) 212 && strncasecmp (type, "message/rfc822", strlen (type)) == 0)
195 || (message_is_multipart (part, &ismulti) == 0 && ismulti)) 213 || (message_is_multipart (part, &ismulti) == 0 && ismulti))
196 { 214 {
197 char tmp[10];
198
199 if (!ismulti) 215 if (!ismulti)
200 { 216 {
201 ret = message_unencapsulate (part, &part, NULL); 217 ret = message_unencapsulate (part, &part, NULL);
...@@ -212,18 +228,15 @@ message_display_parts (message_t msg, const char *indent) ...@@ -212,18 +228,15 @@ message_display_parts (message_t msg, const char *indent)
212 header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); 228 header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
213 header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), 229 header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
214 NULL); 230 NULL);
215 printf ("%sEncapsulated message : %s\t%s\n", indent, from, subject); 231 printf ("%*.*sEncapsulated message : %s\t%s\n",
216 printf 232 indent, indent, "", from, subject);
217 ("%s-------------------------------------------------------------------\n", 233 printf ("%*.*sBegin\n", indent, indent, "");
218 indent);
219 if ((ret = message_get_num_parts (part, &nsubparts)) != 0) 234 if ((ret = message_get_num_parts (part, &nsubparts)) != 0)
220 { 235 {
221 fprintf (stderr, "mime_get_num_parts - %s\n", mu_errstring (ret)); 236 fprintf (stderr, "mime_get_num_parts - %s\n", mu_errstring (ret));
222 exit (2); 237 exit (2);
223 } 238 }
224 strcpy (tmp, indent); 239 message_display_parts (part, indent+indent_level);
225 strcat (tmp, "\t");
226 message_display_parts (part, tmp);
227 message_destroy (&part, NULL); 240 message_destroy (&part, NULL);
228 } 241 }
229 else if (type[0] == '\0' 242 else if (type[0] == '\0'
...@@ -232,10 +245,8 @@ message_display_parts (message_t msg, const char *indent) ...@@ -232,10 +245,8 @@ message_display_parts (message_t msg, const char *indent)
232 || (strncasecmp (type, "text/html", strlen ("text/html")) == 245 || (strncasecmp (type, "text/html", strlen ("text/html")) ==
233 0)) 246 0))
234 { 247 {
235 printf ("%sText Message\n", indent); 248 printf ("%*.*sText Message\n", indent, indent, "");
236 printf 249 printf ("%*.*sBegin\n", indent, indent, "");
237 ("%s-------------------------------------------------------------------\n",
238 indent);
239 message_get_body (part, &body); 250 message_get_body (part, &body);
240 body_get_stream (body, &str); 251 body_get_stream (body, &str);
241 filter_create (&str, str, encoding, 0, 0); 252 filter_create (&str, str, encoding, 0, 0);
...@@ -243,7 +254,7 @@ message_display_parts (message_t msg, const char *indent) ...@@ -243,7 +254,7 @@ message_display_parts (message_t msg, const char *indent)
243 while (stream_readline (str, buf, sizeof (buf), offset, &nbytes) == 254 while (stream_readline (str, buf, sizeof (buf), offset, &nbytes) ==
244 0 && nbytes) 255 0 && nbytes)
245 { 256 {
246 printf ("%s%s", indent, buf); 257 printf ("%*.*s%s", indent, indent, "", buf);
247 offset += nbytes; 258 offset += nbytes;
248 } 259 }
249 stream_destroy (&str, NULL); 260 stream_destroy (&str, NULL);
...@@ -258,17 +269,15 @@ message_display_parts (message_t msg, const char *indent) ...@@ -258,17 +269,15 @@ message_display_parts (message_t msg, const char *indent)
258 char buffer[PATH_MAX + 1]; 269 char buffer[PATH_MAX + 1];
259 fname = tempnam (getcwd (buffer, PATH_MAX), "msg-"); 270 fname = tempnam (getcwd (buffer, PATH_MAX), "msg-");
260 } 271 }
261 printf ("%sAttachment - saving [%s]\n", indent, fname); 272 printf ("%*.*sAttachment - saving [%s]\n", indent, indent, "", fname);
262 printf 273 printf ("%*.*sBegin\n", indent, indent, "");
263 ("%s-------------------------------------------------------------------\n",
264 indent);
265 /*FIXME: What is the 'data' argument for? */ 274 /*FIXME: What is the 'data' argument for? */
266 message_save_attachment (part, NULL, NULL); 275 message_save_attachment (part, NULL, NULL);
276 if (print_attachments)
277 print_file (fname, indent);
267 free (fname); 278 free (fname);
268 } 279 }
269 printf 280 printf ("\n%*.*sEnd\n", indent, indent, "");
270 ("\n%s End -------------------------------------------------------------------\n",
271 indent);
272 } 281 }
273 } 282 }
274 283
......