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