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