]> diplodocus.org Git - nmh/blob - uip/mhstore.c
Simplified m_strn() per Ralph's suggestions.
[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 else
218 folder = pluspath (cp);
219 } else
220 app_msgarg(&msgs, cp);
221 }
222
223 /* null terminate the list of acceptable parts/types */
224 parts[npart] = NULL;
225 types[ntype] = NULL;
226
227 /*
228 * Check if we've specified an additional profile
229 */
230 if ((cp = getenv ("MHSTORE"))) {
231 if ((fp = fopen (cp, "r"))) {
232 readconfig ((struct node **) 0, fp, cp, 0);
233 fclose (fp);
234 } else {
235 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
236 }
237 }
238
239 /*
240 * Read the standard profile setup
241 */
242 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
243 readconfig ((struct node **) 0, fp, cp, 0);
244 fclose (fp);
245 }
246
247 /* Check for public cache location */
248 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
249 cache_public = NULL;
250
251 /* Check for private cache location */
252 if (!(cache_private = context_find (nmhprivcache)))
253 cache_private = ".cache";
254 cache_private = getcpy (m_maildir (cache_private));
255
256 /*
257 * Cache the current directory before we do any chdirs()'s.
258 */
259 cwd = mh_xstrdup(pwd());
260
261 if (!context_find ("path"))
262 free (path ("./", TFOLDER));
263
264 if (file && msgs.size)
265 adios (NULL, "cannot specify msg and file at same time!");
266
267 /*
268 * check if message is coming from file
269 */
270 if (file) {
271 cts = mh_xcalloc(2, sizeof *cts);
272 ctp = cts;
273
274 if ((ct = parse_mime (file))) {
275 *ctp++ = ct;
276 if (outfile) {
277 ct->c_storage = mh_xstrdup(outfile);
278 }
279 }
280 } else {
281 /*
282 * message(s) are coming from a folder
283 */
284 if (!msgs.size)
285 app_msgarg(&msgs, "cur");
286 if (!folder)
287 folder = getfolder (1);
288 maildir = m_maildir (folder);
289
290 if (chdir (maildir) == NOTOK)
291 adios (maildir, "unable to change directory to");
292
293 /* read folder and create message structure */
294 if (!(mp = folder_read (folder, 1)))
295 adios (NULL, "unable to read folder %s", folder);
296
297 /* check for empty folder */
298 if (mp->nummsg == 0)
299 adios (NULL, "no messages in %s", folder);
300
301 /* parse all the message ranges/sequences and set SELECTED */
302 for (msgnum = 0; msgnum < msgs.size; msgnum++)
303 if (!m_convert (mp, msgs.msgs[msgnum]))
304 done (1);
305 seq_setprev (mp); /* set the previous-sequence */
306
307 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
308 ctp = cts;
309
310 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
311 if (is_selected(mp, msgnum)) {
312 char *msgnam;
313
314 msgnam = m_name (msgnum);
315 if ((ct = parse_mime (msgnam))) {
316 *ctp++ = ct;
317 if (outfile) {
318 ct->c_storage = mh_xstrdup(outfile);
319 }
320 }
321 }
322 }
323 }
324
325 if (!*cts)
326 done (1);
327
328 userrs = 1;
329 SIGNAL (SIGQUIT, quitser);
330 SIGNAL (SIGPIPE, pipeser);
331
332 /*
333 * Get the associated umask for the relevant contents.
334 */
335 for (ctp = cts; *ctp; ctp++) {
336 struct stat st;
337
338 ct = *ctp;
339 if (type_ok (ct, 1) && !ct->c_umask) {
340 if (stat (ct->c_file, &st) != NOTOK)
341 ct->c_umask = ~(st.st_mode & 0777);
342 else
343 ct->c_umask = ~m_gmprot();
344 }
345 }
346
347 /*
348 * Store the message content
349 */
350 info = mhstoreinfo_create (cts, cwd, clobbersw, autosw, verbosw);
351 store_all_messages (info);
352 files_not_clobbered = mhstoreinfo_files_not_clobbered(info);
353 mhstoreinfo_free(info);
354
355 /* Now free all the structures for the content */
356 for (ctp = cts; *ctp; ctp++)
357 free_content (*ctp);
358
359 free (cts);
360 cts = NULL;
361
362 /* If reading from a folder, do some updating */
363 if (mp) {
364 context_replace (pfolder, folder);/* update current folder */
365 seq_setcur (mp, mp->hghsel); /* update current message */
366 seq_save (mp); /* synchronize sequences */
367 context_save (); /* save the context file */
368 }
369
370 done (files_not_clobbered);
371 return 1;
372 }
373
374
375 static void
376 pipeser (int i)
377 {
378 if (i == SIGQUIT) {
379 fflush (stdout);
380 fprintf (stderr, "\n");
381 fflush (stderr);
382 }
383
384 done (1);
385 /* NOTREACHED */
386 }