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