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