]>
diplodocus.org Git - nmh/blob - uip/new.c
1 /* new.c -- as new, list all folders with unseen messages
2 * -- as fnext, move to next folder with unseen messages
3 * -- as fprev, move to previous folder with unseen messages
4 * -- as unseen, scan all unseen messages
5 * This code is Copyright (c) 2008, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
9 * Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
12 #include <sys/types.h>
15 #include "sbr/m_getfld.h"
16 #include "sbr/getarguments.h"
17 #include "sbr/concat.h"
18 #include "sbr/smatch.h"
19 #include "sbr/r1bindex.h"
20 #include "sbr/trimcpy.h"
21 #include "sbr/vfgets.h"
22 #include "sbr/getcpy.h"
23 #include "sbr/m_atoi.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_find.h"
27 #include "sbr/brkstring.h"
28 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/error.h"
32 #include "sbr/crawl_folders.h"
35 #include "sbr/lock_file.h"
36 #include "sbr/m_maildir.h"
38 #define NEW_SWITCHES \
39 X("mode", 1, MODESW) \
40 X("folders", 1, FOLDERSSW) \
41 X("version", 1, VERSIONSW) \
42 X("help", 1, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(NEW
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(NEW
, switches
);
52 /* What to do, based on argv[0]. */
60 /* check_folders uses this to maintain state with both .folders list of
61 * folders and with crawl_folders. */
63 struct node
**first
, **cur_node
;
70 /* Return the number of messages in a string list of message numbers. */
72 count_messages(char *field
)
78 field
= getcpy(field
);
80 /* copied from seq_read.c:seq_init */
81 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
82 if ((cp
= strchr(*ap
, '-')))
84 if ((j
= m_atoi (*ap
)) > 0) {
85 k
= cp
? m_atoi (cp
) : j
;
96 /* Return true if the sequence 'name' is in 'sequences'. */
98 seq_in_list(char *name
, char *sequences
[])
102 for (i
= 0; sequences
[i
] != NULL
; i
++) {
103 if (strcmp(name
, sequences
[i
]) == 0) {
111 /* Return the string list of message numbers from the sequences file, or NULL
114 get_msgnums(char *folder
, char *sequences
[])
116 char *seqfile
= NULL
;
119 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
121 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
122 int failed_to_lock
= 0;
123 m_getfld_state_t gstate
;
125 /* copied from seq_read.c:seq_public */
127 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
128 * the "mh-sequences" profile entry, but left it empty),
129 * then just return, and do not initialize any public sequences.
131 if (mh_seq
== NULL
|| *mh_seq
== '\0')
134 /* get filename of sequence file */
135 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
140 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
142 adios (seqfile
, "failed to lock");
147 /* Use m_getfld2 to scan sequence file */
148 gstate
= m_getfld_state_init(fp
);
150 int fieldsz
= sizeof field
;
151 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
154 if (state
== FLDPLUS
) {
156 while (state
== FLDPLUS
) {
157 fieldsz
= sizeof field
;
158 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
159 cp
= add (field
, cp
);
162 /* Here's where we differ from seq_public: if it's in a
163 * sequence we want, save the list of messages. */
164 if (seq_in_list(name
, sequences
)) {
165 this_msgnums
= trimcpy(cp
);
166 if (msgnums
== NULL
) {
167 msgnums
= this_msgnums
;
169 old_msgnums
= msgnums
;
170 msgnums
= concat(old_msgnums
, " ",
179 if (seq_in_list(name
, sequences
)) {
180 this_msgnums
= trimcpy(field
);
181 if (msgnums
== NULL
) {
182 msgnums
= this_msgnums
;
184 old_msgnums
= msgnums
;
185 msgnums
= concat(old_msgnums
, " ",
196 die("no blank lines are permitted in %s", seqfile
);
203 die("%s is poorly formatted", seqfile
);
205 break; /* break from for loop */
207 m_getfld_state_destroy (&gstate
);
209 lkfclosedata (fp
, seqfile
);
216 /* Check `folder' (of length `len') for interesting messages, filling in the
219 check_folder(char *folder
, size_t len
, struct list_state
*b
)
221 char *msgnums
= get_msgnums(folder
, b
->sequences
);
222 int is_cur
= strcmp(folder
, b
->cur
) == 0;
224 if (is_cur
|| msgnums
!= NULL
) {
225 if (*b
->first
== NULL
) {
229 NEW(b
->node
->n_next
);
230 b
->node
= b
->node
->n_next
;
232 b
->node
->n_name
= folder
;
233 b
->node
->n_field
= msgnums
;
235 if (*b
->maxlen
< len
) {
240 /* Save the node for the current folder, so we can fall back to it. */
242 *b
->cur_node
= b
->node
;
247 crawl_callback(char *folder
, void *baton
)
249 check_folder(folder
, strlen(folder
), baton
);
253 /* Scan folders, returning:
254 * first -- list of nodes for all folders which have desired messages;
255 * if the current folder is listed in .folders, it is also in
256 * the list regardless of whether it has any desired messages
257 * last -- last node in list
258 * cur_node -- node of current folder, if listed in .folders
259 * maxlen -- length of longest folder name
261 * `cur' points to the name of the current folder, `folders' points to the
262 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
263 * the array of sequences for which to look.
265 * An empty list is returned as first=last=NULL.
268 check_folders(struct node
**first
, struct node
**last
,
269 struct node
**cur_node
, size_t *maxlen
,
270 char *cur
, char *folders
, char *sequences
[])
277 *first
= *last
= *cur_node
= NULL
;
281 b
.cur_node
= cur_node
;
284 b
.sequences
= sequences
;
286 if (folders
== NULL
) {
287 if (chdir(m_maildir("")) < 0) {
288 advise (m_maildir(""), "chdir");
290 crawl_folders(".", crawl_callback
, &b
);
292 fp
= fopen(folders
, "r");
294 die("failed to read %s", folders
);
296 while (vfgets(fp
, &line
) == OK
) {
297 len
= strlen(line
) - 1;
299 check_folder(mh_xstrdup(line
), len
, &b
);
304 if (*first
!= NULL
) {
305 b
.node
->n_next
= NULL
;
310 /* Return a single string of the `sequences' joined by a space (' '). */
312 join_sequences(char *sequences
[])
318 for (i
= 0; sequences
[i
] != NULL
; i
++) {
319 len
+= strlen(sequences
[i
]) + 1;
321 result
= mh_xmalloc(len
+ 1);
323 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
324 len
= strlen(sequences
[i
]);
325 memcpy(cp
, sequences
[i
], len
);
328 /* -1 to overwrite the last delimiter */
334 /* Return a struct node for the folder to change to. This is the next
335 * (previous, if RM_FPREV mode) folder with desired messages, or the current
336 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
337 * output but don't change folders.
339 * n_name is the folder to change to, and n_field is the string list of
340 * desired message numbers.
343 doit(char *cur
, char *folders
, char *sequences
[])
345 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
347 int count
, total
= 0;
348 char *command
= NULL
, *sequences_s
= NULL
;
350 if (cur
== NULL
|| cur
[0] == '\0') {
354 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
357 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
359 /* No folders at all... */
362 if (first
->n_next
== NULL
) {
363 /* We have only one node; any desired messages in it? */
364 if (first
->n_field
== NULL
) {
369 if (cur_node
== NULL
) {
370 /* Current folder is not listed in .folders, return first. */
373 } else if (run_mode
== RM_UNSEEN
) {
374 sequences_s
= join_sequences(sequences
);
377 for (node
= first
, prev
= NULL
;
379 prev
= node
, node
= node
->n_next
) {
380 if (run_mode
== RM_FNEXT
) {
381 /* If we have a previous node and it is the current
382 * folder, return this node. */
383 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
386 } else if (run_mode
== RM_FPREV
) {
387 if (strcmp(node
->n_name
, cur
) == 0) {
388 /* Found current folder in fprev mode; if we have a
389 * previous node in the list, return it; else return
395 } else if (run_mode
== RM_UNSEEN
) {
398 if (node
->n_field
== NULL
) {
402 printf("\n%d %s messages in %s",
403 count_messages(node
->n_field
),
406 if (strcmp(node
->n_name
, cur
) == 0) {
407 puts(" (*: current folder)");
413 /* TODO: Split enough of scan.c out so that we can call it here. */
414 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
416 status
= system(command
);
417 if (! WIFEXITED (status
)) {
418 adios (command
, "system");
422 if (node
->n_field
== NULL
) {
426 count
= count_messages(node
->n_field
);
429 printf("%-*s %6d.%c %s\n",
430 (int) folder_len
, node
->n_name
,
432 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
437 /* If we're fnext, we haven't checked the last node yet. If it's the
438 * current folder, return the first node. */
439 if (run_mode
== RM_FNEXT
) {
440 assert(last
!= NULL
);
441 if (strcmp(last
->n_name
, cur
) == 0) {
446 if (run_mode
== RM_NEW
) {
447 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
454 main(int argc
, char **argv
)
456 char **ap
, *cp
, **argp
, **arguments
;
458 char *folders
= NULL
;
459 svector_t sequences
= svector_create (0);
464 if (nmh_init(argv
[0], true, true)) { return 1; }
466 arguments
= getarguments (invo_name
, argc
, argv
, 1);
472 while ((cp
= *argp
++)) {
474 switch (smatch (++cp
, switches
)) {
476 ambigsw (cp
, switches
);
479 die("-%s unknown", cp
);
482 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
484 print_help (help
, switches
, 1);
487 print_version(invo_name
);
491 if (!(folders
= *argp
++) || *folders
== '-')
492 die("missing argument to %s", argp
[-2]);
495 if (!(invo_name
= *argp
++) || *invo_name
== '-')
496 die("missing argument to %s", argp
[-2]);
497 invo_name
= r1bindex(invo_name
, '/');
501 /* have a sequence argument */
502 if (!seq_in_list(cp
, svector_strs (sequences
))) {
503 svector_push_back (sequences
, cp
);
508 if (strcmp(invo_name
, "fnext") == 0) {
510 } else if (strcmp(invo_name
, "fprev") == 0) {
512 } else if (strcmp(invo_name
, "unseen") == 0) {
513 run_mode
= RM_UNSEEN
;
516 if (folders
== NULL
) {
519 if (folders
[0] != '/') {
520 folders
= m_maildir(folders
);
525 /* no sequence arguments; use unseen */
526 unseen
= context_find(usequence
);
527 if (unseen
== NULL
|| unseen
[0] == '\0') {
528 die("must specify sequences or set %s", usequence
);
530 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
531 svector_push_back (sequences
, *ap
);
536 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
537 if (folder
== NULL
) {
542 if (run_mode
== RM_UNSEEN
) {
543 /* All the scan(1)s it runs change the current folder, so we
544 * need to put it back. Unfortunately, context_replace lamely
545 * ignores the new value you give it if it is the same one it
546 * has in memory. So, we'll be lame, too. I'm not sure if i
547 * should just change context_replace... */
548 context_replace(pfolder
, "defeat_context_replace_optimization");
551 /* update current folder */
552 context_replace(pfolder
, folder
->n_name
);
554 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
555 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
560 svector_free (sequences
);