34fa826a91e7aa651215124e4f78ae03c9435537
[mmh] / uip / show.c
1 /*
2 ** show.c -- show/list 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 <h/mime.h>
11 #include <h/utils.h>
12
13 static struct swit switches[] = {
14 #define CHECKMIMESW  0
15         { "checkmime", 0 },
16 #define NOCHECKMIMESW  1
17         { "nocheckmime", 0 },
18 #define HEADSW  2
19         { "header", 0 },
20 #define NHEADSW  3
21         { "noheader", 0 },
22 #define FORMSW  4
23         { "form formfile", 0 },
24 #define SHOWSW  5
25         { "showproc program", 0 },
26 #define SHOWMIMESW  6
27         { "showmimeproc program", 0 },
28 #define FILESW  7
29         { "file file", -4 },  /* interface from whatnow (listproc) */
30 #define VERSIONSW  8
31         { "version", 0 },
32 #define HELPSW  9
33         { "help", 0 },
34         { NULL, 0 }
35 };
36
37 /*
38 ** static prototypes
39 */
40 static int is_mime(char *);
41
42 #define SHOW  0
43 #define NEXT  1
44 #define PREV  2
45
46 char *showproc = "mhl";
47 char *showmimeproc = "mhshow";
48
49 int
50 main(int argc, char **argv)
51 {
52         int headersw = 1, msgp = 0;
53         int checkmime = 1, mime;
54         int vecp = 1, procp = 1, mode = SHOW, msgnum;
55         char *cp, *file = NULL, *folder = NULL, *proc;
56         char buf[BUFSIZ], **argp, **arguments;
57         char *msgs[MAXARGS], *vec[MAXARGS];
58         struct msgs *mp = NULL;
59
60 #ifdef LOCALE
61         setlocale(LC_ALL, "");
62 #endif
63         invo_name = mhbasename(argv[0]);
64
65         /* read user profile/context */
66         context_read();
67
68         if (!mh_strcasecmp(invo_name, "next")) {
69                 mode = NEXT;
70         } else if (!mh_strcasecmp(invo_name, "prev")) {
71                 mode = PREV;
72         }
73         arguments = getarguments(invo_name, argc, argv, 1);
74         argp = arguments;
75
76         while ((cp = *argp++)) {
77                 if (*cp == '-') {
78                         switch (smatch(++cp, switches)) {
79                         case AMBIGSW:
80                                 ambigsw(cp, switches);
81                                 done(1);
82                         case UNKWNSW:
83                                 vec[vecp++] = --cp;
84                                 continue;
85
86                         case HELPSW:
87                                 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
88                                 print_help(buf, switches, 1);
89                                 done(1);
90                         case VERSIONSW:
91                                 print_version(invo_name);
92                                 done(1);
93
94                         case FILESW:
95                                 if (mode != SHOW)
96 usage:
97                                         adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
98
99                                 if (file)
100                                         adios(NULL, "only one file at a time!");
101                                 if (!(cp = *argp++) || *cp == '-')
102                                         adios(NULL, "missing argument to %s", argp[-2]);
103                                 file = getcpy(expanddir(cp));
104                                 continue;
105
106                         case HEADSW:
107                                 headersw++;
108                                 continue;
109                         case NHEADSW:
110                                 headersw = 0;
111                                 continue;
112
113                         case FORMSW:
114                                 vec[vecp++] = --cp;
115                                 if (!(cp = *argp++) || *cp == '-')
116                                         adios(NULL, "missing argument to %s",
117                                                         argp[-2]);
118                                 vec[vecp++] = getcpy(etcpath(cp));
119                                 continue;
120
121                         case SHOWSW:
122                                 if (!(showproc = *argp++) || *showproc == '-')
123                                         adios(NULL, "missing argument to %s",
124                                                         argp[-2]);
125                                 continue;
126
127                         case SHOWMIMESW:
128                                 if (!(showmimeproc = *argp++) ||
129                                                 *showmimeproc == '-')
130                                         adios(NULL, "missing argument to %s",
131                                                         argp[-2]);
132                                 continue;
133                         case CHECKMIMESW:
134                                 checkmime++;
135                                 continue;
136                         case NOCHECKMIMESW:
137                                 checkmime = 0;
138                                 continue;
139                         }
140                 }
141                 if (*cp == '+' || *cp == '@') {
142                         if (folder)
143                                 adios(NULL, "only one folder at a time!");
144                         else
145                                 folder = getcpy(expandfol(cp));
146                 } else if (mode != SHOW) {
147                         goto usage;
148                 } else {
149                         msgs[msgp++] = cp;
150                 }
151         }
152         procp = vecp;
153
154         if (file) {
155                 if (msgp)
156                         adios(NULL, "only one file at a time!");
157                 vec[vecp++] = file;
158                 goto go_to_it;
159         }
160
161         if (!msgp) {
162                 switch (mode) {
163                 case NEXT:
164                         msgs[msgp++] = seq_next;
165                         break;
166                 case PREV:
167                         msgs[msgp++] = seq_prev;
168                         break;
169                 default:
170                         msgs[msgp++] = seq_cur;
171                         break;
172                 }
173         }
174
175         if (!folder)
176                 folder = getcurfol();
177
178         /* read folder and create message structure */
179         if (!(mp = folder_read(folder)))
180                 adios(NULL, "unable to read folder %s", folder);
181
182         /* check for empty folder */
183         if (mp->nummsg == 0)
184                 adios(NULL, "no messages in %s", folder);
185
186         /* parse all the message ranges/sequences and set SELECTED */
187         for (msgnum = 0; msgnum < msgp; msgnum++)
188                 if (!m_convert(mp, msgs[msgnum]))
189                         done(1);
190
191         /*
192         ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
193         ** since we will use that as a tag to know which messages
194         ** to remove from the "unseen" sequence.
195         */
196         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
197                 if (is_selected(mp, msgnum))
198                         set_unseen(mp, msgnum);
199
200         seq_setprev(mp);  /* set the Previous-Sequence */
201         seq_setunseen(mp, 0);  /* unset unseen seqs for shown msgs */
202
203         if (mp->numsel > MAXARGS - 2)
204                 adios(NULL, "more than %d messages for show exec",
205                                 MAXARGS - 2);
206
207         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
208                 if (is_selected(mp, msgnum))
209                         vec[vecp++] = getcpy(m_name(msgnum));
210
211         seq_setcur(mp, mp->hghsel);  /* update current message  */
212         seq_save(mp);  /* synchronize sequences   */
213         context_replace(curfolder, folder);  /* update current folder   */
214         context_save();  /* save the context file   */
215
216         if (headersw && vecp == 2)
217                 printf("(Message %s:%s)\n", folder, vec[1]);
218
219 go_to_it: ;
220         fflush(stdout);
221
222         vec[vecp] = NULL;
223
224         /*
225         ** Decide which "proc" to use
226         */
227         mime = 0;
228         /* check if any messages are non-text MIME messages */
229         if (checkmime) {
230                 if (file) {
231                         if (is_mime(vec[vecp - 1])) {
232                                 mime = 1;
233                         }
234                 } else {
235                         for (msgnum = mp->lowsel; msgnum <= mp->hghsel;
236                                         msgnum++) {
237                                 if (!is_selected(mp, msgnum)) {
238                                         continue;
239                                 }
240                                 snprintf(buf, sizeof buf, "%s/%d",
241                                                 toabsdir(folder), msgnum);
242                                 if (is_mime(buf)) {
243                                         mime = 1;
244                                         break;
245                                 }
246                         }
247                 }
248         }
249
250         /* Set the "proc" */
251         proc = (mime) ? showmimeproc : showproc;
252
253         /* Special-cased because mhshow takes msg not files args. */
254         if (strcmp(mhbasename(proc), "mhshow")==0) {
255                 if (file) {
256                         vec[vecp] = vec[vecp - 1];
257                         vec[vecp - 1] = "-file";
258                         vec[++vecp] = NULL;
259                 }
260                 vec[vecp++] = concat("+", folder, NULL);
261                 vec[vecp] = NULL;
262                 vec[0] = mhbasename(proc);
263                 execvp(proc, vec);
264                 adios(proc, "unable to exec");
265         }
266
267         /* Add the path to the message names. */
268         if (!file) {
269                 for (msgnum = procp; msgnum < vecp; msgnum++) {
270                         vec[msgnum] = concat(mp->foldpath, "/",
271                                         vec[msgnum], NULL);
272                 }
273         }
274
275         vec[0] = mhbasename(proc);
276         execvp(proc, vec);
277         adios(proc, "unable to exec");
278         return 0;  /* dead code to satisfy the compiler */
279 }
280
281 /*
282 ** Check if a message or file contains MIME.
283 */
284 static int
285 is_mime(char *msgnam)
286 {
287         int state = FLD;
288         char buf[BUFSIZ], name[NAMESZ];
289         FILE *fp;
290
291         if ((fp = fopen(msgnam, "r")) == NULL)
292                 return 0;
293
294         while (1) {
295                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
296                 case FLD:
297                 case FLDPLUS:
298                 case FLDEOF:
299                         /* Does it have a Mime-Version header? */
300                         if (mh_strcasecmp(name, VRSN_FIELD)==0) {
301                                 fclose(fp);
302                                 return 1;
303                         }
304
305                         /* else skip the rest of this header field and go on */
306                         while (state == FLDPLUS) {
307                                 state = m_getfld(state, name, buf, sizeof(buf),
308                                                 fp);
309                         }
310                         break;
311
312                 default:
313                         /* We've passed the header, so message is just text */
314                         fclose(fp);
315                         return 0;
316                 }
317         }
318 }