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