Rearranged whitespace (and comments) in all the code!
[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]",
151                                                 invo_name);
152                                         print_help (buf, switches, 1);
153                                         done (1);
154                                 case VERSIONSW:
155                                         print_version(invo_name);
156                                         done (1);
157
158                                 case ALLSW:
159                                         all = 1;
160                                         continue;
161
162                                 case NALLSW:
163                                         all = 0;
164                                         continue;
165
166                                 case CREATSW:
167                                         fcreat = 1;
168                                         continue;
169                                 case NCREATSW:
170                                         fcreat = -1;
171                                         continue;
172
173                                 case FASTSW:
174                                         fshort++;
175                                         continue;
176                                 case NFASTSW:
177                                         fshort = 0;
178                                         continue;
179
180                                 case HDRSW:
181                                         fheader = 1;
182                                         continue;
183                                 case NHDRSW:
184                                         fheader = -1;
185                                         continue;
186
187                                 case PACKSW:
188                                         fpack++;
189                                         continue;
190                                 case NPACKSW:
191                                         fpack = 0;
192                                         continue;
193
194                                 case VERBSW:
195                                         fverb++;
196                                         continue;
197                                 case NVERBSW:
198                                         fverb = 0;
199                                         continue;
200
201                                 case RECURSW:
202                                         frecurse++;
203                                         continue;
204                                 case NRECRSW:
205                                         frecurse = 0;
206                                         continue;
207
208                                 case TOTALSW:
209                                         ftotal = 1;
210                                         continue;
211                                 case NTOTLSW:
212                                         ftotal = -1;
213                                         continue;
214
215                                 case PRNTSW:
216                                         printsw = 1;
217                                         continue;
218                                 case NPRNTSW:
219                                         printsw = 0;
220                                         continue;
221
222                                 case LISTSW:
223                                         listsw = 1;
224                                         continue;
225                                 case NLISTSW:
226                                         listsw = 0;
227                                         continue;
228
229                                 case PUSHSW:
230                                         pushsw = 1;
231                                         listsw = 1;
232                                         popsw  = 0;
233                                         continue;
234                                 case POPSW:
235                                         popsw  = 1;
236                                         listsw = 1;
237                                         pushsw = 0;
238                                         continue;
239                         }
240                 }
241                 if (*cp == '+' || *cp == '@') {
242                         if (argfolder)
243                                 adios (NULL, "only one folder at a time!");
244                         else
245                                 argfolder = pluspath (cp);
246                 } else {
247                         if (msg)
248                                 adios (NULL, "only one (current) message at a time!");
249                         else
250                                 msg = cp;
251                 }
252         }
253
254         if (!context_find ("path"))
255                 free (path ("./", TFOLDER));
256         nmhdir = concat (m_maildir (""), "/", NULL);
257
258         /*
259          * If we aren't working with the folder stack
260          * (-push, -pop, -list) then the default is to print.
261          */
262         if (pushsw == 0 && popsw == 0 && listsw == 0)
263                 printsw++;
264
265         /* Pushing a folder onto the folder stack */
266         if (pushsw) {
267                 if (!argfolder) {
268                         /* If no folder is given, the current folder and */
269                         /* the top of the folder stack are swapped. */
270                         if ((cp = context_find (stack))) {
271                                 dp = getcpy (cp);
272                                 ap = brkstring (dp, " ", "\n");
273                                 argfolder = getcpy(*ap++);
274                         } else {
275                                 adios (NULL, "no other folder");
276                         }
277                         for (cp = getcpy (getfolder (1)); *ap; ap++)
278                                 cp = add (*ap, add (" ", cp));
279                         free (dp);
280                         context_replace (stack, cp);  /* update folder stack */
281                 } else {
282                         /* update folder stack */
283                         context_replace (stack,
284                                         (cp = context_find (stack))
285                                         ? concat (getfolder (1), " ", cp, NULL)
286                                         : getcpy (getfolder (1)));
287                 }
288         }
289
290         /* Popping a folder off of the folder stack */
291         if (popsw) {
292                 if (argfolder)
293                         adios (NULL, "sorry, no folders allowed with -pop");
294                 if ((cp = context_find (stack))) {
295                         dp = getcpy (cp);
296                         ap = brkstring (dp, " ", "\n");
297                         argfolder = getcpy(*ap++);
298                 } else {
299                         adios (NULL, "folder stack empty");
300                 }
301                 if (*ap) {
302                         /* if there's anything left in the stack */
303                         cp = getcpy (*ap++);
304                         for (; *ap; ap++)
305                                 cp = add (*ap, add (" ", cp));
306                         context_replace (stack, cp);  /* update folder stack */
307                 } else {
308                         context_del (stack);  /* delete folder stack entry from context */
309                 }
310                 free (dp);
311         }
312         if (pushsw || popsw) {
313                 cp = m_maildir(argfolder);
314                 if (access (cp, F_OK) == NOTOK)
315                         adios (cp, "unable to find folder");
316                 context_replace (pfolder, argfolder);  /* update current folder   */
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                 /* change directory to base of nmh directory for crawl_folders */
348                 if (chdir (nmhdir) == NOTOK)
349                         adios (nmhdir, "unable to change directory to");
350                 if (!argfolder) {
351                         if (msg)
352                                 admonish (NULL, "no folder given for message %s", msg);
353                         readonly_folders (); /* do any readonly folders */
354                         strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
355                         crawl_folders (".", get_folder_info_callback, NULL);
356                 } else {
357                         strncpy (folder, argfolder, sizeof(folder));
358                         if (get_folder_info (argfolder, msg)) {
359                                 context_replace (pfolder, argfolder);/* update current folder */
360                                 context_save ();  /* save the context file */
361                         }
362                         /*
363                          * Since recurse wasn't done in get_folder_info(),
364                          * we still need to list all level-1 sub-folders.
365                          */
366                         if (!frecurse)
367                                 crawl_folders (folder, get_folder_info_callback, NULL);
368                 }
369         } else {
370                 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
371
372                 /*
373                  * Check if folder exists.  If not, then see if
374                  * we should create it, or just exit.
375                  */
376                 create_folder (m_maildir (folder), fcreat, done);
377
378                 if (get_folder_info (folder, msg) && argfolder) {
379                         /* update current folder */
380                         context_replace (pfolder, argfolder);
381                         }
382         }
383
384         /*
385          * Print out folder information
386          */
387         print_folders();
388
389         context_save ();  /* save the context file */
390         done (0);
391         return 1;
392 }
393
394 static int
395 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
396 {
397         int i, retval = 1;
398         struct msgs *mp = NULL;
399
400         i = total_folders++;
401
402         /*
403          * if necessary, reallocate the space
404          * for folder information
405          */
406         if (total_folders >= maxFolderInfo) {
407                 maxFolderInfo += CRAWL_NUMFOLDERS;
408                 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
409         }
410
411         fi[i].name   = fold;
412         fi[i].nummsg = 0;
413         fi[i].curmsg = 0;
414         fi[i].lowmsg = 0;
415         fi[i].hghmsg = 0;
416         fi[i].others = 0;
417         fi[i].error  = 0;
418
419         if ((ftotal > 0) || !fshort || msg || fpack) {
420                 /*
421                  * create message structure and get folder info
422                  */
423                 if (!(mp = folder_read (fold))) {
424                         admonish (NULL, "unable to read folder %s", fold);
425                         return 0;
426                 }
427
428                 /* set the current message */
429                 if (msg && !sfold (mp, msg))
430                         retval = 0;
431
432                 if (fpack) {
433                         if (folder_pack (&mp, fverb) == -1)
434                                 done (1);
435                         seq_save (mp);  /* synchronize the sequences */
436                         context_save ();  /* save the context file */
437                 }
438
439                 /* record info for this folder */
440                 if ((ftotal > 0) || !fshort) {
441                         fi[i].nummsg = mp->nummsg;
442                         fi[i].curmsg = mp->curmsg;
443                         fi[i].lowmsg = mp->lowmsg;
444                         fi[i].hghmsg = mp->hghmsg;
445                         fi[i].others = other_files (mp);
446                 }
447
448                 folder_free (mp); /* free folder/message structure */
449         }
450
451         *crawl_children = (frecurse && (fshort || fi[i].others)
452                 && (fi[i].error == 0));
453         return retval;
454 }
455
456 static boolean
457 get_folder_info_callback (char *fold, void *baton)
458 {
459         boolean crawl_children;
460         get_folder_info_body (fold, NULL, &crawl_children);
461         fflush (stdout);
462         return crawl_children;
463 }
464
465 static int
466 get_folder_info (char *fold, char *msg)
467 {
468         boolean crawl_children;
469         int retval;
470
471         retval = get_folder_info_body (fold, msg, &crawl_children);
472
473         if (crawl_children) {
474                 crawl_folders (fold, get_folder_info_callback, NULL);
475         }
476
477         return retval;
478 }
479
480 /*
481  * Print folder information
482  */
483
484 static void
485 print_folders (void)
486 {
487         int i, len, hasempty = 0, curprinted;
488         int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
489         int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
490         int nummsgdigits, lowmsgdigits;
491         int hghmsgdigits, curmsgdigits;
492         char tmpname[BUFSIZ];
493
494         /*
495          * compute a few values needed to for
496          * printing various fields
497          */
498         for (i = 0; i < total_folders; i++) {
499                 /* length of folder name */
500                 len = strlen (fi[i].name);
501                 if (len > maxlen)
502                         maxlen = len;
503
504                 /* If folder has error, skip the rest */
505                 if (fi[i].error)
506                         continue;
507
508                 /* calculate total number of messages */
509                 total_msgs += fi[i].nummsg;
510
511                 /* maximum number of messages */
512                 if (fi[i].nummsg > maxnummsg)
513                         maxnummsg = fi[i].nummsg;
514
515                 /* maximum low message */
516                 if (fi[i].lowmsg > maxlowmsg)
517                         maxlowmsg = fi[i].lowmsg;
518
519                 /* maximum high message */
520                 if (fi[i].hghmsg > maxhghmsg)
521                         maxhghmsg = fi[i].hghmsg;
522
523                 /* maximum current message */
524                 if (fi[i].curmsg >= fi[i].lowmsg &&
525                         fi[i].curmsg <= fi[i].hghmsg &&
526                         fi[i].curmsg > maxcurmsg)
527                         maxcurmsg = fi[i].curmsg;
528
529                 /* check for empty folders */
530                 if (fi[i].nummsg == 0)
531                         hasempty = 1;
532         }
533         nummsgdigits = num_digits (maxnummsg);
534         lowmsgdigits = num_digits (maxlowmsg);
535         hghmsgdigits = num_digits (maxhghmsg);
536         curmsgdigits = num_digits (maxcurmsg);
537
538         if (hasempty && nummsgdigits < 2)
539                 nummsgdigits = 2;
540
541         /*
542          * Print the header
543          */
544         if (fheader > 0 || (all && !fshort && fheader >= 0))
545                 printf ("%-*s %*s %-*s; %-*s %*s\n",
546                                 maxlen+1, "FOLDER",
547                                 nummsgdigits + 13, "# MESSAGES",
548                                 lowmsgdigits + hghmsgdigits + 4, " RANGE",
549                                 curmsgdigits + 4, "CUR",
550                                 9, "(OTHERS)");
551
552         /*
553          * Print folder information
554          */
555         if (all || fshort || ftotal < 1) {
556                 for (i = 0; i < total_folders; i++) {
557                         if (fshort) {
558                                 printf ("%s\n", fi[i].name);
559                                 continue;
560                         }
561
562                         /* Add `+' to end of name, if folder is current */
563                         if (strcmp (folder, fi[i].name))
564                                 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
565                         else
566                                 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
567
568                         if (fi[i].error) {
569                                 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
570                                 continue;
571                         }
572
573                         printf ("%-*s ", maxlen+1, tmpname);
574
575                         curprinted = 0; /* remember if we print cur */
576                         if (fi[i].nummsg == 0) {
577                                 printf ("has %*s messages%*s",
578                                                 nummsgdigits, "no",
579                                                 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
580                         } else {
581                                 printf ("has %*d message%s  (%*d-%*d)",
582                                                 nummsgdigits, fi[i].nummsg,
583                                                 (fi[i].nummsg == 1) ? " " : "s",
584                                                 lowmsgdigits, fi[i].lowmsg,
585                                                 hghmsgdigits, fi[i].hghmsg);
586                                 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
587                                         curprinted = 1;
588                                         printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
589                                 }
590                         }
591
592                         if (fi[i].others)
593                                 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
594                         printf (".\n");
595                 }
596         }
597
598         /*
599          * Print folder/message totals
600          */
601         if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
602                 if (all)
603                         printf ("\n");
604                 printf ("TOTAL = %d message%c in %d folder%s.\n",
605                                 total_msgs, total_msgs != 1 ? 's' : ' ',
606                                 total_folders, total_folders != 1 ? "s" : "");
607         }
608
609         fflush (stdout);
610 }
611
612 /*
613  * Set the current message and sychronize sequences
614  */
615
616 static int
617 sfold (struct msgs *mp, char *msg)
618 {
619         /* parse the message range/sequence/name and set SELECTED */
620         if (!m_convert (mp, msg))
621                 return 0;
622
623         if (mp->numsel > 1) {
624                 admonish (NULL, "only one message at a time!");
625                 return 0;
626         }
627         seq_setprev (mp);  /* set the previous-sequence */
628         seq_setcur (mp, mp->lowsel);  /* set current message */
629         seq_save (mp);  /* synchronize message sequences */
630         context_save ();  /* save the context file */
631
632         return 1;
633 }
634
635
636 /*
637  * Do the read only folders
638  */
639
640 static void
641 readonly_folders (void)
642 {
643         int atrlen;
644         char atrcur[BUFSIZ];
645         register struct node *np;
646
647         snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
648         atrlen = strlen (atrcur);
649
650         for (np = m_defs; np; np = np->n_next)
651                 if (ssequal (atrcur, np->n_name)
652                                 && !ssequal (nmhdir, np->n_name + atrlen))
653                         get_folder_info (np->n_name + atrlen, NULL);
654 }