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