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