Commit 5c0eb492 5c0eb492308b309e85128ffb806e623e3f012feb by Sam Roberts

Made so it now saves all the attachments from all the messages in

a folder. Also GNU indented.
1 parent d609eea4
1 #include <sys/types.h> 1 #include <sys/types.h>
2 #include <sys/stat.h> 2 #include <sys/stat.h>
3 3
4 #include <assert.h>
4 #include <errno.h> 5 #include <errno.h>
5 #include <fcntl.h> 6 #include <fcntl.h>
6 #include <limits.h> 7 #include <limits.h>
...@@ -11,21 +12,31 @@ ...@@ -11,21 +12,31 @@
11 #include <unistd.h> 12 #include <unistd.h>
12 13
13 #include <mailutils/mailbox.h> 14 #include <mailutils/mailbox.h>
14 #include <mailutils/address.h>
15 #include <mailutils/registrar.h> 15 #include <mailutils/registrar.h>
16 #include <mailutils/parse822.h>
17 16
18 int 17 int
19 main(int argc, char **argv) 18 main (int argc, char **argv)
20 { 19 {
21 mailbox_t mbox; 20 mailbox_t mbox;
22 size_t i; 21 size_t msgno;
23 size_t count = 0; 22 size_t count = 0;
24 char *mailbox_name; 23 char *mbox_name = 0;
24 char *dir_name = 0;
25 int status; 25 int status;
26 26
27 mailbox_name = (argc > 1) ? argv[0] : "+dbuild.details"; 27 if (argc != 3)
28 {
29 printf ("usage: mbox-explode <mbox> <directory>\n");
30 exit (0);
31 }
32 mbox_name = argv[1];
33 dir_name = argv[2];
28 34
35 if (mkdir (dir_name, 0777) != 0)
36 {
37 fprintf (stderr, "mkdir(%s) failed: %s\n", dir_name, strerror (errno));
38 exit (1);
39 }
29 /* Register the desire formats. */ 40 /* Register the desire formats. */
30 { 41 {
31 list_t bookie; 42 list_t bookie;
...@@ -33,136 +44,72 @@ main(int argc, char **argv) ...@@ -33,136 +44,72 @@ main(int argc, char **argv)
33 list_append (bookie, path_record); 44 list_append (bookie, path_record);
34 } 45 }
35 46
36 if ((status = mailbox_create_default (&mbox, mailbox_name)) != 0) 47 if ((status = mailbox_create_default (&mbox, mbox_name)) != 0)
37 { 48 {
38 fprintf (stderr, "could not create <%s>: %s\n", 49 fprintf (stderr, "could not create <%s>: %s\n",
39 mailbox_name, strerror (status)); 50 mbox_name, strerror (status));
40 exit (1); 51 exit (1);
41 } 52 }
42
43 { 53 {
44 debug_t debug; 54 debug_t debug;
45 mailbox_get_debug (mbox, &debug); 55 mailbox_get_debug (mbox, &debug);
46 // debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT); 56 debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT);
47 } 57 }
48 58
49 if ((status = mailbox_open (mbox, MU_STREAM_READ)) != 0) 59 if ((status = mailbox_open (mbox, MU_STREAM_READ)) != 0)
50 { 60 {
51 fprintf (stderr, "could not open <%s>: %s\n", 61 fprintf (stderr, "could not open <%s>: %s\n",
52 mailbox_name, strerror (status)); 62 mbox_name, strerror (status));
53 exit (1); 63 exit (1);
54 } 64 }
55
56 mailbox_messages_count (mbox, &count); 65 mailbox_messages_count (mbox, &count);
57 66
58 for (i = 1; i <= count; ++i) 67 for (msgno = 1; msgno <= count; ++msgno)
59 { 68 {
60 message_t msg; 69 message_t msg = 0;
61 header_t hdr;
62 char subj[128];
63 char date[128];
64 size_t len = 0;
65
66 if (
67 (status = mailbox_get_message (mbox, i, &msg)) != 0 ||
68 (status = message_get_header (msg, &hdr)) != 0)
69 {
70 fprintf (stderr, "msg %d : %s\n", i, strerror(status));
71 exit(2);
72 }
73
74 if (
75 (status = header_get_value (
76 hdr, MU_HEADER_SUBJECT, subj, sizeof (subj), &len)) != 0 ||
77 (status = header_get_value (
78 hdr, MU_HEADER_DATE, date, sizeof (date), &len)) != 0)
79 {
80 fprintf (stderr, "msg %d : No Subject|Date\n", i);
81 continue;
82 }
83
84 if (strcasecmp(subj, "WTLS 1.0 Daily Build-details") == 0)
85 {
86 const char* s = date;
87 struct tm tm;
88 char dir[] = "yyyy.mm.dd";
89 size_t nparts = 0; 70 size_t nparts = 0;
90 size_t partno; 71 size_t partno;
91 72
92 if((status = parse822_date_time(&s, s + strlen(s), &tm))) 73 if ((status = mailbox_get_message (mbox, msgno, &msg)) != 0)
93 { 74 {
94 fprintf (stderr, "parsing <%s> failed: %s\n", date, strerror(status)); 75 fprintf (stderr, "msg %d: get message failed: %s\n",
95 exit(1); 76 msgno, strerror (status));
77 exit (2);
96 } 78 }
97 79 if ((status = message_get_num_parts (msg, &nparts)))
98 printf ("%d Processing for: year %d month %d day %d\n",
99 i, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday);
100
101 snprintf(dir, sizeof(dir), "%d.%02d.%02d",
102 tm.tm_year + 1900, tm.tm_mon, tm.tm_mday);
103
104 status = mkdir(dir, 0777);
105
106 if(status != 0)
107 { 80 {
108 switch(errno) 81 fprintf (stderr, "msg %d: get num parts failed: %s\n",
109 { 82 msgno, strerror (status));
110 case EEXIST: /* we've already done this message */ 83 exit (1);
111 continue;
112 case 0:
113 break;
114 default:
115 fprintf (stderr, "mkdir %s failed: %s\n", dir, strerror(errno));
116 status = 1;
117 goto END;
118 break;
119 }
120 }
121 if((status = message_get_num_parts(msg, &nparts))) {
122 fprintf (stderr, "get num parts failed: %s\n", strerror(status));
123 break;
124 } 84 }
85 printf ("msg %03d: %02d parts\n", msgno, nparts);
125 86
126 for(partno = 1; partno <= nparts; partno++) 87 for (partno = 1; partno <= nparts; partno++)
127 { 88 {
128 message_t part = NULL; 89 message_t part = 0;
129 char content[128] = "<not found>"; 90 char path[128];
130 char* fname = NULL;
131 char path[PATH_MAX];
132 91
133 if((status = message_get_part(msg, partno, &part))) { 92 sprintf (path, "%s/m%03d.p%02d", dir_name, msgno, partno);
134 fprintf (stderr, "get part failed: %s\n", strerror(status));
135 break;
136 }
137 message_get_header (part, &hdr);
138 header_get_value (hdr, MU_HEADER_CONTENT_DISPOSITION,
139 content, sizeof (content), &len);
140 93
141 fname = strrchr(content, '"'); 94 printf ("msg %03d: part %02d > %s\n", msgno, partno, path);
142 95
143 if(fname) 96 if ((status = message_get_part (msg, partno, &part)))
144 { 97 {
145 *fname = 0; 98 fprintf (stderr, "msg %d: get part %d failed: %s\n",
146 99 msgno, partno, strerror (status));
147 fname = strchr(content, '"') + 1; 100 exit (1);
148
149 snprintf(path, sizeof(path), "%s/%s", dir, fname);
150 printf(" filename %s\n", path);
151
152 if((status = message_save_attachment(part, path, NULL))) {
153 fprintf (stderr, "save attachment failed: %s\n", strerror(status));
154 break;
155 }
156 } 101 }
102 if ((status = message_save_attachment (part, path, 0)))
103 {
104 fprintf (stderr, "msg %d part %d: save failed: %s\n",
105 msgno, partno, strerror (status));
106 break;
157 } 107 }
158 status = 0;
159 } 108 }
160 } 109 }
161 110
162 END:
163 mailbox_close (mbox); 111 mailbox_close (mbox);
164 mailbox_destroy (&mbox); 112 mailbox_destroy (&mbox);
165 113
166 return status; 114 return status;
167 } 115 }
168
......