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