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