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