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