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