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