2 ** new.c -- as new, list all folders with unseen messages
3 ** -- as fnext, move to next folder with unseen messages
4 ** -- as fprev, move to previous folder with unseen messages
5 ** -- as unseen, scan all unseen messages
6 ** This code is Copyright (c) 2008, by the authors of nmh. See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
10 ** Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
14 #include <h/crawl_folders.h>
16 #include <sys/types.h>
24 static struct swit switches[] = {
36 char *version=VERSION;
38 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
41 ** check_folders uses this to maintain state with both .folders list of
42 ** folders and with crawl_folders.
45 struct node **first, **cur_node;
52 /* Return the number of messages in a string list of message numbers. */
54 count_messages(char *field)
60 field = mh_xstrdup(field);
62 /* copied from seq_read.c:seq_init */
63 for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
64 if ((cp = strchr(*ap, '-')))
66 if ((j = m_atoi(*ap)) > 0) {
67 k = cp ? m_atoi(cp) : j;
78 /* Return TRUE if the sequence 'name' is in 'sequences'. */
80 seq_in_list(char *name, char *sequences[])
84 for (i = 0; sequences[i] != NULL; i++) {
85 if (strcmp(name, sequences[i]) == 0) {
94 ** Return the string list of message numbers from the sequences file,
98 get_msgnums(char *folder, char *sequences[])
101 struct field f = {{0}};
102 char *seqfile = concat(toabsdir(folder), "/", mh_seq, (void *)NULL);
103 FILE *fp = fopen(seqfile, "r");
104 char *msgnums = NULL, *this_msgnums, *old_msgnums;
106 /* no sequences file -> no messages */
111 for (state = FLD2;;) {
112 switch (state = m_getfld2(state, &f, fp)) {
119 ** if it's in a sequence we want,
120 ** save the list of messages.
122 if (seq_in_list(f.name, sequences)) {
123 this_msgnums = trimcpy(f.value);
124 if (msgnums == NULL) {
125 msgnums = this_msgnums;
127 old_msgnums = msgnums;
128 msgnums = concat(old_msgnums, " ",
131 mh_free0(&old_msgnums);
132 mh_free0(&this_msgnums);
138 adios(EX_DATAERR, NULL, "no blank lines are permitted in %s", seqfile);
145 adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
156 ** Check `folder' (of length `len') for interesting messages,
157 ** filling in the list in `b'.
160 check_folder(char *folder, size_t len, struct list_state *b)
162 char *msgnums = get_msgnums(folder, b->sequences);
163 int is_cur = strcmp(folder, b->cur) == 0;
165 if (is_cur || msgnums != NULL) {
166 if (*b->first == NULL) {
167 *b->first = b->node = mh_xcalloc(1, sizeof(*b->node));
169 b->node->n_next = mh_xcalloc(1, sizeof(*b->node));
170 b->node = b->node->n_next;
172 b->node->n_name = folder;
173 b->node->n_field = msgnums;
175 if (*b->maxlen < len) {
180 /* Save the node for the current folder, so we can fall back to it. */
182 *b->cur_node = b->node;
187 crawl_callback(char *folder, void *baton)
189 check_folder(folder, strlen(folder), baton);
194 ** Scan folders, returning:
195 ** first -- list of nodes for all folders which have desired messages;
196 ** if the current folder is listed in .folders, it is also in
197 ** the list regardless of whether it has any desired messages
198 ** last -- last node in list
199 ** cur_node -- node of current folder, if listed in .folders
200 ** maxlen -- length of longest folder name
202 ** `cur' points to the name of the current folder, `folders' points to the
203 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
204 ** the array of sequences for which to look.
206 ** An empty list is returned as first=last=NULL.
209 check_folders(struct node **first, struct node **last,
210 struct node **cur_node, size_t *maxlen,
211 char *cur, char *folders, char *sequences[])
218 *first = *last = *cur_node = NULL;
222 b.cur_node = cur_node;
225 b.sequences = sequences;
227 if (folders == NULL) {
228 chdir(toabsdir("+"));
229 crawl_folders(".", crawl_callback, &b);
231 fp = fopen(folders, "r");
233 adios(EX_IOERR, NULL, "failed to read %s", folders);
235 while (vfgets(fp, &line) == OK) {
236 len = strlen(line) - 1;
238 check_folder(mh_xstrdup(line), len, &b);
243 if (*first != NULL) {
244 b.node->n_next = NULL;
249 /* Return a single string of the `sequences' joined by a space (' '). */
251 join_sequences(char *sequences[])
257 for (i = 0; sequences[i] != NULL; i++) {
258 len += strlen(sequences[i]) + 1;
260 result = mh_xcalloc(len + 1, sizeof(char));
262 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
263 len = strlen(sequences[i]);
264 memcpy(cp, sequences[i], len);
267 /* -1 to overwrite the last delimiter */
274 ** Return a struct node for the folder to change to. This is the next
275 ** (previous, if FPREV mode) folder with desired messages, or the current
276 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
277 ** output but don't change folders.
279 ** n_name is the folder to change to, and n_field is the string list of
280 ** desired message numbers.
283 doit(char *cur, char *folders, char *sequences[])
285 struct node *first, *cur_node, *node, *last = NULL, *prev;
287 int count, total = 0;
288 char *sequences_s = NULL;
294 if (cur == NULL || cur[0] == '\0') {
298 check_folders(&first, &last, &cur_node, &folder_len, cur,
301 if (run_mode == FNEXT || run_mode == FPREV) {
303 /* No folders at all... */
305 } else if (first->n_next == NULL) {
307 ** We have only one node; any desired messages in it?
309 if (first->n_field == NULL) {
314 } else if (cur_node == NULL) {
316 ** Current folder is not listed in .folders,
321 } else if (run_mode == UNSEEN) {
322 sequences_s = join_sequences(sequences);
325 for (node = first, prev = NULL;
327 prev = node, node = node->n_next) {
328 if (run_mode == FNEXT) {
330 ** If we have a previous node and it is the current
331 ** folder, return this node.
333 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
336 } else if (run_mode == FPREV) {
337 if (strcmp(node->n_name, cur) == 0) {
339 ** Found current folder in fprev mode;
340 ** if we have a previous node in the list,
341 ** return it; else return the last node.
348 } else if (run_mode == UNSEEN) {
349 if (node->n_field == NULL) {
353 printf("\n%d %s messages in %s",
354 count_messages(node->n_field),
357 if (strcmp(node->n_name, cur) == 0) {
358 puts(" (*: current folder)");
365 argv[argc++] = "scan";
366 snprintf(buf, sizeof buf, "+%s", node->n_name);
368 for (seqp=sequences; *seqp; seqp++) {
369 argv[argc++] = *seqp;
371 argv[argc] = (char *)NULL;
372 execprog(*argv, argv);
374 if (node->n_field == NULL) {
378 count = count_messages(node->n_field);
381 printf("%-*s %6d.%c %s\n", (int) folder_len,
383 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
389 ** If we're fnext, we haven't checked the last node yet. If it's the
390 ** current folder, return the first node.
392 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
396 if (run_mode == NEW) {
397 printf("%-*s %6d.\n", (int) folder_len, " total", total);
404 main(int argc, char **argv)
406 char **ap, *cp, **argp, **arguments;
408 char *folders = NULL;
409 char *sequences[NUMATTRS + 1];
417 setlocale(LC_ALL, "");
418 invo_name = mhbasename(argv[0]);
420 /* read user profile/context */
423 arguments = getarguments(invo_name, argc, argv, 1);
429 while ((cp = *argp++)) {
431 switch (smatch(++cp, switches)) {
433 ambigsw(cp, switches);
436 adios(EX_USAGE, NULL, "-%s unknown", cp);
439 snprintf(help, sizeof(help),
440 "%s [switches] [sequences]",
442 print_help(help, switches, 1);
443 exit(argc == 2 ? EX_OK : EX_USAGE);
445 print_version(invo_name);
446 exit(argc == 2 ? EX_OK : EX_USAGE);
449 if (!(folders = *argp++) || *folders == '-')
450 adios(EX_USAGE, NULL, "missing argument to %s",
454 if (!(invo_name = *argp++) || *invo_name == '-')
455 adios(EX_USAGE, NULL, "missing argument to %s",
457 invo_name = mhbasename(invo_name);
461 /* have a sequence argument */
462 if (!seq_in_list(cp, sequences)) {
468 if (strcmp(invo_name, "fnext") == 0) {
470 } else if (strcmp(invo_name, "fprev") == 0) {
472 } else if (strcmp(invo_name, "unseen") == 0) {
476 if (folders == NULL) {
479 if (folders[0] != '/') {
480 folders = toabsdir(folders);
486 /* no sequence arguments; use unseen */
487 if ((unseen = context_find(usequence))) {
489 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
492 unseen = seq_unseen; /* use default */
494 dp = mh_xstrdup(unseen);
495 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
496 sequences[i++] = *ap;
501 folder = doit(context_find(curfolder), folders, sequences);
502 if (folder == NULL) {
507 if (run_mode == UNSEEN) {
509 ** All the scan(1)s it runs change the current folder, so we
510 ** need to put it back. Unfortunately, context_replace lamely
511 ** ignores the new value you give it if it is the same one it
512 ** has in memory. So, we'll be lame, too. I'm not sure if i
513 ** should just change context_replace...
515 context_replace(curfolder, "defeat_context_replace_optimization");
518 /* update current folder */
519 context_replace(curfolder, folder->n_name);
521 if (run_mode == FNEXT || run_mode == FPREV) {
522 printf("%s %s\n", folder->n_name, folder->n_field);