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