]>
diplodocus.org Git - nmh/blob - uip/new.c
5f918da11bbe78e6c56a7991a126494e80600eeb
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/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
19 #include "h/crawl_folders.h"
22 #include "sbr/lock_file.h"
23 #include "sbr/m_maildir.h"
25 #define NEW_SWITCHES \
26 X("mode", 1, MODESW) \
27 X("folders", 1, FOLDERSSW) \
28 X("version", 1, VERSIONSW) \
29 X("help", 1, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(NEW
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(NEW
, switches
);
39 /* What to do, based on argv[0]. */
47 /* check_folders uses this to maintain state with both .folders list of
48 * folders and with crawl_folders. */
50 struct node
**first
, **cur_node
;
57 /* Return the number of messages in a string list of message numbers. */
59 count_messages(char *field
)
65 field
= getcpy(field
);
67 /* copied from seq_read.c:seq_init */
68 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
69 if ((cp
= strchr(*ap
, '-')))
71 if ((j
= m_atoi (*ap
)) > 0) {
72 k
= cp
? m_atoi (cp
) : j
;
83 /* Return true if the sequence 'name' is in 'sequences'. */
85 seq_in_list(char *name
, char *sequences
[])
89 for (i
= 0; sequences
[i
] != NULL
; i
++) {
90 if (strcmp(name
, sequences
[i
]) == 0) {
98 /* Return the string list of message numbers from the sequences file, or NULL
101 get_msgnums(char *folder
, char *sequences
[])
103 char *seqfile
= NULL
;
106 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
108 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
109 int failed_to_lock
= 0;
110 m_getfld_state_t gstate
;
112 /* copied from seq_read.c:seq_public */
114 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
115 * the "mh-sequences" profile entry, but left it empty),
116 * then just return, and do not initialize any public sequences.
118 if (mh_seq
== NULL
|| *mh_seq
== '\0')
121 /* get filename of sequence file */
122 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
127 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
129 adios (seqfile
, "failed to lock");
134 /* Use m_getfld2 to scan sequence file */
135 gstate
= m_getfld_state_init(fp
);
137 int fieldsz
= sizeof field
;
138 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
141 if (state
== FLDPLUS
) {
143 while (state
== FLDPLUS
) {
144 fieldsz
= sizeof field
;
145 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
146 cp
= add (field
, cp
);
149 /* Here's where we differ from seq_public: if it's in a
150 * sequence we want, save the list of messages. */
151 if (seq_in_list(name
, sequences
)) {
152 this_msgnums
= trimcpy(cp
);
153 if (msgnums
== NULL
) {
154 msgnums
= this_msgnums
;
156 old_msgnums
= msgnums
;
157 msgnums
= concat(old_msgnums
, " ",
166 if (seq_in_list(name
, sequences
)) {
167 this_msgnums
= trimcpy(field
);
168 if (msgnums
== NULL
) {
169 msgnums
= this_msgnums
;
171 old_msgnums
= msgnums
;
172 msgnums
= concat(old_msgnums
, " ",
183 die("no blank lines are permitted in %s", seqfile
);
190 die("%s is poorly formatted", seqfile
);
192 break; /* break from for loop */
194 m_getfld_state_destroy (&gstate
);
196 lkfclosedata (fp
, seqfile
);
203 /* Check `folder' (of length `len') for interesting messages, filling in the
206 check_folder(char *folder
, size_t len
, struct list_state
*b
)
208 char *msgnums
= get_msgnums(folder
, b
->sequences
);
209 int is_cur
= strcmp(folder
, b
->cur
) == 0;
211 if (is_cur
|| msgnums
!= NULL
) {
212 if (*b
->first
== NULL
) {
216 NEW(b
->node
->n_next
);
217 b
->node
= b
->node
->n_next
;
219 b
->node
->n_name
= folder
;
220 b
->node
->n_field
= msgnums
;
222 if (*b
->maxlen
< len
) {
227 /* Save the node for the current folder, so we can fall back to it. */
229 *b
->cur_node
= b
->node
;
234 crawl_callback(char *folder
, void *baton
)
236 check_folder(folder
, strlen(folder
), baton
);
240 /* Scan folders, returning:
241 * first -- list of nodes for all folders which have desired messages;
242 * if the current folder is listed in .folders, it is also in
243 * the list regardless of whether it has any desired messages
244 * last -- last node in list
245 * cur_node -- node of current folder, if listed in .folders
246 * maxlen -- length of longest folder name
248 * `cur' points to the name of the current folder, `folders' points to the
249 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
250 * the array of sequences for which to look.
252 * An empty list is returned as first=last=NULL.
255 check_folders(struct node
**first
, struct node
**last
,
256 struct node
**cur_node
, size_t *maxlen
,
257 char *cur
, char *folders
, char *sequences
[])
264 *first
= *last
= *cur_node
= NULL
;
268 b
.cur_node
= cur_node
;
271 b
.sequences
= sequences
;
273 if (folders
== NULL
) {
274 if (chdir(m_maildir("")) < 0) {
275 advise (m_maildir(""), "chdir");
277 crawl_folders(".", crawl_callback
, &b
);
279 fp
= fopen(folders
, "r");
281 die("failed to read %s", folders
);
283 while (vfgets(fp
, &line
) == OK
) {
284 len
= strlen(line
) - 1;
286 check_folder(mh_xstrdup(line
), len
, &b
);
291 if (*first
!= NULL
) {
292 b
.node
->n_next
= NULL
;
297 /* Return a single string of the `sequences' joined by a space (' '). */
299 join_sequences(char *sequences
[])
305 for (i
= 0; sequences
[i
] != NULL
; i
++) {
306 len
+= strlen(sequences
[i
]) + 1;
308 result
= mh_xmalloc(len
+ 1);
310 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
311 len
= strlen(sequences
[i
]);
312 memcpy(cp
, sequences
[i
], len
);
315 /* -1 to overwrite the last delimiter */
321 /* Return a struct node for the folder to change to. This is the next
322 * (previous, if RM_FPREV mode) folder with desired messages, or the current
323 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
324 * output but don't change folders.
326 * n_name is the folder to change to, and n_field is the string list of
327 * desired message numbers.
330 doit(char *cur
, char *folders
, char *sequences
[])
332 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
334 int count
, total
= 0;
335 char *command
= NULL
, *sequences_s
= NULL
;
337 if (cur
== NULL
|| cur
[0] == '\0') {
341 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
344 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
346 /* No folders at all... */
349 if (first
->n_next
== NULL
) {
350 /* We have only one node; any desired messages in it? */
351 if (first
->n_field
== NULL
) {
356 if (cur_node
== NULL
) {
357 /* Current folder is not listed in .folders, return first. */
360 } else if (run_mode
== RM_UNSEEN
) {
361 sequences_s
= join_sequences(sequences
);
364 for (node
= first
, prev
= NULL
;
366 prev
= node
, node
= node
->n_next
) {
367 if (run_mode
== RM_FNEXT
) {
368 /* If we have a previous node and it is the current
369 * folder, return this node. */
370 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
373 } else if (run_mode
== RM_FPREV
) {
374 if (strcmp(node
->n_name
, cur
) == 0) {
375 /* Found current folder in fprev mode; if we have a
376 * previous node in the list, return it; else return
382 } else if (run_mode
== RM_UNSEEN
) {
385 if (node
->n_field
== NULL
) {
389 printf("\n%d %s messages in %s",
390 count_messages(node
->n_field
),
393 if (strcmp(node
->n_name
, cur
) == 0) {
394 puts(" (*: current folder)");
400 /* TODO: Split enough of scan.c out so that we can call it here. */
401 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
403 status
= system(command
);
404 if (! WIFEXITED (status
)) {
405 adios (command
, "system");
409 if (node
->n_field
== NULL
) {
413 count
= count_messages(node
->n_field
);
416 printf("%-*s %6d.%c %s\n",
417 (int) folder_len
, node
->n_name
,
419 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
424 /* If we're fnext, we haven't checked the last node yet. If it's the
425 * current folder, return the first node. */
426 if (run_mode
== RM_FNEXT
) {
427 assert(last
!= NULL
);
428 if (strcmp(last
->n_name
, cur
) == 0) {
433 if (run_mode
== RM_NEW
) {
434 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
441 main(int argc
, char **argv
)
443 char **ap
, *cp
, **argp
, **arguments
;
445 char *folders
= NULL
;
446 svector_t sequences
= svector_create (0);
451 if (nmh_init(argv
[0], true, true)) { return 1; }
453 arguments
= getarguments (invo_name
, argc
, argv
, 1);
459 while ((cp
= *argp
++)) {
461 switch (smatch (++cp
, switches
)) {
463 ambigsw (cp
, switches
);
466 die("-%s unknown", cp
);
469 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
471 print_help (help
, switches
, 1);
474 print_version(invo_name
);
478 if (!(folders
= *argp
++) || *folders
== '-')
479 die("missing argument to %s", argp
[-2]);
482 if (!(invo_name
= *argp
++) || *invo_name
== '-')
483 die("missing argument to %s", argp
[-2]);
484 invo_name
= r1bindex(invo_name
, '/');
488 /* have a sequence argument */
489 if (!seq_in_list(cp
, svector_strs (sequences
))) {
490 svector_push_back (sequences
, cp
);
495 if (strcmp(invo_name
, "fnext") == 0) {
497 } else if (strcmp(invo_name
, "fprev") == 0) {
499 } else if (strcmp(invo_name
, "unseen") == 0) {
500 run_mode
= RM_UNSEEN
;
503 if (folders
== NULL
) {
506 if (folders
[0] != '/') {
507 folders
= m_maildir(folders
);
512 /* no sequence arguments; use unseen */
513 unseen
= context_find(usequence
);
514 if (unseen
== NULL
|| unseen
[0] == '\0') {
515 die("must specify sequences or set %s", usequence
);
517 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
518 svector_push_back (sequences
, *ap
);
523 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
524 if (folder
== NULL
) {
529 if (run_mode
== RM_UNSEEN
) {
530 /* All the scan(1)s it runs change the current folder, so we
531 * need to put it back. Unfortunately, context_replace lamely
532 * ignores the new value you give it if it is the same one it
533 * has in memory. So, we'll be lame, too. I'm not sure if i
534 * should just change context_replace... */
535 context_replace(pfolder
, "defeat_context_replace_optimization");
538 /* update current folder */
539 context_replace(pfolder
, folder
->n_name
);
541 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
542 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
547 svector_free (sequences
);