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