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