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