]>
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/concat.h"
16 #include "sbr/smatch.h"
17 #include "sbr/r1bindex.h"
18 #include "sbr/trimcpy.h"
19 #include "sbr/vfgets.h"
20 #include "sbr/getcpy.h"
21 #include "sbr/m_atoi.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_find.h"
25 #include "sbr/brkstring.h"
26 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/error.h"
30 #include "h/crawl_folders.h"
33 #include "sbr/lock_file.h"
34 #include "sbr/m_maildir.h"
36 #define NEW_SWITCHES \
37 X("mode", 1, MODESW) \
38 X("folders", 1, FOLDERSSW) \
39 X("version", 1, VERSIONSW) \
40 X("help", 1, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(NEW
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(NEW
, switches
);
50 /* What to do, based on argv[0]. */
58 /* check_folders uses this to maintain state with both .folders list of
59 * folders and with crawl_folders. */
61 struct node
**first
, **cur_node
;
68 /* Return the number of messages in a string list of message numbers. */
70 count_messages(char *field
)
76 field
= getcpy(field
);
78 /* copied from seq_read.c:seq_init */
79 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
80 if ((cp
= strchr(*ap
, '-')))
82 if ((j
= m_atoi (*ap
)) > 0) {
83 k
= cp
? m_atoi (cp
) : j
;
94 /* Return true if the sequence 'name' is in 'sequences'. */
96 seq_in_list(char *name
, char *sequences
[])
100 for (i
= 0; sequences
[i
] != NULL
; i
++) {
101 if (strcmp(name
, sequences
[i
]) == 0) {
109 /* Return the string list of message numbers from the sequences file, or NULL
112 get_msgnums(char *folder
, char *sequences
[])
114 char *seqfile
= NULL
;
117 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
119 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
120 int failed_to_lock
= 0;
121 m_getfld_state_t gstate
;
123 /* copied from seq_read.c:seq_public */
125 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
126 * the "mh-sequences" profile entry, but left it empty),
127 * then just return, and do not initialize any public sequences.
129 if (mh_seq
== NULL
|| *mh_seq
== '\0')
132 /* get filename of sequence file */
133 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
138 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
140 adios (seqfile
, "failed to lock");
145 /* Use m_getfld2 to scan sequence file */
146 gstate
= m_getfld_state_init(fp
);
148 int fieldsz
= sizeof field
;
149 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
152 if (state
== FLDPLUS
) {
154 while (state
== FLDPLUS
) {
155 fieldsz
= sizeof field
;
156 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
157 cp
= add (field
, cp
);
160 /* Here's where we differ from seq_public: if it's in a
161 * sequence we want, save the list of messages. */
162 if (seq_in_list(name
, sequences
)) {
163 this_msgnums
= trimcpy(cp
);
164 if (msgnums
== NULL
) {
165 msgnums
= this_msgnums
;
167 old_msgnums
= msgnums
;
168 msgnums
= concat(old_msgnums
, " ",
177 if (seq_in_list(name
, sequences
)) {
178 this_msgnums
= trimcpy(field
);
179 if (msgnums
== NULL
) {
180 msgnums
= this_msgnums
;
182 old_msgnums
= msgnums
;
183 msgnums
= concat(old_msgnums
, " ",
194 die("no blank lines are permitted in %s", seqfile
);
201 die("%s is poorly formatted", seqfile
);
203 break; /* break from for loop */
205 m_getfld_state_destroy (&gstate
);
207 lkfclosedata (fp
, seqfile
);
214 /* Check `folder' (of length `len') for interesting messages, filling in the
217 check_folder(char *folder
, size_t len
, struct list_state
*b
)
219 char *msgnums
= get_msgnums(folder
, b
->sequences
);
220 int is_cur
= strcmp(folder
, b
->cur
) == 0;
222 if (is_cur
|| msgnums
!= NULL
) {
223 if (*b
->first
== NULL
) {
227 NEW(b
->node
->n_next
);
228 b
->node
= b
->node
->n_next
;
230 b
->node
->n_name
= folder
;
231 b
->node
->n_field
= msgnums
;
233 if (*b
->maxlen
< len
) {
238 /* Save the node for the current folder, so we can fall back to it. */
240 *b
->cur_node
= b
->node
;
245 crawl_callback(char *folder
, void *baton
)
247 check_folder(folder
, strlen(folder
), baton
);
251 /* Scan folders, returning:
252 * first -- list of nodes for all folders which have desired messages;
253 * if the current folder is listed in .folders, it is also in
254 * the list regardless of whether it has any desired messages
255 * last -- last node in list
256 * cur_node -- node of current folder, if listed in .folders
257 * maxlen -- length of longest folder name
259 * `cur' points to the name of the current folder, `folders' points to the
260 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
261 * the array of sequences for which to look.
263 * An empty list is returned as first=last=NULL.
266 check_folders(struct node
**first
, struct node
**last
,
267 struct node
**cur_node
, size_t *maxlen
,
268 char *cur
, char *folders
, char *sequences
[])
275 *first
= *last
= *cur_node
= NULL
;
279 b
.cur_node
= cur_node
;
282 b
.sequences
= sequences
;
284 if (folders
== NULL
) {
285 if (chdir(m_maildir("")) < 0) {
286 advise (m_maildir(""), "chdir");
288 crawl_folders(".", crawl_callback
, &b
);
290 fp
= fopen(folders
, "r");
292 die("failed to read %s", folders
);
294 while (vfgets(fp
, &line
) == OK
) {
295 len
= strlen(line
) - 1;
297 check_folder(mh_xstrdup(line
), len
, &b
);
302 if (*first
!= NULL
) {
303 b
.node
->n_next
= NULL
;
308 /* Return a single string of the `sequences' joined by a space (' '). */
310 join_sequences(char *sequences
[])
316 for (i
= 0; sequences
[i
] != NULL
; i
++) {
317 len
+= strlen(sequences
[i
]) + 1;
319 result
= mh_xmalloc(len
+ 1);
321 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
322 len
= strlen(sequences
[i
]);
323 memcpy(cp
, sequences
[i
], len
);
326 /* -1 to overwrite the last delimiter */
332 /* Return a struct node for the folder to change to. This is the next
333 * (previous, if RM_FPREV mode) folder with desired messages, or the current
334 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
335 * output but don't change folders.
337 * n_name is the folder to change to, and n_field is the string list of
338 * desired message numbers.
341 doit(char *cur
, char *folders
, char *sequences
[])
343 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
345 int count
, total
= 0;
346 char *command
= NULL
, *sequences_s
= NULL
;
348 if (cur
== NULL
|| cur
[0] == '\0') {
352 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
355 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
357 /* No folders at all... */
360 if (first
->n_next
== NULL
) {
361 /* We have only one node; any desired messages in it? */
362 if (first
->n_field
== NULL
) {
367 if (cur_node
== NULL
) {
368 /* Current folder is not listed in .folders, return first. */
371 } else if (run_mode
== RM_UNSEEN
) {
372 sequences_s
= join_sequences(sequences
);
375 for (node
= first
, prev
= NULL
;
377 prev
= node
, node
= node
->n_next
) {
378 if (run_mode
== RM_FNEXT
) {
379 /* If we have a previous node and it is the current
380 * folder, return this node. */
381 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
384 } else if (run_mode
== RM_FPREV
) {
385 if (strcmp(node
->n_name
, cur
) == 0) {
386 /* Found current folder in fprev mode; if we have a
387 * previous node in the list, return it; else return
393 } else if (run_mode
== RM_UNSEEN
) {
396 if (node
->n_field
== NULL
) {
400 printf("\n%d %s messages in %s",
401 count_messages(node
->n_field
),
404 if (strcmp(node
->n_name
, cur
) == 0) {
405 puts(" (*: current folder)");
411 /* TODO: Split enough of scan.c out so that we can call it here. */
412 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
414 status
= system(command
);
415 if (! WIFEXITED (status
)) {
416 adios (command
, "system");
420 if (node
->n_field
== NULL
) {
424 count
= count_messages(node
->n_field
);
427 printf("%-*s %6d.%c %s\n",
428 (int) folder_len
, node
->n_name
,
430 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
435 /* If we're fnext, we haven't checked the last node yet. If it's the
436 * current folder, return the first node. */
437 if (run_mode
== RM_FNEXT
) {
438 assert(last
!= NULL
);
439 if (strcmp(last
->n_name
, cur
) == 0) {
444 if (run_mode
== RM_NEW
) {
445 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
452 main(int argc
, char **argv
)
454 char **ap
, *cp
, **argp
, **arguments
;
456 char *folders
= NULL
;
457 svector_t sequences
= svector_create (0);
462 if (nmh_init(argv
[0], true, true)) { return 1; }
464 arguments
= getarguments (invo_name
, argc
, argv
, 1);
470 while ((cp
= *argp
++)) {
472 switch (smatch (++cp
, switches
)) {
474 ambigsw (cp
, switches
);
477 die("-%s unknown", cp
);
480 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
482 print_help (help
, switches
, 1);
485 print_version(invo_name
);
489 if (!(folders
= *argp
++) || *folders
== '-')
490 die("missing argument to %s", argp
[-2]);
493 if (!(invo_name
= *argp
++) || *invo_name
== '-')
494 die("missing argument to %s", argp
[-2]);
495 invo_name
= r1bindex(invo_name
, '/');
499 /* have a sequence argument */
500 if (!seq_in_list(cp
, svector_strs (sequences
))) {
501 svector_push_back (sequences
, cp
);
506 if (strcmp(invo_name
, "fnext") == 0) {
508 } else if (strcmp(invo_name
, "fprev") == 0) {
510 } else if (strcmp(invo_name
, "unseen") == 0) {
511 run_mode
= RM_UNSEEN
;
514 if (folders
== NULL
) {
517 if (folders
[0] != '/') {
518 folders
= m_maildir(folders
);
523 /* no sequence arguments; use unseen */
524 unseen
= context_find(usequence
);
525 if (unseen
== NULL
|| unseen
[0] == '\0') {
526 die("must specify sequences or set %s", usequence
);
528 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
529 svector_push_back (sequences
, *ap
);
534 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
535 if (folder
== NULL
) {
540 if (run_mode
== RM_UNSEEN
) {
541 /* All the scan(1)s it runs change the current folder, so we
542 * need to put it back. Unfortunately, context_replace lamely
543 * ignores the new value you give it if it is the same one it
544 * has in memory. So, we'll be lame, too. I'm not sure if i
545 * should just change context_replace... */
546 context_replace(pfolder
, "defeat_context_replace_optimization");
549 /* update current folder */
550 context_replace(pfolder
, folder
->n_name
);
552 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
553 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
558 svector_free (sequences
);