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