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