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 (strlen(this_msgnums) == 0) {
127 } else if (msgnums == NULL) {
128 msgnums = this_msgnums;
130 old_msgnums = msgnums;
131 msgnums = concat(old_msgnums, " ",
134 mh_free0(&old_msgnums);
135 mh_free0(&this_msgnums);
141 adios(EX_DATAERR, NULL, "no blank lines are permitted in %s", seqfile);
148 adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
159 ** Check `folder' (of length `len') for interesting messages,
160 ** filling in the list in `b'.
163 check_folder(char *folder, size_t len, struct list_state *b)
165 char *msgnums = get_msgnums(folder, b->sequences);
166 int is_cur = strcmp(folder, b->cur) == 0;
168 if (is_cur || msgnums != NULL) {
169 if (*b->first == NULL) {
170 *b->first = b->node = mh_xcalloc(1, sizeof(*b->node));
172 b->node->n_next = mh_xcalloc(1, sizeof(*b->node));
173 b->node = b->node->n_next;
175 b->node->n_name = folder;
176 b->node->n_field = msgnums;
178 if (*b->maxlen < len) {
183 /* Save the node for the current folder, so we can fall back to it. */
185 *b->cur_node = b->node;
190 crawl_callback(char *folder, void *baton)
192 check_folder(folder, strlen(folder), baton);
197 ** Scan folders, returning:
198 ** first -- list of nodes for all folders which have desired messages;
199 ** if the current folder is listed in .folders, it is also in
200 ** the list regardless of whether it has any desired messages
201 ** last -- last node in list
202 ** cur_node -- node of current folder, if listed in .folders
203 ** maxlen -- length of longest folder name
205 ** `cur' points to the name of the current folder, `folders' points to the
206 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
207 ** the array of sequences for which to look.
209 ** An empty list is returned as first=last=NULL.
212 check_folders(struct node **first, struct node **last,
213 struct node **cur_node, size_t *maxlen,
214 char *cur, char *folders, char *sequences[])
221 *first = *last = *cur_node = NULL;
225 b.cur_node = cur_node;
228 b.sequences = sequences;
230 if (folders == NULL) {
231 chdir(toabsdir("+"));
232 crawl_folders(".", crawl_callback, &b);
234 fp = fopen(folders, "r");
236 adios(EX_IOERR, NULL, "failed to read %s", folders);
238 while (vfgets(fp, &line) == OK) {
239 len = strlen(line) - 1;
241 check_folder(mh_xstrdup(line), len, &b);
246 if (*first != NULL) {
247 b.node->n_next = NULL;
252 /* Return a single string of the `sequences' joined by a space (' '). */
254 join_sequences(char *sequences[])
260 for (i = 0; sequences[i] != NULL; i++) {
261 len += strlen(sequences[i]) + 1;
263 result = mh_xcalloc(len + 1, sizeof(char));
265 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
266 len = strlen(sequences[i]);
267 memcpy(cp, sequences[i], len);
270 /* -1 to overwrite the last delimiter */
277 ** Return a struct node for the folder to change to. This is the next
278 ** (previous, if FPREV mode) folder with desired messages, or the current
279 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
280 ** output but don't change folders.
282 ** n_name is the folder to change to, and n_field is the string list of
283 ** desired message numbers.
286 doit(char *cur, char *folders, char *sequences[])
288 struct node *first, *cur_node, *node, *last = NULL, *prev;
290 int count, total = 0;
291 char *sequences_s = NULL;
297 if (cur == NULL || cur[0] == '\0') {
301 check_folders(&first, &last, &cur_node, &folder_len, cur,
304 if (run_mode == FNEXT || run_mode == FPREV) {
306 /* No folders at all... */
308 } else if (first->n_next == NULL) {
310 ** We have only one node; any desired messages in it?
312 if (first->n_field == NULL) {
317 } else if (cur_node == NULL) {
319 ** Current folder is not listed in .folders,
324 } else if (run_mode == UNSEEN) {
325 sequences_s = join_sequences(sequences);
328 for (node = first, prev = NULL;
330 prev = node, node = node->n_next) {
331 if (run_mode == FNEXT) {
333 ** If we have a previous node and it is the current
334 ** folder, return this node.
336 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
339 } else if (run_mode == FPREV) {
340 if (strcmp(node->n_name, cur) == 0) {
342 ** Found current folder in fprev mode;
343 ** if we have a previous node in the list,
344 ** return it; else return the last node.
351 } else if (run_mode == UNSEEN) {
352 if (node->n_field == NULL) {
356 printf("\n%d %s messages in %s",
357 count_messages(node->n_field),
360 if (strcmp(node->n_name, cur) == 0) {
361 puts(" (*: current folder)");
368 argv[argc++] = "scan";
369 snprintf(buf, sizeof buf, "+%s", node->n_name);
371 for (seqp=sequences; *seqp; seqp++) {
372 argv[argc++] = *seqp;
374 argv[argc] = (char *)NULL;
375 execprog(*argv, argv);
377 if (node->n_field == NULL) {
381 count = count_messages(node->n_field);
384 printf("%-*s %6d.%c %s\n", (int) folder_len,
386 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
392 ** If we're fnext, we haven't checked the last node yet. If it's the
393 ** current folder, return the first node.
395 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
399 if (run_mode == NEW) {
400 printf("%-*s %6d.\n", (int) folder_len, " total", total);
407 main(int argc, char **argv)
409 char **ap, *cp, **argp, **arguments;
411 char *folders = NULL;
412 char *sequences[NUMATTRS + 1];
420 setlocale(LC_ALL, "");
421 invo_name = mhbasename(argv[0]);
423 /* read user profile/context */
426 arguments = getarguments(invo_name, argc, argv, 1);
432 while ((cp = *argp++)) {
434 switch (smatch(++cp, switches)) {
436 ambigsw(cp, switches);
439 adios(EX_USAGE, NULL, "-%s unknown", cp);
442 snprintf(help, sizeof(help),
443 "%s [switches] [sequences]",
445 print_help(help, switches, 1);
446 exit(argc == 2 ? EX_OK : EX_USAGE);
448 print_version(invo_name);
449 exit(argc == 2 ? EX_OK : EX_USAGE);
452 if (!(folders = *argp++) || *folders == '-')
453 adios(EX_USAGE, NULL, "missing argument to %s",
457 if (!(invo_name = *argp++) || *invo_name == '-')
458 adios(EX_USAGE, NULL, "missing argument to %s",
460 invo_name = mhbasename(invo_name);
464 /* have a sequence argument */
465 if (!seq_in_list(cp, sequences)) {
471 if (strcmp(invo_name, "fnext") == 0) {
473 } else if (strcmp(invo_name, "fprev") == 0) {
475 } else if (strcmp(invo_name, "unseen") == 0) {
479 if (folders == NULL) {
482 if (folders[0] != '/') {
483 folders = toabsdir(folders);
489 /* no sequence arguments; use unseen */
490 if ((unseen = context_find(usequence))) {
492 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
495 unseen = seq_unseen; /* use default */
497 dp = mh_xstrdup(unseen);
498 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
499 sequences[i++] = *ap;
504 folder = doit(context_find(curfolder), folders, sequences);
505 if (folder == NULL) {
510 if (run_mode == UNSEEN) {
512 ** All the scan(1)s it runs change the current folder, so we
513 ** need to put it back. Unfortunately, context_replace lamely
514 ** ignores the new value you give it if it is the same one it
515 ** has in memory. So, we'll be lame, too. I'm not sure if i
516 ** should just change context_replace...
518 context_replace(curfolder, "defeat_context_replace_optimization");
521 /* update current folder */
522 context_replace(curfolder, folder->n_name);
524 if (run_mode == FNEXT || run_mode == FPREV) {
525 printf("%s %s\n", folder->n_name, folder->n_field);