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