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(m_maildir(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), fp)) {
115 if (state == FLDPLUS) {
117 while (state == FLDPLUS) {
118 state = m_getfld (state, name,
122 cp = add (field, cp);
126 ** Here's where we differ from
127 ** seq_public: if it's in a
128 ** sequence we want, save the list
131 if (seq_in_list(name, sequences)) {
132 this_msgnums = trimcpy(cp);
133 if (msgnums == NULL) {
134 msgnums = this_msgnums;
136 old_msgnums = msgnums;
137 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
145 if (seq_in_list(name, sequences)) {
146 this_msgnums = trimcpy(field);
147 if (msgnums == NULL) {
148 msgnums = this_msgnums;
150 old_msgnums = msgnums;
151 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
164 adios (NULL, "no blank lines are permitted in %s", seqfile);
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(m_maildir(""));
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, *prev;
313 int count, total = 0;
314 char *command = NULL, *sequences_s = NULL;
316 if (cur == NULL || cur[0] == '\0') {
320 check_folders(&first, &last, &cur_node, &folder_len, cur,
323 if (run_mode == FNEXT || run_mode == FPREV) {
325 /* No folders at all... */
327 } else if (first->n_next == NULL) {
329 ** We have only one node; any desired messages in it?
331 if (first->n_field == NULL) {
336 } else if (cur_node == NULL) {
338 ** Current folder is not listed in .folders,
343 } else if (run_mode == UNSEEN) {
344 sequences_s = join_sequences(sequences);
347 for (node = first, prev = NULL;
349 prev = node, node = node->n_next) {
350 if (run_mode == FNEXT) {
352 ** If we have a previous node and it is the current
353 ** folder, return this node.
355 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
358 } else if (run_mode == FPREV) {
359 if (strcmp(node->n_name, cur) == 0) {
361 ** Found current folder in fprev mode;
362 ** if we have a previous node in the list,
363 ** return it; else return the last node.
370 } else if (run_mode == UNSEEN) {
371 if (node->n_field == NULL) {
375 printf("\n%d %s messages in %s",
376 count_messages(node->n_field),
379 if (strcmp(node->n_name, cur) == 0) {
380 puts(" (*: current folder)");
387 ** TODO: Split enough of scan.c out so that we can
390 command = concat("scan +", node->n_name, " ",
391 sequences_s, (void *)NULL);
395 if (node->n_field == NULL) {
399 count = count_messages(node->n_field);
402 printf("%-*s %6d.%c %s\n",
403 (int) folder_len, node->n_name,
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 = r1bindex(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 = r1bindex(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 = m_maildir(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(pfolder), 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(pfolder, "defeat_context_replace_optimization");
533 /* update current folder */
534 context_replace(pfolder, folder->n_name);
536 if (run_mode == FNEXT || run_mode == FPREV) {
537 printf("%s %s\n", folder->n_name, folder->n_field);