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