3 * new.c -- as new, list all folders with unseen messages
4 * -- as fnext, move to next folder with unseen messages
5 * -- as fprev, move to previous folder with unseen messages
6 * -- as unseen, scan all unseen messages
9 * This code is Copyright (c) 2008, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
13 * Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
16 #include <sys/types.h>
23 #include <h/crawl_folders.h>
26 static struct swit switches[] = {
38 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
40 /* check_folders uses this to maintain state with both .folders list of
41 * 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) {
91 /* Return the string list of message numbers from the sequences file, or NULL
94 get_msgnums(char *folder, char *sequences[])
96 char *seqfile = concat(m_maildir(folder), "/", mh_seq, (void *)NULL);
97 FILE *fp = fopen(seqfile, "r");
99 char name[NAMESZ], field[BUFSIZ];
101 char *msgnums = NULL, *this_msgnums, *old_msgnums;
103 /* no sequences file -> no messages */
108 /* copied from seq_read.c:seq_public */
109 for (state = FLD;;) {
110 switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
114 if (state == FLDPLUS) {
116 while (state == FLDPLUS) {
117 state = m_getfld (state, name, field,
119 cp = add (field, cp);
122 /* Here's where we differ from seq_public: if it's in a
123 * sequence we want, save the list of messages. */
124 if (seq_in_list(name, sequences)) {
125 this_msgnums = trimcpy(cp);
126 if (msgnums == NULL) {
127 msgnums = this_msgnums;
129 old_msgnums = msgnums;
130 msgnums = concat(old_msgnums, " ",
131 this_msgnums, (void *)NULL);
139 if (seq_in_list(name, sequences)) {
140 this_msgnums = trimcpy(field);
141 if (msgnums == NULL) {
142 msgnums = this_msgnums;
144 old_msgnums = msgnums;
145 msgnums = concat(old_msgnums, " ",
146 this_msgnums, (void *)NULL);
159 adios (NULL, "no blank lines are permitted in %s", seqfile);
166 adios (NULL, "%s is poorly formatted", seqfile);
168 break; /* break from for loop */
176 /* Check `folder' (of length `len') for interesting messages, filling in the
179 check_folder(char *folder, size_t len, struct list_state *b)
181 char *msgnums = get_msgnums(folder, b->sequences);
182 int is_cur = strcmp(folder, b->cur) == 0;
184 if (is_cur || msgnums != NULL) {
185 if (*b->first == NULL) {
186 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
188 b->node->n_next = mh_xmalloc(sizeof(*b->node));
189 b->node = b->node->n_next;
191 b->node->n_name = folder;
192 b->node->n_field = msgnums;
194 if (*b->maxlen < len) {
199 /* Save the node for the current folder, so we can fall back to it. */
201 *b->cur_node = b->node;
206 crawl_callback(char *folder, void *baton)
208 check_folder(folder, strlen(folder), baton);
212 /* Scan folders, returning:
213 * first -- list of nodes for all folders which have desired messages;
214 * if the current folder is listed in .folders, it is also in
215 * the list regardless of whether it has any desired messages
216 * last -- last node in list
217 * cur_node -- node of current folder, if listed in .folders
218 * maxlen -- length of longest folder name
220 * `cur' points to the name of the current folder, `folders' points to the
221 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
222 * the array of sequences for which to look.
225 check_folders(struct node **first, struct node **last,
226 struct node **cur_node, size_t *maxlen,
227 char *cur, char *folders, char *sequences[])
234 *first = *cur_node = NULL;
238 b.cur_node = cur_node;
241 b.sequences = sequences;
243 if (folders == NULL) {
244 chdir(m_maildir(""));
245 crawl_folders(".", crawl_callback, &b);
247 fp = fopen(folders, "r");
249 adios(NULL, "failed to read %s", folders);
251 while (vfgets(fp, &line) == OK) {
252 len = strlen(line) - 1;
254 check_folder(getcpy(line), len, &b);
259 if (*first != NULL) {
260 b.node->n_next = NULL;
265 /* Return a single string of the `sequences' joined by a space (' '). */
267 join_sequences(char *sequences[])
273 for (i = 0; sequences[i] != NULL; i++) {
274 len += strlen(sequences[i]) + 1;
276 result = mh_xmalloc(len + 1);
278 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
279 len = strlen(sequences[i]);
280 memcpy(cp, sequences[i], len);
283 /* -1 to overwrite the last delimiter */
289 /* Return a struct node for the folder to change to. This is the next
290 * (previous, if FPREV mode) folder with desired messages, or the current
291 * folder if no folders have desired. If NEW or UNSEEN mode, print the
292 * output but don't change folders.
294 * n_name is the folder to change to, and n_field is the string list of
295 * desired message numbers.
298 doit(char *cur, char *folders, char *sequences[])
300 struct node *first, *cur_node, *node, *last, *prev;
302 int count, total = 0;
303 char *command = NULL, *sequences_s = NULL;
305 if (cur == NULL || cur[0] == '\0') {
309 check_folders(&first, &last, &cur_node, &folder_len, cur,
312 if (run_mode == FNEXT || run_mode == FPREV) {
313 if (first->n_next == NULL) {
314 /* We have only one node; any desired messages in it? */
315 if (first->n_field == NULL) {
320 } else if (cur_node == NULL) {
321 /* Current folder is not listed in .folders, return first. */
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) {
332 /* If we have a previous node and it is the current
333 * folder, return this node. */
334 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
337 } else if (run_mode == FPREV) {
338 if (strcmp(node->n_name, cur) == 0) {
339 /* Found current folder in fprev mode; if we have a
340 * previous node in the list, return it; else return
347 } else if (run_mode == UNSEEN) {
348 if (node->n_field == NULL) {
352 printf("\n%d %s messages in %s",
353 count_messages(node->n_field),
356 if (strcmp(node->n_name, cur) == 0) {
357 puts(" (*: current folder)");
363 /* TODO: Split enough of scan.c out so that we can call it here. */
364 command = concat("scan +", node->n_name, " ", sequences_s,
369 if (node->n_field == NULL) {
373 count = count_messages(node->n_field);
376 printf("%-*s %6d.%c %s\n",
377 (int) folder_len, node->n_name,
379 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
384 /* If we're fnext, we haven't checked the last node yet. If it's the
385 * 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];
409 setlocale(LC_ALL, "");
411 invo_name = r1bindex(argv[0], '/');
413 /* read user profile/context */
416 arguments = getarguments (invo_name, argc, argv, 1);
422 while ((cp = *argp++)) {
424 switch (smatch (++cp, switches)) {
426 ambigsw (cp, switches);
429 adios (NULL, "-%s unknown", cp);
432 snprintf (help, sizeof(help), "%s [switches] [sequences]",
434 print_help (help, switches, 1);
437 print_version(invo_name);
441 if (!(folders = *argp++) || *folders == '-')
442 adios(NULL, "missing argument to %s", argp[-2]);
445 if (!(invo_name = *argp++) || *invo_name == '-')
446 adios(NULL, "missing argument to %s", argp[-2]);
447 invo_name = r1bindex(invo_name, '/');
451 /* have a sequence argument */
452 if (!seq_in_list(cp, sequences)) {
457 if (strcmp(invo_name, "fnext") == 0) {
459 } else if (strcmp(invo_name, "fprev") == 0) {
461 } else if (strcmp(invo_name, "unseen") == 0) {
465 if (folders == NULL) {
468 if (folders[0] != '/') {
469 folders = m_maildir(folders);
474 /* no sequence arguments; use unseen */
475 unseen = context_find(usequence);
476 if (unseen == NULL || unseen[0] == '\0') {
477 adios(NULL, "must specify sequences or set %s", usequence);
479 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
480 sequences[i++] = *ap;
485 folder = doit(context_find(pfolder), folders, sequences);
486 if (folder == NULL) {
491 if (run_mode == UNSEEN) {
492 /* All the scan(1)s it runs change the current folder, so we
493 * need to put it back. Unfortunately, context_replace lamely
494 * ignores the new value you give it if it is the same one it
495 * has in memory. So, we'll be lame, too. I'm not sure if i
496 * should just change context_replace... */
497 context_replace(pfolder, "defeat_context_replace_optimization");
500 /* update current folder */
501 context_replace(pfolder, folder->n_name);
503 if (run_mode == FNEXT || run_mode == FPREV) {
504 printf("%s %s\n", folder->n_name, folder->n_field);