]>
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>
18 #include "sbr/lock_file.h"
19 #include "sbr/m_maildir.h"
21 #define NEW_SWITCHES \
22 X("mode", 1, MODESW) \
23 X("folders", 1, FOLDERSSW) \
24 X("version", 1, VERSIONSW) \
25 X("help", 1, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(NEW
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(NEW
, switches
);
35 /* What to do, based on argv[0]. */
43 /* check_folders uses this to maintain state with both .folders list of
44 * folders and with crawl_folders. */
46 struct node
**first
, **cur_node
;
53 /* Return the number of messages in a string list of message numbers. */
55 count_messages(char *field
)
61 field
= getcpy(field
);
63 /* copied from seq_read.c:seq_init */
64 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
65 if ((cp
= strchr(*ap
, '-')))
67 if ((j
= m_atoi (*ap
)) > 0) {
68 k
= cp
? m_atoi (cp
) : j
;
79 /* Return true if the sequence 'name' is in 'sequences'. */
81 seq_in_list(char *name
, char *sequences
[])
85 for (i
= 0; sequences
[i
] != NULL
; i
++) {
86 if (strcmp(name
, sequences
[i
]) == 0) {
94 /* Return the string list of message numbers from the sequences file, or NULL
97 get_msgnums(char *folder
, char *sequences
[])
102 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
104 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
105 int failed_to_lock
= 0;
106 m_getfld_state_t gstate
;
108 /* copied from seq_read.c:seq_public */
110 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
111 * the "mh-sequences" profile entry, but left it empty),
112 * then just return, and do not initialize any public sequences.
114 if (mh_seq
== NULL
|| *mh_seq
== '\0')
117 /* get filename of sequence file */
118 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
123 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
125 adios (seqfile
, "failed to lock");
130 /* Use m_getfld2 to scan sequence file */
131 gstate
= m_getfld_state_init(fp
);
133 int fieldsz
= sizeof field
;
134 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
137 if (state
== FLDPLUS
) {
139 while (state
== FLDPLUS
) {
140 fieldsz
= sizeof field
;
141 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
142 cp
= add (field
, cp
);
145 /* Here's where we differ from seq_public: if it's in a
146 * sequence we want, save the list of messages. */
147 if (seq_in_list(name
, sequences
)) {
148 this_msgnums
= trimcpy(cp
);
149 if (msgnums
== NULL
) {
150 msgnums
= this_msgnums
;
152 old_msgnums
= msgnums
;
153 msgnums
= concat(old_msgnums
, " ",
162 if (seq_in_list(name
, sequences
)) {
163 this_msgnums
= trimcpy(field
);
164 if (msgnums
== NULL
) {
165 msgnums
= this_msgnums
;
167 old_msgnums
= msgnums
;
168 msgnums
= concat(old_msgnums
, " ",
179 die("no blank lines are permitted in %s", seqfile
);
186 die("%s is poorly formatted", seqfile
);
188 break; /* break from for loop */
190 m_getfld_state_destroy (&gstate
);
192 lkfclosedata (fp
, seqfile
);
199 /* Check `folder' (of length `len') for interesting messages, filling in the
202 check_folder(char *folder
, size_t len
, struct list_state
*b
)
204 char *msgnums
= get_msgnums(folder
, b
->sequences
);
205 int is_cur
= strcmp(folder
, b
->cur
) == 0;
207 if (is_cur
|| msgnums
!= NULL
) {
208 if (*b
->first
== NULL
) {
212 NEW(b
->node
->n_next
);
213 b
->node
= b
->node
->n_next
;
215 b
->node
->n_name
= folder
;
216 b
->node
->n_field
= msgnums
;
218 if (*b
->maxlen
< len
) {
223 /* Save the node for the current folder, so we can fall back to it. */
225 *b
->cur_node
= b
->node
;
230 crawl_callback(char *folder
, void *baton
)
232 check_folder(folder
, strlen(folder
), baton
);
236 /* Scan folders, returning:
237 * first -- list of nodes for all folders which have desired messages;
238 * if the current folder is listed in .folders, it is also in
239 * the list regardless of whether it has any desired messages
240 * last -- last node in list
241 * cur_node -- node of current folder, if listed in .folders
242 * maxlen -- length of longest folder name
244 * `cur' points to the name of the current folder, `folders' points to the
245 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
246 * the array of sequences for which to look.
248 * An empty list is returned as first=last=NULL.
251 check_folders(struct node
**first
, struct node
**last
,
252 struct node
**cur_node
, size_t *maxlen
,
253 char *cur
, char *folders
, char *sequences
[])
260 *first
= *last
= *cur_node
= NULL
;
264 b
.cur_node
= cur_node
;
267 b
.sequences
= sequences
;
269 if (folders
== NULL
) {
270 if (chdir(m_maildir("")) < 0) {
271 advise (m_maildir(""), "chdir");
273 crawl_folders(".", crawl_callback
, &b
);
275 fp
= fopen(folders
, "r");
277 die("failed to read %s", folders
);
279 while (vfgets(fp
, &line
) == OK
) {
280 len
= strlen(line
) - 1;
282 check_folder(mh_xstrdup(line
), len
, &b
);
287 if (*first
!= NULL
) {
288 b
.node
->n_next
= NULL
;
293 /* Return a single string of the `sequences' joined by a space (' '). */
295 join_sequences(char *sequences
[])
301 for (i
= 0; sequences
[i
] != NULL
; i
++) {
302 len
+= strlen(sequences
[i
]) + 1;
304 result
= mh_xmalloc(len
+ 1);
306 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
307 len
= strlen(sequences
[i
]);
308 memcpy(cp
, sequences
[i
], len
);
311 /* -1 to overwrite the last delimiter */
317 /* Return a struct node for the folder to change to. This is the next
318 * (previous, if RM_FPREV mode) folder with desired messages, or the current
319 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
320 * output but don't change folders.
322 * n_name is the folder to change to, and n_field is the string list of
323 * desired message numbers.
326 doit(char *cur
, char *folders
, char *sequences
[])
328 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
330 int count
, total
= 0;
331 char *command
= NULL
, *sequences_s
= NULL
;
333 if (cur
== NULL
|| cur
[0] == '\0') {
337 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
340 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
342 /* No folders at all... */
345 if (first
->n_next
== NULL
) {
346 /* We have only one node; any desired messages in it? */
347 if (first
->n_field
== NULL
) {
352 if (cur_node
== NULL
) {
353 /* Current folder is not listed in .folders, return first. */
356 } else if (run_mode
== RM_UNSEEN
) {
357 sequences_s
= join_sequences(sequences
);
360 for (node
= first
, prev
= NULL
;
362 prev
= node
, node
= node
->n_next
) {
363 if (run_mode
== RM_FNEXT
) {
364 /* If we have a previous node and it is the current
365 * folder, return this node. */
366 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
369 } else if (run_mode
== RM_FPREV
) {
370 if (strcmp(node
->n_name
, cur
) == 0) {
371 /* Found current folder in fprev mode; if we have a
372 * 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], true, true)) { return 1; }
449 arguments
= getarguments (invo_name
, argc
, argv
, 1);
455 while ((cp
= *argp
++)) {
457 switch (smatch (++cp
, switches
)) {
459 ambigsw (cp
, switches
);
462 die("-%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 die("missing argument to %s", argp
[-2]);
478 if (!(invo_name
= *argp
++) || *invo_name
== '-')
479 die("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 die("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
);