]>
diplodocus.org Git - nmh/blob - uip/new.c
2fa483787be43606ce0b1efc8b75bc59daa985ee
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_help.h"
16 #include "sbr/error.h"
17 #include "h/crawl_folders.h"
20 #include "sbr/lock_file.h"
21 #include "sbr/m_maildir.h"
23 #define NEW_SWITCHES \
24 X("mode", 1, MODESW) \
25 X("folders", 1, FOLDERSSW) \
26 X("version", 1, VERSIONSW) \
27 X("help", 1, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(NEW
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(NEW
, switches
);
37 /* What to do, based on argv[0]. */
45 /* check_folders uses this to maintain state with both .folders list of
46 * folders and with crawl_folders. */
48 struct node
**first
, **cur_node
;
55 /* Return the number of messages in a string list of message numbers. */
57 count_messages(char *field
)
63 field
= getcpy(field
);
65 /* copied from seq_read.c:seq_init */
66 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
67 if ((cp
= strchr(*ap
, '-')))
69 if ((j
= m_atoi (*ap
)) > 0) {
70 k
= cp
? m_atoi (cp
) : j
;
81 /* Return true if the sequence 'name' is in 'sequences'. */
83 seq_in_list(char *name
, char *sequences
[])
87 for (i
= 0; sequences
[i
] != NULL
; i
++) {
88 if (strcmp(name
, sequences
[i
]) == 0) {
96 /* Return the string list of message numbers from the sequences file, or NULL
99 get_msgnums(char *folder
, char *sequences
[])
101 char *seqfile
= NULL
;
104 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
106 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
107 int failed_to_lock
= 0;
108 m_getfld_state_t gstate
;
110 /* copied from seq_read.c:seq_public */
112 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
113 * the "mh-sequences" profile entry, but left it empty),
114 * then just return, and do not initialize any public sequences.
116 if (mh_seq
== NULL
|| *mh_seq
== '\0')
119 /* get filename of sequence file */
120 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
125 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
127 adios (seqfile
, "failed to lock");
132 /* Use m_getfld2 to scan sequence file */
133 gstate
= m_getfld_state_init(fp
);
135 int fieldsz
= sizeof field
;
136 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
139 if (state
== FLDPLUS
) {
141 while (state
== FLDPLUS
) {
142 fieldsz
= sizeof field
;
143 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
144 cp
= add (field
, cp
);
147 /* Here's where we differ from seq_public: if it's in a
148 * sequence we want, save the list of messages. */
149 if (seq_in_list(name
, sequences
)) {
150 this_msgnums
= trimcpy(cp
);
151 if (msgnums
== NULL
) {
152 msgnums
= this_msgnums
;
154 old_msgnums
= msgnums
;
155 msgnums
= concat(old_msgnums
, " ",
164 if (seq_in_list(name
, sequences
)) {
165 this_msgnums
= trimcpy(field
);
166 if (msgnums
== NULL
) {
167 msgnums
= this_msgnums
;
169 old_msgnums
= msgnums
;
170 msgnums
= concat(old_msgnums
, " ",
181 die("no blank lines are permitted in %s", seqfile
);
188 die("%s is poorly formatted", seqfile
);
190 break; /* break from for loop */
192 m_getfld_state_destroy (&gstate
);
194 lkfclosedata (fp
, seqfile
);
201 /* Check `folder' (of length `len') for interesting messages, filling in the
204 check_folder(char *folder
, size_t len
, struct list_state
*b
)
206 char *msgnums
= get_msgnums(folder
, b
->sequences
);
207 int is_cur
= strcmp(folder
, b
->cur
) == 0;
209 if (is_cur
|| msgnums
!= NULL
) {
210 if (*b
->first
== NULL
) {
214 NEW(b
->node
->n_next
);
215 b
->node
= b
->node
->n_next
;
217 b
->node
->n_name
= folder
;
218 b
->node
->n_field
= msgnums
;
220 if (*b
->maxlen
< len
) {
225 /* Save the node for the current folder, so we can fall back to it. */
227 *b
->cur_node
= b
->node
;
232 crawl_callback(char *folder
, void *baton
)
234 check_folder(folder
, strlen(folder
), baton
);
238 /* Scan folders, returning:
239 * first -- list of nodes for all folders which have desired messages;
240 * if the current folder is listed in .folders, it is also in
241 * the list regardless of whether it has any desired messages
242 * last -- last node in list
243 * cur_node -- node of current folder, if listed in .folders
244 * maxlen -- length of longest folder name
246 * `cur' points to the name of the current folder, `folders' points to the
247 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
248 * the array of sequences for which to look.
250 * An empty list is returned as first=last=NULL.
253 check_folders(struct node
**first
, struct node
**last
,
254 struct node
**cur_node
, size_t *maxlen
,
255 char *cur
, char *folders
, char *sequences
[])
262 *first
= *last
= *cur_node
= NULL
;
266 b
.cur_node
= cur_node
;
269 b
.sequences
= sequences
;
271 if (folders
== NULL
) {
272 if (chdir(m_maildir("")) < 0) {
273 advise (m_maildir(""), "chdir");
275 crawl_folders(".", crawl_callback
, &b
);
277 fp
= fopen(folders
, "r");
279 die("failed to read %s", folders
);
281 while (vfgets(fp
, &line
) == OK
) {
282 len
= strlen(line
) - 1;
284 check_folder(mh_xstrdup(line
), len
, &b
);
289 if (*first
!= NULL
) {
290 b
.node
->n_next
= NULL
;
295 /* Return a single string of the `sequences' joined by a space (' '). */
297 join_sequences(char *sequences
[])
303 for (i
= 0; sequences
[i
] != NULL
; i
++) {
304 len
+= strlen(sequences
[i
]) + 1;
306 result
= mh_xmalloc(len
+ 1);
308 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
309 len
= strlen(sequences
[i
]);
310 memcpy(cp
, sequences
[i
], len
);
313 /* -1 to overwrite the last delimiter */
319 /* Return a struct node for the folder to change to. This is the next
320 * (previous, if RM_FPREV mode) folder with desired messages, or the current
321 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
322 * output but don't change folders.
324 * n_name is the folder to change to, and n_field is the string list of
325 * desired message numbers.
328 doit(char *cur
, char *folders
, char *sequences
[])
330 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
332 int count
, total
= 0;
333 char *command
= NULL
, *sequences_s
= NULL
;
335 if (cur
== NULL
|| cur
[0] == '\0') {
339 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
342 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
344 /* No folders at all... */
347 if (first
->n_next
== NULL
) {
348 /* We have only one node; any desired messages in it? */
349 if (first
->n_field
== NULL
) {
354 if (cur_node
== NULL
) {
355 /* Current folder is not listed in .folders, return first. */
358 } else if (run_mode
== RM_UNSEEN
) {
359 sequences_s
= join_sequences(sequences
);
362 for (node
= first
, prev
= NULL
;
364 prev
= node
, node
= node
->n_next
) {
365 if (run_mode
== RM_FNEXT
) {
366 /* If we have a previous node and it is the current
367 * folder, return this node. */
368 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
371 } else if (run_mode
== RM_FPREV
) {
372 if (strcmp(node
->n_name
, cur
) == 0) {
373 /* Found current folder in fprev mode; if we have a
374 * previous node in the list, return it; else return
380 } else if (run_mode
== RM_UNSEEN
) {
383 if (node
->n_field
== NULL
) {
387 printf("\n%d %s messages in %s",
388 count_messages(node
->n_field
),
391 if (strcmp(node
->n_name
, cur
) == 0) {
392 puts(" (*: current folder)");
398 /* TODO: Split enough of scan.c out so that we can call it here. */
399 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
401 status
= system(command
);
402 if (! WIFEXITED (status
)) {
403 adios (command
, "system");
407 if (node
->n_field
== NULL
) {
411 count
= count_messages(node
->n_field
);
414 printf("%-*s %6d.%c %s\n",
415 (int) folder_len
, node
->n_name
,
417 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
422 /* If we're fnext, we haven't checked the last node yet. If it's the
423 * current folder, return the first node. */
424 if (run_mode
== RM_FNEXT
) {
425 assert(last
!= NULL
);
426 if (strcmp(last
->n_name
, cur
) == 0) {
431 if (run_mode
== RM_NEW
) {
432 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
439 main(int argc
, char **argv
)
441 char **ap
, *cp
, **argp
, **arguments
;
443 char *folders
= NULL
;
444 svector_t sequences
= svector_create (0);
449 if (nmh_init(argv
[0], true, true)) { return 1; }
451 arguments
= getarguments (invo_name
, argc
, argv
, 1);
457 while ((cp
= *argp
++)) {
459 switch (smatch (++cp
, switches
)) {
461 ambigsw (cp
, switches
);
464 die("-%s unknown", cp
);
467 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
469 print_help (help
, switches
, 1);
472 print_version(invo_name
);
476 if (!(folders
= *argp
++) || *folders
== '-')
477 die("missing argument to %s", argp
[-2]);
480 if (!(invo_name
= *argp
++) || *invo_name
== '-')
481 die("missing argument to %s", argp
[-2]);
482 invo_name
= r1bindex(invo_name
, '/');
486 /* have a sequence argument */
487 if (!seq_in_list(cp
, svector_strs (sequences
))) {
488 svector_push_back (sequences
, cp
);
493 if (strcmp(invo_name
, "fnext") == 0) {
495 } else if (strcmp(invo_name
, "fprev") == 0) {
497 } else if (strcmp(invo_name
, "unseen") == 0) {
498 run_mode
= RM_UNSEEN
;
501 if (folders
== NULL
) {
504 if (folders
[0] != '/') {
505 folders
= m_maildir(folders
);
510 /* no sequence arguments; use unseen */
511 unseen
= context_find(usequence
);
512 if (unseen
== NULL
|| unseen
[0] == '\0') {
513 die("must specify sequences or set %s", usequence
);
515 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
516 svector_push_back (sequences
, *ap
);
521 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
522 if (folder
== NULL
) {
527 if (run_mode
== RM_UNSEEN
) {
528 /* All the scan(1)s it runs change the current folder, so we
529 * need to put it back. Unfortunately, context_replace lamely
530 * ignores the new value you give it if it is the same one it
531 * has in memory. So, we'll be lame, too. I'm not sure if i
532 * should just change context_replace... */
533 context_replace(pfolder
, "defeat_context_replace_optimization");
536 /* update current folder */
537 context_replace(pfolder
, folder
->n_name
);
539 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
540 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
545 svector_free (sequences
);