60bb80637d7fe0690b46c0e2afff2c656d5ed3ba
[mmh] / uip / new.c
1 /*
2 ** new.c -- as new,    list all folders with unseen messages
3 **       -- as fnext,  move to next folder with unseen messages
4 **       -- as fprev,  move to previous folder with unseen messages
5 **       -- as unseen, scan all unseen messages
6 ** This code is Copyright (c) 2008, by the authors of nmh.  See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
9 **
10 ** Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
11 */
12
13 #include <h/mh.h>
14 #include <h/crawl_folders.h>
15 #include <h/utils.h>
16 #include <sys/types.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <locale.h>
22 #include <sysexits.h>
23
24 static struct swit switches[] = {
25 #define MODESW 0
26         { "mode", 0 },
27 #define FOLDERSSW 1
28         { "folders", 0 },
29 #define VERSIONSW 2
30         { "Version", 0 },
31 #define HELPSW 3
32         { "help", 0 },
33         { NULL, 0 }
34 };
35
36 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
37
38 /*
39 ** check_folders uses this to maintain state with both .folders list of
40 ** folders and with crawl_folders.
41 */
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 /*
92 ** Return the string list of message numbers from the sequences file,
93 ** or NULL if none.
94 */
95 static char *
96 get_msgnums(char *folder, char *sequences[])
97 {
98         enum state state;
99         struct field f = free_field;
100         char *seqfile = concat(toabsdir(folder), "/", mh_seq, (void *)NULL);
101         FILE *fp = fopen(seqfile, "r");
102         char *msgnums = NULL, *this_msgnums, *old_msgnums;
103
104         /* no sequences file -> no messages */
105         if (fp == NULL) {
106                 return NULL;
107         }
108
109         for (state = FLD2;;) {
110                 switch (state = m_getfld2(state, &f, fp)) {
111                 case FLD2:
112                         /*
113                         ** if it's in a sequence we want,
114                         ** save the list of messages.
115                         */
116                         if (seq_in_list(f.name, sequences)) {
117                                 this_msgnums = trimcpy(f.value);
118                                 if (msgnums == NULL) {
119                                         msgnums = this_msgnums;
120                                 } else {
121                                         old_msgnums = msgnums;
122                                         msgnums = concat(old_msgnums, " ",
123                                                         this_msgnums,
124                                                         (void *)NULL);
125                                         free(old_msgnums);
126                                         free(this_msgnums);
127                                 }
128                         }
129                         continue;
130
131                 case BODY2:
132                         adios(EX_DATAERR, NULL, "no blank lines are permitted in %s", seqfile);
133                         /* FALL */
134
135                 case FILEEOF2:
136                         break;
137
138                 default:
139                         adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
140                 }
141                 break;
142         }
143
144         fclose(fp);
145
146         return msgnums;
147 }
148
149 /*
150 ** Check `folder' (of length `len') for interesting messages,
151 ** filling in the list in `b'.
152 */
153 static void
154 check_folder(char *folder, size_t len, struct list_state *b)
155 {
156         char *msgnums = get_msgnums(folder, b->sequences);
157         int is_cur = strcmp(folder, b->cur) == 0;
158
159         if (is_cur || msgnums != NULL) {
160                 if (*b->first == NULL) {
161                         *b->first = b->node = mh_xmalloc(sizeof(*b->node));
162                 } else {
163                         b->node->n_next = mh_xmalloc(sizeof(*b->node));
164                         b->node = b->node->n_next;
165                 }
166                 b->node->n_name = folder;
167                 b->node->n_field = msgnums;
168
169                 if (*b->maxlen < len) {
170                         *b->maxlen = len;
171                 }
172         }
173
174         /* Save the node for the current folder, so we can fall back to it. */
175         if (is_cur) {
176                 *b->cur_node = b->node;
177         }
178 }
179
180 static boolean
181 crawl_callback(char *folder, void *baton)
182 {
183         check_folder(folder, strlen(folder), baton);
184         return TRUE;
185 }
186
187 /*
188 ** Scan folders, returning:
189 ** first        -- list of nodes for all folders which have desired messages;
190 **                 if the current folder is listed in .folders, it is also in
191 **                 the list regardless of whether it has any desired messages
192 ** last         -- last node in list
193 ** cur_node     -- node of current folder, if listed in .folders
194 ** maxlen       -- length of longest folder name
195 **
196 ** `cur' points to the name of the current folder, `folders' points to the
197 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
198 ** the array of sequences for which to look.
199 **
200 ** An empty list is returned as first=last=NULL.
201 */
202 static void
203 check_folders(struct node **first, struct node **last,
204         struct node **cur_node, size_t *maxlen,
205         char *cur, char *folders, char *sequences[])
206 {
207         struct list_state b;
208         FILE *fp;
209         char *line;
210         size_t len;
211
212         *first = *last = *cur_node = NULL;
213         *maxlen = 0;
214
215         b.first = first;
216         b.cur_node = cur_node;
217         b.maxlen = maxlen;
218         b.cur = cur;
219         b.sequences = sequences;
220
221         if (folders == NULL) {
222                 chdir(toabsdir("+"));
223                 crawl_folders(".", crawl_callback, &b);
224         } else {
225                 fp = fopen(folders, "r");
226                 if (fp  == NULL) {
227                         adios(EX_IOERR, NULL, "failed to read %s", folders);
228                 }
229                 while (vfgets(fp, &line) == OK) {
230                         len = strlen(line) - 1;
231                         line[len] = '\0';
232                         check_folder(getcpy(line), len, &b);
233                 }
234                 fclose(fp);
235         }
236
237         if (*first != NULL) {
238                 b.node->n_next = NULL;
239                 *last = b.node;
240         }
241 }
242
243 /* Return a single string of the `sequences' joined by a space (' '). */
244 static char *
245 join_sequences(char *sequences[])
246 {
247         int i;
248         size_t len = 0;
249         char *result, *cp;
250
251         for (i = 0; sequences[i] != NULL; i++) {
252                 len += strlen(sequences[i]) + 1;
253         }
254         result = mh_xmalloc(len + 1);
255
256         for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
257                 len = strlen(sequences[i]);
258                 memcpy(cp, sequences[i], len);
259                 cp[len] = ' ';
260         }
261         /* -1 to overwrite the last delimiter */
262         *--cp = '\0';
263
264         return result;
265 }
266
267 /*
268 ** Return a struct node for the folder to change to.  This is the next
269 ** (previous, if FPREV mode) folder with desired messages, or the current
270 ** folder if no folders have desired.  If NEW or UNSEEN mode, print the
271 ** output but don't change folders.
272 **
273 ** n_name is the folder to change to, and n_field is the string list of
274 ** desired message numbers.
275 */
276 static struct node *
277 doit(char *cur, char *folders, char *sequences[])
278 {
279         struct node *first, *cur_node, *node, *last = NULL, *prev;
280         size_t folder_len;
281         int count, total = 0;
282         char *sequences_s = NULL;
283         int argc = 0;
284         char *argv[MAXARGS];
285         char **seqp;
286         char buf[BUFSIZ];
287
288         if (cur == NULL || cur[0] == '\0') {
289                 cur = "inbox";
290         }
291
292         check_folders(&first, &last, &cur_node, &folder_len, cur,
293                         folders, sequences);
294
295         if (run_mode == FNEXT || run_mode == FPREV) {
296                 if (first == NULL) {
297                         /* No folders at all... */
298                         return NULL;
299                 } else if (first->n_next == NULL) {
300                         /*
301                         ** We have only one node; any desired messages in it?
302                         */
303                         if (first->n_field == NULL) {
304                                 return NULL;
305                         } else {
306                                 return first;
307                         }
308                 } else if (cur_node == NULL) {
309                         /*
310                         ** Current folder is not listed in .folders,
311                         ** return first.
312                         */
313                         return first;
314                 }
315         } else if (run_mode == UNSEEN) {
316                 sequences_s = join_sequences(sequences);
317         }
318
319         for (node = first, prev = NULL;
320                  node != NULL;
321                  prev = node, node = node->n_next) {
322                 if (run_mode == FNEXT) {
323                         /*
324                         ** If we have a previous node and it is the current
325                         ** folder, return this node.
326                         */
327                         if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
328                                 return node;
329                         }
330                 } else if (run_mode == FPREV) {
331                         if (strcmp(node->n_name, cur) == 0) {
332                                 /*
333                                 ** Found current folder in fprev mode;
334                                 ** if we have a previous node in the list,
335                                 ** return it; else return the last node.
336                                 */
337                                 if (prev == NULL) {
338                                         return last;
339                                 }
340                                 return prev;
341                         }
342                 } else if (run_mode == UNSEEN) {
343                         if (node->n_field == NULL) {
344                                 continue;
345                         }
346
347                         printf("\n%d %s messages in %s",
348                                    count_messages(node->n_field),
349                                    sequences_s,
350                                    node->n_name);
351                         if (strcmp(node->n_name, cur) == 0) {
352                                 puts(" (*: current folder)");
353                         } else {
354                                 puts("");
355                         }
356                         fflush(stdout);
357
358                         argc = 0;
359                         argv[argc++] = "scan";
360                         snprintf(buf, sizeof buf, "+%s", node->n_name);
361                         argv[argc++] = buf;
362                         for (seqp=sequences; *seqp; seqp++) {
363                                 argv[argc++] = *seqp;
364                         }
365                         argv[argc] = (char *)NULL;
366                         execprog(*argv, argv);
367                 } else {
368                         if (node->n_field == NULL) {
369                                 continue;
370                         }
371
372                         count = count_messages(node->n_field);
373                         total += count;
374
375                         printf("%-*s %6d.%c %s\n", (int) folder_len,
376                                 node->n_name, count,
377                                 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
378                                 node->n_field);
379                 }
380         }
381
382         /*
383         ** If we're fnext, we haven't checked the last node yet.  If it's the
384         ** current folder, return the first node.
385         */
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         char *sequences[NUMATTRS + 1];
404         int i = 0;
405         char *unseen;
406         struct node *folder;
407
408         sequences[0] = NULL;
409         sequences[1] = NULL;
410
411         setlocale(LC_ALL, "");
412         invo_name = mhbasename(argv[0]);
413
414         /* read user profile/context */
415         context_read();
416
417         arguments = getarguments(invo_name, argc, argv, 1);
418         argp = arguments;
419
420         /*
421         ** Parse arguments
422         */
423         while ((cp = *argp++)) {
424                 if (*cp == '-') {
425                         switch (smatch(++cp, switches)) {
426                         case AMBIGSW:
427                                 ambigsw(cp, switches);
428                                 exit(EX_USAGE);
429                         case UNKWNSW:
430                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
431
432                         case HELPSW:
433                                 snprintf(help, sizeof(help),
434                                                 "%s [switches] [sequences]",
435                                                 invo_name);
436                                 print_help(help, switches, 1);
437                                 exit(argc == 2 ? EX_OK : EX_USAGE);
438                         case VERSIONSW:
439                                 print_version(invo_name);
440                                 exit(argc == 2 ? EX_OK : EX_USAGE);
441
442                         case FOLDERSSW:
443                                 if (!(folders = *argp++) || *folders == '-')
444                                         adios(EX_USAGE, NULL, "missing argument to %s",
445                                                         argp[-2]);
446                                 continue;
447                         case MODESW:
448                                 if (!(invo_name = *argp++) || *invo_name == '-')
449                                         adios(EX_USAGE, NULL, "missing argument to %s",
450                                                         argp[-2]);
451                                 invo_name = mhbasename(invo_name);
452                                 continue;
453                         }
454                 }
455                 /* have a sequence argument */
456                 if (!seq_in_list(cp, sequences)) {
457                         sequences[i++] = cp;
458                         sequences[i] = NULL;
459                 }
460         }
461
462         if (strcmp(invo_name, "fnext") == 0) {
463                 run_mode = FNEXT;
464         } else if (strcmp(invo_name, "fprev") == 0) {
465                 run_mode = FPREV;
466         } else if (strcmp(invo_name, "unseen") == 0) {
467                 run_mode = UNSEEN;
468         }
469
470         if (folders == NULL) {
471                 /* will flists */
472         } else {
473                 if (folders[0] != '/') {
474                         folders = toabsdir(folders);
475                 }
476         }
477
478         if (i == 0) {
479                 char *dp;
480                 /* no sequence arguments; use unseen */
481                 if ((unseen = context_find(usequence))) {
482                         if (!*unseen) {
483                                 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
484                         }
485                 } else {
486                         unseen = seq_unseen;  /* use default */
487                 }
488                 dp = getcpy(unseen);
489                 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
490                         sequences[i++] = *ap;
491                 }
492         }
493         sequences[i] = NULL;
494
495         folder = doit(context_find(curfolder), folders, sequences);
496         if (folder == NULL) {
497                 exit(EX_OK);
498                 return 1;
499         }
500
501         if (run_mode == UNSEEN) {
502                 /*
503                 ** All the scan(1)s it runs change the current folder, so we
504                 ** need to put it back.  Unfortunately, context_replace lamely
505                 ** ignores the new value you give it if it is the same one it
506                 ** has in memory.  So, we'll be lame, too.  I'm not sure if i
507                 ** should just change context_replace...
508                 */
509                 context_replace(curfolder, "defeat_context_replace_optimization");
510         }
511
512         /* update current folder */
513         context_replace(curfolder, folder->n_name);
514
515         if (run_mode == FNEXT || run_mode == FPREV) {
516                 printf("%s  %s\n", folder->n_name, folder->n_field);
517         }
518
519         context_save();
520
521         return 0;
522 }