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