Implemented --draft and --file switches
Showing
1 changed file
with
103 additions
and
6 deletions
... | @@ -18,6 +18,11 @@ | ... | @@ -18,6 +18,11 @@ |
18 | /* MH refile command */ | 18 | /* MH refile command */ |
19 | 19 | ||
20 | #include <mh.h> | 20 | #include <mh.h> |
21 | #include <sys/types.h> | ||
22 | #include <sys/stat.h> | ||
23 | #include <unistd.h> | ||
24 | #include <errno.h> | ||
25 | #include <fcntl.h> | ||
21 | 26 | ||
22 | const char *argp_program_version = "refile (" PACKAGE_STRING ")"; | 27 | const char *argp_program_version = "refile (" PACKAGE_STRING ")"; |
23 | static char doc[] = "GNU MH refile"; | 28 | static char doc[] = "GNU MH refile"; |
... | @@ -132,7 +137,7 @@ opt_handler (int key, char *arg, void *unused) | ... | @@ -132,7 +137,7 @@ opt_handler (int key, char *arg, void *unused) |
132 | break; | 137 | break; |
133 | 138 | ||
134 | case 'd': | 139 | case 'd': |
135 | source_file = "draft"; | 140 | source_file = mh_expand_name ("draft", 0); |
136 | break; | 141 | break; |
137 | 142 | ||
138 | case 'l': | 143 | case 'l': |
... | @@ -190,6 +195,90 @@ refile (mailbox_t mbox, message_t msg, size_t num, void *data) | ... | @@ -190,6 +195,90 @@ refile (mailbox_t mbox, message_t msg, size_t num, void *data) |
190 | } | 195 | } |
191 | } | 196 | } |
192 | 197 | ||
198 | mailbox_t | ||
199 | open_source (char *file_name) | ||
200 | { | ||
201 | struct stat st; | ||
202 | char *buffer; | ||
203 | int fd; | ||
204 | size_t len = 0; | ||
205 | mailbox_t tmp; | ||
206 | stream_t stream; | ||
207 | char *p; | ||
208 | |||
209 | if (stat (file_name, &st) < 0) | ||
210 | { | ||
211 | mh_error ("can't stat file %s: %s", file_name, strerror (errno)); | ||
212 | return NULL; | ||
213 | } | ||
214 | |||
215 | buffer = xmalloc (st.st_size+1); | ||
216 | fd = open (file_name, O_RDONLY); | ||
217 | if (fd == -1) | ||
218 | { | ||
219 | mh_error ("can't open file %s: %s", file_name, strerror (errno)); | ||
220 | return NULL; | ||
221 | } | ||
222 | |||
223 | if (read (fd, buffer, st.st_size) != st.st_size) | ||
224 | { | ||
225 | mh_error ("error reading file %s: %s", file_name, strerror (errno)); | ||
226 | return NULL; | ||
227 | } | ||
228 | |||
229 | buffer[st.st_size] = 0; | ||
230 | close (fd); | ||
231 | |||
232 | if (mailbox_create (&tmp, "/dev/null") | ||
233 | || mailbox_open (tmp, MU_STREAM_READ) != 0) | ||
234 | { | ||
235 | mh_error ("can't create temporary mailbox"); | ||
236 | return NULL; | ||
237 | } | ||
238 | |||
239 | if (memory_stream_create (&stream, 0, MU_STREAM_RDWR) | ||
240 | || stream_open (stream)) | ||
241 | { | ||
242 | mailbox_close (tmp); | ||
243 | mh_error ("can't create temporary stream"); | ||
244 | return NULL; | ||
245 | } | ||
246 | |||
247 | for (p = buffer; *p && isspace (*p); p++) | ||
248 | ; | ||
249 | |||
250 | if (strncmp (p, "From ", 5)) | ||
251 | { | ||
252 | struct tm *tm; | ||
253 | time_t t; | ||
254 | char date[80]; | ||
255 | |||
256 | time(&t); | ||
257 | tm = gmtime(&t); | ||
258 | strftime (date, sizeof (date), | ||
259 | "From GNU-MH-refile %a %b %e %H:%M:%S %Y%n", | ||
260 | tm); | ||
261 | stream_write (stream, date, strlen (date), 0, &len); | ||
262 | } | ||
263 | |||
264 | stream_write (stream, p, strlen (p), len, &len); | ||
265 | mailbox_set_stream (tmp, stream); | ||
266 | if (mailbox_messages_count (tmp, &len) | ||
267 | || len < 1) | ||
268 | { | ||
269 | mh_error ("input file %s is not a valid message file", file_name); | ||
270 | return NULL; | ||
271 | } | ||
272 | else if (len > 1) | ||
273 | { | ||
274 | mh_error ("input file %s contains %lu messages", | ||
275 | (unsigned long) len); | ||
276 | return NULL; | ||
277 | } | ||
278 | free (buffer); | ||
279 | return tmp; | ||
280 | } | ||
281 | |||
193 | int | 282 | int |
194 | main (int argc, char **argv) | 283 | main (int argc, char **argv) |
195 | { | 284 | { |
... | @@ -201,16 +290,24 @@ main (int argc, char **argv) | ... | @@ -201,16 +290,24 @@ main (int argc, char **argv) |
201 | mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, | 290 | mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, |
202 | opt_handler, NULL, &index); | 291 | opt_handler, NULL, &index); |
203 | 292 | ||
204 | if (source_file && index < argc) | 293 | if (source_file) |
205 | { | 294 | { |
206 | mh_error ("both message set and source file given"); | 295 | if (index < argc) |
207 | exit (1); | 296 | { |
297 | mh_error ("both message set and source file given"); | ||
298 | exit (1); | ||
299 | } | ||
300 | mbox = open_source (source_file); | ||
301 | mh_msgset_parse (mbox, &msgset, 0, NULL, "first"); | ||
302 | } | ||
303 | else | ||
304 | { | ||
305 | mbox = mh_open_folder (current_folder, 0); | ||
306 | mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur"); | ||
208 | } | 307 | } |
209 | 308 | ||
210 | open_folders (); | 309 | open_folders (); |
211 | 310 | ||
212 | mbox = mh_open_folder (current_folder, 0); | ||
213 | mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur"); | ||
214 | status = mh_iterate (mbox, &msgset, refile, NULL); | 311 | status = mh_iterate (mbox, &msgset, refile, NULL); |
215 | 312 | ||
216 | enumerate_folders (_close_folder, NULL); | 313 | enumerate_folders (_close_folder, NULL); | ... | ... |
-
Please register or sign in to post a comment