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