]> diplodocus.org Git - nmh/blob - uip/mhstore.c
Documentation improvements, from Ralph Corderoy.
[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 /* mhstoresbr.c */
55 typedef struct mhstoreinfo *mhstoreinfo_t;
56 mhstoreinfo_t mhstoreinfo_create(CT *, char *, const char *, int, int);
57 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t);
58 void mhstoreinfo_free(mhstoreinfo_t);
59
60 /* mhmisc.c */
61 extern int npart;
62 extern int ntype;
63 extern char *parts[NPARTS + 1];
64 extern char *types[NTYPES + 1];
65 extern int userrs;
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, int);
75 int type_ok (CT, int);
76 void flush_errors (void);
77
78 /* mhstoresbr.c */
79 void store_all_messages (mhstoreinfo_t);
80
81 /* mhfree.c */
82 extern CT *cts;
83 void freects_done (int) NORETURN;
84
85 /*
86 * static prototypes
87 */
88 static void pipeser (int);
89
90
91 int
92 main (int argc, char **argv)
93 {
94 /* verbosw defaults to 1 for backward compatibility. */
95 int msgnum, *icachesw, autosw = 0, verbosw = 1;
96 const char *clobbersw = "always";
97 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
98 char *maildir, buf[100], **argp;
99 char **arguments;
100 char *cwd;
101 struct msgs_array msgs = { 0, 0, NULL };
102 struct msgs *mp = NULL;
103 CT ct, *ctp;
104 FILE *fp;
105 int files_not_clobbered;
106 mhstoreinfo_t info;
107
108 if (nmh_init(argv[0], 1)) { return 1; }
109
110 done=freects_done;
111
112 arguments = getarguments (invo_name, argc, argv, 1);
113 argp = arguments;
114
115 /*
116 * Parse arguments
117 */
118 while ((cp = *argp++)) {
119 if (*cp == '-') {
120 switch (smatch (++cp, switches)) {
121 case AMBIGSW:
122 ambigsw (cp, switches);
123 done (1);
124 case UNKWNSW:
125 adios (NULL, "-%s unknown", cp);
126
127 case HELPSW:
128 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
129 invo_name);
130 print_help (buf, switches, 1);
131 done (0);
132 case VERSIONSW:
133 print_version(invo_name);
134 done (0);
135
136 case AUTOSW:
137 autosw++;
138 continue;
139 case NAUTOSW:
140 autosw = 0;
141 continue;
142
143 case RCACHESW:
144 icachesw = &rcachesw;
145 goto do_cache;
146 case WCACHESW:
147 icachesw = &wcachesw;
148 do_cache:
149 if (!(cp = *argp++) || *cp == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
151 switch (*icachesw = smatch (cp, caches)) {
152 case AMBIGSW:
153 ambigsw (cp, caches);
154 done (1);
155 case UNKWNSW:
156 adios (NULL, "%s unknown", cp);
157 default:
158 break;
159 }
160 continue;
161
162 case CHECKSW:
163 checksw++;
164 continue;
165 case NCHECKSW:
166 checksw = 0;
167 continue;
168
169 case PARTSW:
170 if (!(cp = *argp++) || *cp == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 if (npart >= NPARTS)
173 adios (NULL, "too many parts (starting with %s), %d max",
174 cp, NPARTS);
175 parts[npart++] = cp;
176 continue;
177
178 case TYPESW:
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 if (ntype >= NTYPES)
182 adios (NULL, "too many types (starting with %s), %d max",
183 cp, NTYPES);
184 types[ntype++] = cp;
185 continue;
186
187 case FILESW:
188 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
189 adios (NULL, "missing argument to %s", argp[-2]);
190 file = *cp == '-' ? cp : path (cp, TFILE);
191 continue;
192
193 case OUTFILESW:
194 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
195 adios (NULL, "missing argument to %s", argp[-2]);
196 outfile = *cp == '-' ? cp : path (cp, TFILE);
197 continue;
198
199 case VERBSW:
200 verbosw = 1;
201 continue;
202 case NVERBSW:
203 verbosw = 0;
204 continue;
205 case CLOBBERSW:
206 if (!(cp = *argp++) || *cp == '-')
207 adios (NULL, "missing argument to %s", argp[-2]);
208 clobbersw = cp;
209 continue;
210 case DEBUGSW:
211 debugsw = 1;
212 continue;
213 }
214 }
215 if (*cp == '+' || *cp == '@') {
216 if (folder)
217 adios (NULL, "only one folder at a time!");
218 else
219 folder = pluspath (cp);
220 } else
221 app_msgarg(&msgs, cp);
222 }
223
224 /* null terminate the list of acceptable parts/types */
225 parts[npart] = NULL;
226 types[ntype] = NULL;
227
228 /*
229 * Check if we've specified an additional profile
230 */
231 if ((cp = getenv ("MHSTORE"))) {
232 if ((fp = fopen (cp, "r"))) {
233 readconfig ((struct node **) 0, fp, cp, 0);
234 fclose (fp);
235 } else {
236 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
237 }
238 }
239
240 /*
241 * Read the standard profile setup
242 */
243 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
244 readconfig ((struct node **) 0, fp, cp, 0);
245 fclose (fp);
246 }
247
248 /* Check for public cache location */
249 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
250 cache_public = NULL;
251
252 /* Check for private cache location */
253 if (!(cache_private = context_find (nmhprivcache)))
254 cache_private = ".cache";
255 cache_private = getcpy (m_maildir (cache_private));
256
257 /*
258 * Cache the current directory before we do any chdirs()'s.
259 */
260 cwd = add(pwd(), NULL);
261
262 if (!context_find ("path"))
263 free (path ("./", TFOLDER));
264
265 if (file && msgs.size)
266 adios (NULL, "cannot specify msg and file at same time!");
267
268 /*
269 * check if message is coming from file
270 */
271 if (file) {
272 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
273 adios (NULL, "out of memory");
274 ctp = cts;
275
276 if ((ct = parse_mime (file))) {
277 *ctp++ = ct;
278 if (outfile) {
279 ct->c_storage = outfile;
280 }
281 }
282 } else {
283 /*
284 * message(s) are coming from a folder
285 */
286 if (!msgs.size)
287 app_msgarg(&msgs, "cur");
288 if (!folder)
289 folder = getfolder (1);
290 maildir = m_maildir (folder);
291
292 if (chdir (maildir) == NOTOK)
293 adios (maildir, "unable to change directory to");
294
295 /* read folder and create message structure */
296 if (!(mp = folder_read (folder, 1)))
297 adios (NULL, "unable to read folder %s", folder);
298
299 /* check for empty folder */
300 if (mp->nummsg == 0)
301 adios (NULL, "no messages in %s", folder);
302
303 /* parse all the message ranges/sequences and set SELECTED */
304 for (msgnum = 0; msgnum < msgs.size; msgnum++)
305 if (!m_convert (mp, msgs.msgs[msgnum]))
306 done (1);
307 seq_setprev (mp); /* set the previous-sequence */
308
309 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
310 adios (NULL, "out of memory");
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 = add (outfile, NULL);
322 }
323 }
324 }
325 }
326 }
327
328 if (!*cts)
329 done (1);
330
331 userrs = 1;
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 }