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