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