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