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