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