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