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