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