]>
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/smatch.h"
16 #include "sbr/r1bindex.h"
17 #include "sbr/trimcpy.h"
18 #include "sbr/vfgets.h"
19 #include "sbr/getcpy.h"
20 #include "sbr/m_atoi.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
29 #include "h/crawl_folders.h"
32 #include "sbr/lock_file.h"
33 #include "sbr/m_maildir.h"
35 #define NEW_SWITCHES \
36 X("mode", 1, MODESW) \
37 X("folders", 1, FOLDERSSW) \
38 X("version", 1, VERSIONSW) \
39 X("help", 1, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(NEW
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(NEW
, switches
);
49 /* What to do, based on argv[0]. */
57 /* check_folders uses this to maintain state with both .folders list of
58 * folders and with crawl_folders. */
60 struct node
**first
, **cur_node
;
67 /* Return the number of messages in a string list of message numbers. */
69 count_messages(char *field
)
75 field
= getcpy(field
);
77 /* copied from seq_read.c:seq_init */
78 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
79 if ((cp
= strchr(*ap
, '-')))
81 if ((j
= m_atoi (*ap
)) > 0) {
82 k
= cp
? m_atoi (cp
) : j
;
93 /* Return true if the sequence 'name' is in 'sequences'. */
95 seq_in_list(char *name
, char *sequences
[])
99 for (i
= 0; sequences
[i
] != NULL
; i
++) {
100 if (strcmp(name
, sequences
[i
]) == 0) {
108 /* Return the string list of message numbers from the sequences file, or NULL
111 get_msgnums(char *folder
, char *sequences
[])
113 char *seqfile
= NULL
;
116 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
118 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
119 int failed_to_lock
= 0;
120 m_getfld_state_t gstate
;
122 /* copied from seq_read.c:seq_public */
124 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
125 * the "mh-sequences" profile entry, but left it empty),
126 * then just return, and do not initialize any public sequences.
128 if (mh_seq
== NULL
|| *mh_seq
== '\0')
131 /* get filename of sequence file */
132 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
137 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
139 adios (seqfile
, "failed to lock");
144 /* Use m_getfld2 to scan sequence file */
145 gstate
= m_getfld_state_init(fp
);
147 int fieldsz
= sizeof field
;
148 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
151 if (state
== FLDPLUS
) {
153 while (state
== FLDPLUS
) {
154 fieldsz
= sizeof field
;
155 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
156 cp
= add (field
, cp
);
159 /* Here's where we differ from seq_public: if it's in a
160 * sequence we want, save the list of messages. */
161 if (seq_in_list(name
, sequences
)) {
162 this_msgnums
= trimcpy(cp
);
163 if (msgnums
== NULL
) {
164 msgnums
= this_msgnums
;
166 old_msgnums
= msgnums
;
167 msgnums
= concat(old_msgnums
, " ",
176 if (seq_in_list(name
, sequences
)) {
177 this_msgnums
= trimcpy(field
);
178 if (msgnums
== NULL
) {
179 msgnums
= this_msgnums
;
181 old_msgnums
= msgnums
;
182 msgnums
= concat(old_msgnums
, " ",
193 die("no blank lines are permitted in %s", seqfile
);
200 die("%s is poorly formatted", seqfile
);
202 break; /* break from for loop */
204 m_getfld_state_destroy (&gstate
);
206 lkfclosedata (fp
, seqfile
);
213 /* Check `folder' (of length `len') for interesting messages, filling in the
216 check_folder(char *folder
, size_t len
, struct list_state
*b
)
218 char *msgnums
= get_msgnums(folder
, b
->sequences
);
219 int is_cur
= strcmp(folder
, b
->cur
) == 0;
221 if (is_cur
|| msgnums
!= NULL
) {
222 if (*b
->first
== NULL
) {
226 NEW(b
->node
->n_next
);
227 b
->node
= b
->node
->n_next
;
229 b
->node
->n_name
= folder
;
230 b
->node
->n_field
= msgnums
;
232 if (*b
->maxlen
< len
) {
237 /* Save the node for the current folder, so we can fall back to it. */
239 *b
->cur_node
= b
->node
;
244 crawl_callback(char *folder
, void *baton
)
246 check_folder(folder
, strlen(folder
), baton
);
250 /* Scan folders, returning:
251 * first -- list of nodes for all folders which have desired messages;
252 * if the current folder is listed in .folders, it is also in
253 * the list regardless of whether it has any desired messages
254 * last -- last node in list
255 * cur_node -- node of current folder, if listed in .folders
256 * maxlen -- length of longest folder name
258 * `cur' points to the name of the current folder, `folders' points to the
259 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
260 * the array of sequences for which to look.
262 * An empty list is returned as first=last=NULL.
265 check_folders(struct node
**first
, struct node
**last
,
266 struct node
**cur_node
, size_t *maxlen
,
267 char *cur
, char *folders
, char *sequences
[])
274 *first
= *last
= *cur_node
= NULL
;
278 b
.cur_node
= cur_node
;
281 b
.sequences
= sequences
;
283 if (folders
== NULL
) {
284 if (chdir(m_maildir("")) < 0) {
285 advise (m_maildir(""), "chdir");
287 crawl_folders(".", crawl_callback
, &b
);
289 fp
= fopen(folders
, "r");
291 die("failed to read %s", folders
);
293 while (vfgets(fp
, &line
) == OK
) {
294 len
= strlen(line
) - 1;
296 check_folder(mh_xstrdup(line
), len
, &b
);
301 if (*first
!= NULL
) {
302 b
.node
->n_next
= NULL
;
307 /* Return a single string of the `sequences' joined by a space (' '). */
309 join_sequences(char *sequences
[])
315 for (i
= 0; sequences
[i
] != NULL
; i
++) {
316 len
+= strlen(sequences
[i
]) + 1;
318 result
= mh_xmalloc(len
+ 1);
320 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
321 len
= strlen(sequences
[i
]);
322 memcpy(cp
, sequences
[i
], len
);
325 /* -1 to overwrite the last delimiter */
331 /* Return a struct node for the folder to change to. This is the next
332 * (previous, if RM_FPREV mode) folder with desired messages, or the current
333 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
334 * output but don't change folders.
336 * n_name is the folder to change to, and n_field is the string list of
337 * desired message numbers.
340 doit(char *cur
, char *folders
, char *sequences
[])
342 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
344 int count
, total
= 0;
345 char *command
= NULL
, *sequences_s
= NULL
;
347 if (cur
== NULL
|| cur
[0] == '\0') {
351 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
354 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
356 /* No folders at all... */
359 if (first
->n_next
== NULL
) {
360 /* We have only one node; any desired messages in it? */
361 if (first
->n_field
== NULL
) {
366 if (cur_node
== NULL
) {
367 /* Current folder is not listed in .folders, return first. */
370 } else if (run_mode
== RM_UNSEEN
) {
371 sequences_s
= join_sequences(sequences
);
374 for (node
= first
, prev
= NULL
;
376 prev
= node
, node
= node
->n_next
) {
377 if (run_mode
== RM_FNEXT
) {
378 /* If we have a previous node and it is the current
379 * folder, return this node. */
380 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
383 } else if (run_mode
== RM_FPREV
) {
384 if (strcmp(node
->n_name
, cur
) == 0) {
385 /* Found current folder in fprev mode; if we have a
386 * previous node in the list, return it; else return
392 } else if (run_mode
== RM_UNSEEN
) {
395 if (node
->n_field
== NULL
) {
399 printf("\n%d %s messages in %s",
400 count_messages(node
->n_field
),
403 if (strcmp(node
->n_name
, cur
) == 0) {
404 puts(" (*: current folder)");
410 /* TODO: Split enough of scan.c out so that we can call it here. */
411 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
413 status
= system(command
);
414 if (! WIFEXITED (status
)) {
415 adios (command
, "system");
419 if (node
->n_field
== NULL
) {
423 count
= count_messages(node
->n_field
);
426 printf("%-*s %6d.%c %s\n",
427 (int) folder_len
, node
->n_name
,
429 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
434 /* If we're fnext, we haven't checked the last node yet. If it's the
435 * current folder, return the first node. */
436 if (run_mode
== RM_FNEXT
) {
437 assert(last
!= NULL
);
438 if (strcmp(last
->n_name
, cur
) == 0) {
443 if (run_mode
== RM_NEW
) {
444 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
451 main(int argc
, char **argv
)
453 char **ap
, *cp
, **argp
, **arguments
;
455 char *folders
= NULL
;
456 svector_t sequences
= svector_create (0);
461 if (nmh_init(argv
[0], true, true)) { return 1; }
463 arguments
= getarguments (invo_name
, argc
, argv
, 1);
469 while ((cp
= *argp
++)) {
471 switch (smatch (++cp
, switches
)) {
473 ambigsw (cp
, switches
);
476 die("-%s unknown", cp
);
479 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
481 print_help (help
, switches
, 1);
484 print_version(invo_name
);
488 if (!(folders
= *argp
++) || *folders
== '-')
489 die("missing argument to %s", argp
[-2]);
492 if (!(invo_name
= *argp
++) || *invo_name
== '-')
493 die("missing argument to %s", argp
[-2]);
494 invo_name
= r1bindex(invo_name
, '/');
498 /* have a sequence argument */
499 if (!seq_in_list(cp
, svector_strs (sequences
))) {
500 svector_push_back (sequences
, cp
);
505 if (strcmp(invo_name
, "fnext") == 0) {
507 } else if (strcmp(invo_name
, "fprev") == 0) {
509 } else if (strcmp(invo_name
, "unseen") == 0) {
510 run_mode
= RM_UNSEEN
;
513 if (folders
== NULL
) {
516 if (folders
[0] != '/') {
517 folders
= m_maildir(folders
);
522 /* no sequence arguments; use unseen */
523 unseen
= context_find(usequence
);
524 if (unseen
== NULL
|| unseen
[0] == '\0') {
525 die("must specify sequences or set %s", usequence
);
527 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
528 svector_push_back (sequences
, *ap
);
533 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
534 if (folder
== NULL
) {
539 if (run_mode
== RM_UNSEEN
) {
540 /* All the scan(1)s it runs change the current folder, so we
541 * need to put it back. Unfortunately, context_replace lamely
542 * ignores the new value you give it if it is the same one it
543 * has in memory. So, we'll be lame, too. I'm not sure if i
544 * should just change context_replace... */
545 context_replace(pfolder
, "defeat_context_replace_optimization");
548 /* update current folder */
549 context_replace(pfolder
, folder
->n_name
);
551 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
552 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
557 svector_free (sequences
);