]>
diplodocus.org Git - nmh/blob - uip/new.c
f4e08e7fac35ebe9dfa531c15e30b66ebcc3e764
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/r1bindex.h"
16 #include "sbr/trimcpy.h"
17 #include "sbr/vfgets.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/m_atoi.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/brkstring.h"
24 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
28 #include "h/crawl_folders.h"
31 #include "sbr/lock_file.h"
32 #include "sbr/m_maildir.h"
34 #define NEW_SWITCHES \
35 X("mode", 1, MODESW) \
36 X("folders", 1, FOLDERSSW) \
37 X("version", 1, VERSIONSW) \
38 X("help", 1, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(NEW
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(NEW
, switches
);
48 /* What to do, based on argv[0]. */
56 /* check_folders uses this to maintain state with both .folders list of
57 * folders and with crawl_folders. */
59 struct node
**first
, **cur_node
;
66 /* Return the number of messages in a string list of message numbers. */
68 count_messages(char *field
)
74 field
= getcpy(field
);
76 /* copied from seq_read.c:seq_init */
77 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
78 if ((cp
= strchr(*ap
, '-')))
80 if ((j
= m_atoi (*ap
)) > 0) {
81 k
= cp
? m_atoi (cp
) : j
;
92 /* Return true if the sequence 'name' is in 'sequences'. */
94 seq_in_list(char *name
, char *sequences
[])
98 for (i
= 0; sequences
[i
] != NULL
; i
++) {
99 if (strcmp(name
, sequences
[i
]) == 0) {
107 /* Return the string list of message numbers from the sequences file, or NULL
110 get_msgnums(char *folder
, char *sequences
[])
112 char *seqfile
= NULL
;
115 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
117 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
118 int failed_to_lock
= 0;
119 m_getfld_state_t gstate
;
121 /* copied from seq_read.c:seq_public */
123 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
124 * the "mh-sequences" profile entry, but left it empty),
125 * then just return, and do not initialize any public sequences.
127 if (mh_seq
== NULL
|| *mh_seq
== '\0')
130 /* get filename of sequence file */
131 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
136 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
138 adios (seqfile
, "failed to lock");
143 /* Use m_getfld2 to scan sequence file */
144 gstate
= m_getfld_state_init(fp
);
146 int fieldsz
= sizeof field
;
147 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
150 if (state
== FLDPLUS
) {
152 while (state
== FLDPLUS
) {
153 fieldsz
= sizeof field
;
154 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
155 cp
= add (field
, cp
);
158 /* Here's where we differ from seq_public: if it's in a
159 * sequence we want, save the list of messages. */
160 if (seq_in_list(name
, sequences
)) {
161 this_msgnums
= trimcpy(cp
);
162 if (msgnums
== NULL
) {
163 msgnums
= this_msgnums
;
165 old_msgnums
= msgnums
;
166 msgnums
= concat(old_msgnums
, " ",
175 if (seq_in_list(name
, sequences
)) {
176 this_msgnums
= trimcpy(field
);
177 if (msgnums
== NULL
) {
178 msgnums
= this_msgnums
;
180 old_msgnums
= msgnums
;
181 msgnums
= concat(old_msgnums
, " ",
192 die("no blank lines are permitted in %s", seqfile
);
199 die("%s is poorly formatted", seqfile
);
201 break; /* break from for loop */
203 m_getfld_state_destroy (&gstate
);
205 lkfclosedata (fp
, seqfile
);
212 /* Check `folder' (of length `len') for interesting messages, filling in the
215 check_folder(char *folder
, size_t len
, struct list_state
*b
)
217 char *msgnums
= get_msgnums(folder
, b
->sequences
);
218 int is_cur
= strcmp(folder
, b
->cur
) == 0;
220 if (is_cur
|| msgnums
!= NULL
) {
221 if (*b
->first
== NULL
) {
225 NEW(b
->node
->n_next
);
226 b
->node
= b
->node
->n_next
;
228 b
->node
->n_name
= folder
;
229 b
->node
->n_field
= msgnums
;
231 if (*b
->maxlen
< len
) {
236 /* Save the node for the current folder, so we can fall back to it. */
238 *b
->cur_node
= b
->node
;
243 crawl_callback(char *folder
, void *baton
)
245 check_folder(folder
, strlen(folder
), baton
);
249 /* Scan folders, returning:
250 * first -- list of nodes for all folders which have desired messages;
251 * if the current folder is listed in .folders, it is also in
252 * the list regardless of whether it has any desired messages
253 * last -- last node in list
254 * cur_node -- node of current folder, if listed in .folders
255 * maxlen -- length of longest folder name
257 * `cur' points to the name of the current folder, `folders' points to the
258 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
259 * the array of sequences for which to look.
261 * An empty list is returned as first=last=NULL.
264 check_folders(struct node
**first
, struct node
**last
,
265 struct node
**cur_node
, size_t *maxlen
,
266 char *cur
, char *folders
, char *sequences
[])
273 *first
= *last
= *cur_node
= NULL
;
277 b
.cur_node
= cur_node
;
280 b
.sequences
= sequences
;
282 if (folders
== NULL
) {
283 if (chdir(m_maildir("")) < 0) {
284 advise (m_maildir(""), "chdir");
286 crawl_folders(".", crawl_callback
, &b
);
288 fp
= fopen(folders
, "r");
290 die("failed to read %s", folders
);
292 while (vfgets(fp
, &line
) == OK
) {
293 len
= strlen(line
) - 1;
295 check_folder(mh_xstrdup(line
), len
, &b
);
300 if (*first
!= NULL
) {
301 b
.node
->n_next
= NULL
;
306 /* Return a single string of the `sequences' joined by a space (' '). */
308 join_sequences(char *sequences
[])
314 for (i
= 0; sequences
[i
] != NULL
; i
++) {
315 len
+= strlen(sequences
[i
]) + 1;
317 result
= mh_xmalloc(len
+ 1);
319 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
320 len
= strlen(sequences
[i
]);
321 memcpy(cp
, sequences
[i
], len
);
324 /* -1 to overwrite the last delimiter */
330 /* Return a struct node for the folder to change to. This is the next
331 * (previous, if RM_FPREV mode) folder with desired messages, or the current
332 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
333 * output but don't change folders.
335 * n_name is the folder to change to, and n_field is the string list of
336 * desired message numbers.
339 doit(char *cur
, char *folders
, char *sequences
[])
341 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
343 int count
, total
= 0;
344 char *command
= NULL
, *sequences_s
= NULL
;
346 if (cur
== NULL
|| cur
[0] == '\0') {
350 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
353 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
355 /* No folders at all... */
358 if (first
->n_next
== NULL
) {
359 /* We have only one node; any desired messages in it? */
360 if (first
->n_field
== NULL
) {
365 if (cur_node
== NULL
) {
366 /* Current folder is not listed in .folders, return first. */
369 } else if (run_mode
== RM_UNSEEN
) {
370 sequences_s
= join_sequences(sequences
);
373 for (node
= first
, prev
= NULL
;
375 prev
= node
, node
= node
->n_next
) {
376 if (run_mode
== RM_FNEXT
) {
377 /* If we have a previous node and it is the current
378 * folder, return this node. */
379 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
382 } else if (run_mode
== RM_FPREV
) {
383 if (strcmp(node
->n_name
, cur
) == 0) {
384 /* Found current folder in fprev mode; if we have a
385 * previous node in the list, return it; else return
391 } else if (run_mode
== RM_UNSEEN
) {
394 if (node
->n_field
== NULL
) {
398 printf("\n%d %s messages in %s",
399 count_messages(node
->n_field
),
402 if (strcmp(node
->n_name
, cur
) == 0) {
403 puts(" (*: current folder)");
409 /* TODO: Split enough of scan.c out so that we can call it here. */
410 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
412 status
= system(command
);
413 if (! WIFEXITED (status
)) {
414 adios (command
, "system");
418 if (node
->n_field
== NULL
) {
422 count
= count_messages(node
->n_field
);
425 printf("%-*s %6d.%c %s\n",
426 (int) folder_len
, node
->n_name
,
428 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
433 /* If we're fnext, we haven't checked the last node yet. If it's the
434 * current folder, return the first node. */
435 if (run_mode
== RM_FNEXT
) {
436 assert(last
!= NULL
);
437 if (strcmp(last
->n_name
, cur
) == 0) {
442 if (run_mode
== RM_NEW
) {
443 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
450 main(int argc
, char **argv
)
452 char **ap
, *cp
, **argp
, **arguments
;
454 char *folders
= NULL
;
455 svector_t sequences
= svector_create (0);
460 if (nmh_init(argv
[0], true, true)) { return 1; }
462 arguments
= getarguments (invo_name
, argc
, argv
, 1);
468 while ((cp
= *argp
++)) {
470 switch (smatch (++cp
, switches
)) {
472 ambigsw (cp
, switches
);
475 die("-%s unknown", cp
);
478 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
480 print_help (help
, switches
, 1);
483 print_version(invo_name
);
487 if (!(folders
= *argp
++) || *folders
== '-')
488 die("missing argument to %s", argp
[-2]);
491 if (!(invo_name
= *argp
++) || *invo_name
== '-')
492 die("missing argument to %s", argp
[-2]);
493 invo_name
= r1bindex(invo_name
, '/');
497 /* have a sequence argument */
498 if (!seq_in_list(cp
, svector_strs (sequences
))) {
499 svector_push_back (sequences
, cp
);
504 if (strcmp(invo_name
, "fnext") == 0) {
506 } else if (strcmp(invo_name
, "fprev") == 0) {
508 } else if (strcmp(invo_name
, "unseen") == 0) {
509 run_mode
= RM_UNSEEN
;
512 if (folders
== NULL
) {
515 if (folders
[0] != '/') {
516 folders
= m_maildir(folders
);
521 /* no sequence arguments; use unseen */
522 unseen
= context_find(usequence
);
523 if (unseen
== NULL
|| unseen
[0] == '\0') {
524 die("must specify sequences or set %s", usequence
);
526 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
527 svector_push_back (sequences
, *ap
);
532 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
533 if (folder
== NULL
) {
538 if (run_mode
== RM_UNSEEN
) {
539 /* All the scan(1)s it runs change the current folder, so we
540 * need to put it back. Unfortunately, context_replace lamely
541 * ignores the new value you give it if it is the same one it
542 * has in memory. So, we'll be lame, too. I'm not sure if i
543 * should just change context_replace... */
544 context_replace(pfolder
, "defeat_context_replace_optimization");
547 /* update current folder */
548 context_replace(pfolder
, folder
->n_name
);
550 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
551 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
556 svector_free (sequences
);