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