]> diplodocus.org Git - nmh/blob - uip/new.c
seq_setprev.c: Move interface to own file.
[nmh] / uip / new.c
1 /* new.c -- as new, list all folders with unseen messages
2 * -- as fnext, move to next folder with unseen messages
3 * -- as fprev, move to previous folder with unseen messages
4 * -- as unseen, scan all unseen messages
5 * This code is Copyright (c) 2008, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 *
9 * Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
10 */
11
12 #include <sys/types.h>
13
14 #include "h/mh.h"
15 #include "sbr/smatch.h"
16 #include "sbr/r1bindex.h"
17 #include "sbr/trimcpy.h"
18 #include "sbr/vfgets.h"
19 #include "sbr/getcpy.h"
20 #include "sbr/m_atoi.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
29 #include "h/crawl_folders.h"
30 #include "h/done.h"
31 #include "h/utils.h"
32 #include "sbr/lock_file.h"
33 #include "sbr/m_maildir.h"
34
35 #define NEW_SWITCHES \
36 X("mode", 1, MODESW) \
37 X("folders", 1, FOLDERSSW) \
38 X("version", 1, VERSIONSW) \
39 X("help", 1, HELPSW) \
40
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(NEW);
43 #undef X
44
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(NEW, switches);
47 #undef X
48
49 /* What to do, based on argv[0]. */
50 static enum {
51 RM_NEW,
52 RM_FNEXT,
53 RM_FPREV,
54 RM_UNSEEN
55 } run_mode = RM_NEW;
56
57 /* check_folders uses this to maintain state with both .folders list of
58 * folders and with crawl_folders. */
59 struct list_state {
60 struct node **first, **cur_node;
61 size_t *maxlen;
62 char *cur;
63 char **sequences;
64 struct node *node;
65 };
66
67 /* Return the number of messages in a string list of message numbers. */
68 static int
69 count_messages(char *field)
70 {
71 int total = 0;
72 int j, k;
73 char *cp, **ap;
74
75 field = getcpy(field);
76
77 /* copied from seq_read.c:seq_init */
78 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
79 if ((cp = strchr(*ap, '-')))
80 *cp++ = '\0';
81 if ((j = m_atoi (*ap)) > 0) {
82 k = cp ? m_atoi (cp) : j;
83
84 total += k - j + 1;
85 }
86 }
87
88 free(field);
89
90 return total;
91 }
92
93 /* Return true if the sequence 'name' is in 'sequences'. */
94 static bool
95 seq_in_list(char *name, char *sequences[])
96 {
97 int i;
98
99 for (i = 0; sequences[i] != NULL; i++) {
100 if (strcmp(name, sequences[i]) == 0) {
101 return true;
102 }
103 }
104
105 return false;
106 }
107
108 /* Return the string list of message numbers from the sequences file, or NULL
109 * if none. */
110 static char *
111 get_msgnums(char *folder, char *sequences[])
112 {
113 char *seqfile = NULL;
114 FILE *fp;
115 int state;
116 char name[NAMESZ], field[NMH_BUFSIZ];
117 char *cp;
118 char *msgnums = NULL, *this_msgnums, *old_msgnums;
119 int failed_to_lock = 0;
120 m_getfld_state_t gstate;
121
122 /* copied from seq_read.c:seq_public */
123 /*
124 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
125 * the "mh-sequences" profile entry, but left it empty),
126 * then just return, and do not initialize any public sequences.
127 */
128 if (mh_seq == NULL || *mh_seq == '\0')
129 return NULL;
130
131 /* get filename of sequence file */
132 seqfile = concat(m_maildir(folder), "/", mh_seq, NULL);
133
134 if (seqfile == NULL)
135 return NULL;
136
137 if ((fp = lkfopendata (seqfile, "r", & failed_to_lock)) == NULL) {
138 if (failed_to_lock)
139 adios (seqfile, "failed to lock");
140 free(seqfile);
141 return NULL;
142 }
143
144 /* Use m_getfld2 to scan sequence file */
145 gstate = m_getfld_state_init(fp);
146 for (;;) {
147 int fieldsz = sizeof field;
148 switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
149 case FLD:
150 case FLDPLUS:
151 if (state == FLDPLUS) {
152 cp = getcpy (field);
153 while (state == FLDPLUS) {
154 fieldsz = sizeof field;
155 state = m_getfld2(&gstate, name, field, &fieldsz);
156 cp = add (field, cp);
157 }
158
159 /* Here's where we differ from seq_public: if it's in a
160 * sequence we want, save the list of messages. */
161 if (seq_in_list(name, sequences)) {
162 this_msgnums = trimcpy(cp);
163 if (msgnums == NULL) {
164 msgnums = this_msgnums;
165 } else {
166 old_msgnums = msgnums;
167 msgnums = concat(old_msgnums, " ",
168 this_msgnums, NULL);
169 free(old_msgnums);
170 free(this_msgnums);
171 }
172 }
173 free (cp);
174 } else {
175 /* and here */
176 if (seq_in_list(name, sequences)) {
177 this_msgnums = trimcpy(field);
178 if (msgnums == NULL) {
179 msgnums = this_msgnums;
180 } else {
181 old_msgnums = msgnums;
182 msgnums = concat(old_msgnums, " ",
183 this_msgnums, NULL);
184 free(old_msgnums);
185 free(this_msgnums);
186 }
187 }
188 }
189
190 continue;
191
192 case BODY:
193 die("no blank lines are permitted in %s", seqfile);
194 break;
195
196 case FILEEOF:
197 break;
198
199 default:
200 die("%s is poorly formatted", seqfile);
201 }
202 break; /* break from for loop */
203 }
204 m_getfld_state_destroy (&gstate);
205
206 lkfclosedata (fp, seqfile);
207
208 free(seqfile);
209
210 return msgnums;
211 }
212
213 /* Check `folder' (of length `len') for interesting messages, filling in the
214 * list in `b'. */
215 static void
216 check_folder(char *folder, size_t len, struct list_state *b)
217 {
218 char *msgnums = get_msgnums(folder, b->sequences);
219 int is_cur = strcmp(folder, b->cur) == 0;
220
221 if (is_cur || msgnums != NULL) {
222 if (*b->first == NULL) {
223 NEW(b->node);
224 *b->first = b->node;
225 } else {
226 NEW(b->node->n_next);
227 b->node = b->node->n_next;
228 }
229 b->node->n_name = folder;
230 b->node->n_field = msgnums;
231
232 if (*b->maxlen < len) {
233 *b->maxlen = len;
234 }
235 }
236
237 /* Save the node for the current folder, so we can fall back to it. */
238 if (is_cur) {
239 *b->cur_node = b->node;
240 }
241 }
242
243 static bool
244 crawl_callback(char *folder, void *baton)
245 {
246 check_folder(folder, strlen(folder), baton);
247 return true;
248 }
249
250 /* Scan folders, returning:
251 * first -- list of nodes for all folders which have desired messages;
252 * if the current folder is listed in .folders, it is also in
253 * the list regardless of whether it has any desired messages
254 * last -- last node in list
255 * cur_node -- node of current folder, if listed in .folders
256 * maxlen -- length of longest folder name
257 *
258 * `cur' points to the name of the current folder, `folders' points to the
259 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
260 * the array of sequences for which to look.
261 *
262 * An empty list is returned as first=last=NULL.
263 */
264 static void
265 check_folders(struct node **first, struct node **last,
266 struct node **cur_node, size_t *maxlen,
267 char *cur, char *folders, char *sequences[])
268 {
269 struct list_state b;
270 FILE *fp;
271 char *line;
272 size_t len;
273
274 *first = *last = *cur_node = NULL;
275 *maxlen = 0;
276
277 b.first = first;
278 b.cur_node = cur_node;
279 b.maxlen = maxlen;
280 b.cur = cur;
281 b.sequences = sequences;
282
283 if (folders == NULL) {
284 if (chdir(m_maildir("")) < 0) {
285 advise (m_maildir(""), "chdir");
286 }
287 crawl_folders(".", crawl_callback, &b);
288 } else {
289 fp = fopen(folders, "r");
290 if (fp == NULL) {
291 die("failed to read %s", folders);
292 }
293 while (vfgets(fp, &line) == OK) {
294 len = strlen(line) - 1;
295 line[len] = '\0';
296 check_folder(mh_xstrdup(line), len, &b);
297 }
298 fclose(fp);
299 }
300
301 if (*first != NULL) {
302 b.node->n_next = NULL;
303 *last = b.node;
304 }
305 }
306
307 /* Return a single string of the `sequences' joined by a space (' '). */
308 static char *
309 join_sequences(char *sequences[])
310 {
311 int i;
312 size_t len = 0;
313 char *result, *cp;
314
315 for (i = 0; sequences[i] != NULL; i++) {
316 len += strlen(sequences[i]) + 1;
317 }
318 result = mh_xmalloc(len + 1);
319
320 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
321 len = strlen(sequences[i]);
322 memcpy(cp, sequences[i], len);
323 cp[len] = ' ';
324 }
325 /* -1 to overwrite the last delimiter */
326 *--cp = '\0';
327
328 return result;
329 }
330
331 /* Return a struct node for the folder to change to. This is the next
332 * (previous, if RM_FPREV mode) folder with desired messages, or the current
333 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
334 * output but don't change folders.
335 *
336 * n_name is the folder to change to, and n_field is the string list of
337 * desired message numbers.
338 */
339 static struct node *
340 doit(char *cur, char *folders, char *sequences[])
341 {
342 struct node *first, *cur_node, *node, *last, *prev;
343 size_t folder_len;
344 int count, total = 0;
345 char *command = NULL, *sequences_s = NULL;
346
347 if (cur == NULL || cur[0] == '\0') {
348 cur = "inbox";
349 }
350
351 check_folders(&first, &last, &cur_node, &folder_len, cur,
352 folders, sequences);
353
354 if (run_mode == RM_FNEXT || run_mode == RM_FPREV) {
355 if (first == NULL) {
356 /* No folders at all... */
357 return NULL;
358 }
359 if (first->n_next == NULL) {
360 /* We have only one node; any desired messages in it? */
361 if (first->n_field == NULL) {
362 return NULL;
363 }
364 return first;
365 }
366 if (cur_node == NULL) {
367 /* Current folder is not listed in .folders, return first. */
368 return first;
369 }
370 } else if (run_mode == RM_UNSEEN) {
371 sequences_s = join_sequences(sequences);
372 }
373
374 for (node = first, prev = NULL;
375 node != NULL;
376 prev = node, node = node->n_next) {
377 if (run_mode == RM_FNEXT) {
378 /* If we have a previous node and it is the current
379 * folder, return this node. */
380 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
381 return node;
382 }
383 } else if (run_mode == RM_FPREV) {
384 if (strcmp(node->n_name, cur) == 0) {
385 /* Found current folder in fprev mode; if we have a
386 * previous node in the list, return it; else return
387 * the last node. */
388 if (prev)
389 return prev;
390 return last;
391 }
392 } else if (run_mode == RM_UNSEEN) {
393 int status;
394
395 if (node->n_field == NULL) {
396 continue;
397 }
398
399 printf("\n%d %s messages in %s",
400 count_messages(node->n_field),
401 sequences_s,
402 node->n_name);
403 if (strcmp(node->n_name, cur) == 0) {
404 puts(" (*: current folder)");
405 } else {
406 putchar('\n');
407 }
408 fflush(stdout);
409
410 /* TODO: Split enough of scan.c out so that we can call it here. */
411 command = concat("scan +", node->n_name, " ", sequences_s,
412 NULL);
413 status = system(command);
414 if (! WIFEXITED (status)) {
415 adios (command, "system");
416 }
417 free(command);
418 } else {
419 if (node->n_field == NULL) {
420 continue;
421 }
422
423 count = count_messages(node->n_field);
424 total += count;
425
426 printf("%-*s %6d.%c %s\n",
427 (int) folder_len, node->n_name,
428 count,
429 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
430 node->n_field);
431 }
432 }
433
434 /* If we're fnext, we haven't checked the last node yet. If it's the
435 * current folder, return the first node. */
436 if (run_mode == RM_FNEXT) {
437 assert(last != NULL);
438 if (strcmp(last->n_name, cur) == 0) {
439 return first;
440 }
441 }
442
443 if (run_mode == RM_NEW) {
444 printf("%-*s %6d.\n", (int) folder_len, " total", total);
445 }
446
447 return cur_node;
448 }
449
450 int
451 main(int argc, char **argv)
452 {
453 char **ap, *cp, **argp, **arguments;
454 char help[BUFSIZ];
455 char *folders = NULL;
456 svector_t sequences = svector_create (0);
457 int i = 0;
458 char *unseen;
459 struct node *folder;
460
461 if (nmh_init(argv[0], true, true)) { return 1; }
462
463 arguments = getarguments (invo_name, argc, argv, 1);
464 argp = arguments;
465
466 /*
467 * Parse arguments
468 */
469 while ((cp = *argp++)) {
470 if (*cp == '-') {
471 switch (smatch (++cp, switches)) {
472 case AMBIGSW:
473 ambigsw (cp, switches);
474 done (1);
475 case UNKWNSW:
476 die("-%s unknown", cp);
477
478 case HELPSW:
479 snprintf (help, sizeof(help), "%s [switches] [sequences]",
480 invo_name);
481 print_help (help, switches, 1);
482 done (0);
483 case VERSIONSW:
484 print_version(invo_name);
485 done (0);
486
487 case FOLDERSSW:
488 if (!(folders = *argp++) || *folders == '-')
489 die("missing argument to %s", argp[-2]);
490 continue;
491 case MODESW:
492 if (!(invo_name = *argp++) || *invo_name == '-')
493 die("missing argument to %s", argp[-2]);
494 invo_name = r1bindex(invo_name, '/');
495 continue;
496 }
497 }
498 /* have a sequence argument */
499 if (!seq_in_list(cp, svector_strs (sequences))) {
500 svector_push_back (sequences, cp);
501 ++i;
502 }
503 }
504
505 if (strcmp(invo_name, "fnext") == 0) {
506 run_mode = RM_FNEXT;
507 } else if (strcmp(invo_name, "fprev") == 0) {
508 run_mode = RM_FPREV;
509 } else if (strcmp(invo_name, "unseen") == 0) {
510 run_mode = RM_UNSEEN;
511 }
512
513 if (folders == NULL) {
514 /* will flists */
515 } else {
516 if (folders[0] != '/') {
517 folders = m_maildir(folders);
518 }
519 }
520
521 if (i == 0) {
522 /* no sequence arguments; use unseen */
523 unseen = context_find(usequence);
524 if (unseen == NULL || unseen[0] == '\0') {
525 die("must specify sequences or set %s", usequence);
526 }
527 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
528 svector_push_back (sequences, *ap);
529 ++i;
530 }
531 }
532
533 folder = doit(context_find(pfolder), folders, svector_strs (sequences));
534 if (folder == NULL) {
535 done(0);
536 return 1;
537 }
538
539 if (run_mode == RM_UNSEEN) {
540 /* All the scan(1)s it runs change the current folder, so we
541 * need to put it back. Unfortunately, context_replace lamely
542 * ignores the new value you give it if it is the same one it
543 * has in memory. So, we'll be lame, too. I'm not sure if i
544 * should just change context_replace... */
545 context_replace(pfolder, "defeat_context_replace_optimization");
546 }
547
548 /* update current folder */
549 context_replace(pfolder, folder->n_name);
550
551 if (run_mode == RM_FNEXT || run_mode == RM_FPREV) {
552 printf("%s %s\n", folder->n_name, folder->n_field);
553 }
554
555 context_save();
556
557 svector_free (sequences);
558 done (0);
559 return 1;
560 }