]> diplodocus.org Git - nmh/blob - uip/mhstore.c
050310cfec82c97403ff570d38440d7188d1c210
[nmh] / uip / mhstore.c
1
2 /*
3 * mhstore.c -- store the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
19 #include <h/utils.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("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 int save_clobber_policy (const char *);
49 extern int files_not_clobbered;
50
51 /* mhcachesbr.c */
52 extern int rcachesw;
53 extern int wcachesw;
54 extern char *cache_public;
55 extern char *cache_private;
56
57 /* mhstoresbr.c */
58 extern int autosw;
59 extern char *cwd; /* cache current working directory */
60
61 /* mhmisc.c */
62 extern int npart;
63 extern int ntype;
64 extern char *parts[NPARTS + 1];
65 extern char *types[NTYPES + 1];
66 extern int userrs;
67
68 int debugsw = 0;
69 int verbosw = 0;
70
71 #define quitser pipeser
72
73 /* mhparse.c */
74 CT parse_mime (char *);
75
76 /* mhmisc.c */
77 int part_ok (CT, int);
78 int type_ok (CT, int);
79 void flush_errors (void);
80
81 /* mhstoresbr.c */
82 void store_all_messages (CT *);
83
84 /* mhfree.c */
85 extern CT *cts;
86 void freects_done (int) NORETURN;
87
88 /*
89 * static prototypes
90 */
91 static void pipeser (int);
92
93
94 int
95 main (int argc, char **argv)
96 {
97 int msgnum, *icachesw;
98 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
99 char *maildir, buf[100], **argp;
100 char **arguments;
101 struct msgs_array msgs = { 0, 0, NULL };
102 struct msgs *mp = NULL;
103 CT ct, *ctp;
104 FILE *fp;
105
106 done=freects_done;
107
108 #ifdef LOCALE
109 setlocale(LC_ALL, "");
110 #endif
111 invo_name = r1bindex (argv[0], '/');
112
113 /* read user profile/context */
114 context_read();
115
116 arguments = getarguments (invo_name, argc, argv, 1);
117 argp = arguments;
118
119 /*
120 * Parse arguments
121 */
122 while ((cp = *argp++)) {
123 if (*cp == '-') {
124 switch (smatch (++cp, switches)) {
125 case AMBIGSW:
126 ambigsw (cp, switches);
127 done (1);
128 case UNKWNSW:
129 adios (NULL, "-%s unknown", cp);
130
131 case HELPSW:
132 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
133 invo_name);
134 print_help (buf, switches, 1);
135 done (0);
136 case VERSIONSW:
137 print_version(invo_name);
138 done (0);
139
140 case AUTOSW:
141 autosw++;
142 continue;
143 case NAUTOSW:
144 autosw = 0;
145 continue;
146
147 case RCACHESW:
148 icachesw = &rcachesw;
149 goto do_cache;
150 case WCACHESW:
151 icachesw = &wcachesw;
152 do_cache:
153 if (!(cp = *argp++) || *cp == '-')
154 adios (NULL, "missing argument to %s", argp[-2]);
155 switch (*icachesw = smatch (cp, caches)) {
156 case AMBIGSW:
157 ambigsw (cp, caches);
158 done (1);
159 case UNKWNSW:
160 adios (NULL, "%s unknown", cp);
161 default:
162 break;
163 }
164 continue;
165
166 case CHECKSW:
167 checksw++;
168 continue;
169 case NCHECKSW:
170 checksw = 0;
171 continue;
172
173 case PARTSW:
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 if (npart >= NPARTS)
177 adios (NULL, "too many parts (starting with %s), %d max",
178 cp, NPARTS);
179 parts[npart++] = cp;
180 continue;
181
182 case TYPESW:
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 if (ntype >= NTYPES)
186 adios (NULL, "too many types (starting with %s), %d max",
187 cp, NTYPES);
188 types[ntype++] = 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 if (save_clobber_policy (cp)) {
213 adios (NULL, "invalid argument, %s, to %s", argp[-1],
214 argp[-2]);
215 }
216 continue;
217 case DEBUGSW:
218 debugsw = 1;
219 continue;
220 }
221 }
222 if (*cp == '+' || *cp == '@') {
223 if (folder)
224 adios (NULL, "only one folder at a time!");
225 else
226 folder = pluspath (cp);
227 } else
228 app_msgarg(&msgs, cp);
229 }
230
231 /* null terminate the list of acceptable parts/types */
232 parts[npart] = NULL;
233 types[ntype] = NULL;
234
235 /*
236 * Check if we've specified an additional profile
237 */
238 if ((cp = getenv ("MHSTORE"))) {
239 if ((fp = fopen (cp, "r"))) {
240 readconfig ((struct node **) 0, fp, cp, 0);
241 fclose (fp);
242 } else {
243 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
244 }
245 }
246
247 /*
248 * Read the standard profile setup
249 */
250 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node **) 0, fp, cp, 0);
252 fclose (fp);
253 }
254
255 /* Check for public cache location */
256 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
257 cache_public = NULL;
258
259 /* Check for private cache location */
260 if (!(cache_private = context_find (nmhprivcache)))
261 cache_private = ".cache";
262 cache_private = getcpy (m_maildir (cache_private));
263
264 /*
265 * Cache the current directory before we do any chdirs()'s.
266 */
267 cwd = getcpy (pwd());
268
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
271
272 if (file && msgs.size)
273 adios (NULL, "cannot specify msg and file at same time!");
274
275 /*
276 * check if message is coming from file
277 */
278 if (file) {
279 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
280 adios (NULL, "out of memory");
281 ctp = cts;
282
283 if ((ct = parse_mime (file))) {
284 *ctp++ = ct;
285 if (outfile) {
286 ct->c_storage = 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 adios (NULL, "unable to read folder %s", folder);
305
306 /* check for empty folder */
307 if (mp->nummsg == 0)
308 adios (NULL, "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 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
317 adios (NULL, "out of memory");
318 ctp = cts;
319
320 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
321 if (is_selected(mp, msgnum)) {
322 char *msgnam;
323
324 msgnam = m_name (msgnum);
325 if ((ct = parse_mime (msgnam))) {
326 *ctp++ = ct;
327 if (outfile) {
328 ct->c_storage = add (outfile, NULL);
329 }
330 }
331 }
332 }
333 }
334
335 if (!*cts)
336 done (1);
337
338 userrs = 1;
339 SIGNAL (SIGQUIT, quitser);
340 SIGNAL (SIGPIPE, pipeser);
341
342 /*
343 * Get the associated umask for the relevant contents.
344 */
345 for (ctp = cts; *ctp; ctp++) {
346 struct stat st;
347
348 ct = *ctp;
349 if (type_ok (ct, 1) && !ct->c_umask) {
350 if (stat (ct->c_file, &st) != NOTOK)
351 ct->c_umask = ~(st.st_mode & 0777);
352 else
353 ct->c_umask = ~m_gmprot();
354 }
355 }
356
357 /*
358 * Store the message content
359 */
360 store_all_messages (cts);
361
362 /* Now free all the structures for the content */
363 for (ctp = cts; *ctp; ctp++)
364 free_content (*ctp);
365
366 free ((char *) cts);
367 cts = NULL;
368
369 /* If reading from a folder, do some updating */
370 if (mp) {
371 context_replace (pfolder, folder);/* update current folder */
372 seq_setcur (mp, mp->hghsel); /* update current message */
373 seq_save (mp); /* synchronize sequences */
374 context_save (); /* save the context file */
375 }
376
377 done (files_not_clobbered);
378 return 1;
379 }
380
381
382 static void
383 pipeser (int i)
384 {
385 if (i == SIGQUIT) {
386 fflush (stdout);
387 fprintf (stderr, "\n");
388 fflush (stderr);
389 }
390
391 done (1);
392 /* NOTREACHED */
393 }