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 = mh_xstrdup(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 = {{0}};
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)) {
117 ** if it's in a sequence we want,
118 ** save the list of messages.
120 if (seq_in_list(f.name, sequences)) {
121 this_msgnums = trimcpy(f.value);
122 if (msgnums == NULL) {
123 msgnums = this_msgnums;
125 old_msgnums = msgnums;
126 msgnums = concat(old_msgnums, " ",
129 mh_free0(&old_msgnums);
130 mh_free0(&this_msgnums);
136 adios(EX_DATAERR, NULL, "no blank lines are permitted in %s", seqfile);
143 adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
154 ** Check `folder' (of length `len') for interesting messages,
155 ** filling in the list in `b'.
158 check_folder(char *folder, size_t len, struct list_state *b)
160 char *msgnums = get_msgnums(folder, b->sequences);
161 int is_cur = strcmp(folder, b->cur) == 0;
163 if (is_cur || msgnums != NULL) {
164 if (*b->first == NULL) {
165 *b->first = b->node = mh_xcalloc(1, sizeof(*b->node));
167 b->node->n_next = mh_xcalloc(1, sizeof(*b->node));
168 b->node = b->node->n_next;
170 b->node->n_name = folder;
171 b->node->n_field = msgnums;
173 if (*b->maxlen < len) {
178 /* Save the node for the current folder, so we can fall back to it. */
180 *b->cur_node = b->node;
185 crawl_callback(char *folder, void *baton)
187 check_folder(folder, strlen(folder), baton);
192 ** Scan folders, returning:
193 ** first -- list of nodes for all folders which have desired messages;
194 ** if the current folder is listed in .folders, it is also in
195 ** the list regardless of whether it has any desired messages
196 ** last -- last node in list
197 ** cur_node -- node of current folder, if listed in .folders
198 ** maxlen -- length of longest folder name
200 ** `cur' points to the name of the current folder, `folders' points to the
201 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
202 ** the array of sequences for which to look.
204 ** An empty list is returned as first=last=NULL.
207 check_folders(struct node **first, struct node **last,
208 struct node **cur_node, size_t *maxlen,
209 char *cur, char *folders, char *sequences[])
216 *first = *last = *cur_node = NULL;
220 b.cur_node = cur_node;
223 b.sequences = sequences;
225 if (folders == NULL) {
226 chdir(toabsdir("+"));
227 crawl_folders(".", crawl_callback, &b);
229 fp = fopen(folders, "r");
231 adios(EX_IOERR, NULL, "failed to read %s", folders);
233 while (vfgets(fp, &line) == OK) {
234 len = strlen(line) - 1;
236 check_folder(mh_xstrdup(line), len, &b);
241 if (*first != NULL) {
242 b.node->n_next = NULL;
247 /* Return a single string of the `sequences' joined by a space (' '). */
249 join_sequences(char *sequences[])
255 for (i = 0; sequences[i] != NULL; i++) {
256 len += strlen(sequences[i]) + 1;
258 result = mh_xcalloc(len + 1, sizeof(char));
260 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
261 len = strlen(sequences[i]);
262 memcpy(cp, sequences[i], len);
265 /* -1 to overwrite the last delimiter */
272 ** Return a struct node for the folder to change to. This is the next
273 ** (previous, if FPREV mode) folder with desired messages, or the current
274 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
275 ** output but don't change folders.
277 ** n_name is the folder to change to, and n_field is the string list of
278 ** desired message numbers.
281 doit(char *cur, char *folders, char *sequences[])
283 struct node *first, *cur_node, *node, *last = NULL, *prev;
285 int count, total = 0;
286 char *sequences_s = NULL;
292 if (cur == NULL || cur[0] == '\0') {
296 check_folders(&first, &last, &cur_node, &folder_len, cur,
299 if (run_mode == FNEXT || run_mode == FPREV) {
301 /* No folders at all... */
303 } else if (first->n_next == NULL) {
305 ** We have only one node; any desired messages in it?
307 if (first->n_field == NULL) {
312 } else if (cur_node == NULL) {
314 ** Current folder is not listed in .folders,
319 } else if (run_mode == UNSEEN) {
320 sequences_s = join_sequences(sequences);
323 for (node = first, prev = NULL;
325 prev = node, node = node->n_next) {
326 if (run_mode == FNEXT) {
328 ** If we have a previous node and it is the current
329 ** folder, return this node.
331 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
334 } else if (run_mode == FPREV) {
335 if (strcmp(node->n_name, cur) == 0) {
337 ** Found current folder in fprev mode;
338 ** if we have a previous node in the list,
339 ** return it; else return the last node.
346 } else if (run_mode == UNSEEN) {
347 if (node->n_field == NULL) {
351 printf("\n%d %s messages in %s",
352 count_messages(node->n_field),
355 if (strcmp(node->n_name, cur) == 0) {
356 puts(" (*: current folder)");
363 argv[argc++] = "scan";
364 snprintf(buf, sizeof buf, "+%s", node->n_name);
366 for (seqp=sequences; *seqp; seqp++) {
367 argv[argc++] = *seqp;
369 argv[argc] = (char *)NULL;
370 execprog(*argv, argv);
372 if (node->n_field == NULL) {
376 count = count_messages(node->n_field);
379 printf("%-*s %6d.%c %s\n", (int) folder_len,
381 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
387 ** If we're fnext, we haven't checked the last node yet. If it's the
388 ** current folder, return the first node.
390 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
394 if (run_mode == NEW) {
395 printf("%-*s %6d.\n", (int) folder_len, " total", total);
402 main(int argc, char **argv)
404 char **ap, *cp, **argp, **arguments;
406 char *folders = NULL;
407 char *sequences[NUMATTRS + 1];
415 setlocale(LC_ALL, "");
416 invo_name = mhbasename(argv[0]);
418 /* read user profile/context */
421 arguments = getarguments(invo_name, argc, argv, 1);
427 while ((cp = *argp++)) {
429 switch (smatch(++cp, switches)) {
431 ambigsw(cp, switches);
434 adios(EX_USAGE, NULL, "-%s unknown", cp);
437 snprintf(help, sizeof(help),
438 "%s [switches] [sequences]",
440 print_help(help, switches, 1);
441 exit(argc == 2 ? EX_OK : EX_USAGE);
443 print_version(invo_name);
444 exit(argc == 2 ? EX_OK : EX_USAGE);
447 if (!(folders = *argp++) || *folders == '-')
448 adios(EX_USAGE, NULL, "missing argument to %s",
452 if (!(invo_name = *argp++) || *invo_name == '-')
453 adios(EX_USAGE, NULL, "missing argument to %s",
455 invo_name = mhbasename(invo_name);
459 /* have a sequence argument */
460 if (!seq_in_list(cp, sequences)) {
466 if (strcmp(invo_name, "fnext") == 0) {
468 } else if (strcmp(invo_name, "fprev") == 0) {
470 } else if (strcmp(invo_name, "unseen") == 0) {
474 if (folders == NULL) {
477 if (folders[0] != '/') {
478 folders = toabsdir(folders);
484 /* no sequence arguments; use unseen */
485 if ((unseen = context_find(usequence))) {
487 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
490 unseen = seq_unseen; /* use default */
492 dp = mh_xstrdup(unseen);
493 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
494 sequences[i++] = *ap;
499 folder = doit(context_find(curfolder), folders, sequences);
500 if (folder == NULL) {
505 if (run_mode == UNSEEN) {
507 ** All the scan(1)s it runs change the current folder, so we
508 ** need to put it back. Unfortunately, context_replace lamely
509 ** ignores the new value you give it if it is the same one it
510 ** has in memory. So, we'll be lame, too. I'm not sure if i
511 ** should just change context_replace...
513 context_replace(curfolder, "defeat_context_replace_optimization");
516 /* update current folder */
517 context_replace(curfolder, folder->n_name);
519 if (run_mode == FNEXT || run_mode == FPREV) {
520 printf("%s %s\n", folder->n_name, folder->n_field);