]> diplodocus.org Git - nmh/blob - uip/folder.c
read_yes_or_no_if_tty.c: Move interface to own file.
[nmh] / uip / folder.c
1 /* folder.c -- set/list the current message and/or folder
2 * -- push/pop a folder onto/from the folder stack
3 * -- list the folder stack
4 *
5 * This code is Copyright (c) 2002, 2008, 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 "sbr/concat.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/ssequal.h"
17 #include "sbr/getcpy.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/folder_pack.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_del.h"
26 #include "sbr/context_find.h"
27 #include "sbr/brkstring.h"
28 #include "sbr/ambigsw.h"
29 #include "sbr/path.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
33 #include "h/crawl_folders.h"
34 #include "h/done.h"
35 #include "h/utils.h"
36 #include "sbr/m_maildir.h"
37
38 #define FOLDER_SWITCHES \
39 X("all", 0, ALLSW) \
40 X("noall", 0, NALLSW) \
41 X("create", 0, CREATSW) \
42 X("nocreate", 0, NCREATSW) \
43 X("fast", 0, FASTSW) \
44 X("nofast", 0, NFASTSW) \
45 X("header", 0, HDRSW) \
46 X("noheader", 0, NHDRSW) \
47 X("pack", 0, PACKSW) \
48 X("nopack", 0, NPACKSW) \
49 X("verbose", 0, VERBSW) \
50 X("noverbose", 0, NVERBSW) \
51 X("recurse", 0, RECURSW) \
52 X("norecurse", 0, NRECRSW) \
53 X("total", 0, TOTALSW) \
54 X("nototal", 0, NTOTLSW) \
55 X("list", 0, LISTSW) \
56 X("nolist", 0, NLISTSW) \
57 X("print", 0, PRNTSW) \
58 X("noprint", 0, NPRNTSW) \
59 X("push", 0, PUSHSW) \
60 X("pop", 0, POPSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(FOLDER);
66 #undef X
67
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(FOLDER, switches);
70 #undef X
71
72 static bool fshort; /* output only folder names */
73 static int fcreat = 0; /* should we ask to create new folders? */
74 static bool fpack; /* are we packing the folder? */
75 static bool fverb; /* print actions taken while packing folder */
76 static int fheader = 0; /* should we output a header? */
77 static bool frecurse; /* recurse through subfolders */
78 static int ftotal = 0; /* should we output the totals? */
79 static bool all; /* should we output all folders */
80
81 static int total_folders = 0; /* total number of folders */
82
83 static char *nmhdir;
84 static char *stack = "Folder-Stack";
85 static char folder[BUFSIZ];
86
87 /*
88 * Structure to hold information about
89 * folders as we scan them.
90 */
91 struct FolderInfo {
92 char *name;
93 int nummsg;
94 int curmsg;
95 int lowmsg;
96 int hghmsg;
97 int others; /* others == 1 if other files in folder */
98 int error; /* error == 1 for unreadable folder */
99 };
100
101 /*
102 * Dynamically allocated space to hold
103 * all the folder information.
104 */
105 static struct FolderInfo *fi;
106 static int maxFolderInfo;
107
108 /*
109 * static prototypes
110 */
111 static int get_folder_info (char *, char *);
112 static crawl_callback_t get_folder_info_callback;
113 static void print_folders (void);
114 static int sfold (struct msgs *, char *);
115 static void readonly_folders (void);
116
117 /*
118 * Function for printing error message if folder does not exist with
119 * -nocreate.
120 */
121 static void
122 nonexistent_folder (int status)
123 {
124 NMH_UNUSED (status);
125 die("folder %s does not exist", folder);
126 }
127
128
129 int
130 main (int argc, char **argv)
131 {
132 bool printsw = false;
133 bool listsw = false;
134 bool pushsw = false;
135 bool popsw = false;
136 char *cp, *dp, *msg = NULL, *argfolder = NULL;
137 char **ap, **argp, buf[BUFSIZ], **arguments;
138
139 if (nmh_init(argv[0], true, true)) { return 1; }
140
141 /*
142 * If program was invoked with name ending
143 * in `s', then add switch `-all'.
144 */
145 all = has_suffix_c(argv[0], 's');
146
147 arguments = getarguments (invo_name, argc, argv, 1);
148 argp = arguments;
149
150 while ((cp = *argp++)) {
151 if (*cp == '-') {
152 switch (smatch (++cp, switches)) {
153 case AMBIGSW:
154 ambigsw (cp, switches);
155 done (1);
156 case UNKWNSW:
157 die("-%s unknown", cp);
158
159 case HELPSW:
160 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
161 invo_name);
162 print_help (buf, switches, 1);
163 done (0);
164 case VERSIONSW:
165 print_version(invo_name);
166 done (0);
167
168 case ALLSW:
169 all = true;
170 continue;
171
172 case NALLSW:
173 all = false;
174 continue;
175
176 case CREATSW:
177 fcreat = 1;
178 continue;
179 case NCREATSW:
180 fcreat = -1;
181 continue;
182
183 case FASTSW:
184 fshort = true;
185 continue;
186 case NFASTSW:
187 fshort = false;
188 continue;
189
190 case HDRSW:
191 fheader = 1;
192 continue;
193 case NHDRSW:
194 fheader = -1;
195 continue;
196
197 case PACKSW:
198 fpack = true;
199 continue;
200 case NPACKSW:
201 fpack = false;
202 continue;
203
204 case VERBSW:
205 fverb = true;
206 continue;
207 case NVERBSW:
208 fverb = false;
209 continue;
210
211 case RECURSW:
212 frecurse = true;
213 continue;
214 case NRECRSW:
215 frecurse = false;
216 continue;
217
218 case TOTALSW:
219 ftotal = 1;
220 continue;
221 case NTOTLSW:
222 ftotal = -1;
223 continue;
224
225 case PRNTSW:
226 printsw = true;
227 continue;
228 case NPRNTSW:
229 printsw = false;
230 continue;
231
232 case LISTSW:
233 listsw = true;
234 continue;
235 case NLISTSW:
236 listsw = false;
237 continue;
238
239 case PUSHSW:
240 pushsw = true;
241 listsw = true;
242 popsw = false;
243 continue;
244 case POPSW:
245 popsw = true;
246 listsw = true;
247 pushsw = false;
248 continue;
249 }
250 }
251 if (*cp == '+' || *cp == '@') {
252 if (argfolder)
253 die("only one folder at a time!");
254 argfolder = pluspath (cp);
255 } else {
256 if (msg)
257 die("only one (current) message at a time!");
258 msg = cp;
259 }
260 }
261
262 if (!context_find ("path"))
263 free (path ("./", TFOLDER));
264 nmhdir = concat (m_maildir (""), "/", NULL);
265
266 /*
267 * If we aren't working with the folder stack
268 * (-push, -pop, -list) then the default is to print.
269 */
270 if (!pushsw && !popsw && !listsw)
271 printsw = true;
272
273 /* Pushing a folder onto the folder stack */
274 if (pushsw) {
275 if (!argfolder) {
276 /* If no folder is given, the current folder and */
277 /* the top of the folder stack are swapped. */
278 if ((cp = context_find (stack))) {
279 dp = mh_xstrdup(cp);
280 ap = brkstring (dp, " ", "\n");
281 argfolder = getcpy(*ap++);
282 } else {
283 die("no other folder");
284 }
285 for (cp = mh_xstrdup(getfolder(1)); *ap; ap++)
286 cp = add (*ap, add (" ", cp));
287 free (dp);
288 context_replace (stack, cp); /* update folder stack */
289 } else {
290 /* update folder stack */
291 context_replace (stack,
292 (cp = context_find (stack))
293 ? concat (getfolder (1), " ", cp, NULL)
294 : mh_xstrdup(getfolder(1)));
295 }
296 }
297
298 /* Popping a folder off of the folder stack */
299 if (popsw) {
300 if (argfolder)
301 die("sorry, no folders allowed with -pop");
302 if ((cp = context_find (stack))) {
303 dp = mh_xstrdup(cp);
304 ap = brkstring (dp, " ", "\n");
305 argfolder = getcpy(*ap++);
306 } else {
307 die("folder stack empty");
308 }
309 if (*ap) {
310 /* if there's anything left in the stack */
311 cp = getcpy (*ap++);
312 for (; *ap; ap++)
313 cp = add (*ap, add (" ", cp));
314 context_replace (stack, cp); /* update folder stack */
315 } else {
316 context_del (stack); /* delete folder stack entry from context */
317 }
318 free (dp);
319 }
320 if (pushsw || popsw) {
321 cp = m_maildir(argfolder);
322 if (access (cp, F_OK) == NOTOK)
323 adios (cp, "unable to find folder");
324 context_replace (pfolder, argfolder); /* update current folder */
325 context_save (); /* save the context file */
326 argfolder = NULL;
327 }
328
329 /* Listing the folder stack */
330 if (listsw) {
331 fputs(argfolder ? argfolder : getfolder (1), stdout);
332 if ((cp = context_find (stack))) {
333 dp = mh_xstrdup(cp);
334 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
335 printf (" %s", *ap);
336 free (dp);
337 }
338 putchar('\n');
339
340 if (!printsw)
341 done (0);
342 }
343
344 /* Allocate initial space to record folder information */
345 maxFolderInfo = CRAWL_NUMFOLDERS;
346 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
347
348 /*
349 * Scan the folders
350 */
351 /* change directory to base of nmh directory for crawl_folders */
352 if (chdir (nmhdir) == NOTOK)
353 adios (nmhdir, "unable to change directory to");
354 if (all || ftotal > 0) {
355 /*
356 * If no folder is given, do them all
357 */
358 if (!argfolder) {
359 if (msg)
360 inform("no folder given for message %s, continuing...", msg);
361 readonly_folders (); /* do any readonly folders */
362 cp = context_find(pfolder);
363 strncpy (folder, FENDNULL(cp), sizeof(folder));
364 crawl_folders (".", get_folder_info_callback, NULL);
365 } else {
366 strncpy (folder, argfolder, sizeof(folder));
367 if (get_folder_info (argfolder, msg)) {
368 context_replace (pfolder, argfolder);/* update current folder */
369 context_save (); /* save the context file */
370 }
371 /*
372 * Since recurse wasn't done in get_folder_info(),
373 * we still need to list all level-1 sub-folders.
374 */
375 if (!frecurse)
376 crawl_folders (folder, get_folder_info_callback, NULL);
377 }
378 } else {
379 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
380
381 /*
382 * Check if folder exists. If not, then see if
383 * we should create it, or just exit.
384 */
385 create_folder (m_maildir (folder), fcreat, nonexistent_folder);
386
387 if (get_folder_info (folder, msg) && argfolder) {
388 /* update current folder */
389 context_replace (pfolder, argfolder);
390 }
391 }
392
393 /*
394 * Print out folder information
395 */
396 print_folders();
397
398 context_save (); /* save the context file */
399 done (0);
400 return 1;
401 }
402
403 static int
404 get_folder_info_body (char *fold, char *msg, bool *crawl_children)
405 {
406 int i, retval = 1;
407 struct msgs *mp = NULL;
408
409 i = total_folders++;
410
411 /*
412 * if necessary, reallocate the space
413 * for folder information
414 */
415 if (total_folders >= maxFolderInfo) {
416 maxFolderInfo += CRAWL_NUMFOLDERS;
417 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
418 }
419
420 fi[i].name = fold;
421 fi[i].nummsg = 0;
422 fi[i].curmsg = 0;
423 fi[i].lowmsg = 0;
424 fi[i].hghmsg = 0;
425 fi[i].others = 0;
426 fi[i].error = 0;
427
428 if ((ftotal > 0) || !fshort || msg || fpack) {
429 /*
430 * create message structure and get folder info
431 */
432 if (!(mp = folder_read (fold, fpack))) {
433 inform("unable to read folder %s, continuing...", fold);
434 *crawl_children = false;
435 return 0;
436 }
437
438 /* set the current message */
439 if (msg && !sfold (mp, msg))
440 retval = 0;
441
442 if (fpack) {
443 if (folder_pack (&mp, fverb) == -1) {
444 *crawl_children = false; /* to please clang static analyzer */
445 done (1);
446 }
447 seq_save (mp); /* synchronize the sequences */
448 context_save (); /* save the context file */
449 }
450
451 /* record info for this folder */
452 if ((ftotal > 0) || !fshort) {
453 fi[i].nummsg = mp->nummsg;
454 fi[i].curmsg = mp->curmsg;
455 fi[i].lowmsg = mp->lowmsg;
456 fi[i].hghmsg = mp->hghmsg;
457 fi[i].others = other_files (mp);
458 }
459
460 folder_free (mp); /* free folder/message structure */
461 }
462
463 *crawl_children = (frecurse && (fshort || fi[i].others)
464 && (fi[i].error == 0));
465 return retval;
466 }
467
468 static bool
469 get_folder_info_callback (char *fold, void *baton)
470 {
471 bool crawl_children;
472 NMH_UNUSED (baton);
473
474 get_folder_info_body (fold, NULL, &crawl_children);
475 fflush (stdout);
476 return crawl_children;
477 }
478
479 static int
480 get_folder_info (char *fold, char *msg)
481 {
482 bool crawl_children;
483 int retval;
484
485 retval = get_folder_info_body (fold, msg, &crawl_children);
486
487 if (crawl_children) {
488 crawl_folders (fold, get_folder_info_callback, NULL);
489 }
490
491 return retval;
492 }
493
494 /*
495 * Print folder information
496 */
497
498 static void
499 print_folders (void)
500 {
501 int i, len;
502 bool hasempty = false;
503 bool curprinted;
504 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
505 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
506 int nummsgdigits, lowmsgdigits;
507 int hghmsgdigits, curmsgdigits;
508 char tmpname[BUFSIZ];
509
510 /*
511 * compute a few values needed to for
512 * printing various fields
513 */
514 for (i = 0; i < total_folders; i++) {
515 /* length of folder name */
516 len = strlen (fi[i].name);
517 if (len > maxlen)
518 maxlen = len;
519
520 /* If folder has error, skip the rest */
521 if (fi[i].error)
522 continue;
523
524 /* calculate total number of messages */
525 total_msgs += fi[i].nummsg;
526
527 /* maximum number of messages */
528 if (fi[i].nummsg > maxnummsg)
529 maxnummsg = fi[i].nummsg;
530
531 /* maximum low message */
532 if (fi[i].lowmsg > maxlowmsg)
533 maxlowmsg = fi[i].lowmsg;
534
535 /* maximum high message */
536 if (fi[i].hghmsg > maxhghmsg)
537 maxhghmsg = fi[i].hghmsg;
538
539 /* maximum current message */
540 if (fi[i].curmsg >= fi[i].lowmsg &&
541 fi[i].curmsg <= fi[i].hghmsg &&
542 fi[i].curmsg > maxcurmsg)
543 maxcurmsg = fi[i].curmsg;
544
545 /* check for empty folders */
546 if (fi[i].nummsg == 0)
547 hasempty = true;
548 }
549 nummsgdigits = num_digits (maxnummsg);
550 lowmsgdigits = num_digits (maxlowmsg);
551 hghmsgdigits = num_digits (maxhghmsg);
552 curmsgdigits = num_digits (maxcurmsg);
553
554 if (hasempty && nummsgdigits < 2)
555 nummsgdigits = 2;
556
557 /*
558 * Print the header
559 */
560 if (fheader > 0 || (all && !fshort && fheader >= 0))
561 printf ("%-*s %*s %-*s; %-*s %*s\n",
562 maxlen+1, "FOLDER",
563 nummsgdigits + 13, "# MESSAGES",
564 lowmsgdigits + hghmsgdigits + 4, " RANGE",
565 curmsgdigits + 4, "CUR",
566 9, "(OTHERS)");
567
568 /*
569 * Print folder information
570 */
571 if (all || fshort || ftotal < 1) {
572 for (i = 0; i < total_folders; i++) {
573 if (fshort) {
574 puts(fi[i].name);
575 continue;
576 }
577
578 /* Add `+' to end of name, if folder is current */
579 if (strcmp (folder, fi[i].name))
580 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
581 else
582 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
583
584 if (fi[i].error) {
585 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
586 continue;
587 }
588
589 printf ("%-*s ", maxlen+1, tmpname);
590
591 curprinted = false; /* remember if we print cur */
592 if (fi[i].nummsg == 0) {
593 printf ("has %*s messages%*s",
594 nummsgdigits, "no",
595 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
596 } else {
597 printf ("has %*d message%1s (%*d-%*d)",
598 nummsgdigits, fi[i].nummsg,
599 PLURALS(fi[i].nummsg),
600 lowmsgdigits, fi[i].lowmsg,
601 hghmsgdigits, fi[i].hghmsg);
602 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
603 curprinted = true;
604 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
605 }
606 }
607
608 if (fi[i].others)
609 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
610 puts(".");
611 }
612 }
613
614 /*
615 * Print folder/message totals
616 */
617 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
618 if (all)
619 putchar('\n');
620 printf ("TOTAL = %d message%s in %d folder%s.\n",
621 total_msgs, PLURALS(total_msgs),
622 total_folders, PLURALS(total_folders));
623 }
624
625 fflush (stdout);
626 }
627
628 /*
629 * Set the current message and synchronize sequences
630 */
631
632 static int
633 sfold (struct msgs *mp, char *msg)
634 {
635 /* parse the message range/sequence/name and set SELECTED */
636 if (!m_convert (mp, msg))
637 return 0;
638
639 if (mp->numsel > 1) {
640 inform("only one message at a time!, continuing...");
641 return 0;
642 }
643 seq_setprev (mp); /* set the previous-sequence */
644 seq_setcur (mp, mp->lowsel);/* set current message */
645 seq_save (mp); /* synchronize message sequences */
646 context_save (); /* save the context file */
647
648 return 1;
649 }
650
651
652 /*
653 * Do the read only folders
654 */
655
656 static void
657 readonly_folders (void)
658 {
659 int atrlen;
660 char atrcur[BUFSIZ];
661 struct node *np;
662
663 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
664 atrlen = strlen (atrcur);
665
666 for (np = m_defs; np; np = np->n_next)
667 if (ssequal (atrcur, np->n_name)
668 && !ssequal (nmhdir, np->n_name + atrlen))
669 get_folder_info (np->n_name + atrlen, NULL);
670 }