The framework for the refile command.
Showing
1 changed file
with
149 additions
and
0 deletions
mh/refile.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 2002 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | /* MH refile command */ | ||
19 | |||
20 | #include <mh.h> | ||
21 | |||
22 | const char *argp_program_version = "refile (" PACKAGE_STRING ")"; | ||
23 | static char doc[] = "GNU MH refile"; | ||
24 | static char args_doc[] = "messages folder [folder...]"; | ||
25 | |||
26 | /* GNU options */ | ||
27 | static struct argp_option options[] = { | ||
28 | {"folder", 'f', "FOLDER", 0, "Specify folder to operate upon"}, | ||
29 | {"draft", 'd', NULL, 0, "Use <mh-dir>/draft as the source message"}, | ||
30 | {"link", 'l', "BOOL", OPTION_ARG_OPTIONAL, "Preserve the source folder copy"}, | ||
31 | {"preserve", 'p', "BOOL", OPTION_ARG_OPTIONAL, "Do not preserve the source folder copy (default)"}, | ||
32 | {"source", 's', "FOLDER", 0, "Specify source folder. FOLDER will became the current folder after the program exits."}, | ||
33 | {"src", 0, NULL, OPTION_ALIAS, NULL}, | ||
34 | {"file", 'F', "FILE", 0, "Use FILE as the source message"}, | ||
35 | { "\nUse -help switch to obtain the list of traditional MH options. ", 0, 0, OPTION_DOC, "" }, | ||
36 | { 0 } | ||
37 | }; | ||
38 | |||
39 | /* Traditional MH options */ | ||
40 | struct mh_option mh_option[] = { | ||
41 | {"file", 2, 'F', 0, "input-file"}, | ||
42 | {"draft", 1, 'd', 0, NULL }, | ||
43 | {"link", 1, 'l', MH_OPT_BOOL, NULL }, | ||
44 | {"preserve", 1, 'p', MH_OPT_BOOL, NULL }, | ||
45 | {"src", 1, 's', 0, "+folder" }, | ||
46 | { 0 } | ||
47 | }; | ||
48 | |||
49 | int link_flag = 1; | ||
50 | int preserve_flag = 0; | ||
51 | char *source_file = NULL; | ||
52 | list_t out_folder_list = NULL; | ||
53 | |||
54 | void | ||
55 | add_folder (const char *folder) | ||
56 | { | ||
57 | char *p; | ||
58 | |||
59 | if (!out_folder_list && list_create (&out_folder_list)) | ||
60 | { | ||
61 | mh_error ("can't create folder list"); | ||
62 | exit (1); | ||
63 | } | ||
64 | if ((p = strdup (folder)) == NULL) | ||
65 | { | ||
66 | mh_error("not enough memory"); | ||
67 | exit (1); | ||
68 | } | ||
69 | list_append (out_folder_list, p); | ||
70 | } | ||
71 | |||
72 | void | ||
73 | enumerate_folders (void (*f) __P((void *, char *)), void *data) | ||
74 | { | ||
75 | iterator_t itr; | ||
76 | int rc = 0; | ||
77 | |||
78 | if (iterator_create (&itr, out_folder_list)) | ||
79 | { | ||
80 | mh_error ("can't create iterator"); | ||
81 | exit (1); | ||
82 | } | ||
83 | |||
84 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) | ||
85 | { | ||
86 | char *name; | ||
87 | iterator_current (itr, (void **)&name); | ||
88 | (*f) (data, name); | ||
89 | } | ||
90 | iterator_destroy (&itr); | ||
91 | } | ||
92 | |||
93 | |||
94 | static int | ||
95 | opt_handler (int key, char *arg, void *unused) | ||
96 | { | ||
97 | switch (key) | ||
98 | { | ||
99 | case '+': | ||
100 | case 'f': | ||
101 | add_folder (arg); | ||
102 | break; | ||
103 | |||
104 | case 'd': | ||
105 | source_file = "draft"; | ||
106 | break; | ||
107 | |||
108 | case 'l': | ||
109 | link_flag = arg[0] == 'y'; | ||
110 | break; | ||
111 | |||
112 | case 'p': | ||
113 | preserve_flag = arg[0] == 'y'; | ||
114 | break; | ||
115 | |||
116 | case 's': | ||
117 | current_folder = arg; | ||
118 | break; | ||
119 | |||
120 | case 'F': | ||
121 | source_file = arg; | ||
122 | break; | ||
123 | |||
124 | default: | ||
125 | return 1; | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | |||
131 | int | ||
132 | main (int argc, char **argv) | ||
133 | { | ||
134 | int i, index; | ||
135 | mh_msgset_t msgset; | ||
136 | mailbox_t mbox; | ||
137 | int status; | ||
138 | |||
139 | mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, | ||
140 | opt_handler, NULL, &index); | ||
141 | |||
142 | mbox = mh_open_folder (current_folder); | ||
143 | mh_msgset_parse (mbox, &msgset, argc - index, argv + index); | ||
144 | // status = refile (mbox, &msgset); | ||
145 | mailbox_expunge (mbox); | ||
146 | mailbox_close (mbox); | ||
147 | mailbox_destroy (&mbox); | ||
148 | return status; | ||
149 | } |
-
Please register or sign in to post a comment