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