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,
127 ** Here's where we differ from
128 ** seq_public: if it's in a
129 ** sequence we want, save the list
132 if (seq_in_list(name, sequences)) {
133 this_msgnums = trimcpy(cp);
134 if (msgnums == NULL) {
135 msgnums = this_msgnums;
137 old_msgnums = msgnums;
138 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
146 if (seq_in_list(name, sequences)) {
147 this_msgnums = trimcpy(field);
148 if (msgnums == NULL) {
149 msgnums = this_msgnums;
151 old_msgnums = msgnums;
152 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
165 adios(NULL, "no blank lines are permitted in %s", seqfile);
172 adios(NULL, "%s is poorly formatted", seqfile);
174 break; /* break from for loop */
183 ** Check `folder' (of length `len') for interesting messages,
184 ** filling in the list in `b'.
187 check_folder(char *folder, size_t len, struct list_state *b)
189 char *msgnums = get_msgnums(folder, b->sequences);
190 int is_cur = strcmp(folder, b->cur) == 0;
192 if (is_cur || msgnums != NULL) {
193 if (*b->first == NULL) {
194 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
196 b->node->n_next = mh_xmalloc(sizeof(*b->node));
197 b->node = b->node->n_next;
199 b->node->n_name = folder;
200 b->node->n_field = msgnums;
202 if (*b->maxlen < len) {
207 /* Save the node for the current folder, so we can fall back to it. */
209 *b->cur_node = b->node;
214 crawl_callback(char *folder, void *baton)
216 check_folder(folder, strlen(folder), baton);
221 ** Scan folders, returning:
222 ** first -- list of nodes for all folders which have desired messages;
223 ** if the current folder is listed in .folders, it is also in
224 ** the list regardless of whether it has any desired messages
225 ** last -- last node in list
226 ** cur_node -- node of current folder, if listed in .folders
227 ** maxlen -- length of longest folder name
229 ** `cur' points to the name of the current folder, `folders' points to the
230 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
231 ** the array of sequences for which to look.
233 ** An empty list is returned as first=last=NULL.
236 check_folders(struct node **first, struct node **last,
237 struct node **cur_node, size_t *maxlen,
238 char *cur, char *folders, char *sequences[])
245 *first = *last = *cur_node = NULL;
249 b.cur_node = cur_node;
252 b.sequences = sequences;
254 if (folders == NULL) {
255 chdir(toabsdir("+"));
256 crawl_folders(".", crawl_callback, &b);
258 fp = fopen(folders, "r");
260 adios(NULL, "failed to read %s", folders);
262 while (vfgets(fp, &line) == OK) {
263 len = strlen(line) - 1;
265 check_folder(getcpy(line), len, &b);
270 if (*first != NULL) {
271 b.node->n_next = NULL;
276 /* Return a single string of the `sequences' joined by a space (' '). */
278 join_sequences(char *sequences[])
284 for (i = 0; sequences[i] != NULL; i++) {
285 len += strlen(sequences[i]) + 1;
287 result = mh_xmalloc(len + 1);
289 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
290 len = strlen(sequences[i]);
291 memcpy(cp, sequences[i], len);
294 /* -1 to overwrite the last delimiter */
301 ** Return a struct node for the folder to change to. This is the next
302 ** (previous, if FPREV mode) folder with desired messages, or the current
303 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
304 ** output but don't change folders.
306 ** n_name is the folder to change to, and n_field is the string list of
307 ** desired message numbers.
310 doit(char *cur, char *folders, char *sequences[])
312 struct node *first, *cur_node, *node, *last, *prev;
314 int count, total = 0;
315 char *command = NULL, *sequences_s = NULL;
317 if (cur == NULL || cur[0] == '\0') {
321 check_folders(&first, &last, &cur_node, &folder_len, cur,
324 if (run_mode == FNEXT || run_mode == FPREV) {
326 /* No folders at all... */
328 } else if (first->n_next == NULL) {
330 ** We have only one node; any desired messages in it?
332 if (first->n_field == NULL) {
337 } else if (cur_node == NULL) {
339 ** Current folder is not listed in .folders,
344 } else if (run_mode == UNSEEN) {
345 sequences_s = join_sequences(sequences);
348 for (node = first, prev = NULL;
350 prev = node, node = node->n_next) {
351 if (run_mode == FNEXT) {
353 ** If we have a previous node and it is the current
354 ** folder, return this node.
356 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
359 } else if (run_mode == FPREV) {
360 if (strcmp(node->n_name, cur) == 0) {
362 ** Found current folder in fprev mode;
363 ** if we have a previous node in the list,
364 ** return it; else return the last node.
371 } else if (run_mode == UNSEEN) {
372 if (node->n_field == NULL) {
376 printf("\n%d %s messages in %s",
377 count_messages(node->n_field),
380 if (strcmp(node->n_name, cur) == 0) {
381 puts(" (*: current folder)");
388 ** TODO: Split enough of scan.c out so that we can
391 command = concat("scan +", node->n_name, " ",
392 sequences_s, (void *)NULL);
396 if (node->n_field == NULL) {
400 count = count_messages(node->n_field);
403 printf("%-*s %6d.%c %s\n", (int) folder_len,
405 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
411 ** If we're fnext, we haven't checked the last node yet. If it's the
412 ** current folder, return the first node.
414 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
418 if (run_mode == NEW) {
419 printf("%-*s %6d.\n", (int) folder_len, " total", total);
426 main(int argc, char **argv)
428 char **ap, *cp, **argp, **arguments;
430 char *folders = NULL;
431 char *sequences[NUMATTRS + 1];
437 setlocale(LC_ALL, "");
439 invo_name = mhbasename(argv[0]);
441 /* read user profile/context */
444 arguments = getarguments(invo_name, argc, argv, 1);
450 while ((cp = *argp++)) {
452 switch (smatch(++cp, switches)) {
454 ambigsw(cp, switches);
457 adios(NULL, "-%s unknown", cp);
460 snprintf(help, sizeof(help),
461 "%s [switches] [sequences]",
463 print_help(help, switches, 1);
466 print_version(invo_name);
470 if (!(folders = *argp++) || *folders == '-')
471 adios(NULL, "missing argument to %s",
475 if (!(invo_name = *argp++) || *invo_name == '-')
476 adios(NULL, "missing argument to %s",
478 invo_name = mhbasename(invo_name);
482 /* have a sequence argument */
483 if (!seq_in_list(cp, sequences)) {
488 if (strcmp(invo_name, "fnext") == 0) {
490 } else if (strcmp(invo_name, "fprev") == 0) {
492 } else if (strcmp(invo_name, "unseen") == 0) {
496 if (folders == NULL) {
499 if (folders[0] != '/') {
500 folders = toabsdir(folders);
505 /* no sequence arguments; use unseen */
506 unseen = context_find(usequence);
507 if (unseen == NULL || unseen[0] == '\0') {
508 adios(NULL, "must specify sequences or set %s", usequence);
510 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
511 sequences[i++] = *ap;
516 folder = doit(context_find(curfolder), folders, sequences);
517 if (folder == NULL) {
522 if (run_mode == UNSEEN) {
524 ** All the scan(1)s it runs change the current folder, so we
525 ** need to put it back. Unfortunately, context_replace lamely
526 ** ignores the new value you give it if it is the same one it
527 ** has in memory. So, we'll be lame, too. I'm not sure if i
528 ** should just change context_replace...
530 context_replace(curfolder, "defeat_context_replace_optimization");
533 /* update current folder */
534 context_replace(curfolder, folder->n_name);
536 if (run_mode == FNEXT || run_mode == FPREV) {
537 printf("%s %s\n", folder->n_name, folder->n_field);