]> diplodocus.org Git - nmh/blob - uip/new.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[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 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) {
410 assert(last != NULL);
411 if (strcmp(last->n_name, cur) == 0) {
412 return first;
413 }
414 }
415
416 if (run_mode == NEW) {
417 printf("%-*s %6d.\n", (int) folder_len, " total", total);
418 }
419
420 return cur_node;
421 }
422
423 int
424 main(int argc, char **argv)
425 {
426 char **ap, *cp, **argp, **arguments;
427 char help[BUFSIZ];
428 char *folders = NULL;
429 svector_t sequences = svector_create (0);
430 int i = 0;
431 char *unseen;
432 struct node *folder;
433
434 if (nmh_init(argv[0], 1)) { return 1; }
435
436 arguments = getarguments (invo_name, argc, argv, 1);
437 argp = arguments;
438
439 /*
440 * Parse arguments
441 */
442 while ((cp = *argp++)) {
443 if (*cp == '-') {
444 switch (smatch (++cp, switches)) {
445 case AMBIGSW:
446 ambigsw (cp, switches);
447 done (1);
448 case UNKWNSW:
449 adios (NULL, "-%s unknown", cp);
450
451 case HELPSW:
452 snprintf (help, sizeof(help), "%s [switches] [sequences]",
453 invo_name);
454 print_help (help, switches, 1);
455 done (0);
456 case VERSIONSW:
457 print_version(invo_name);
458 done (0);
459
460 case FOLDERSSW:
461 if (!(folders = *argp++) || *folders == '-')
462 adios(NULL, "missing argument to %s", argp[-2]);
463 continue;
464 case MODESW:
465 if (!(invo_name = *argp++) || *invo_name == '-')
466 adios(NULL, "missing argument to %s", argp[-2]);
467 invo_name = r1bindex(invo_name, '/');
468 continue;
469 }
470 }
471 /* have a sequence argument */
472 if (!seq_in_list(cp, svector_strs (sequences))) {
473 svector_push_back (sequences, cp);
474 ++i;
475 }
476 }
477
478 if (strcmp(invo_name, "fnext") == 0) {
479 run_mode = FNEXT;
480 } else if (strcmp(invo_name, "fprev") == 0) {
481 run_mode = FPREV;
482 } else if (strcmp(invo_name, "unseen") == 0) {
483 run_mode = UNSEEN;
484 }
485
486 if (folders == NULL) {
487 /* will flists */
488 } else {
489 if (folders[0] != '/') {
490 folders = m_maildir(folders);
491 }
492 }
493
494 if (i == 0) {
495 /* no sequence arguments; use unseen */
496 unseen = context_find(usequence);
497 if (unseen == NULL || unseen[0] == '\0') {
498 adios(NULL, "must specify sequences or set %s", usequence);
499 }
500 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
501 svector_push_back (sequences, *ap);
502 ++i;
503 }
504 }
505
506 folder = doit(context_find(pfolder), folders, svector_strs (sequences));
507 if (folder == NULL) {
508 done(0);
509 return 1;
510 }
511
512 if (run_mode == UNSEEN) {
513 /* All the scan(1)s it runs change the current folder, so we
514 * need to put it back. Unfortunately, context_replace lamely
515 * ignores the new value you give it if it is the same one it
516 * has in memory. So, we'll be lame, too. I'm not sure if i
517 * should just change context_replace... */
518 context_replace(pfolder, "defeat_context_replace_optimization");
519 }
520
521 /* update current folder */
522 context_replace(pfolder, folder->n_name);
523
524 if (run_mode == FNEXT || run_mode == FPREV) {
525 printf("%s %s\n", folder->n_name, folder->n_field);
526 }
527
528 context_save();
529
530 svector_free (sequences);
531 done (0);
532 return 1;
533 }