]> diplodocus.org Git - nmh/blob - uip/new.c
Added context_find_prefix().
[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
118 if (failed_to_lock) {
119 adios (seqfile, "failed to lock");
120 } else {
121 free(seqfile);
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 if (chdir(m_maildir("")) < 0) {
265 advise (m_maildir(""), "chdir");
266 }
267 crawl_folders(".", crawl_callback, &b);
268 } else {
269 fp = fopen(folders, "r");
270 if (fp == NULL) {
271 adios(NULL, "failed to read %s", folders);
272 }
273 while (vfgets(fp, &line) == OK) {
274 len = strlen(line) - 1;
275 line[len] = '\0';
276 check_folder(getcpy(line), len, &b);
277 }
278 fclose(fp);
279 }
280
281 if (*first != NULL) {
282 b.node->n_next = NULL;
283 *last = b.node;
284 }
285 }
286
287 /* Return a single string of the `sequences' joined by a space (' '). */
288 static char *
289 join_sequences(char *sequences[])
290 {
291 int i;
292 size_t len = 0;
293 char *result, *cp;
294
295 for (i = 0; sequences[i] != NULL; i++) {
296 len += strlen(sequences[i]) + 1;
297 }
298 result = mh_xmalloc(len + 1);
299
300 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
301 len = strlen(sequences[i]);
302 memcpy(cp, sequences[i], len);
303 cp[len] = ' ';
304 }
305 /* -1 to overwrite the last delimiter */
306 *--cp = '\0';
307
308 return result;
309 }
310
311 /* Return a struct node for the folder to change to. This is the next
312 * (previous, if FPREV mode) folder with desired messages, or the current
313 * folder if no folders have desired. If NEW or UNSEEN mode, print the
314 * output but don't change folders.
315 *
316 * n_name is the folder to change to, and n_field is the string list of
317 * desired message numbers.
318 */
319 static struct node *
320 doit(char *cur, char *folders, char *sequences[])
321 {
322 struct node *first, *cur_node, *node, *last, *prev;
323 size_t folder_len;
324 int count, total = 0;
325 char *command = NULL, *sequences_s = NULL;
326
327 if (cur == NULL || cur[0] == '\0') {
328 cur = "inbox";
329 }
330
331 check_folders(&first, &last, &cur_node, &folder_len, cur,
332 folders, sequences);
333
334 if (run_mode == FNEXT || run_mode == FPREV) {
335 if (first == NULL) {
336 /* No folders at all... */
337 return NULL;
338 } else if (first->n_next == NULL) {
339 /* We have only one node; any desired messages in it? */
340 if (first->n_field == NULL) {
341 return NULL;
342 } else {
343 return first;
344 }
345 } else if (cur_node == NULL) {
346 /* Current folder is not listed in .folders, return first. */
347 return first;
348 }
349 } else if (run_mode == UNSEEN) {
350 sequences_s = join_sequences(sequences);
351 }
352
353 for (node = first, prev = NULL;
354 node != NULL;
355 prev = node, node = node->n_next) {
356 if (run_mode == FNEXT) {
357 /* If we have a previous node and it is the current
358 * folder, return this node. */
359 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
360 return node;
361 }
362 } else if (run_mode == FPREV) {
363 if (strcmp(node->n_name, cur) == 0) {
364 /* Found current folder in fprev mode; if we have a
365 * previous node in the list, return it; else return
366 * the last node. */
367 if (prev == NULL) {
368 return last;
369 }
370 return prev;
371 }
372 } else if (run_mode == UNSEEN) {
373 int status;
374
375 if (node->n_field == NULL) {
376 continue;
377 }
378
379 printf("\n%d %s messages in %s",
380 count_messages(node->n_field),
381 sequences_s,
382 node->n_name);
383 if (strcmp(node->n_name, cur) == 0) {
384 puts(" (*: current folder)");
385 } else {
386 puts("");
387 }
388 fflush(stdout);
389
390 /* TODO: Split enough of scan.c out so that we can call it here. */
391 command = concat("scan +", node->n_name, " ", sequences_s,
392 (void *)NULL);
393 status = system(command);
394 if (! WIFEXITED (status)) {
395 adios (command, "system");
396 }
397 free(command);
398 } else {
399 if (node->n_field == NULL) {
400 continue;
401 }
402
403 count = count_messages(node->n_field);
404 total += count;
405
406 printf("%-*s %6d.%c %s\n",
407 (int) folder_len, node->n_name,
408 count,
409 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
410 node->n_field);
411 }
412 }
413
414 /* If we're fnext, we haven't checked the last node yet. If it's the
415 * current folder, return the first node. */
416 if (run_mode == FNEXT) {
417 assert(last != NULL);
418 if (strcmp(last->n_name, cur) == 0) {
419 return first;
420 }
421 }
422
423 if (run_mode == NEW) {
424 printf("%-*s %6d.\n", (int) folder_len, " total", total);
425 }
426
427 return cur_node;
428 }
429
430 int
431 main(int argc, char **argv)
432 {
433 char **ap, *cp, **argp, **arguments;
434 char help[BUFSIZ];
435 char *folders = NULL;
436 svector_t sequences = svector_create (0);
437 int i = 0;
438 char *unseen;
439 struct node *folder;
440
441 if (nmh_init(argv[0], 1)) { return 1; }
442
443 arguments = getarguments (invo_name, argc, argv, 1);
444 argp = arguments;
445
446 /*
447 * Parse arguments
448 */
449 while ((cp = *argp++)) {
450 if (*cp == '-') {
451 switch (smatch (++cp, switches)) {
452 case AMBIGSW:
453 ambigsw (cp, switches);
454 done (1);
455 case UNKWNSW:
456 adios (NULL, "-%s unknown", cp);
457
458 case HELPSW:
459 snprintf (help, sizeof(help), "%s [switches] [sequences]",
460 invo_name);
461 print_help (help, switches, 1);
462 done (0);
463 case VERSIONSW:
464 print_version(invo_name);
465 done (0);
466
467 case FOLDERSSW:
468 if (!(folders = *argp++) || *folders == '-')
469 adios(NULL, "missing argument to %s", argp[-2]);
470 continue;
471 case MODESW:
472 if (!(invo_name = *argp++) || *invo_name == '-')
473 adios(NULL, "missing argument to %s", argp[-2]);
474 invo_name = r1bindex(invo_name, '/');
475 continue;
476 }
477 }
478 /* have a sequence argument */
479 if (!seq_in_list(cp, svector_strs (sequences))) {
480 svector_push_back (sequences, cp);
481 ++i;
482 }
483 }
484
485 if (strcmp(invo_name, "fnext") == 0) {
486 run_mode = FNEXT;
487 } else if (strcmp(invo_name, "fprev") == 0) {
488 run_mode = FPREV;
489 } else if (strcmp(invo_name, "unseen") == 0) {
490 run_mode = UNSEEN;
491 }
492
493 if (folders == NULL) {
494 /* will flists */
495 } else {
496 if (folders[0] != '/') {
497 folders = m_maildir(folders);
498 }
499 }
500
501 if (i == 0) {
502 /* no sequence arguments; use unseen */
503 unseen = context_find(usequence);
504 if (unseen == NULL || unseen[0] == '\0') {
505 adios(NULL, "must specify sequences or set %s", usequence);
506 }
507 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
508 svector_push_back (sequences, *ap);
509 ++i;
510 }
511 }
512
513 folder = doit(context_find(pfolder), folders, svector_strs (sequences));
514 if (folder == NULL) {
515 done(0);
516 return 1;
517 }
518
519 if (run_mode == UNSEEN) {
520 /* All the scan(1)s it runs change the current folder, so we
521 * need to put it back. Unfortunately, context_replace lamely
522 * ignores the new value you give it if it is the same one it
523 * has in memory. So, we'll be lame, too. I'm not sure if i
524 * should just change context_replace... */
525 context_replace(pfolder, "defeat_context_replace_optimization");
526 }
527
528 /* update current folder */
529 context_replace(pfolder, folder->n_name);
530
531 if (run_mode == FNEXT || run_mode == FPREV) {
532 printf("%s %s\n", folder->n_name, folder->n_field);
533 }
534
535 context_save();
536
537 svector_free (sequences);
538 done (0);
539 return 1;
540 }