]> diplodocus.org Git - nmh/blob - uip/show.c
Note in dist, mh-profile, and repl man pages that the @ link
[nmh] / uip / show.c
1
2 /*
3 * show.c -- show/list messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/mime.h>
12 #include <h/utils.h>
13
14 static struct swit switches[] = {
15 #define CHECKMIMESW 0
16 { "checkmime", 0 },
17 #define NOCHECKMIMESW 1
18 { "nocheckmime", 0 },
19 #define HEADSW 2
20 { "header", 0 },
21 #define NHEADSW 3
22 { "noheader", 0 },
23 #define FORMSW 4
24 { "form formfile", 0 },
25 #define PROGSW 5
26 { "moreproc program", 0 },
27 #define NPROGSW 6
28 { "nomoreproc", 0 },
29 #define LENSW 7
30 { "length lines", 0 },
31 #define WIDTHSW 8
32 { "width columns", 0 },
33 #define SHOWSW 9
34 { "showproc program", 0 },
35 #define SHOWMIMESW 10
36 { "showmimeproc program", 0 },
37 #define NSHOWSW 11
38 { "noshowproc", 0 },
39 #define DRFTSW 12
40 { "draft", 0 },
41 #define FILESW 13
42 { "file file", -4 }, /* interface from showfile */
43 #define FMTPROCSW 14
44 { "fmtproc program", 0 },
45 #define NFMTPROCSW 15
46 { "nofmtproc", 0 },
47 #define VERSIONSW 16
48 { "version", 0 },
49 #define HELPSW 17
50 { "help", 0 },
51 { NULL, 0 }
52 };
53
54 /*
55 * static prototypes
56 */
57 static int is_nontext(char *);
58
59 #define SHOW 0
60 #define NEXT 1
61 #define PREV 2
62
63
64 int
65 main (int argc, char **argv)
66 {
67 int draftsw = 0, headersw = 1;
68 int nshow = 0, checkmime = 1, mime;
69 int isdf = 0, mode = SHOW, msgnum;
70 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
71 char buf[BUFSIZ], **argp, **arguments;
72 struct msgs *mp = NULL;
73 struct msgs_array msgs = { 0, 0, NULL };
74 struct msgs_array vec = { 0, 0, NULL };
75
76 #ifdef LOCALE
77 setlocale(LC_ALL, "");
78 #endif
79 invo_name = r1bindex (argv[0], '/');
80
81 app_msgarg(&vec, NULL); /* placeholder, filled later with proc name */
82
83 /* read user profile/context */
84 context_read();
85
86 if (!mh_strcasecmp (invo_name, "next")) {
87 mode = NEXT;
88 } else if (!mh_strcasecmp (invo_name, "prev")) {
89 mode = PREV;
90 }
91 arguments = getarguments (invo_name, argc, argv, 1);
92 argp = arguments;
93
94 while ((cp = *argp++)) {
95 if (*cp == '-') {
96 switch (smatch (++cp, switches)) {
97 case AMBIGSW:
98 ambigsw (cp, switches);
99 done (1);
100 case UNKWNSW:
101 case NPROGSW:
102 case NFMTPROCSW:
103 app_msgarg(&vec, --cp);
104 continue;
105
106 case HELPSW:
107 snprintf (buf, sizeof(buf),
108 "%s [+folder] %s[switches] [switches for showproc]",
109 invo_name, mode == SHOW ? "[msgs] ": "");
110 print_help (buf, switches, 1);
111 done (0);
112 case VERSIONSW:
113 print_version(invo_name);
114 done (0);
115
116 case DRFTSW:
117 if (file)
118 adios (NULL, "only one file at a time!");
119 draftsw++;
120 if (mode == SHOW)
121 continue;
122 usage:
123 adios (NULL,
124 "usage: %s [+folder] [switches] [switches for showproc]",
125 invo_name);
126 case FILESW:
127 if (mode != SHOW)
128 goto usage;
129 if (draftsw || file)
130 adios (NULL, "only one file at a time!");
131 if (!(cp = *argp++) || *cp == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 file = path (cp, TFILE);
134 continue;
135
136 case HEADSW:
137 headersw++;
138 continue;
139 case NHEADSW:
140 headersw = 0;
141 continue;
142
143 case FORMSW:
144 app_msgarg(&vec, --cp);
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 app_msgarg(&vec, getcpy (etcpath(cp)));
148 continue;
149
150 case PROGSW:
151 case LENSW:
152 case WIDTHSW:
153 case FMTPROCSW:
154 app_msgarg(&vec, --cp);
155 if (!(cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
157 app_msgarg(&vec, cp);
158 continue;
159
160 case SHOWSW:
161 if (!(showproc = *argp++) || *showproc == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 nshow = 0;
164 continue;
165 case NSHOWSW:
166 nshow++;
167 continue;
168
169 case SHOWMIMESW:
170 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 nshow = 0;
173 continue;
174 case CHECKMIMESW:
175 checkmime++;
176 continue;
177 case NOCHECKMIMESW:
178 checkmime = 0;
179 continue;
180 }
181 }
182 if (*cp == '+' || *cp == '@') {
183 if (folder)
184 adios (NULL, "only one folder at a time!");
185 else
186 folder = pluspath (cp);
187 } else {
188 if (mode != SHOW)
189 goto usage;
190 else
191 app_msgarg(&msgs, cp);
192 }
193 }
194
195 if (!context_find ("path"))
196 free (path ("./", TFOLDER));
197
198 if (draftsw || file) {
199 if (msgs.size)
200 adios (NULL, "only one file at a time!");
201 if (draftsw)
202 app_msgarg(&vec, getcpy (m_draft (folder, NULL, 1, &isdf)));
203 else
204 app_msgarg(&vec, file);
205 goto go_to_it;
206 }
207
208 #ifdef WHATNOW
209 if (!msgs.size && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
210 draftsw++;
211 app_msgarg(&vec, cp);
212 goto go_to_it;
213 }
214 #endif /* WHATNOW */
215
216 if (!msgs.size) {
217 switch (mode) {
218 case NEXT:
219 app_msgarg(&msgs, "next");
220 break;
221 case PREV:
222 app_msgarg(&msgs, "prev");
223 break;
224 default:
225 app_msgarg(&msgs, "cur");
226 break;
227 }
228 }
229
230 if (!folder)
231 folder = getfolder (1);
232 maildir = m_maildir (folder);
233
234 if (chdir (maildir) == NOTOK)
235 adios (maildir, "unable to change directory to");
236
237 /* read folder and create message structure */
238 if (!(mp = folder_read (folder)))
239 adios (NULL, "unable to read folder %s", folder);
240
241 /* check for empty folder */
242 if (mp->nummsg == 0)
243 adios (NULL, "no messages in %s", folder);
244
245 /* parse all the message ranges/sequences and set SELECTED */
246 for (msgnum = 0; msgnum < msgs.size; msgnum++)
247 if (!m_convert (mp, msgs.msgs[msgnum]))
248 done (1);
249
250 /*
251 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
252 * since we will use that as a tag to know which messages
253 * to remove from the "unseen" sequence.
254 */
255 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
256 if (is_selected(mp, msgnum))
257 set_unseen (mp, msgnum);
258
259 seq_setprev (mp); /* set the Previous-Sequence */
260 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
261
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
263 if (is_selected(mp, msgnum))
264 app_msgarg(&vec, getcpy (m_name (msgnum)));
265
266 seq_setcur (mp, mp->hghsel); /* update current message */
267 seq_save (mp); /* synchronize sequences */
268 context_replace (pfolder, folder); /* update current folder */
269 context_save (); /* save the context file */
270
271 if (headersw && vec.size == 2)
272 printf ("(Message %s:%s)\n", folder, vec.msgs[1]);
273
274 go_to_it: ;
275 fflush (stdout);
276
277 /*
278 * Decide which "proc" to use
279 */
280 mime = 0;
281 if (nshow) {
282 proc = catproc;
283 } else {
284 /* check if any messages are non-text MIME messages */
285 if (checkmime) {
286 if (!draftsw && !file) {
287 /* loop through selected messages and check for MIME */
288 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
289 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
290 mime = 1;
291 break;
292 }
293 } else {
294 /* check the file or draft for MIME */
295 if (is_nontext (vec.msgs[vec.size - 1]))
296 mime = 1;
297 }
298 }
299
300 /* Set the "proc" */
301 if (mime)
302 proc = showmimeproc;
303 else
304 proc = showproc;
305 }
306
307 if (folder && !draftsw && !file)
308 m_putenv ("mhfolder", folder);
309
310 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
311 /* Add "-file" if showing file or draft, */
312 if (draftsw || file) {
313 app_msgarg(&vec, vec.msgs[vec.size - 1]);
314 vec.msgs[vec.size - 2] = "-file";
315 }
316 /* and add -show for backward compatibility */
317 app_msgarg(&vec, "-show");
318 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
319 /* If "mhshow", add "-file" if showing file or draft. */
320 if (draftsw || file) {
321 app_msgarg(&vec, vec.msgs[vec.size - 1]);
322 vec.msgs[vec.size - 2] = "-file";
323 }
324 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
325 /* If "mhl", then run it internally */
326 vec.msgs[0] = "mhl";
327 app_msgarg(&vec, NULL);
328 mhl (vec.size, vec.msgs);
329 done (0);
330 }
331
332 vec.msgs[0] = r1bindex (proc, '/');
333 app_msgarg(&vec, NULL);
334 execvp (proc, vec.msgs);
335 adios (proc, "unable to exec");
336 return 0; /* dead code to satisfy the compiler */
337 }
338
339
340 /*
341 * Check if a message or file contains any non-text parts
342 */
343 static int
344 is_nontext (char *msgnam)
345 {
346 int result, state;
347 unsigned char *bp, *dp;
348 char *cp;
349 char buf[BUFSIZ], name[NAMESZ];
350 FILE *fp;
351
352 if ((fp = fopen (msgnam, "r")) == NULL)
353 return 0;
354
355 for (state = FLD;;) {
356 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
357 case FLD:
358 case FLDPLUS:
359 case FLDEOF:
360 /*
361 * Check Content-Type field
362 */
363 if (!mh_strcasecmp (name, TYPE_FIELD)) {
364 int passno;
365 char c;
366
367 cp = add (buf, NULL);
368 while (state == FLDPLUS) {
369 state = m_getfld (state, name, buf, sizeof(buf), fp);
370 cp = add (buf, cp);
371 }
372 bp = cp;
373 passno = 1;
374
375 again:
376 for (; isspace (*bp); bp++)
377 continue;
378 if (*bp == '(') {
379 int i;
380
381 for (bp++, i = 0;;) {
382 switch (*bp++) {
383 case '\0':
384 invalid:
385 result = 0;
386 goto out;
387 case '\\':
388 if (*bp++ == '\0')
389 goto invalid;
390 continue;
391 case '(':
392 i++;
393 /* and fall... */
394 default:
395 continue;
396 case ')':
397 if (--i < 0)
398 break;
399 continue;
400 }
401 break;
402 }
403 }
404 if (passno == 2) {
405 if (*bp != '/')
406 goto invalid;
407 bp++;
408 passno = 3;
409 goto again;
410 }
411 for (dp = bp; istoken (*dp); dp++)
412 continue;
413 c = *dp;
414 *dp = '\0';
415 if (!*bp)
416 goto invalid;
417 if (passno > 1) {
418 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
419 goto out;
420 *dp = c;
421 for (dp++; isspace (*dp); dp++)
422 continue;
423 if (*dp) {
424 if ((result = !uprf (dp, "charset")))
425 goto out;
426 dp += sizeof("charset") - 1;
427 while (isspace (*dp))
428 dp++;
429 if (*dp++ != '=')
430 goto invalid;
431 while (isspace (*dp))
432 dp++;
433 if (*dp == '"') {
434 if ((bp = strchr(++dp, '"')))
435 *bp = '\0';
436 } else {
437 for (bp = dp; *bp; bp++)
438 if (!istoken (*bp)) {
439 *bp = '\0';
440 break;
441 }
442 }
443 } else {
444 /* Default character set */
445 dp = "US-ASCII";
446 }
447 /* Check the character set */
448 result = !check_charset (dp, strlen (dp));
449 } else {
450 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
451 *dp = c;
452 bp = dp;
453 passno = 2;
454 goto again;
455 }
456 }
457 out:
458 free (cp);
459 if (result) {
460 fclose (fp);
461 return result;
462 }
463 break;
464 }
465
466 /*
467 * Check Content-Transfer-Encoding field
468 */
469 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
470 cp = add (buf, NULL);
471 while (state == FLDPLUS) {
472 state = m_getfld (state, name, buf, sizeof(buf), fp);
473 cp = add (buf, cp);
474 }
475 for (bp = cp; isspace (*bp); bp++)
476 continue;
477 for (dp = bp; istoken (*dp); dp++)
478 continue;
479 *dp = '\0';
480 result = (mh_strcasecmp (bp, "7bit")
481 && mh_strcasecmp (bp, "8bit")
482 && mh_strcasecmp (bp, "binary"));
483
484 free (cp);
485 if (result) {
486 fclose (fp);
487 return result;
488 }
489 break;
490 }
491
492 /*
493 * Just skip the rest of this header
494 * field and go to next one.
495 */
496 while (state == FLDPLUS)
497 state = m_getfld (state, name, buf, sizeof(buf), fp);
498 break;
499
500 /*
501 * We've passed the message header,
502 * so message is just text.
503 */
504 default:
505 fclose (fp);
506 return 0;
507 }
508 }
509 }