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