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