]> diplodocus.org Git - nmh/blob - uip/new.c
Another pass at cleaning up (some of) the manpages.
[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 if (node->n_field == NULL) {
374 continue;
375 }
376
377 printf("\n%d %s messages in %s",
378 count_messages(node->n_field),
379 sequences_s,
380 node->n_name);
381 if (strcmp(node->n_name, cur) == 0) {
382 puts(" (*: current folder)");
383 } else {
384 puts("");
385 }
386 fflush(stdout);
387
388 /* TODO: Split enough of scan.c out so that we can call it here. */
389 command = concat("scan +", node->n_name, " ", sequences_s,
390 (void *)NULL);
391 if (system(command) == NOTOK) {
392 adios (command, "system");
393 }
394 free(command);
395 } else {
396 if (node->n_field == NULL) {
397 continue;
398 }
399
400 count = count_messages(node->n_field);
401 total += count;
402
403 printf("%-*s %6d.%c %s\n",
404 (int) folder_len, node->n_name,
405 count,
406 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
407 node->n_field);
408 }
409 }
410
411 /* If we're fnext, we haven't checked the last node yet. If it's the
412 * current folder, return the first node. */
413 if (run_mode == FNEXT) {
414 assert(last != NULL);
415 if (strcmp(last->n_name, cur) == 0) {
416 return first;
417 }
418 }
419
420 if (run_mode == NEW) {
421 printf("%-*s %6d.\n", (int) folder_len, " total", total);
422 }
423
424 return cur_node;
425 }
426
427 int
428 main(int argc, char **argv)
429 {
430 char **ap, *cp, **argp, **arguments;
431 char help[BUFSIZ];
432 char *folders = NULL;
433 svector_t sequences = svector_create (0);
434 int i = 0;
435 char *unseen;
436 struct node *folder;
437
438 if (nmh_init(argv[0], 1)) { return 1; }
439
440 arguments = getarguments (invo_name, argc, argv, 1);
441 argp = arguments;
442
443 /*
444 * Parse arguments
445 */
446 while ((cp = *argp++)) {
447 if (*cp == '-') {
448 switch (smatch (++cp, switches)) {
449 case AMBIGSW:
450 ambigsw (cp, switches);
451 done (1);
452 case UNKWNSW:
453 adios (NULL, "-%s unknown", cp);
454
455 case HELPSW:
456 snprintf (help, sizeof(help), "%s [switches] [sequences]",
457 invo_name);
458 print_help (help, switches, 1);
459 done (0);
460 case VERSIONSW:
461 print_version(invo_name);
462 done (0);
463
464 case FOLDERSSW:
465 if (!(folders = *argp++) || *folders == '-')
466 adios(NULL, "missing argument to %s", argp[-2]);
467 continue;
468 case MODESW:
469 if (!(invo_name = *argp++) || *invo_name == '-')
470 adios(NULL, "missing argument to %s", argp[-2]);
471 invo_name = r1bindex(invo_name, '/');
472 continue;
473 }
474 }
475 /* have a sequence argument */
476 if (!seq_in_list(cp, svector_strs (sequences))) {
477 svector_push_back (sequences, cp);
478 ++i;
479 }
480 }
481
482 if (strcmp(invo_name, "fnext") == 0) {
483 run_mode = FNEXT;
484 } else if (strcmp(invo_name, "fprev") == 0) {
485 run_mode = FPREV;
486 } else if (strcmp(invo_name, "unseen") == 0) {
487 run_mode = UNSEEN;
488 }
489
490 if (folders == NULL) {
491 /* will flists */
492 } else {
493 if (folders[0] != '/') {
494 folders = m_maildir(folders);
495 }
496 }
497
498 if (i == 0) {
499 /* no sequence arguments; use unseen */
500 unseen = context_find(usequence);
501 if (unseen == NULL || unseen[0] == '\0') {
502 adios(NULL, "must specify sequences or set %s", usequence);
503 }
504 for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
505 svector_push_back (sequences, *ap);
506 ++i;
507 }
508 }
509
510 folder = doit(context_find(pfolder), folders, svector_strs (sequences));
511 if (folder == NULL) {
512 done(0);
513 return 1;
514 }
515
516 if (run_mode == UNSEEN) {
517 /* All the scan(1)s it runs change the current folder, so we
518 * need to put it back. Unfortunately, context_replace lamely
519 * ignores the new value you give it if it is the same one it
520 * has in memory. So, we'll be lame, too. I'm not sure if i
521 * should just change context_replace... */
522 context_replace(pfolder, "defeat_context_replace_optimization");
523 }
524
525 /* update current folder */
526 context_replace(pfolder, folder->n_name);
527
528 if (run_mode == FNEXT || run_mode == FPREV) {
529 printf("%s %s\n", folder->n_name, folder->n_field);
530 }
531
532 context_save();
533
534 svector_free (sequences);
535 done (0);
536 return 1;
537 }