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