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