]>
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/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/brkstring.h"
19 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/error.h"
23 #include "h/crawl_folders.h"
26 #include "sbr/lock_file.h"
27 #include "sbr/m_maildir.h"
29 #define NEW_SWITCHES \
30 X("mode", 1, MODESW) \
31 X("folders", 1, FOLDERSSW) \
32 X("version", 1, VERSIONSW) \
33 X("help", 1, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(NEW
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(NEW
, switches
);
43 /* What to do, based on argv[0]. */
51 /* check_folders uses this to maintain state with both .folders list of
52 * folders and with crawl_folders. */
54 struct node
**first
, **cur_node
;
61 /* Return the number of messages in a string list of message numbers. */
63 count_messages(char *field
)
69 field
= getcpy(field
);
71 /* copied from seq_read.c:seq_init */
72 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
73 if ((cp
= strchr(*ap
, '-')))
75 if ((j
= m_atoi (*ap
)) > 0) {
76 k
= cp
? m_atoi (cp
) : j
;
87 /* Return true if the sequence 'name' is in 'sequences'. */
89 seq_in_list(char *name
, char *sequences
[])
93 for (i
= 0; sequences
[i
] != NULL
; i
++) {
94 if (strcmp(name
, sequences
[i
]) == 0) {
102 /* Return the string list of message numbers from the sequences file, or NULL
105 get_msgnums(char *folder
, char *sequences
[])
107 char *seqfile
= NULL
;
110 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
112 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
113 int failed_to_lock
= 0;
114 m_getfld_state_t gstate
;
116 /* copied from seq_read.c:seq_public */
118 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
119 * the "mh-sequences" profile entry, but left it empty),
120 * then just return, and do not initialize any public sequences.
122 if (mh_seq
== NULL
|| *mh_seq
== '\0')
125 /* get filename of sequence file */
126 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
131 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
133 adios (seqfile
, "failed to lock");
138 /* Use m_getfld2 to scan sequence file */
139 gstate
= m_getfld_state_init(fp
);
141 int fieldsz
= sizeof field
;
142 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
145 if (state
== FLDPLUS
) {
147 while (state
== FLDPLUS
) {
148 fieldsz
= sizeof field
;
149 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
150 cp
= add (field
, cp
);
153 /* Here's where we differ from seq_public: if it's in a
154 * sequence we want, save the list of messages. */
155 if (seq_in_list(name
, sequences
)) {
156 this_msgnums
= trimcpy(cp
);
157 if (msgnums
== NULL
) {
158 msgnums
= this_msgnums
;
160 old_msgnums
= msgnums
;
161 msgnums
= concat(old_msgnums
, " ",
170 if (seq_in_list(name
, sequences
)) {
171 this_msgnums
= trimcpy(field
);
172 if (msgnums
== NULL
) {
173 msgnums
= this_msgnums
;
175 old_msgnums
= msgnums
;
176 msgnums
= concat(old_msgnums
, " ",
187 die("no blank lines are permitted in %s", seqfile
);
194 die("%s is poorly formatted", seqfile
);
196 break; /* break from for loop */
198 m_getfld_state_destroy (&gstate
);
200 lkfclosedata (fp
, seqfile
);
207 /* Check `folder' (of length `len') for interesting messages, filling in the
210 check_folder(char *folder
, size_t len
, struct list_state
*b
)
212 char *msgnums
= get_msgnums(folder
, b
->sequences
);
213 int is_cur
= strcmp(folder
, b
->cur
) == 0;
215 if (is_cur
|| msgnums
!= NULL
) {
216 if (*b
->first
== NULL
) {
220 NEW(b
->node
->n_next
);
221 b
->node
= b
->node
->n_next
;
223 b
->node
->n_name
= folder
;
224 b
->node
->n_field
= msgnums
;
226 if (*b
->maxlen
< len
) {
231 /* Save the node for the current folder, so we can fall back to it. */
233 *b
->cur_node
= b
->node
;
238 crawl_callback(char *folder
, void *baton
)
240 check_folder(folder
, strlen(folder
), baton
);
244 /* Scan folders, returning:
245 * first -- list of nodes for all folders which have desired messages;
246 * if the current folder is listed in .folders, it is also in
247 * the list regardless of whether it has any desired messages
248 * last -- last node in list
249 * cur_node -- node of current folder, if listed in .folders
250 * maxlen -- length of longest folder name
252 * `cur' points to the name of the current folder, `folders' points to the
253 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
254 * the array of sequences for which to look.
256 * An empty list is returned as first=last=NULL.
259 check_folders(struct node
**first
, struct node
**last
,
260 struct node
**cur_node
, size_t *maxlen
,
261 char *cur
, char *folders
, char *sequences
[])
268 *first
= *last
= *cur_node
= NULL
;
272 b
.cur_node
= cur_node
;
275 b
.sequences
= sequences
;
277 if (folders
== NULL
) {
278 if (chdir(m_maildir("")) < 0) {
279 advise (m_maildir(""), "chdir");
281 crawl_folders(".", crawl_callback
, &b
);
283 fp
= fopen(folders
, "r");
285 die("failed to read %s", folders
);
287 while (vfgets(fp
, &line
) == OK
) {
288 len
= strlen(line
) - 1;
290 check_folder(mh_xstrdup(line
), len
, &b
);
295 if (*first
!= NULL
) {
296 b
.node
->n_next
= NULL
;
301 /* Return a single string of the `sequences' joined by a space (' '). */
303 join_sequences(char *sequences
[])
309 for (i
= 0; sequences
[i
] != NULL
; i
++) {
310 len
+= strlen(sequences
[i
]) + 1;
312 result
= mh_xmalloc(len
+ 1);
314 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
315 len
= strlen(sequences
[i
]);
316 memcpy(cp
, sequences
[i
], len
);
319 /* -1 to overwrite the last delimiter */
325 /* Return a struct node for the folder to change to. This is the next
326 * (previous, if RM_FPREV mode) folder with desired messages, or the current
327 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
328 * output but don't change folders.
330 * n_name is the folder to change to, and n_field is the string list of
331 * desired message numbers.
334 doit(char *cur
, char *folders
, char *sequences
[])
336 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
338 int count
, total
= 0;
339 char *command
= NULL
, *sequences_s
= NULL
;
341 if (cur
== NULL
|| cur
[0] == '\0') {
345 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
348 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
350 /* No folders at all... */
353 if (first
->n_next
== NULL
) {
354 /* We have only one node; any desired messages in it? */
355 if (first
->n_field
== NULL
) {
360 if (cur_node
== NULL
) {
361 /* Current folder is not listed in .folders, return first. */
364 } else if (run_mode
== RM_UNSEEN
) {
365 sequences_s
= join_sequences(sequences
);
368 for (node
= first
, prev
= NULL
;
370 prev
= node
, node
= node
->n_next
) {
371 if (run_mode
== RM_FNEXT
) {
372 /* If we have a previous node and it is the current
373 * folder, return this node. */
374 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
377 } else if (run_mode
== RM_FPREV
) {
378 if (strcmp(node
->n_name
, cur
) == 0) {
379 /* Found current folder in fprev mode; if we have a
380 * previous node in the list, return it; else return
386 } else if (run_mode
== RM_UNSEEN
) {
389 if (node
->n_field
== NULL
) {
393 printf("\n%d %s messages in %s",
394 count_messages(node
->n_field
),
397 if (strcmp(node
->n_name
, cur
) == 0) {
398 puts(" (*: current folder)");
404 /* TODO: Split enough of scan.c out so that we can call it here. */
405 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
407 status
= system(command
);
408 if (! WIFEXITED (status
)) {
409 adios (command
, "system");
413 if (node
->n_field
== NULL
) {
417 count
= count_messages(node
->n_field
);
420 printf("%-*s %6d.%c %s\n",
421 (int) folder_len
, node
->n_name
,
423 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
428 /* If we're fnext, we haven't checked the last node yet. If it's the
429 * current folder, return the first node. */
430 if (run_mode
== RM_FNEXT
) {
431 assert(last
!= NULL
);
432 if (strcmp(last
->n_name
, cur
) == 0) {
437 if (run_mode
== RM_NEW
) {
438 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
445 main(int argc
, char **argv
)
447 char **ap
, *cp
, **argp
, **arguments
;
449 char *folders
= NULL
;
450 svector_t sequences
= svector_create (0);
455 if (nmh_init(argv
[0], true, true)) { return 1; }
457 arguments
= getarguments (invo_name
, argc
, argv
, 1);
463 while ((cp
= *argp
++)) {
465 switch (smatch (++cp
, switches
)) {
467 ambigsw (cp
, switches
);
470 die("-%s unknown", cp
);
473 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
475 print_help (help
, switches
, 1);
478 print_version(invo_name
);
482 if (!(folders
= *argp
++) || *folders
== '-')
483 die("missing argument to %s", argp
[-2]);
486 if (!(invo_name
= *argp
++) || *invo_name
== '-')
487 die("missing argument to %s", argp
[-2]);
488 invo_name
= r1bindex(invo_name
, '/');
492 /* have a sequence argument */
493 if (!seq_in_list(cp
, svector_strs (sequences
))) {
494 svector_push_back (sequences
, cp
);
499 if (strcmp(invo_name
, "fnext") == 0) {
501 } else if (strcmp(invo_name
, "fprev") == 0) {
503 } else if (strcmp(invo_name
, "unseen") == 0) {
504 run_mode
= RM_UNSEEN
;
507 if (folders
== NULL
) {
510 if (folders
[0] != '/') {
511 folders
= m_maildir(folders
);
516 /* no sequence arguments; use unseen */
517 unseen
= context_find(usequence
);
518 if (unseen
== NULL
|| unseen
[0] == '\0') {
519 die("must specify sequences or set %s", usequence
);
521 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
522 svector_push_back (sequences
, *ap
);
527 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
528 if (folder
== NULL
) {
533 if (run_mode
== RM_UNSEEN
) {
534 /* All the scan(1)s it runs change the current folder, so we
535 * need to put it back. Unfortunately, context_replace lamely
536 * ignores the new value you give it if it is the same one it
537 * has in memory. So, we'll be lame, too. I'm not sure if i
538 * should just change context_replace... */
539 context_replace(pfolder
, "defeat_context_replace_optimization");
542 /* update current folder */
543 context_replace(pfolder
, folder
->n_name
);
545 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
546 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
551 svector_free (sequences
);