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