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