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