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 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
39 ** check_folders uses this to maintain state with both .folders list of
40 ** folders and with crawl_folders.
43 struct node **first, **cur_node;
50 /* Return the number of messages in a string list of message numbers. */
52 count_messages(char *field)
58 field = getcpy(field);
60 /* copied from seq_read.c:seq_init */
61 for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
62 if ((cp = strchr(*ap, '-')))
64 if ((j = m_atoi(*ap)) > 0) {
65 k = cp ? m_atoi(cp) : j;
76 /* Return TRUE if the sequence 'name' is in 'sequences'. */
78 seq_in_list(char *name, char *sequences[])
82 for (i = 0; sequences[i] != NULL; i++) {
83 if (strcmp(name, sequences[i]) == 0) {
92 ** Return the string list of message numbers from the sequences file,
96 get_msgnums(char *folder, char *sequences[])
99 struct field f = free_field;
100 char *seqfile = concat(toabsdir(folder), "/", mh_seq, (void *)NULL);
101 FILE *fp = fopen(seqfile, "r");
102 char *msgnums = NULL, *this_msgnums, *old_msgnums;
104 /* no sequences file -> no messages */
109 for (state = FLD2;;) {
110 switch (state = m_getfld2(state, &f, fp)) {
113 ** if it's in a sequence we want,
114 ** save the list of messages.
116 if (seq_in_list(f.name, sequences)) {
117 this_msgnums = trimcpy(f.value);
118 if (msgnums == NULL) {
119 msgnums = this_msgnums;
121 old_msgnums = msgnums;
122 msgnums = concat(old_msgnums, " ",
132 adios(EX_DATAERR, NULL, "no blank lines are permitted in %s", seqfile);
139 adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
150 ** Check `folder' (of length `len') for interesting messages,
151 ** filling in the list in `b'.
154 check_folder(char *folder, size_t len, struct list_state *b)
156 char *msgnums = get_msgnums(folder, b->sequences);
157 int is_cur = strcmp(folder, b->cur) == 0;
159 if (is_cur || msgnums != NULL) {
160 if (*b->first == NULL) {
161 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
163 b->node->n_next = mh_xmalloc(sizeof(*b->node));
164 b->node = b->node->n_next;
166 b->node->n_name = folder;
167 b->node->n_field = msgnums;
169 if (*b->maxlen < len) {
174 /* Save the node for the current folder, so we can fall back to it. */
176 *b->cur_node = b->node;
181 crawl_callback(char *folder, void *baton)
183 check_folder(folder, strlen(folder), baton);
188 ** Scan folders, returning:
189 ** first -- list of nodes for all folders which have desired messages;
190 ** if the current folder is listed in .folders, it is also in
191 ** the list regardless of whether it has any desired messages
192 ** last -- last node in list
193 ** cur_node -- node of current folder, if listed in .folders
194 ** maxlen -- length of longest folder name
196 ** `cur' points to the name of the current folder, `folders' points to the
197 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
198 ** the array of sequences for which to look.
200 ** An empty list is returned as first=last=NULL.
203 check_folders(struct node **first, struct node **last,
204 struct node **cur_node, size_t *maxlen,
205 char *cur, char *folders, char *sequences[])
212 *first = *last = *cur_node = NULL;
216 b.cur_node = cur_node;
219 b.sequences = sequences;
221 if (folders == NULL) {
222 chdir(toabsdir("+"));
223 crawl_folders(".", crawl_callback, &b);
225 fp = fopen(folders, "r");
227 adios(EX_IOERR, NULL, "failed to read %s", folders);
229 while (vfgets(fp, &line) == OK) {
230 len = strlen(line) - 1;
232 check_folder(getcpy(line), len, &b);
237 if (*first != NULL) {
238 b.node->n_next = NULL;
243 /* Return a single string of the `sequences' joined by a space (' '). */
245 join_sequences(char *sequences[])
251 for (i = 0; sequences[i] != NULL; i++) {
252 len += strlen(sequences[i]) + 1;
254 result = mh_xmalloc(len + 1);
256 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
257 len = strlen(sequences[i]);
258 memcpy(cp, sequences[i], len);
261 /* -1 to overwrite the last delimiter */
268 ** Return a struct node for the folder to change to. This is the next
269 ** (previous, if FPREV mode) folder with desired messages, or the current
270 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
271 ** output but don't change folders.
273 ** n_name is the folder to change to, and n_field is the string list of
274 ** desired message numbers.
277 doit(char *cur, char *folders, char *sequences[])
279 struct node *first, *cur_node, *node, *last = NULL, *prev;
281 int count, total = 0;
282 char *sequences_s = NULL;
288 if (cur == NULL || cur[0] == '\0') {
292 check_folders(&first, &last, &cur_node, &folder_len, cur,
295 if (run_mode == FNEXT || run_mode == FPREV) {
297 /* No folders at all... */
299 } else if (first->n_next == NULL) {
301 ** We have only one node; any desired messages in it?
303 if (first->n_field == NULL) {
308 } else if (cur_node == NULL) {
310 ** Current folder is not listed in .folders,
315 } else if (run_mode == UNSEEN) {
316 sequences_s = join_sequences(sequences);
319 for (node = first, prev = NULL;
321 prev = node, node = node->n_next) {
322 if (run_mode == FNEXT) {
324 ** If we have a previous node and it is the current
325 ** folder, return this node.
327 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
330 } else if (run_mode == FPREV) {
331 if (strcmp(node->n_name, cur) == 0) {
333 ** Found current folder in fprev mode;
334 ** if we have a previous node in the list,
335 ** return it; else return the last node.
342 } else if (run_mode == UNSEEN) {
343 if (node->n_field == NULL) {
347 printf("\n%d %s messages in %s",
348 count_messages(node->n_field),
351 if (strcmp(node->n_name, cur) == 0) {
352 puts(" (*: current folder)");
359 argv[argc++] = "scan";
360 snprintf(buf, sizeof buf, "+%s", node->n_name);
362 for (seqp=sequences; *seqp; seqp++) {
363 argv[argc++] = *seqp;
365 argv[argc] = (char *)NULL;
366 execprog(*argv, argv);
368 if (node->n_field == NULL) {
372 count = count_messages(node->n_field);
375 printf("%-*s %6d.%c %s\n", (int) folder_len,
377 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
383 ** If we're fnext, we haven't checked the last node yet. If it's the
384 ** current folder, return the first node.
386 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
390 if (run_mode == NEW) {
391 printf("%-*s %6d.\n", (int) folder_len, " total", total);
398 main(int argc, char **argv)
400 char **ap, *cp, **argp, **arguments;
402 char *folders = NULL;
403 char *sequences[NUMATTRS + 1];
411 setlocale(LC_ALL, "");
412 invo_name = mhbasename(argv[0]);
414 /* read user profile/context */
417 arguments = getarguments(invo_name, argc, argv, 1);
423 while ((cp = *argp++)) {
425 switch (smatch(++cp, switches)) {
427 ambigsw(cp, switches);
430 adios(EX_USAGE, NULL, "-%s unknown", cp);
433 snprintf(help, sizeof(help),
434 "%s [switches] [sequences]",
436 print_help(help, switches, 1);
437 exit(argc == 2 ? EX_OK : EX_USAGE);
439 print_version(invo_name);
440 exit(argc == 2 ? EX_OK : EX_USAGE);
443 if (!(folders = *argp++) || *folders == '-')
444 adios(EX_USAGE, NULL, "missing argument to %s",
448 if (!(invo_name = *argp++) || *invo_name == '-')
449 adios(EX_USAGE, NULL, "missing argument to %s",
451 invo_name = mhbasename(invo_name);
455 /* have a sequence argument */
456 if (!seq_in_list(cp, sequences)) {
462 if (strcmp(invo_name, "fnext") == 0) {
464 } else if (strcmp(invo_name, "fprev") == 0) {
466 } else if (strcmp(invo_name, "unseen") == 0) {
470 if (folders == NULL) {
473 if (folders[0] != '/') {
474 folders = toabsdir(folders);
480 /* no sequence arguments; use unseen */
481 if ((unseen = context_find(usequence))) {
483 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
486 unseen = seq_unseen; /* use default */
489 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
490 sequences[i++] = *ap;
495 folder = doit(context_find(curfolder), folders, sequences);
496 if (folder == NULL) {
501 if (run_mode == UNSEEN) {
503 ** All the scan(1)s it runs change the current folder, so we
504 ** need to put it back. Unfortunately, context_replace lamely
505 ** ignores the new value you give it if it is the same one it
506 ** has in memory. So, we'll be lame, too. I'm not sure if i
507 ** should just change context_replace...
509 context_replace(curfolder, "defeat_context_replace_optimization");
512 /* update current folder */
513 context_replace(curfolder, folder->n_name);
515 if (run_mode == FNEXT || run_mode == FPREV) {
516 printf("%s %s\n", folder->n_name, folder->n_field);