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