]> diplodocus.org Git - nmh/blob - uip/folder.c
Fix warning in getaddrinfo() call.
[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, 2008, 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/crawl_folders.h>
16 #include <h/utils.h>
17 #include <errno.h>
18
19 static struct swit switches[] = {
20 #define ALLSW 0
21 { "all", 0 },
22 #define NALLSW 1
23 { "noall", 0 },
24 #define CREATSW 2
25 { "create", 0 },
26 #define NCREATSW 3
27 { "nocreate", 0 },
28 #define FASTSW 4
29 { "fast", 0 },
30 #define NFASTSW 5
31 { "nofast", 0 },
32 #define HDRSW 6
33 { "header", 0 },
34 #define NHDRSW 7
35 { "noheader", 0 },
36 #define PACKSW 8
37 { "pack", 0 },
38 #define NPACKSW 9
39 { "nopack", 0 },
40 #define VERBSW 10
41 { "verbose", 0 },
42 #define NVERBSW 11
43 { "noverbose", 0 },
44 #define RECURSW 12
45 { "recurse", 0 },
46 #define NRECRSW 13
47 { "norecurse", 0 },
48 #define TOTALSW 14
49 { "total", 0 },
50 #define NTOTLSW 15
51 { "nototal", 0 },
52 #define LISTSW 16
53 { "list", 0 },
54 #define NLISTSW 17
55 { "nolist", 0 },
56 #define PRNTSW 18
57 { "print", 0 },
58 #define NPRNTSW 19
59 { "noprint", -4 },
60 #define PUSHSW 20
61 { "push", 0 },
62 #define POPSW 21
63 { "pop", 0 },
64 #define VERSIONSW 22
65 { "version", 0 },
66 #define HELPSW 23
67 { "help", 0 },
68 { NULL, 0 }
69 };
70
71 static int fshort = 0; /* output only folder names */
72 static int fcreat = 0; /* should we ask to create new folders? */
73 static int fpack = 0; /* are we packing the folder? */
74 static int fverb = 0; /* print actions taken while packing folder */
75 static int fheader = 0; /* should we output a header? */
76 static int frecurse = 0; /* recurse through subfolders */
77 static int ftotal = 0; /* should we output the totals? */
78 static int all = 0; /* should we output all folders */
79
80 static int total_folders = 0; /* total number of folders */
81
82 static char *nmhdir;
83 static char *stack = "Folder-Stack";
84 static char folder[BUFSIZ];
85
86 /*
87 * Structure to hold information about
88 * folders as we scan them.
89 */
90 struct FolderInfo {
91 char *name;
92 int nummsg;
93 int curmsg;
94 int lowmsg;
95 int hghmsg;
96 int others; /* others == 1 if other files in folder */
97 int error; /* error == 1 for unreadable folder */
98 };
99
100 /*
101 * Dynamically allocated space to hold
102 * all the folder information.
103 */
104 static struct FolderInfo *fi;
105 static int maxFolderInfo;
106
107 /*
108 * static prototypes
109 */
110 static int get_folder_info (char *, char *);
111 static crawl_callback_t get_folder_info_callback;
112 static void print_folders (void);
113 static int sfold (struct msgs *, char *);
114 static void readonly_folders (void);
115
116
117 int
118 main (int argc, char **argv)
119 {
120 int printsw = 0, listsw = 0;
121 int pushsw = 0, popsw = 0;
122 char *cp, *dp, *msg = NULL, *argfolder = NULL;
123 char **ap, **argp, buf[BUFSIZ], **arguments;
124
125 #ifdef LOCALE
126 setlocale(LC_ALL, "");
127 #endif
128 invo_name = r1bindex (argv[0], '/');
129
130 /* read user profile/context */
131 context_read();
132
133 /*
134 * If program was invoked with name ending
135 * in `s', then add switch `-all'.
136 */
137 if (argv[0][strlen (argv[0]) - 1] == 's')
138 all = 1;
139
140 arguments = getarguments (invo_name, argc, argv, 1);
141 argp = arguments;
142
143 while ((cp = *argp++)) {
144 if (*cp == '-') {
145 switch (smatch (++cp, switches)) {
146 case AMBIGSW:
147 ambigsw (cp, switches);
148 done (1);
149 case UNKWNSW:
150 adios (NULL, "-%s unknown", cp);
151
152 case HELPSW:
153 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
154 invo_name);
155 print_help (buf, switches, 1);
156 done (1);
157 case VERSIONSW:
158 print_version(invo_name);
159 done (1);
160
161 case ALLSW:
162 all = 1;
163 continue;
164
165 case NALLSW:
166 all = 0;
167 continue;
168
169 case CREATSW:
170 fcreat = 1;
171 continue;
172 case NCREATSW:
173 fcreat = -1;
174 continue;
175
176 case FASTSW:
177 fshort++;
178 continue;
179 case NFASTSW:
180 fshort = 0;
181 continue;
182
183 case HDRSW:
184 fheader = 1;
185 continue;
186 case NHDRSW:
187 fheader = -1;
188 continue;
189
190 case PACKSW:
191 fpack++;
192 continue;
193 case NPACKSW:
194 fpack = 0;
195 continue;
196
197 case VERBSW:
198 fverb++;
199 continue;
200 case NVERBSW:
201 fverb = 0;
202 continue;
203
204 case RECURSW:
205 frecurse++;
206 continue;
207 case NRECRSW:
208 frecurse = 0;
209 continue;
210
211 case TOTALSW:
212 ftotal = 1;
213 continue;
214 case NTOTLSW:
215 ftotal = -1;
216 continue;
217
218 case PRNTSW:
219 printsw = 1;
220 continue;
221 case NPRNTSW:
222 printsw = 0;
223 continue;
224
225 case LISTSW:
226 listsw = 1;
227 continue;
228 case NLISTSW:
229 listsw = 0;
230 continue;
231
232 case PUSHSW:
233 pushsw = 1;
234 listsw = 1;
235 popsw = 0;
236 continue;
237 case POPSW:
238 popsw = 1;
239 listsw = 1;
240 pushsw = 0;
241 continue;
242 }
243 }
244 if (*cp == '+' || *cp == '@') {
245 if (argfolder)
246 adios (NULL, "only one folder at a time!");
247 else
248 argfolder = pluspath (cp);
249 } else {
250 if (msg)
251 adios (NULL, "only one (current) message at a time!");
252 else
253 msg = cp;
254 }
255 }
256
257 if (!context_find ("path"))
258 free (path ("./", TFOLDER));
259 nmhdir = concat (m_maildir (""), "/", NULL);
260
261 /*
262 * If we aren't working with the folder stack
263 * (-push, -pop, -list) then the default is to print.
264 */
265 if (pushsw == 0 && popsw == 0 && listsw == 0)
266 printsw++;
267
268 /* Pushing a folder onto the folder stack */
269 if (pushsw) {
270 if (!argfolder) {
271 /* If no folder is given, the current folder and */
272 /* the top of the folder stack are swapped. */
273 if ((cp = context_find (stack))) {
274 dp = getcpy (cp);
275 ap = brkstring (dp, " ", "\n");
276 argfolder = getcpy(*ap++);
277 } else {
278 adios (NULL, "no other folder");
279 }
280 for (cp = getcpy (getfolder (1)); *ap; ap++)
281 cp = add (*ap, add (" ", cp));
282 free (dp);
283 context_replace (stack, cp); /* update folder stack */
284 } else {
285 /* update folder stack */
286 context_replace (stack,
287 (cp = context_find (stack))
288 ? concat (getfolder (1), " ", cp, NULL)
289 : getcpy (getfolder (1)));
290 }
291 }
292
293 /* Popping a folder off of the folder stack */
294 if (popsw) {
295 if (argfolder)
296 adios (NULL, "sorry, no folders allowed with -pop");
297 if ((cp = context_find (stack))) {
298 dp = getcpy (cp);
299 ap = brkstring (dp, " ", "\n");
300 argfolder = getcpy(*ap++);
301 } else {
302 adios (NULL, "folder stack empty");
303 }
304 if (*ap) {
305 /* if there's anything left in the stack */
306 cp = getcpy (*ap++);
307 for (; *ap; ap++)
308 cp = add (*ap, add (" ", cp));
309 context_replace (stack, cp); /* update folder stack */
310 } else {
311 context_del (stack); /* delete folder stack entry from context */
312 }
313 free (dp);
314 }
315 if (pushsw || popsw) {
316 cp = m_maildir(argfolder);
317 if (access (cp, F_OK) == NOTOK)
318 adios (cp, "unable to find folder");
319 context_replace (pfolder, argfolder); /* update current folder */
320 context_save (); /* save the context file */
321 argfolder = NULL;
322 }
323
324 /* Listing the folder stack */
325 if (listsw) {
326 printf ("%s", argfolder ? argfolder : getfolder (1));
327 if ((cp = context_find (stack))) {
328 dp = getcpy (cp);
329 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
330 printf (" %s", *ap);
331 free (dp);
332 }
333 printf ("\n");
334
335 if (!printsw)
336 done (0);
337 }
338
339 /* Allocate initial space to record folder information */
340 maxFolderInfo = CRAWL_NUMFOLDERS;
341 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
342
343 /*
344 * Scan the folders
345 */
346 if (all || ftotal > 0) {
347 /*
348 * If no folder is given, do them all
349 */
350 /* change directory to base of nmh directory for crawl_folders */
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)) ? cp : "", sizeof(folder));
358 crawl_folders (".", get_folder_info_callback, NULL);
359 } else {
360 strncpy (folder, argfolder, sizeof(folder));
361 if (get_folder_info (argfolder, msg)) {
362 context_replace (pfolder, argfolder);/* update current folder */
363 context_save (); /* save the context file */
364 }
365 /*
366 * Since recurse wasn't done in get_folder_info(),
367 * we still need to list all level-1 sub-folders.
368 */
369 if (!frecurse)
370 crawl_folders (folder, get_folder_info_callback, NULL);
371 }
372 } else {
373 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
374
375 /*
376 * Check if folder exists. If not, then see if
377 * we should create it, or just exit.
378 */
379 create_folder (m_maildir (folder), fcreat, done);
380
381 if (get_folder_info (folder, msg) && argfolder) {
382 /* update current folder */
383 context_replace (pfolder, argfolder);
384 }
385 }
386
387 /*
388 * Print out folder information
389 */
390 print_folders();
391
392 context_save (); /* save the context file */
393 done (0);
394 return 1;
395 }
396
397 static int
398 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
399 {
400 int i, retval = 1;
401 struct msgs *mp = NULL;
402
403 i = total_folders++;
404
405 /*
406 * if necessary, reallocate the space
407 * for folder information
408 */
409 if (total_folders >= maxFolderInfo) {
410 maxFolderInfo += CRAWL_NUMFOLDERS;
411 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
412 }
413
414 fi[i].name = fold;
415 fi[i].nummsg = 0;
416 fi[i].curmsg = 0;
417 fi[i].lowmsg = 0;
418 fi[i].hghmsg = 0;
419 fi[i].others = 0;
420 fi[i].error = 0;
421
422 if ((ftotal > 0) || !fshort || msg || fpack) {
423 /*
424 * create message structure and get folder info
425 */
426 if (!(mp = folder_read (fold))) {
427 admonish (NULL, "unable to read folder %s", fold);
428 return 0;
429 }
430
431 /* set the current message */
432 if (msg && !sfold (mp, msg))
433 retval = 0;
434
435 if (fpack) {
436 if (folder_pack (&mp, fverb) == -1)
437 done (1);
438 seq_save (mp); /* synchronize the sequences */
439 context_save (); /* save the context file */
440 }
441
442 /* record info for this folder */
443 if ((ftotal > 0) || !fshort) {
444 fi[i].nummsg = mp->nummsg;
445 fi[i].curmsg = mp->curmsg;
446 fi[i].lowmsg = mp->lowmsg;
447 fi[i].hghmsg = mp->hghmsg;
448 fi[i].others = other_files (mp);
449 }
450
451 folder_free (mp); /* free folder/message structure */
452 }
453
454 *crawl_children = (frecurse && (fshort || fi[i].others)
455 && (fi[i].error == 0));
456 return retval;
457 }
458
459 static boolean
460 get_folder_info_callback (char *fold, void *baton)
461 {
462 boolean crawl_children;
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 }