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