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;
37 /* check_folders uses this to maintain state with both .folders list of
38 * folders and with crawl_folders. */
40 struct node **first, **cur_node;
47 /* Return the number of messages in a string list of message numbers. */
49 count_messages(char *field)
55 field = getcpy(field);
57 /* copied from seq_read.c:seq_init */
58 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
59 if ((cp = strchr(*ap, '-')))
61 if ((j = m_atoi (*ap)) > 0) {
62 k = cp ? m_atoi (cp) : j;
73 /* Return TRUE if the sequence 'name' is in 'sequences'. */
75 seq_in_list(char *name, char *sequences[])
79 for (i = 0; sequences[i] != NULL; i++) {
80 if (strcmp(name, sequences[i]) == 0) {
88 /* Return the string list of message numbers from the sequences file, or NULL
91 get_msgnums(char *folder, char *sequences[])
93 char *seqfile = concat(m_maildir(folder), "/", mh_seq, (void *)NULL);
94 FILE *fp = fopen(seqfile, "r");
96 char name[NAMESZ], field[BUFSIZ];
98 char *msgnums = NULL, *this_msgnums, *old_msgnums;
100 /* no sequences file -> no messages */
105 /* copied from seq_read.c:seq_public */
106 for (state = FLD;;) {
107 switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
111 if (state == FLDPLUS) {
113 while (state == FLDPLUS) {
114 state = m_getfld (state, name, field,
116 cp = add (field, cp);
119 /* Here's where we differ from seq_public: if it's in a
120 * sequence we want, save the list of messages. */
121 if (seq_in_list(name, sequences)) {
122 this_msgnums = trimcpy(cp);
123 if (msgnums == NULL) {
124 msgnums = this_msgnums;
126 old_msgnums = msgnums;
127 msgnums = concat(old_msgnums, " ",
128 this_msgnums, (void *)NULL);
136 if (seq_in_list(name, sequences)) {
137 this_msgnums = trimcpy(field);
138 if (msgnums == NULL) {
139 msgnums = this_msgnums;
141 old_msgnums = msgnums;
142 msgnums = concat(old_msgnums, " ",
143 this_msgnums, (void *)NULL);
156 adios (NULL, "no blank lines are permitted in %s", seqfile);
163 adios (NULL, "%s is poorly formatted", seqfile);
165 break; /* break from for loop */
173 /* Check `folder' (of length `len') for interesting messages, filling in the
176 check_folder(char *folder, size_t len, struct list_state *b)
178 char *msgnums = get_msgnums(folder, b->sequences);
179 int is_cur = strcmp(folder, b->cur) == 0;
181 if (is_cur || msgnums != NULL) {
182 if (*b->first == NULL) {
183 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
185 b->node->n_next = mh_xmalloc(sizeof(*b->node));
186 b->node = b->node->n_next;
188 b->node->n_name = folder;
189 b->node->n_field = msgnums;
191 if (*b->maxlen < len) {
196 /* Save the node for the current folder, so we can fall back to it. */
198 *b->cur_node = b->node;
203 crawl_callback(char *folder, void *baton)
205 check_folder(folder, strlen(folder), baton);
209 /* Scan folders, returning:
210 * first -- list of nodes for all folders which have desired messages;
211 * if the current folder is listed in .folders, it is also in
212 * the list regardless of whether it has any desired messages
213 * last -- last node in list
214 * cur_node -- node of current folder, if listed in .folders
215 * maxlen -- length of longest folder name
217 * `cur' points to the name of the current folder, `folders' points to the
218 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
219 * the array of sequences for which to look.
221 * An empty list is returned as first=last=NULL.
224 check_folders(struct node **first, struct node **last,
225 struct node **cur_node, size_t *maxlen,
226 char *cur, char *folders, char *sequences[])
233 *first = *last = *cur_node = NULL;
237 b.cur_node = cur_node;
240 b.sequences = sequences;
242 if (folders == NULL) {
243 chdir(m_maildir(""));
244 crawl_folders(".", crawl_callback, &b);
246 fp = fopen(folders, "r");
248 adios(NULL, "failed to read %s", folders);
250 while (vfgets(fp, &line) == OK) {
251 len = strlen(line) - 1;
253 check_folder(getcpy(line), len, &b);
258 if (*first != NULL) {
259 b.node->n_next = NULL;
264 /* Return a single string of the `sequences' joined by a space (' '). */
266 join_sequences(char *sequences[])
272 for (i = 0; sequences[i] != NULL; i++) {
273 len += strlen(sequences[i]) + 1;
275 result = mh_xmalloc(len + 1);
277 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
278 len = strlen(sequences[i]);
279 memcpy(cp, sequences[i], len);
282 /* -1 to overwrite the last delimiter */
288 /* Return a struct node for the folder to change to. This is the next
289 * (previous, if FPREV mode) folder with desired messages, or the current
290 * folder if no folders have desired. If NEW or UNSEEN mode, print the
291 * output but don't change folders.
293 * n_name is the folder to change to, and n_field is the string list of
294 * desired message numbers.
297 doit(char *cur, char *folders, char *sequences[])
299 struct node *first, *cur_node, *node, *last, *prev;
301 int count, total = 0;
302 char *command = NULL, *sequences_s = NULL;
304 if (cur == NULL || cur[0] == '\0') {
308 check_folders(&first, &last, &cur_node, &folder_len, cur,
311 if (run_mode == FNEXT || run_mode == FPREV) {
313 /* No folders at all... */
315 } else if (first->n_next == NULL) {
316 /* We have only one node; any desired messages in it? */
317 if (first->n_field == NULL) {
322 } else if (cur_node == NULL) {
323 /* Current folder is not listed in .folders, return first. */
326 } else if (run_mode == UNSEEN) {
327 sequences_s = join_sequences(sequences);
330 for (node = first, prev = NULL;
332 prev = node, node = node->n_next) {
333 if (run_mode == FNEXT) {
334 /* If we have a previous node and it is the current
335 * 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) {
341 /* Found current folder in fprev mode; if we have a
342 * previous node in the list, return it; else return
349 } else if (run_mode == UNSEEN) {
350 if (node->n_field == NULL) {
354 printf("\n%d %s messages in %s",
355 count_messages(node->n_field),
358 if (strcmp(node->n_name, cur) == 0) {
359 puts(" (*: current folder)");
365 /* TODO: Split enough of scan.c out so that we can call it here. */
366 command = concat("scan +", node->n_name, " ", sequences_s,
371 if (node->n_field == NULL) {
375 count = count_messages(node->n_field);
378 printf("%-*s %6d.%c %s\n",
379 (int) folder_len, node->n_name,
381 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
386 /* If we're fnext, we haven't checked the last node yet. If it's the
387 * current folder, return the first node. */
388 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
392 if (run_mode == NEW) {
393 printf("%-*s %6d.\n", (int) folder_len, " total", total);
400 main(int argc, char **argv)
402 char **ap, *cp, **argp, **arguments;
404 char *folders = NULL;
405 char *sequences[NUMATTRS + 1];
411 setlocale(LC_ALL, "");
413 invo_name = r1bindex(argv[0], '/');
415 /* read user profile/context */
418 arguments = getarguments (invo_name, argc, argv, 1);
424 while ((cp = *argp++)) {
426 switch (smatch (++cp, switches)) {
428 ambigsw (cp, switches);
431 adios (NULL, "-%s unknown", cp);
434 snprintf (help, sizeof(help), "%s [switches] [sequences]",
436 print_help (help, switches, 1);
439 print_version(invo_name);
443 if (!(folders = *argp++) || *folders == '-')
444 adios(NULL, "missing argument to %s", argp[-2]);
447 if (!(invo_name = *argp++) || *invo_name == '-')
448 adios(NULL, "missing argument to %s", argp[-2]);
449 invo_name = r1bindex(invo_name, '/');
453 /* have a sequence argument */
454 if (!seq_in_list(cp, sequences)) {
459 if (strcmp(invo_name, "fnext") == 0) {
461 } else if (strcmp(invo_name, "fprev") == 0) {
463 } else if (strcmp(invo_name, "unseen") == 0) {
467 if (folders == NULL) {
470 if (folders[0] != '/') {
471 folders = m_maildir(folders);
476 /* no sequence arguments; use unseen */
477 unseen = context_find(usequence);
478 if (unseen == NULL || unseen[0] == '\0') {
479 adios(NULL, "must specify sequences or set %s", usequence);
481 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
482 sequences[i++] = *ap;
487 folder = doit(context_find(pfolder), folders, sequences);
488 if (folder == NULL) {
493 if (run_mode == UNSEEN) {
494 /* All the scan(1)s it runs change the current folder, so we
495 * need to put it back. Unfortunately, context_replace lamely
496 * ignores the new value you give it if it is the same one it
497 * has in memory. So, we'll be lame, too. I'm not sure if i
498 * should just change context_replace... */
499 context_replace(pfolder, "defeat_context_replace_optimization");
502 /* update current folder */
503 context_replace(pfolder, folder->n_name);
505 if (run_mode == FNEXT || run_mode == FPREV) {
506 printf("%s %s\n", folder->n_name, folder->n_field);