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