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