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