]> diplodocus.org Git - nmh/blob - uip/mhstore.c
2552cd897e0c167f7129eae1453a704153b27144
[nmh] / uip / mhstore.c
1 /* mhstore.c -- store the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/smatch.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/readconfig.h"
17 #include "sbr/ambigsw.h"
18 #include "sbr/path.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
22 #include <fcntl.h>
23 #include "h/signals.h"
24 #include "h/md5.h"
25 #include "h/mts.h"
26 #include "h/tws.h"
27 #include "h/mime.h"
28 #include "h/mhparse.h"
29 #include "h/mhcachesbr.h"
30 #include "h/done.h"
31 #include "h/utils.h"
32 #include "mhmisc.h"
33 #include "sbr/m_maildir.h"
34 #include "mhfree.h"
35
36 #define MHSTORE_SWITCHES \
37 X("auto", 0, AUTOSW) \
38 X("noauto", 0, NAUTOSW) \
39 X("check", 0, CHECKSW) \
40 X("nocheck", 0, NCHECKSW) \
41 X("verbose", 0, VERBSW) \
42 X("noverbose", 0, NVERBSW) \
43 X("file file", 0, FILESW) /* interface from show */ \
44 X("outfile outfile", 0, OUTFILESW) \
45 X("part number", 0, PARTSW) \
46 X("type content", 0, TYPESW) \
47 X("prefer content", 0, PREFERSW) \
48 X("noprefer", 0, NPREFERSW) \
49 X("rcache policy", 0, RCACHESW) \
50 X("wcache policy", 0, WCACHESW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
54 X("debug", -5, DEBUGSW) \
55
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MHSTORE);
58 #undef X
59
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MHSTORE, switches);
62 #undef X
63
64
65 #define quitser pipeser
66
67 /* mhparse.c */
68 int debugsw = 0;
69
70 /*
71 * static prototypes
72 */
73 static void pipeser (int);
74
75
76 int
77 main (int argc, char **argv)
78 {
79 int msgnum, *icachesw;
80 bool autosw = false;
81 /* verbosw defaults to 1 for backward compatibility. */
82 bool verbosw = true;
83 const char *clobbersw = "always";
84 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
85 char *maildir, buf[100], **argp;
86 char **arguments;
87 char *cwd;
88 struct msgs_array msgs = { 0, 0, NULL };
89 struct msgs *mp = NULL;
90 CT ct, *ctp;
91 FILE *fp;
92 int files_not_clobbered;
93 mhstoreinfo_t info;
94
95 if (nmh_init(argv[0], true, true)) { return 1; }
96
97 set_done(freects_done);
98
99 arguments = getarguments (invo_name, argc, argv, 1);
100 argp = arguments;
101
102 /*
103 * Parse arguments
104 */
105 while ((cp = *argp++)) {
106 if (*cp == '-') {
107 switch (smatch (++cp, switches)) {
108 case AMBIGSW:
109 ambigsw (cp, switches);
110 done (1);
111 case UNKWNSW:
112 die("-%s unknown", cp);
113
114 case HELPSW:
115 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
116 invo_name);
117 print_help (buf, switches, 1);
118 done (0);
119 case VERSIONSW:
120 print_version(invo_name);
121 done (0);
122
123 case AUTOSW:
124 autosw = true;
125 continue;
126 case NAUTOSW:
127 autosw = false;
128 continue;
129
130 case RCACHESW:
131 icachesw = &rcachesw;
132 goto do_cache;
133 case WCACHESW:
134 icachesw = &wcachesw;
135 do_cache:
136 if (!(cp = *argp++) || *cp == '-')
137 die("missing argument to %s", argp[-2]);
138 switch (*icachesw = smatch (cp, cache_policy)) {
139 case AMBIGSW:
140 ambigsw (cp, cache_policy);
141 done (1);
142 case UNKWNSW:
143 die("%s unknown", cp);
144 default:
145 break;
146 }
147 continue;
148
149 case CHECKSW:
150 checksw++;
151 continue;
152 case NCHECKSW:
153 checksw = 0;
154 continue;
155
156 case PARTSW:
157 if (!(cp = *argp++) || *cp == '-')
158 die("missing argument to %s", argp[-2]);
159 if (npart >= NPARTS)
160 die("too many parts (starting with %s), %d max",
161 cp, NPARTS);
162 parts[npart++] = cp;
163 continue;
164
165 case TYPESW:
166 if (!(cp = *argp++) || *cp == '-')
167 die("missing argument to %s", argp[-2]);
168 if (ntype >= NTYPES)
169 die("too many types (starting with %s), %d max",
170 cp, NTYPES);
171 types[ntype++] = cp;
172 continue;
173
174 case PREFERSW:
175 if (!(cp = *argp++) || *cp == '-')
176 die("missing argument to %s", argp[-2]);
177 if (npreferred >= NPREFS)
178 die("too many preferred types (starting with %s), %d max",
179 cp, NPREFS);
180 mime_preference[npreferred].type = cp;
181 cp = strchr(cp, '/');
182 if (cp) *cp++ = '\0';
183 mime_preference[npreferred++].subtype = cp;
184 continue;
185
186 case NPREFERSW:
187 npreferred = 0;
188 continue;
189
190 case FILESW:
191 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
192 die("missing argument to %s", argp[-2]);
193 file = *cp == '-' ? cp : path (cp, TFILE);
194 continue;
195
196 case OUTFILESW:
197 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
198 die("missing argument to %s", argp[-2]);
199 outfile = *cp == '-' ? cp : path (cp, TFILE);
200 continue;
201
202 case VERBSW:
203 verbosw = true;
204 continue;
205 case NVERBSW:
206 verbosw = false;
207 continue;
208 case CLOBBERSW:
209 if (!(cp = *argp++) || *cp == '-')
210 die("missing argument to %s", argp[-2]);
211 clobbersw = cp;
212 continue;
213 case DEBUGSW:
214 debugsw = 1;
215 continue;
216 }
217 }
218 if (*cp == '+' || *cp == '@') {
219 if (folder)
220 die("only one folder at a time!");
221 folder = pluspath (cp);
222 } else
223 app_msgarg(&msgs, cp);
224 }
225
226 /* null terminate the list of acceptable parts/types */
227 parts[npart] = NULL;
228 types[ntype] = NULL;
229
230 /*
231 * Check if we've specified an additional profile
232 */
233 if ((cp = getenv ("MHSTORE"))) {
234 if ((fp = fopen (cp, "r"))) {
235 readconfig(NULL, fp, cp, 0);
236 fclose (fp);
237 } else {
238 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
239 }
240 }
241
242 /*
243 * Read the standard profile setup
244 */
245 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
246 readconfig(NULL, fp, cp, 0);
247 fclose (fp);
248 }
249
250 /* Check for public cache location */
251 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
252 cache_public = NULL;
253
254 /* Check for private cache location */
255 if (!(cache_private = context_find (nmhprivcache)))
256 cache_private = ".cache";
257 cache_private = mh_xstrdup(m_maildir(cache_private));
258
259 /*
260 * Cache the current directory before we do any chdirs()'s.
261 */
262 cwd = mh_xstrdup(pwd());
263
264 if (!context_find ("path"))
265 free (path ("./", TFOLDER));
266
267 if (file && msgs.size)
268 die("cannot specify msg and file at same time!");
269
270 /*
271 * check if message is coming from file
272 */
273 if (file) {
274 cts = mh_xcalloc(2, sizeof *cts);
275 ctp = cts;
276
277 if ((ct = parse_mime (file))) {
278 *ctp++ = ct;
279 if (outfile) {
280 ct->c_storage = mh_xstrdup(outfile);
281 }
282 }
283 } else {
284 /*
285 * message(s) are coming from a folder
286 */
287 if (!msgs.size)
288 app_msgarg(&msgs, "cur");
289 if (!folder)
290 folder = getfolder (1);
291 maildir = m_maildir (folder);
292
293 if (chdir (maildir) == NOTOK)
294 adios (maildir, "unable to change directory to");
295
296 /* read folder and create message structure */
297 if (!(mp = folder_read (folder, 1)))
298 die("unable to read folder %s", folder);
299
300 /* check for empty folder */
301 if (mp->nummsg == 0)
302 die("no messages in %s", folder);
303
304 /* parse all the message ranges/sequences and set SELECTED */
305 for (msgnum = 0; msgnum < msgs.size; msgnum++)
306 if (!m_convert (mp, msgs.msgs[msgnum]))
307 done (1);
308 seq_setprev (mp); /* set the previous-sequence */
309
310 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
311 ctp = cts;
312
313 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
314 if (is_selected(mp, msgnum)) {
315 char *msgnam;
316
317 msgnam = m_name (msgnum);
318 if ((ct = parse_mime (msgnam))) {
319 *ctp++ = ct;
320 if (outfile) {
321 ct->c_storage = mh_xstrdup(outfile);
322 }
323 }
324 }
325 }
326 }
327
328 if (!*cts)
329 done (1);
330
331 userrs = true;
332 SIGNAL (SIGQUIT, quitser);
333 SIGNAL (SIGPIPE, pipeser);
334
335 /*
336 * Get the associated umask for the relevant contents.
337 */
338 for (ctp = cts; *ctp; ctp++) {
339 struct stat st;
340
341 ct = *ctp;
342 if (type_ok (ct, 1) && !ct->c_umask) {
343 if (stat (ct->c_file, &st) != NOTOK)
344 ct->c_umask = ~(st.st_mode & 0777);
345 else
346 ct->c_umask = ~m_gmprot();
347 }
348 }
349
350 /*
351 * Store the message content
352 */
353 info = mhstoreinfo_create (cts, cwd, clobbersw, autosw, verbosw);
354 store_all_messages (info);
355 files_not_clobbered = mhstoreinfo_files_not_clobbered(info);
356 mhstoreinfo_free(info);
357
358 /* Now free all the structures for the content */
359 for (ctp = cts; *ctp; ctp++)
360 free_content (*ctp);
361
362 free (cts);
363 cts = NULL;
364
365 /* If reading from a folder, do some updating */
366 if (mp) {
367 context_replace (pfolder, folder);/* update current folder */
368 seq_setcur (mp, mp->hghsel); /* update current message */
369 seq_save (mp); /* synchronize sequences */
370 context_save (); /* save the context file */
371 }
372
373 done (files_not_clobbered);
374 return 1;
375 }
376
377
378 static void
379 pipeser (int i)
380 {
381 if (i == SIGQUIT) {
382 fflush (stdout);
383 fprintf (stderr, "\n");
384 fflush (stderr);
385 }
386
387 done (1);
388 /* NOTREACHED */
389 }