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