]>
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 <h/crawl_folders.h>
17 #include "sbr/lock_file.h"
18 #include "sbr/m_maildir.h"
20 #define NEW_SWITCHES \
21 X("mode", 1, MODESW) \
22 X("folders", 1, FOLDERSSW) \
23 X("version", 1, VERSIONSW) \
24 X("help", 1, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(NEW
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(NEW
, switches
);
34 /* What to do, based on argv[0]. */
42 /* check_folders uses this to maintain state with both .folders list of
43 * folders and with crawl_folders. */
45 struct node
**first
, **cur_node
;
52 /* Return the number of messages in a string list of message numbers. */
54 count_messages(char *field
)
60 field
= getcpy(field
);
62 /* copied from seq_read.c:seq_init */
63 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
64 if ((cp
= strchr(*ap
, '-')))
66 if ((j
= m_atoi (*ap
)) > 0) {
67 k
= cp
? m_atoi (cp
) : j
;
78 /* Return true if the sequence 'name' is in 'sequences'. */
80 seq_in_list(char *name
, char *sequences
[])
84 for (i
= 0; sequences
[i
] != NULL
; i
++) {
85 if (strcmp(name
, sequences
[i
]) == 0) {
93 /* Return the string list of message numbers from the sequences file, or NULL
96 get_msgnums(char *folder
, char *sequences
[])
101 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
103 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
104 int failed_to_lock
= 0;
105 m_getfld_state_t gstate
;
107 /* copied from seq_read.c:seq_public */
109 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
110 * the "mh-sequences" profile entry, but left it empty),
111 * then just return, and do not initialize any public sequences.
113 if (mh_seq
== NULL
|| *mh_seq
== '\0')
116 /* get filename of sequence file */
117 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, (void *)NULL
);
122 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
124 adios (seqfile
, "failed to lock");
129 /* Use m_getfld2 to scan sequence file */
130 gstate
= m_getfld_state_init(fp
);
132 int fieldsz
= sizeof field
;
133 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
136 if (state
== FLDPLUS
) {
138 while (state
== FLDPLUS
) {
139 fieldsz
= sizeof field
;
140 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
141 cp
= add (field
, cp
);
144 /* Here's where we differ from seq_public: if it's in a
145 * sequence we want, save the list of messages. */
146 if (seq_in_list(name
, sequences
)) {
147 this_msgnums
= trimcpy(cp
);
148 if (msgnums
== NULL
) {
149 msgnums
= this_msgnums
;
151 old_msgnums
= msgnums
;
152 msgnums
= concat(old_msgnums
, " ",
153 this_msgnums
, (void *)NULL
);
161 if (seq_in_list(name
, sequences
)) {
162 this_msgnums
= trimcpy(field
);
163 if (msgnums
== NULL
) {
164 msgnums
= this_msgnums
;
166 old_msgnums
= msgnums
;
167 msgnums
= concat(old_msgnums
, " ",
168 this_msgnums
, (void *)NULL
);
178 adios (NULL
, "no blank lines are permitted in %s", seqfile
);
185 adios (NULL
, "%s is poorly formatted", seqfile
);
187 break; /* break from for loop */
189 m_getfld_state_destroy (&gstate
);
191 lkfclosedata (fp
, seqfile
);
198 /* Check `folder' (of length `len') for interesting messages, filling in the
201 check_folder(char *folder
, size_t len
, struct list_state
*b
)
203 char *msgnums
= get_msgnums(folder
, b
->sequences
);
204 int is_cur
= strcmp(folder
, b
->cur
) == 0;
206 if (is_cur
|| msgnums
!= NULL
) {
207 if (*b
->first
== NULL
) {
211 NEW(b
->node
->n_next
);
212 b
->node
= b
->node
->n_next
;
214 b
->node
->n_name
= folder
;
215 b
->node
->n_field
= msgnums
;
217 if (*b
->maxlen
< len
) {
222 /* Save the node for the current folder, so we can fall back to it. */
224 *b
->cur_node
= b
->node
;
229 crawl_callback(char *folder
, void *baton
)
231 check_folder(folder
, strlen(folder
), baton
);
235 /* Scan folders, returning:
236 * first -- list of nodes for all folders which have desired messages;
237 * if the current folder is listed in .folders, it is also in
238 * the list regardless of whether it has any desired messages
239 * last -- last node in list
240 * cur_node -- node of current folder, if listed in .folders
241 * maxlen -- length of longest folder name
243 * `cur' points to the name of the current folder, `folders' points to the
244 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
245 * the array of sequences for which to look.
247 * An empty list is returned as first=last=NULL.
250 check_folders(struct node
**first
, struct node
**last
,
251 struct node
**cur_node
, size_t *maxlen
,
252 char *cur
, char *folders
, char *sequences
[])
259 *first
= *last
= *cur_node
= NULL
;
263 b
.cur_node
= cur_node
;
266 b
.sequences
= sequences
;
268 if (folders
== NULL
) {
269 if (chdir(m_maildir("")) < 0) {
270 advise (m_maildir(""), "chdir");
272 crawl_folders(".", crawl_callback
, &b
);
274 fp
= fopen(folders
, "r");
276 adios(NULL
, "failed to read %s", folders
);
278 while (vfgets(fp
, &line
) == OK
) {
279 len
= strlen(line
) - 1;
281 check_folder(mh_xstrdup(line
), len
, &b
);
286 if (*first
!= NULL
) {
287 b
.node
->n_next
= NULL
;
292 /* Return a single string of the `sequences' joined by a space (' '). */
294 join_sequences(char *sequences
[])
300 for (i
= 0; sequences
[i
] != NULL
; i
++) {
301 len
+= strlen(sequences
[i
]) + 1;
303 result
= mh_xmalloc(len
+ 1);
305 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
306 len
= strlen(sequences
[i
]);
307 memcpy(cp
, sequences
[i
], len
);
310 /* -1 to overwrite the last delimiter */
316 /* Return a struct node for the folder to change to. This is the next
317 * (previous, if RM_FPREV mode) folder with desired messages, or the current
318 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
319 * output but don't change folders.
321 * n_name is the folder to change to, and n_field is the string list of
322 * desired message numbers.
325 doit(char *cur
, char *folders
, char *sequences
[])
327 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
329 int count
, total
= 0;
330 char *command
= NULL
, *sequences_s
= NULL
;
332 if (cur
== NULL
|| cur
[0] == '\0') {
336 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
339 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
341 /* No folders at all... */
344 if (first
->n_next
== NULL
) {
345 /* We have only one node; any desired messages in it? */
346 if (first
->n_field
== NULL
) {
351 if (cur_node
== NULL
) {
352 /* Current folder is not listed in .folders, return first. */
355 } else if (run_mode
== RM_UNSEEN
) {
356 sequences_s
= join_sequences(sequences
);
359 for (node
= first
, prev
= NULL
;
361 prev
= node
, node
= node
->n_next
) {
362 if (run_mode
== RM_FNEXT
) {
363 /* If we have a previous node and it is the current
364 * folder, return this node. */
365 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
368 } else if (run_mode
== RM_FPREV
) {
369 if (strcmp(node
->n_name
, cur
) == 0) {
370 /* Found current folder in fprev mode; if we have a
371 * previous node in the list, return it; else return
378 } else if (run_mode
== RM_UNSEEN
) {
381 if (node
->n_field
== NULL
) {
385 printf("\n%d %s messages in %s",
386 count_messages(node
->n_field
),
389 if (strcmp(node
->n_name
, cur
) == 0) {
390 puts(" (*: current folder)");
396 /* TODO: Split enough of scan.c out so that we can call it here. */
397 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
399 status
= system(command
);
400 if (! WIFEXITED (status
)) {
401 adios (command
, "system");
405 if (node
->n_field
== NULL
) {
409 count
= count_messages(node
->n_field
);
412 printf("%-*s %6d.%c %s\n",
413 (int) folder_len
, node
->n_name
,
415 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
420 /* If we're fnext, we haven't checked the last node yet. If it's the
421 * current folder, return the first node. */
422 if (run_mode
== RM_FNEXT
) {
423 assert(last
!= NULL
);
424 if (strcmp(last
->n_name
, cur
) == 0) {
429 if (run_mode
== RM_NEW
) {
430 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
437 main(int argc
, char **argv
)
439 char **ap
, *cp
, **argp
, **arguments
;
441 char *folders
= NULL
;
442 svector_t sequences
= svector_create (0);
447 if (nmh_init(argv
[0], 1)) { return 1; }
449 arguments
= getarguments (invo_name
, argc
, argv
, 1);
455 while ((cp
= *argp
++)) {
457 switch (smatch (++cp
, switches
)) {
459 ambigsw (cp
, switches
);
462 adios (NULL
, "-%s unknown", cp
);
465 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
467 print_help (help
, switches
, 1);
470 print_version(invo_name
);
474 if (!(folders
= *argp
++) || *folders
== '-')
475 adios(NULL
, "missing argument to %s", argp
[-2]);
478 if (!(invo_name
= *argp
++) || *invo_name
== '-')
479 adios(NULL
, "missing argument to %s", argp
[-2]);
480 invo_name
= r1bindex(invo_name
, '/');
484 /* have a sequence argument */
485 if (!seq_in_list(cp
, svector_strs (sequences
))) {
486 svector_push_back (sequences
, cp
);
491 if (strcmp(invo_name
, "fnext") == 0) {
493 } else if (strcmp(invo_name
, "fprev") == 0) {
495 } else if (strcmp(invo_name
, "unseen") == 0) {
496 run_mode
= RM_UNSEEN
;
499 if (folders
== NULL
) {
502 if (folders
[0] != '/') {
503 folders
= m_maildir(folders
);
508 /* no sequence arguments; use unseen */
509 unseen
= context_find(usequence
);
510 if (unseen
== NULL
|| unseen
[0] == '\0') {
511 adios(NULL
, "must specify sequences or set %s", usequence
);
513 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
514 svector_push_back (sequences
, *ap
);
519 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
520 if (folder
== NULL
) {
525 if (run_mode
== RM_UNSEEN
) {
526 /* All the scan(1)s it runs change the current folder, so we
527 * need to put it back. Unfortunately, context_replace lamely
528 * ignores the new value you give it if it is the same one it
529 * has in memory. So, we'll be lame, too. I'm not sure if i
530 * should just change context_replace... */
531 context_replace(pfolder
, "defeat_context_replace_optimization");
534 /* update current folder */
535 context_replace(pfolder
, folder
->n_name
);
537 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
538 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
543 svector_free (sequences
);