]> diplodocus.org Git - nmh/blob - uip/show.c
Replaced hard-coded value with MAX_DELIMITER_SIZE in m_getfld.c.
[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 m_getfld_state_t gstate;
352
353 if ((fp = fopen (msgnam, "r")) == NULL)
354 return 0;
355
356 m_getfld_state_init (&gstate);
357 for (;;) {
358 int bufsz = sizeof buf;
359 switch (state = m_getfld (gstate, name, buf, &bufsz, fp)) {
360 case FLD:
361 case FLDPLUS:
362 /*
363 * Check Content-Type field
364 */
365 if (!mh_strcasecmp (name, TYPE_FIELD)) {
366 int passno;
367 char c;
368
369 cp = add (buf, NULL);
370 while (state == FLDPLUS) {
371 bufsz = sizeof buf;
372 state = m_getfld (gstate, name, buf, &bufsz, fp);
373 cp = add (buf, cp);
374 }
375 bp = cp;
376 passno = 1;
377
378 again:
379 for (; isspace (*bp); bp++)
380 continue;
381 if (*bp == '(') {
382 int i;
383
384 for (bp++, i = 0;;) {
385 switch (*bp++) {
386 case '\0':
387 invalid:
388 result = 0;
389 goto out;
390 case '\\':
391 if (*bp++ == '\0')
392 goto invalid;
393 continue;
394 case '(':
395 i++;
396 /* and fall... */
397 default:
398 continue;
399 case ')':
400 if (--i < 0)
401 break;
402 continue;
403 }
404 break;
405 }
406 }
407 if (passno == 2) {
408 if (*bp != '/')
409 goto invalid;
410 bp++;
411 passno = 3;
412 goto again;
413 }
414 for (dp = bp; istoken (*dp); dp++)
415 continue;
416 c = *dp;
417 *dp = '\0';
418 if (!*bp)
419 goto invalid;
420 if (passno > 1) {
421 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
422 goto out;
423 *dp = c;
424 for (dp++; isspace (*dp); dp++)
425 continue;
426 if (*dp) {
427 if ((result = !uprf (dp, "charset")))
428 goto out;
429 dp += sizeof("charset") - 1;
430 while (isspace (*dp))
431 dp++;
432 if (*dp++ != '=')
433 goto invalid;
434 while (isspace (*dp))
435 dp++;
436 if (*dp == '"') {
437 if ((bp = strchr(++dp, '"')))
438 *bp = '\0';
439 } else {
440 for (bp = dp; *bp; bp++)
441 if (!istoken (*bp)) {
442 *bp = '\0';
443 break;
444 }
445 }
446 } else {
447 /* Default character set */
448 dp = "US-ASCII";
449 }
450 /* Check the character set */
451 result = !check_charset (dp, strlen (dp));
452 } else {
453 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
454 *dp = c;
455 bp = dp;
456 passno = 2;
457 goto again;
458 }
459 }
460 out:
461 free (cp);
462 if (result) {
463 fclose (fp);
464 return result;
465 }
466 break;
467 }
468
469 /*
470 * Check Content-Transfer-Encoding field
471 */
472 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
473 cp = add (buf, NULL);
474 while (state == FLDPLUS) {
475 bufsz = sizeof buf;
476 state = m_getfld (gstate, name, buf, &bufsz, fp);
477 cp = add (buf, cp);
478 }
479 for (bp = cp; isspace (*bp); bp++)
480 continue;
481 for (dp = bp; istoken (*dp); dp++)
482 continue;
483 *dp = '\0';
484 result = (mh_strcasecmp (bp, "7bit")
485 && mh_strcasecmp (bp, "8bit")
486 && mh_strcasecmp (bp, "binary"));
487
488 free (cp);
489 if (result) {
490 fclose (fp);
491 return result;
492 }
493 break;
494 }
495
496 /*
497 * Just skip the rest of this header
498 * field and go to next one.
499 */
500 while (state == FLDPLUS) {
501 bufsz = sizeof buf;
502 state = m_getfld (gstate, name, buf, &bufsz, fp);
503 }
504 break;
505
506 /*
507 * We've passed the message header,
508 * so message is just text.
509 */
510 default:
511 fclose (fp);
512 return 0;
513 }
514 }
515 m_getfld_state_destroy (&gstate);
516 }