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