]>
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/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
18 #include "h/crawl_folders.h"
21 #include "sbr/lock_file.h"
22 #include "sbr/m_maildir.h"
24 #define NEW_SWITCHES \
25 X("mode", 1, MODESW) \
26 X("folders", 1, FOLDERSSW) \
27 X("version", 1, VERSIONSW) \
28 X("help", 1, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(NEW
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(NEW
, switches
);
38 /* What to do, based on argv[0]. */
46 /* check_folders uses this to maintain state with both .folders list of
47 * folders and with crawl_folders. */
49 struct node
**first
, **cur_node
;
56 /* Return the number of messages in a string list of message numbers. */
58 count_messages(char *field
)
64 field
= getcpy(field
);
66 /* copied from seq_read.c:seq_init */
67 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
68 if ((cp
= strchr(*ap
, '-')))
70 if ((j
= m_atoi (*ap
)) > 0) {
71 k
= cp
? m_atoi (cp
) : j
;
82 /* Return true if the sequence 'name' is in 'sequences'. */
84 seq_in_list(char *name
, char *sequences
[])
88 for (i
= 0; sequences
[i
] != NULL
; i
++) {
89 if (strcmp(name
, sequences
[i
]) == 0) {
97 /* Return the string list of message numbers from the sequences file, or NULL
100 get_msgnums(char *folder
, char *sequences
[])
102 char *seqfile
= NULL
;
105 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
107 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
108 int failed_to_lock
= 0;
109 m_getfld_state_t gstate
;
111 /* copied from seq_read.c:seq_public */
113 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
114 * the "mh-sequences" profile entry, but left it empty),
115 * then just return, and do not initialize any public sequences.
117 if (mh_seq
== NULL
|| *mh_seq
== '\0')
120 /* get filename of sequence file */
121 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
126 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
128 adios (seqfile
, "failed to lock");
133 /* Use m_getfld2 to scan sequence file */
134 gstate
= m_getfld_state_init(fp
);
136 int fieldsz
= sizeof field
;
137 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
140 if (state
== FLDPLUS
) {
142 while (state
== FLDPLUS
) {
143 fieldsz
= sizeof field
;
144 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
145 cp
= add (field
, cp
);
148 /* Here's where we differ from seq_public: if it's in a
149 * sequence we want, save the list of messages. */
150 if (seq_in_list(name
, sequences
)) {
151 this_msgnums
= trimcpy(cp
);
152 if (msgnums
== NULL
) {
153 msgnums
= this_msgnums
;
155 old_msgnums
= msgnums
;
156 msgnums
= concat(old_msgnums
, " ",
165 if (seq_in_list(name
, sequences
)) {
166 this_msgnums
= trimcpy(field
);
167 if (msgnums
== NULL
) {
168 msgnums
= this_msgnums
;
170 old_msgnums
= msgnums
;
171 msgnums
= concat(old_msgnums
, " ",
182 die("no blank lines are permitted in %s", seqfile
);
189 die("%s is poorly formatted", seqfile
);
191 break; /* break from for loop */
193 m_getfld_state_destroy (&gstate
);
195 lkfclosedata (fp
, seqfile
);
202 /* Check `folder' (of length `len') for interesting messages, filling in the
205 check_folder(char *folder
, size_t len
, struct list_state
*b
)
207 char *msgnums
= get_msgnums(folder
, b
->sequences
);
208 int is_cur
= strcmp(folder
, b
->cur
) == 0;
210 if (is_cur
|| msgnums
!= NULL
) {
211 if (*b
->first
== NULL
) {
215 NEW(b
->node
->n_next
);
216 b
->node
= b
->node
->n_next
;
218 b
->node
->n_name
= folder
;
219 b
->node
->n_field
= msgnums
;
221 if (*b
->maxlen
< len
) {
226 /* Save the node for the current folder, so we can fall back to it. */
228 *b
->cur_node
= b
->node
;
233 crawl_callback(char *folder
, void *baton
)
235 check_folder(folder
, strlen(folder
), baton
);
239 /* Scan folders, returning:
240 * first -- list of nodes for all folders which have desired messages;
241 * if the current folder is listed in .folders, it is also in
242 * the list regardless of whether it has any desired messages
243 * last -- last node in list
244 * cur_node -- node of current folder, if listed in .folders
245 * maxlen -- length of longest folder name
247 * `cur' points to the name of the current folder, `folders' points to the
248 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
249 * the array of sequences for which to look.
251 * An empty list is returned as first=last=NULL.
254 check_folders(struct node
**first
, struct node
**last
,
255 struct node
**cur_node
, size_t *maxlen
,
256 char *cur
, char *folders
, char *sequences
[])
263 *first
= *last
= *cur_node
= NULL
;
267 b
.cur_node
= cur_node
;
270 b
.sequences
= sequences
;
272 if (folders
== NULL
) {
273 if (chdir(m_maildir("")) < 0) {
274 advise (m_maildir(""), "chdir");
276 crawl_folders(".", crawl_callback
, &b
);
278 fp
= fopen(folders
, "r");
280 die("failed to read %s", folders
);
282 while (vfgets(fp
, &line
) == OK
) {
283 len
= strlen(line
) - 1;
285 check_folder(mh_xstrdup(line
), len
, &b
);
290 if (*first
!= NULL
) {
291 b
.node
->n_next
= NULL
;
296 /* Return a single string of the `sequences' joined by a space (' '). */
298 join_sequences(char *sequences
[])
304 for (i
= 0; sequences
[i
] != NULL
; i
++) {
305 len
+= strlen(sequences
[i
]) + 1;
307 result
= mh_xmalloc(len
+ 1);
309 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
310 len
= strlen(sequences
[i
]);
311 memcpy(cp
, sequences
[i
], len
);
314 /* -1 to overwrite the last delimiter */
320 /* Return a struct node for the folder to change to. This is the next
321 * (previous, if RM_FPREV mode) folder with desired messages, or the current
322 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
323 * output but don't change folders.
325 * n_name is the folder to change to, and n_field is the string list of
326 * desired message numbers.
329 doit(char *cur
, char *folders
, char *sequences
[])
331 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
333 int count
, total
= 0;
334 char *command
= NULL
, *sequences_s
= NULL
;
336 if (cur
== NULL
|| cur
[0] == '\0') {
340 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
343 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
345 /* No folders at all... */
348 if (first
->n_next
== NULL
) {
349 /* We have only one node; any desired messages in it? */
350 if (first
->n_field
== NULL
) {
355 if (cur_node
== NULL
) {
356 /* Current folder is not listed in .folders, return first. */
359 } else if (run_mode
== RM_UNSEEN
) {
360 sequences_s
= join_sequences(sequences
);
363 for (node
= first
, prev
= NULL
;
365 prev
= node
, node
= node
->n_next
) {
366 if (run_mode
== RM_FNEXT
) {
367 /* If we have a previous node and it is the current
368 * folder, return this node. */
369 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
372 } else if (run_mode
== RM_FPREV
) {
373 if (strcmp(node
->n_name
, cur
) == 0) {
374 /* Found current folder in fprev mode; if we have a
375 * previous node in the list, return it; else return
381 } else if (run_mode
== RM_UNSEEN
) {
384 if (node
->n_field
== NULL
) {
388 printf("\n%d %s messages in %s",
389 count_messages(node
->n_field
),
392 if (strcmp(node
->n_name
, cur
) == 0) {
393 puts(" (*: current folder)");
399 /* TODO: Split enough of scan.c out so that we can call it here. */
400 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
402 status
= system(command
);
403 if (! WIFEXITED (status
)) {
404 adios (command
, "system");
408 if (node
->n_field
== NULL
) {
412 count
= count_messages(node
->n_field
);
415 printf("%-*s %6d.%c %s\n",
416 (int) folder_len
, node
->n_name
,
418 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
423 /* If we're fnext, we haven't checked the last node yet. If it's the
424 * current folder, return the first node. */
425 if (run_mode
== RM_FNEXT
) {
426 assert(last
!= NULL
);
427 if (strcmp(last
->n_name
, cur
) == 0) {
432 if (run_mode
== RM_NEW
) {
433 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
440 main(int argc
, char **argv
)
442 char **ap
, *cp
, **argp
, **arguments
;
444 char *folders
= NULL
;
445 svector_t sequences
= svector_create (0);
450 if (nmh_init(argv
[0], true, true)) { return 1; }
452 arguments
= getarguments (invo_name
, argc
, argv
, 1);
458 while ((cp
= *argp
++)) {
460 switch (smatch (++cp
, switches
)) {
462 ambigsw (cp
, switches
);
465 die("-%s unknown", cp
);
468 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
470 print_help (help
, switches
, 1);
473 print_version(invo_name
);
477 if (!(folders
= *argp
++) || *folders
== '-')
478 die("missing argument to %s", argp
[-2]);
481 if (!(invo_name
= *argp
++) || *invo_name
== '-')
482 die("missing argument to %s", argp
[-2]);
483 invo_name
= r1bindex(invo_name
, '/');
487 /* have a sequence argument */
488 if (!seq_in_list(cp
, svector_strs (sequences
))) {
489 svector_push_back (sequences
, cp
);
494 if (strcmp(invo_name
, "fnext") == 0) {
496 } else if (strcmp(invo_name
, "fprev") == 0) {
498 } else if (strcmp(invo_name
, "unseen") == 0) {
499 run_mode
= RM_UNSEEN
;
502 if (folders
== NULL
) {
505 if (folders
[0] != '/') {
506 folders
= m_maildir(folders
);
511 /* no sequence arguments; use unseen */
512 unseen
= context_find(usequence
);
513 if (unseen
== NULL
|| unseen
[0] == '\0') {
514 die("must specify sequences or set %s", usequence
);
516 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
517 svector_push_back (sequences
, *ap
);
522 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
523 if (folder
== NULL
) {
528 if (run_mode
== RM_UNSEEN
) {
529 /* All the scan(1)s it runs change the current folder, so we
530 * need to put it back. Unfortunately, context_replace lamely
531 * ignores the new value you give it if it is the same one it
532 * has in memory. So, we'll be lame, too. I'm not sure if i
533 * should just change context_replace... */
534 context_replace(pfolder
, "defeat_context_replace_optimization");
537 /* update current folder */
538 context_replace(pfolder
, folder
->n_name
);
540 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
541 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
546 svector_free (sequences
);