Made so it now saves all the attachments from all the messages in
a folder. Also GNU indented.
Showing
1 changed file
with
69 additions
and
122 deletions
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 | { | ||
51 | fprintf (stderr, "could not open <%s>: %s\n", | ||
52 | mailbox_name, strerror (status)); | ||
53 | exit (1); | ||
54 | } | ||
55 | |||
56 | mailbox_messages_count (mbox, &count); | ||
57 | |||
58 | for (i = 1; i <= count; ++i) | ||
59 | { | ||
60 | message_t msg; | ||
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 | { | 60 | { |
80 | fprintf (stderr, "msg %d : No Subject|Date\n", i); | 61 | fprintf (stderr, "could not open <%s>: %s\n", |
81 | continue; | 62 | mbox_name, strerror (status)); |
63 | exit (1); | ||
82 | } | 64 | } |
65 | mailbox_messages_count (mbox, &count); | ||
83 | 66 | ||
84 | if (strcasecmp(subj, "WTLS 1.0 Daily Build-details") == 0) | 67 | for (msgno = 1; msgno <= count; ++msgno) |
85 | { | 68 | { |
86 | const char* s = date; | 69 | message_t msg = 0; |
87 | struct tm tm; | 70 | size_t nparts = 0; |
88 | char dir[] = "yyyy.mm.dd"; | 71 | size_t partno; |
89 | size_t nparts = 0; | 72 | |
90 | size_t partno; | 73 | if ((status = mailbox_get_message (mbox, msgno, &msg)) != 0) |
91 | 74 | { | |
92 | if((status = parse822_date_time(&s, s + strlen(s), &tm))) | 75 | fprintf (stderr, "msg %d: get message failed: %s\n", |
93 | { | 76 | msgno, strerror (status)); |
94 | fprintf (stderr, "parsing <%s> failed: %s\n", date, strerror(status)); | 77 | exit (2); |
95 | exit(1); | 78 | } |
96 | } | 79 | if ((status = message_get_num_parts (msg, &nparts))) |
97 | 80 | { | |
98 | printf ("%d Processing for: year %d month %d day %d\n", | 81 | fprintf (stderr, "msg %d: get num parts failed: %s\n", |
99 | i, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday); | 82 | msgno, strerror (status)); |
100 | 83 | exit (1); | |
101 | snprintf(dir, sizeof(dir), "%d.%02d.%02d", | 84 | } |
102 | tm.tm_year + 1900, tm.tm_mon, tm.tm_mday); | 85 | printf ("msg %03d: %02d parts\n", msgno, nparts); |
103 | 86 | ||
104 | status = mkdir(dir, 0777); | 87 | for (partno = 1; partno <= nparts; partno++) |
105 | 88 | { | |
106 | if(status != 0) | 89 | message_t part = 0; |
107 | { | 90 | char path[128]; |
108 | switch(errno) | 91 | |
109 | { | 92 | sprintf (path, "%s/m%03d.p%02d", dir_name, msgno, partno); |
110 | case EEXIST: /* we've already done this message */ | 93 | |
111 | continue; | 94 | printf ("msg %03d: part %02d > %s\n", msgno, partno, path); |
112 | case 0: | 95 | |
113 | break; | 96 | if ((status = message_get_part (msg, partno, &part))) |
114 | default: | 97 | { |
115 | fprintf (stderr, "mkdir %s failed: %s\n", dir, strerror(errno)); | 98 | fprintf (stderr, "msg %d: get part %d failed: %s\n", |
116 | status = 1; | 99 | msgno, partno, strerror (status)); |
117 | goto END; | 100 | exit (1); |
118 | break; | 101 | } |
119 | } | 102 | if ((status = message_save_attachment (part, path, 0))) |
120 | } | 103 | { |
121 | if((status = message_get_num_parts(msg, &nparts))) { | 104 | fprintf (stderr, "msg %d part %d: save failed: %s\n", |
122 | fprintf (stderr, "get num parts failed: %s\n", strerror(status)); | 105 | msgno, partno, strerror (status)); |
123 | break; | 106 | break; |
124 | } | 107 | } |
125 | 108 | } | |
126 | for(partno = 1; partno <= nparts; partno++) | ||
127 | { | ||
128 | message_t part = NULL; | ||
129 | char content[128] = "<not found>"; | ||
130 | char* fname = NULL; | ||
131 | char path[PATH_MAX]; | ||
132 | |||
133 | if((status = message_get_part(msg, partno, &part))) { | ||
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 | |||
141 | fname = strrchr(content, '"'); | ||
142 | |||
143 | if(fname) | ||
144 | { | ||
145 | *fname = 0; | ||
146 | |||
147 | fname = strchr(content, '"') + 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 | } | ||
157 | } | ||
158 | status = 0; | ||
159 | } | 109 | } |
160 | } | ||
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 | ... | ... |
-
Please register or sign in to post a comment