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