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