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
13 #include <sys/types.h>
20 #include <h/crawl_folders.h>
23 static struct swit switches[] = {
35 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
38 ** check_folders uses this to maintain state with both .folders list of
39 ** folders and with crawl_folders.
42 struct node **first, **cur_node;
49 /* Return the number of messages in a string list of message numbers. */
51 count_messages(char *field)
57 field = getcpy(field);
59 /* copied from seq_read.c:seq_init */
60 for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
61 if ((cp = strchr(*ap, '-')))
63 if ((j = m_atoi(*ap)) > 0) {
64 k = cp ? m_atoi(cp) : j;
75 /* Return TRUE if the sequence 'name' is in 'sequences'. */
77 seq_in_list(char *name, char *sequences[])
81 for (i = 0; sequences[i] != NULL; i++) {
82 if (strcmp(name, sequences[i]) == 0) {
91 ** Return the string list of message numbers from the sequences file,
95 get_msgnums(char *folder, char *sequences[])
97 char *seqfile = concat(toabsdir(folder), "/", mh_seq, (void *)NULL);
98 FILE *fp = fopen(seqfile, "r");
100 char name[NAMESZ], field[BUFSIZ];
102 char *msgnums = NULL, *this_msgnums, *old_msgnums;
104 /* no sequences file -> no messages */
109 /* copied from seq_read.c:seq_public */
110 for (state = FLD;;) {
111 switch (state = m_getfld(state, name, field, sizeof(field),
116 if (state == FLDPLUS) {
118 while (state == FLDPLUS) {
119 state = m_getfld(state, name, field,
125 ** Here's where we differ from
126 ** seq_public: if it's in a
127 ** sequence we want, save the list
130 if (seq_in_list(name, sequences)) {
131 this_msgnums = trimcpy(cp);
132 if (msgnums == NULL) {
133 msgnums = this_msgnums;
135 old_msgnums = msgnums;
136 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
144 if (seq_in_list(name, sequences)) {
145 this_msgnums = trimcpy(field);
146 if (msgnums == NULL) {
147 msgnums = this_msgnums;
149 old_msgnums = msgnums;
150 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
163 adios(NULL, "no blank lines are permitted in %s",
171 adios(NULL, "%s is poorly formatted", seqfile);
173 break; /* break from for loop */
182 ** Check `folder' (of length `len') for interesting messages,
183 ** filling in the list in `b'.
186 check_folder(char *folder, size_t len, struct list_state *b)
188 char *msgnums = get_msgnums(folder, b->sequences);
189 int is_cur = strcmp(folder, b->cur) == 0;
191 if (is_cur || msgnums != NULL) {
192 if (*b->first == NULL) {
193 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
195 b->node->n_next = mh_xmalloc(sizeof(*b->node));
196 b->node = b->node->n_next;
198 b->node->n_name = folder;
199 b->node->n_field = msgnums;
201 if (*b->maxlen < len) {
206 /* Save the node for the current folder, so we can fall back to it. */
208 *b->cur_node = b->node;
213 crawl_callback(char *folder, void *baton)
215 check_folder(folder, strlen(folder), baton);
220 ** Scan folders, returning:
221 ** first -- list of nodes for all folders which have desired messages;
222 ** if the current folder is listed in .folders, it is also in
223 ** the list regardless of whether it has any desired messages
224 ** last -- last node in list
225 ** cur_node -- node of current folder, if listed in .folders
226 ** maxlen -- length of longest folder name
228 ** `cur' points to the name of the current folder, `folders' points to the
229 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
230 ** the array of sequences for which to look.
232 ** An empty list is returned as first=last=NULL.
235 check_folders(struct node **first, struct node **last,
236 struct node **cur_node, size_t *maxlen,
237 char *cur, char *folders, char *sequences[])
244 *first = *last = *cur_node = NULL;
248 b.cur_node = cur_node;
251 b.sequences = sequences;
253 if (folders == NULL) {
254 chdir(toabsdir("+"));
255 crawl_folders(".", crawl_callback, &b);
257 fp = fopen(folders, "r");
259 adios(NULL, "failed to read %s", folders);
261 while (vfgets(fp, &line) == OK) {
262 len = strlen(line) - 1;
264 check_folder(getcpy(line), len, &b);
269 if (*first != NULL) {
270 b.node->n_next = NULL;
275 /* Return a single string of the `sequences' joined by a space (' '). */
277 join_sequences(char *sequences[])
283 for (i = 0; sequences[i] != NULL; i++) {
284 len += strlen(sequences[i]) + 1;
286 result = mh_xmalloc(len + 1);
288 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
289 len = strlen(sequences[i]);
290 memcpy(cp, sequences[i], len);
293 /* -1 to overwrite the last delimiter */
300 ** Return a struct node for the folder to change to. This is the next
301 ** (previous, if FPREV mode) folder with desired messages, or the current
302 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
303 ** output but don't change folders.
305 ** n_name is the folder to change to, and n_field is the string list of
306 ** desired message numbers.
309 doit(char *cur, char *folders, char *sequences[])
311 struct node *first, *cur_node, *node, *last = NULL, *prev;
313 int count, total = 0;
314 char *sequences_s = NULL;
320 if (cur == NULL || cur[0] == '\0') {
324 check_folders(&first, &last, &cur_node, &folder_len, cur,
327 if (run_mode == FNEXT || run_mode == FPREV) {
329 /* No folders at all... */
331 } else if (first->n_next == NULL) {
333 ** We have only one node; any desired messages in it?
335 if (first->n_field == NULL) {
340 } else if (cur_node == NULL) {
342 ** Current folder is not listed in .folders,
347 } else if (run_mode == UNSEEN) {
348 sequences_s = join_sequences(sequences);
351 for (node = first, prev = NULL;
353 prev = node, node = node->n_next) {
354 if (run_mode == FNEXT) {
356 ** If we have a previous node and it is the current
357 ** folder, return this node.
359 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
362 } else if (run_mode == FPREV) {
363 if (strcmp(node->n_name, cur) == 0) {
365 ** Found current folder in fprev mode;
366 ** if we have a previous node in the list,
367 ** return it; else return the last node.
374 } else if (run_mode == UNSEEN) {
375 if (node->n_field == NULL) {
379 printf("\n%d %s messages in %s",
380 count_messages(node->n_field),
383 if (strcmp(node->n_name, cur) == 0) {
384 puts(" (*: current folder)");
391 argv[argc++] = "scan";
392 snprintf(buf, sizeof buf, "+%s", node->n_name);
394 for (seqp=sequences; *seqp; seqp++) {
395 argv[argc++] = *seqp;
397 argv[argc] = (char *)NULL;
398 execprog(*argv, argv);
400 if (node->n_field == NULL) {
404 count = count_messages(node->n_field);
407 printf("%-*s %6d.%c %s\n", (int) folder_len,
409 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
415 ** If we're fnext, we haven't checked the last node yet. If it's the
416 ** current folder, return the first node.
418 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
422 if (run_mode == NEW) {
423 printf("%-*s %6d.\n", (int) folder_len, " total", total);
430 main(int argc, char **argv)
432 char **ap, *cp, **argp, **arguments;
434 char *folders = NULL;
435 char *sequences[NUMATTRS + 1];
443 setlocale(LC_ALL, "");
444 invo_name = mhbasename(argv[0]);
446 /* read user profile/context */
449 arguments = getarguments(invo_name, argc, argv, 1);
455 while ((cp = *argp++)) {
457 switch (smatch(++cp, switches)) {
459 ambigsw(cp, switches);
462 adios(NULL, "-%s unknown", cp);
465 snprintf(help, sizeof(help),
466 "%s [switches] [sequences]",
468 print_help(help, switches, 1);
471 print_version(invo_name);
475 if (!(folders = *argp++) || *folders == '-')
476 adios(NULL, "missing argument to %s",
480 if (!(invo_name = *argp++) || *invo_name == '-')
481 adios(NULL, "missing argument to %s",
483 invo_name = mhbasename(invo_name);
487 /* have a sequence argument */
488 if (!seq_in_list(cp, sequences)) {
494 if (strcmp(invo_name, "fnext") == 0) {
496 } else if (strcmp(invo_name, "fprev") == 0) {
498 } else if (strcmp(invo_name, "unseen") == 0) {
502 if (folders == NULL) {
505 if (folders[0] != '/') {
506 folders = toabsdir(folders);
512 /* no sequence arguments; use unseen */
513 if ((unseen = context_find(usequence))) {
515 adios(NULL, "profile entry %s set, but empty, and no sequences given", usequence);
518 unseen = seq_unseen; /* use default */
521 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
522 sequences[i++] = *ap;
527 folder = doit(context_find(curfolder), folders, sequences);
528 if (folder == NULL) {
533 if (run_mode == UNSEEN) {
535 ** All the scan(1)s it runs change the current folder, so we
536 ** need to put it back. Unfortunately, context_replace lamely
537 ** ignores the new value you give it if it is the same one it
538 ** has in memory. So, we'll be lame, too. I'm not sure if i
539 ** should just change context_replace...
541 context_replace(curfolder, "defeat_context_replace_optimization");
544 /* update current folder */
545 context_replace(curfolder, folder->n_name);
547 if (run_mode == FNEXT || run_mode == FPREV) {
548 printf("%s %s\n", folder->n_name, folder->n_field);