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