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