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