]> diplodocus.org Git - nmh/blob - uip/folder.c
Note in dist, mh-profile, and repl man pages that the @ link
[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 * 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 (0);
155 case VERSIONSW:
156 print_version(invo_name);
157 done (0);
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 /* change directory to base of nmh directory for crawl_folders */
345 if (chdir (nmhdir) == NOTOK)
346 adios (nmhdir, "unable to change directory to");
347 if (all || ftotal > 0) {
348 /*
349 * If no folder is given, do them all
350 */
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 NMH_UNUSED (baton);
462
463 get_folder_info_body (fold, NULL, &crawl_children);
464 fflush (stdout);
465 return crawl_children;
466 }
467
468 static int
469 get_folder_info (char *fold, char *msg)
470 {
471 boolean crawl_children;
472 int retval;
473
474 retval = get_folder_info_body (fold, msg, &crawl_children);
475
476 if (crawl_children) {
477 crawl_folders (fold, get_folder_info_callback, NULL);
478 }
479
480 return retval;
481 }
482
483 /*
484 * Print folder information
485 */
486
487 static void
488 print_folders (void)
489 {
490 int i, len, hasempty = 0, curprinted;
491 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
492 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
493 int nummsgdigits, lowmsgdigits;
494 int hghmsgdigits, curmsgdigits;
495 char tmpname[BUFSIZ];
496
497 /*
498 * compute a few values needed to for
499 * printing various fields
500 */
501 for (i = 0; i < total_folders; i++) {
502 /* length of folder name */
503 len = strlen (fi[i].name);
504 if (len > maxlen)
505 maxlen = len;
506
507 /* If folder has error, skip the rest */
508 if (fi[i].error)
509 continue;
510
511 /* calculate total number of messages */
512 total_msgs += fi[i].nummsg;
513
514 /* maximum number of messages */
515 if (fi[i].nummsg > maxnummsg)
516 maxnummsg = fi[i].nummsg;
517
518 /* maximum low message */
519 if (fi[i].lowmsg > maxlowmsg)
520 maxlowmsg = fi[i].lowmsg;
521
522 /* maximum high message */
523 if (fi[i].hghmsg > maxhghmsg)
524 maxhghmsg = fi[i].hghmsg;
525
526 /* maximum current message */
527 if (fi[i].curmsg >= fi[i].lowmsg &&
528 fi[i].curmsg <= fi[i].hghmsg &&
529 fi[i].curmsg > maxcurmsg)
530 maxcurmsg = fi[i].curmsg;
531
532 /* check for empty folders */
533 if (fi[i].nummsg == 0)
534 hasempty = 1;
535 }
536 nummsgdigits = num_digits (maxnummsg);
537 lowmsgdigits = num_digits (maxlowmsg);
538 hghmsgdigits = num_digits (maxhghmsg);
539 curmsgdigits = num_digits (maxcurmsg);
540
541 if (hasempty && nummsgdigits < 2)
542 nummsgdigits = 2;
543
544 /*
545 * Print the header
546 */
547 if (fheader > 0 || (all && !fshort && fheader >= 0))
548 printf ("%-*s %*s %-*s; %-*s %*s\n",
549 maxlen+1, "FOLDER",
550 nummsgdigits + 13, "# MESSAGES",
551 lowmsgdigits + hghmsgdigits + 4, " RANGE",
552 curmsgdigits + 4, "CUR",
553 9, "(OTHERS)");
554
555 /*
556 * Print folder information
557 */
558 if (all || fshort || ftotal < 1) {
559 for (i = 0; i < total_folders; i++) {
560 if (fshort) {
561 printf ("%s\n", fi[i].name);
562 continue;
563 }
564
565 /* Add `+' to end of name, if folder is current */
566 if (strcmp (folder, fi[i].name))
567 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
568 else
569 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
570
571 if (fi[i].error) {
572 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
573 continue;
574 }
575
576 printf ("%-*s ", maxlen+1, tmpname);
577
578 curprinted = 0; /* remember if we print cur */
579 if (fi[i].nummsg == 0) {
580 printf ("has %*s messages%*s",
581 nummsgdigits, "no",
582 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
583 } else {
584 printf ("has %*d message%s (%*d-%*d)",
585 nummsgdigits, fi[i].nummsg,
586 (fi[i].nummsg == 1) ? " " : "s",
587 lowmsgdigits, fi[i].lowmsg,
588 hghmsgdigits, fi[i].hghmsg);
589 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
590 curprinted = 1;
591 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
592 }
593 }
594
595 if (fi[i].others)
596 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
597 printf (".\n");
598 }
599 }
600
601 /*
602 * Print folder/message totals
603 */
604 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
605 if (all)
606 printf ("\n");
607 printf ("TOTAL = %d message%c in %d folder%s.\n",
608 total_msgs, total_msgs != 1 ? 's' : ' ',
609 total_folders, total_folders != 1 ? "s" : "");
610 }
611
612 fflush (stdout);
613 }
614
615 /*
616 * Set the current message and sychronize sequences
617 */
618
619 static int
620 sfold (struct msgs *mp, char *msg)
621 {
622 /* parse the message range/sequence/name and set SELECTED */
623 if (!m_convert (mp, msg))
624 return 0;
625
626 if (mp->numsel > 1) {
627 admonish (NULL, "only one message at a time!");
628 return 0;
629 }
630 seq_setprev (mp); /* set the previous-sequence */
631 seq_setcur (mp, mp->lowsel);/* set current message */
632 seq_save (mp); /* synchronize message sequences */
633 context_save (); /* save the context file */
634
635 return 1;
636 }
637
638
639 /*
640 * Do the read only folders
641 */
642
643 static void
644 readonly_folders (void)
645 {
646 int atrlen;
647 char atrcur[BUFSIZ];
648 register struct node *np;
649
650 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
651 atrlen = strlen (atrcur);
652
653 for (np = m_defs; np; np = np->n_next)
654 if (ssequal (atrcur, np->n_name)
655 && !ssequal (nmhdir, np->n_name + atrlen))
656 get_folder_info (np->n_name + atrlen, NULL);
657 }