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