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