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