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