]> diplodocus.org Git - nmh/blob - uip/new.c
vector.c: Move interface to own file.
[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/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
18 #include "h/crawl_folders.h"
19 #include "h/done.h"
20 #include "h/utils.h"
21 #include "sbr/lock_file.h"
22 #include "sbr/m_maildir.h"
23
24 #define NEW_SWITCHES \
25 X("mode", 1, MODESW) \
26 X("folders", 1, FOLDERSSW) \
27 X("version", 1, VERSIONSW) \
28 X("help", 1, HELPSW) \
29
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(NEW);
32 #undef X
33
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(NEW, switches);
36 #undef X
37
38 /* What to do, based on argv[0]. */
39 static enum {
40 RM_NEW,
41 RM_FNEXT,
42 RM_FPREV,
43 RM_UNSEEN
44 } run_mode = RM_NEW;
45
46 /* check_folders uses this to maintain state with both .folders list of
47 * folders and with crawl_folders. */
48 struct list_state {
49 struct node **first, **cur_node;
50 size_t *maxlen;
51 char *cur;
52 char **sequences;
53 struct node *node;
54 };
55
56 /* Return the number of messages in a string list of message numbers. */
57 static int
58 count_messages(char *field)
59 {
60 int total = 0;
61 int j, k;
62 char *cp, **ap;
63
64 field = getcpy(field);
65
66 /* copied from seq_read.c:seq_init */
67 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
68 if ((cp = strchr(*ap, '-')))
69 *cp++ = '\0';
70 if ((j = m_atoi (*ap)) > 0) {
71 k = cp ? m_atoi (cp) : j;
72
73 total += k - j + 1;
74 }
75 }
76
77 free(field);
78
79 return total;
80 }
81
82 /* Return true if the sequence 'name' is in 'sequences'. */
83 static bool
84 seq_in_list(char *name, char *sequences[])
85 {
86 int i;
87
88 for (i = 0; sequences[i] != NULL; i++) {
89 if (strcmp(name, sequences[i]) == 0) {
90 return true;
91 }
92 }
93
94 return false;
95 }
96
97 /* Return the string list of message numbers from the sequences file, or NULL
98 * if none. */
99 static char *
100 get_msgnums(char *folder, char *sequences[])
101 {
102 char *seqfile = NULL;
103 FILE *fp;
104 int state;
105 char name[NAMESZ], field[NMH_BUFSIZ];
106 char *cp;
107 char *msgnums = NULL, *this_msgnums, *old_msgnums;
108 int failed_to_lock = 0;
109 m_getfld_state_t gstate;
110
111 /* copied from seq_read.c:seq_public */
112 /*
113 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
114 * the "mh-sequences" profile entry, but left it empty),
115 * then just return, and do not initialize any public sequences.
116 */
117 if (mh_seq == NULL || *mh_seq == '\0')
118 return NULL;
119
120 /* get filename of sequence file */
121 seqfile = concat(m_maildir(folder), "/", mh_seq, NULL);
122
123 if (seqfile == NULL)
124 return NULL;
125
126 if ((fp = lkfopendata (seqfile, "r", & failed_to_lock)) == NULL) {
127 if (failed_to_lock)
128 adios (seqfile, "failed to lock");
129 free(seqfile);
130 return NULL;
131 }
132
133 /* Use m_getfld2 to scan sequence file */
134 gstate = m_getfld_state_init(fp);
135 for (;;) {
136 int fieldsz = sizeof field;
137 switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
138 case FLD:
139 case FLDPLUS:
140 if (state == FLDPLUS) {
141 cp = getcpy (field);
142 while (state == FLDPLUS) {
143 fieldsz = sizeof field;
144 state = m_getfld2(&gstate, name, field, &fieldsz);
145 cp = add (field, cp);
146 }
147
148 /* Here's where we differ from seq_public: if it's in a
149 * sequence we want, save the list of messages. */
150 if (seq_in_list(name, sequences)) {
151 this_msgnums = trimcpy(cp);
152 if (msgnums == NULL) {
153 msgnums = this_msgnums;
154 } else {
155 old_msgnums = msgnums;
156 msgnums = concat(old_msgnums, " ",
157 this_msgnums, NULL);
158 free(old_msgnums);
159 free(this_msgnums);
160 }
161 }
162 free (cp);
163 } else {
164 /* and here */
165 if (seq_in_list(name, sequences)) {
166 this_msgnums = trimcpy(field);
167 if (msgnums == NULL) {
168 msgnums = this_msgnums;
169 } else {
170 old_msgnums = msgnums;
171 msgnums = concat(old_msgnums, " ",
172 this_msgnums, NULL);
173 free(old_msgnums);
174 free(this_msgnums);
175 }
176 }
177 }
178
179 continue;
180
181 case BODY:
182 die("no blank lines are permitted in %s", seqfile);
183 break;
184
185 case FILEEOF:
186 break;
187
188 default:
189 die("%s is poorly formatted", seqfile);
190 }
191 break; /* break from for loop */
192 }
193 m_getfld_state_destroy (&gstate);
194
195 lkfclosedata (fp, seqfile);
196
197 free(seqfile);
198
199 return msgnums;
200 }
201
202 /* Check `folder' (of length `len') for interesting messages, filling in the
203 * list in `b'. */
204 static void
205 check_folder(char *folder, size_t len, struct list_state *b)
206 {
207 char *msgnums = get_msgnums(folder, b->sequences);
208 int is_cur = strcmp(folder, b->cur) == 0;
209
210 if (is_cur || msgnums != NULL) {
211 if (*b->first == NULL) {
212 NEW(b->node);
213 *b->first = b->node;
214 } else {
215 NEW(b->node->n_next);
216 b->node = b->node->n_next;
217 }
218 b->node->n_name = folder;
219 b->node->n_field = msgnums;
220
221 if (*b->maxlen < len) {
222 *b->maxlen = len;
223 }
224 }
225
226 /* Save the node for the current folder, so we can fall back to it. */
227 if (is_cur) {
228 *b->cur_node = b->node;
229 }
230 }
231
232 static bool
233 crawl_callback(char *folder, void *baton)
234 {
235 check_folder(folder, strlen(folder), baton);
236 return true;
237 }
238
239 /* Scan folders, returning:
240 * first -- list of nodes for all folders which have desired messages;
241 * if the current folder is listed in .folders, it is also in
242 * the list regardless of whether it has any desired messages
243 * last -- last node in list
244 * cur_node -- node of current folder, if listed in .folders
245 * maxlen -- length of longest folder name
246 *
247 * `cur' points to the name of the current folder, `folders' points to the
248 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
249 * the array of sequences for which to look.
250 *
251 * An empty list is returned as first=last=NULL.
252 */
253 static void
254 check_folders(struct node **first, struct node **last,
255 struct node **cur_node, size_t *maxlen,
256 char *cur, char *folders, char *sequences[])
257 {
258 struct list_state b;
259 FILE *fp;
260 char *line;
261 size_t len;
262
263 *first = *last = *cur_node = NULL;
264 *maxlen = 0;
265
266 b.first = first;
267 b.cur_node = cur_node;
268 b.maxlen = maxlen;
269 b.cur = cur;
270 b.sequences = sequences;
271
272 if (folders == NULL) {
273 if (chdir(m_maildir("")) < 0) {
274 advise (m_maildir(""), "chdir");
275 }
276 crawl_folders(".", crawl_callback, &b);
277 } else {
278 fp = fopen(folders, "r");
279 if (fp == NULL) {
280 die("failed to read %s", folders);
281 }
282 while (vfgets(fp, &line) == OK) {
283 len = strlen(line) - 1;
284 line[len] = '\0';
285 check_folder(mh_xstrdup(line), len, &b);
286 }
287 fclose(fp);
288 }
289
290 if (*first != NULL) {
291 b.node->n_next = NULL;
292 *last = b.node;
293 }
294 }
295
296 /* Return a single string of the `sequences' joined by a space (' '). */
297 static char *
298 join_sequences(char *sequences[])
299 {
300 int i;
301 size_t len = 0;
302 char *result, *cp;
303
304 for (i = 0; sequences[i] != NULL; i++) {
305 len += strlen(sequences[i]) + 1;
306 }
307 result = mh_xmalloc(len + 1);
308
309 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
310 len = strlen(sequences[i]);
311 memcpy(cp, sequences[i], len);
312 cp[len] = ' ';
313 }
314 /* -1 to overwrite the last delimiter */
315 *--cp = '\0';
316
317 return result;
318 }
319
320 /* Return a struct node for the folder to change to. This is the next
321 * (previous, if RM_FPREV mode) folder with desired messages, or the current
322 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
323 * output but don't change folders.
324 *
325 * n_name is the folder to change to, and n_field is the string list of
326 * desired message numbers.
327 */
328 static struct node *
329 doit(char *cur, char *folders, char *sequences[])
330 {
331 struct node *first, *cur_node, *node, *last, *prev;
332 size_t folder_len;
333 int count, total = 0;
334 char *command = NULL, *sequences_s = NULL;
335
336 if (cur == NULL || cur[0] == '\0') {
337 cur = "inbox";
338 }
339
340 check_folders(&first, &last, &cur_node, &folder_len, cur,
341 folders, sequences);
342
343 if (run_mode == RM_FNEXT || run_mode == RM_FPREV) {
344 if (first == NULL) {
345 /* No folders at all... */
346 return NULL;
347 }
348 if (first->n_next == NULL) {
349 /* We have only one node; any desired messages in it? */
350 if (first->n_field == NULL) {
351 return NULL;
352 }
353 return first;
354 }
355 if (cur_node == NULL) {
356 /* Current folder is not listed in .folders, return first. */
357 return first;
358 }
359 } else if (run_mode == RM_UNSEEN) {
360 sequences_s = join_sequences(sequences);
361 }
362
363 for (node = first, prev = NULL;
364 node != NULL;
365 prev = node, node = node->n_next) {
366 if (run_mode == RM_FNEXT) {
367 /* If we have a previous node and it is the current
368 * folder, return this node. */
369 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
370 return node;
371 }
372 } else if (run_mode == RM_FPREV) {
373 if (strcmp(node->n_name, cur) == 0) {
374 /* Found current folder in fprev mode; if we have a
375 * previous node in the list, return it; else return
376 * the last node. */
377 if (prev)
378 return prev;
379 return last;
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 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], true, true)) { 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 die("-%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 die("missing argument to %s", argp[-2]);
479 continue;
480 case MODESW:
481 if (!(invo_name = *argp++) || *invo_name == '-')
482 die("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 die("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 }