Commit 34c6537f 34c6537ffa2620c946848c0ed6f0b07b032758ea by Sergey Poznyakoff

Initial implementation.

1 parent 35ad2a8c
Showing 1 changed file with 89 additions and 17 deletions
...@@ -28,7 +28,7 @@ static struct argp_option options[] = { ...@@ -28,7 +28,7 @@ static struct argp_option options[] = {
28 {"folder", 'f', "FOLDER", 0, "Specify folder to operate upon"}, 28 {"folder", 'f', "FOLDER", 0, "Specify folder to operate upon"},
29 {"draft", 'd', NULL, 0, "Use <mh-dir>/draft as the source message"}, 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"}, 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)"}, 31 {"preserve", 'p', "BOOL", OPTION_ARG_OPTIONAL, "Try to preserve message sequence numbers"},
32 {"source", 's', "FOLDER", 0, "Specify source folder. FOLDER will became the current folder after the program exits."}, 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}, 33 {"src", 0, NULL, OPTION_ALIAS, NULL},
34 {"file", 'F', "FILE", 0, "Use FILE as the source message"}, 34 {"file", 'F', "FILE", 0, "Use FILE as the source message"},
...@@ -49,33 +49,63 @@ struct mh_option mh_option[] = { ...@@ -49,33 +49,63 @@ struct mh_option mh_option[] = {
49 int link_flag = 1; 49 int link_flag = 1;
50 int preserve_flag = 0; 50 int preserve_flag = 0;
51 char *source_file = NULL; 51 char *source_file = NULL;
52 list_t out_folder_list = NULL; 52 list_t folder_name_list = NULL;
53 list_t folder_mbox_list = NULL;
53 54
54 void 55 void
55 add_folder (const char *folder) 56 add_folder (const char *folder)
56 { 57 {
57 char *p; 58 if (!folder_name_list && list_create (&folder_name_list))
58
59 if (!out_folder_list && list_create (&out_folder_list))
60 { 59 {
61 mh_error ("can't create folder list"); 60 mh_error ("can't create folder list");
62 exit (1); 61 exit (1);
63 } 62 }
64 if ((p = strdup (folder)) == NULL) 63 list_append (folder_name_list, strdup (folder));
64 }
65
66 void
67 open_folders ()
68 {
69 iterator_t itr;
70
71 if (!folder_name_list)
65 { 72 {
66 mh_error("not enough memory"); 73 mh_error ("no folder specified");
67 exit (1); 74 exit (1);
68 } 75 }
69 list_append (out_folder_list, p); 76
77 if (list_create (&folder_mbox_list))
78 {
79 mh_error ("can't create folder list");
80 exit (1);
81 }
82
83 if (iterator_create (&itr, folder_name_list))
84 {
85 mh_error ("can't create iterator");
86 exit (1);
87 }
88
89 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
90 {
91 char *name = NULL;
92 mailbox_t mbox;
93
94 iterator_current (itr, (void **)&name);
95 mbox = mh_open_folder (name, 1);
96 list_append (folder_mbox_list, mbox);
97 free (name);
98 }
99 iterator_destroy (&itr);
100 list_destroy (&folder_name_list);
70 } 101 }
71 102
72 void 103 void
73 enumerate_folders (void (*f) __P((void *, char *)), void *data) 104 enumerate_folders (void (*f) __P((void *, mailbox_t)), void *data)
74 { 105 {
75 iterator_t itr; 106 iterator_t itr;
76 int rc = 0;
77 107
78 if (iterator_create (&itr, out_folder_list)) 108 if (iterator_create (&itr, folder_mbox_list))
79 { 109 {
80 mh_error ("can't create iterator"); 110 mh_error ("can't create iterator");
81 exit (1); 111 exit (1);
...@@ -83,9 +113,9 @@ enumerate_folders (void (*f) __P((void *, char *)), void *data) ...@@ -83,9 +113,9 @@ enumerate_folders (void (*f) __P((void *, char *)), void *data)
83 113
84 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) 114 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
85 { 115 {
86 char *name; 116 mailbox_t mbox;
87 iterator_current (itr, (void **)&name); 117 iterator_current (itr, (void **)&mbox);
88 (*f) (data, name); 118 (*f) (data, mbox);
89 } 119 }
90 iterator_destroy (&itr); 120 iterator_destroy (&itr);
91 } 121 }
...@@ -127,11 +157,43 @@ opt_handler (int key, char *arg, void *unused) ...@@ -127,11 +157,43 @@ opt_handler (int key, char *arg, void *unused)
127 return 0; 157 return 0;
128 } 158 }
129 159
160 void
161 _close_folder (void *unuses, mailbox_t mbox)
162 {
163 mailbox_close (mbox);
164 mailbox_destroy (&mbox);
165 }
166
167 void
168 refile_folder (void *data, mailbox_t mbox)
169 {
170 message_t msg = data;
171 int rc;
172
173 rc = mailbox_append_message (mbox, msg);
174 if (rc)
175 {
176 mh_error ("error appending message: %s", mu_errstring (rc));
177 exit (1);
178 }
179 }
180
181 void
182 refile (mailbox_t mbox, message_t msg, size_t num, void *data)
183 {
184 enumerate_folders (refile_folder, msg);
185 if (!link_flag)
186 {
187 attribute_t attr;
188 message_get_attribute (msg, &attr);
189 attribute_set_deleted (attr);
190 }
191 }
130 192
131 int 193 int
132 main (int argc, char **argv) 194 main (int argc, char **argv)
133 { 195 {
134 int i, index; 196 int index;
135 mh_msgset_t msgset; 197 mh_msgset_t msgset;
136 mailbox_t mbox; 198 mailbox_t mbox;
137 int status; 199 int status;
...@@ -139,9 +201,19 @@ main (int argc, char **argv) ...@@ -139,9 +201,19 @@ main (int argc, char **argv)
139 mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, 201 mh_argp_parse (argc, argv, options, mh_option, args_doc, doc,
140 opt_handler, NULL, &index); 202 opt_handler, NULL, &index);
141 203
142 mbox = mh_open_folder (current_folder); 204 if (source_file && index < argc)
205 {
206 mh_error ("both message set and source file given");
207 exit (1);
208 }
209
210 open_folders ();
211
212 mbox = mh_open_folder (current_folder, 0);
143 mh_msgset_parse (mbox, &msgset, argc - index, argv + index); 213 mh_msgset_parse (mbox, &msgset, argc - index, argv + index);
144 // status = refile (mbox, &msgset); 214 status = mh_iterate (mbox, &msgset, refile, NULL);
215
216 enumerate_folders (_close_folder, NULL);
145 mailbox_expunge (mbox); 217 mailbox_expunge (mbox);
146 mailbox_close (mbox); 218 mailbox_close (mbox);
147 mailbox_destroy (&mbox); 219 mailbox_destroy (&mbox);
......