]>
diplodocus.org Git - nmh/blob - uip/new.c
4fdbeeaa7d35438390608887ddee02b39f9c21e0
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/vfgets.h"
16 #include "sbr/getcpy.h"
17 #include "sbr/m_atoi.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/brkstring.h"
22 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
26 #include "h/crawl_folders.h"
29 #include "sbr/lock_file.h"
30 #include "sbr/m_maildir.h"
32 #define NEW_SWITCHES \
33 X("mode", 1, MODESW) \
34 X("folders", 1, FOLDERSSW) \
35 X("version", 1, VERSIONSW) \
36 X("help", 1, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(NEW
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(NEW
, switches
);
46 /* What to do, based on argv[0]. */
54 /* check_folders uses this to maintain state with both .folders list of
55 * folders and with crawl_folders. */
57 struct node
**first
, **cur_node
;
64 /* Return the number of messages in a string list of message numbers. */
66 count_messages(char *field
)
72 field
= getcpy(field
);
74 /* copied from seq_read.c:seq_init */
75 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
76 if ((cp
= strchr(*ap
, '-')))
78 if ((j
= m_atoi (*ap
)) > 0) {
79 k
= cp
? m_atoi (cp
) : j
;
90 /* Return true if the sequence 'name' is in 'sequences'. */
92 seq_in_list(char *name
, char *sequences
[])
96 for (i
= 0; sequences
[i
] != NULL
; i
++) {
97 if (strcmp(name
, sequences
[i
]) == 0) {
105 /* Return the string list of message numbers from the sequences file, or NULL
108 get_msgnums(char *folder
, char *sequences
[])
110 char *seqfile
= NULL
;
113 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
115 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
116 int failed_to_lock
= 0;
117 m_getfld_state_t gstate
;
119 /* copied from seq_read.c:seq_public */
121 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
122 * the "mh-sequences" profile entry, but left it empty),
123 * then just return, and do not initialize any public sequences.
125 if (mh_seq
== NULL
|| *mh_seq
== '\0')
128 /* get filename of sequence file */
129 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
134 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
136 adios (seqfile
, "failed to lock");
141 /* Use m_getfld2 to scan sequence file */
142 gstate
= m_getfld_state_init(fp
);
144 int fieldsz
= sizeof field
;
145 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
148 if (state
== FLDPLUS
) {
150 while (state
== FLDPLUS
) {
151 fieldsz
= sizeof field
;
152 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
153 cp
= add (field
, cp
);
156 /* Here's where we differ from seq_public: if it's in a
157 * sequence we want, save the list of messages. */
158 if (seq_in_list(name
, sequences
)) {
159 this_msgnums
= trimcpy(cp
);
160 if (msgnums
== NULL
) {
161 msgnums
= this_msgnums
;
163 old_msgnums
= msgnums
;
164 msgnums
= concat(old_msgnums
, " ",
173 if (seq_in_list(name
, sequences
)) {
174 this_msgnums
= trimcpy(field
);
175 if (msgnums
== NULL
) {
176 msgnums
= this_msgnums
;
178 old_msgnums
= msgnums
;
179 msgnums
= concat(old_msgnums
, " ",
190 die("no blank lines are permitted in %s", seqfile
);
197 die("%s is poorly formatted", seqfile
);
199 break; /* break from for loop */
201 m_getfld_state_destroy (&gstate
);
203 lkfclosedata (fp
, seqfile
);
210 /* Check `folder' (of length `len') for interesting messages, filling in the
213 check_folder(char *folder
, size_t len
, struct list_state
*b
)
215 char *msgnums
= get_msgnums(folder
, b
->sequences
);
216 int is_cur
= strcmp(folder
, b
->cur
) == 0;
218 if (is_cur
|| msgnums
!= NULL
) {
219 if (*b
->first
== NULL
) {
223 NEW(b
->node
->n_next
);
224 b
->node
= b
->node
->n_next
;
226 b
->node
->n_name
= folder
;
227 b
->node
->n_field
= msgnums
;
229 if (*b
->maxlen
< len
) {
234 /* Save the node for the current folder, so we can fall back to it. */
236 *b
->cur_node
= b
->node
;
241 crawl_callback(char *folder
, void *baton
)
243 check_folder(folder
, strlen(folder
), baton
);
247 /* Scan folders, returning:
248 * first -- list of nodes for all folders which have desired messages;
249 * if the current folder is listed in .folders, it is also in
250 * the list regardless of whether it has any desired messages
251 * last -- last node in list
252 * cur_node -- node of current folder, if listed in .folders
253 * maxlen -- length of longest folder name
255 * `cur' points to the name of the current folder, `folders' points to the
256 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
257 * the array of sequences for which to look.
259 * An empty list is returned as first=last=NULL.
262 check_folders(struct node
**first
, struct node
**last
,
263 struct node
**cur_node
, size_t *maxlen
,
264 char *cur
, char *folders
, char *sequences
[])
271 *first
= *last
= *cur_node
= NULL
;
275 b
.cur_node
= cur_node
;
278 b
.sequences
= sequences
;
280 if (folders
== NULL
) {
281 if (chdir(m_maildir("")) < 0) {
282 advise (m_maildir(""), "chdir");
284 crawl_folders(".", crawl_callback
, &b
);
286 fp
= fopen(folders
, "r");
288 die("failed to read %s", folders
);
290 while (vfgets(fp
, &line
) == OK
) {
291 len
= strlen(line
) - 1;
293 check_folder(mh_xstrdup(line
), len
, &b
);
298 if (*first
!= NULL
) {
299 b
.node
->n_next
= NULL
;
304 /* Return a single string of the `sequences' joined by a space (' '). */
306 join_sequences(char *sequences
[])
312 for (i
= 0; sequences
[i
] != NULL
; i
++) {
313 len
+= strlen(sequences
[i
]) + 1;
315 result
= mh_xmalloc(len
+ 1);
317 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
318 len
= strlen(sequences
[i
]);
319 memcpy(cp
, sequences
[i
], len
);
322 /* -1 to overwrite the last delimiter */
328 /* Return a struct node for the folder to change to. This is the next
329 * (previous, if RM_FPREV mode) folder with desired messages, or the current
330 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
331 * output but don't change folders.
333 * n_name is the folder to change to, and n_field is the string list of
334 * desired message numbers.
337 doit(char *cur
, char *folders
, char *sequences
[])
339 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
341 int count
, total
= 0;
342 char *command
= NULL
, *sequences_s
= NULL
;
344 if (cur
== NULL
|| cur
[0] == '\0') {
348 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
351 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
353 /* No folders at all... */
356 if (first
->n_next
== NULL
) {
357 /* We have only one node; any desired messages in it? */
358 if (first
->n_field
== NULL
) {
363 if (cur_node
== NULL
) {
364 /* Current folder is not listed in .folders, return first. */
367 } else if (run_mode
== RM_UNSEEN
) {
368 sequences_s
= join_sequences(sequences
);
371 for (node
= first
, prev
= NULL
;
373 prev
= node
, node
= node
->n_next
) {
374 if (run_mode
== RM_FNEXT
) {
375 /* If we have a previous node and it is the current
376 * folder, return this node. */
377 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
380 } else if (run_mode
== RM_FPREV
) {
381 if (strcmp(node
->n_name
, cur
) == 0) {
382 /* Found current folder in fprev mode; if we have a
383 * previous node in the list, return it; else return
389 } else if (run_mode
== RM_UNSEEN
) {
392 if (node
->n_field
== NULL
) {
396 printf("\n%d %s messages in %s",
397 count_messages(node
->n_field
),
400 if (strcmp(node
->n_name
, cur
) == 0) {
401 puts(" (*: current folder)");
407 /* TODO: Split enough of scan.c out so that we can call it here. */
408 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
410 status
= system(command
);
411 if (! WIFEXITED (status
)) {
412 adios (command
, "system");
416 if (node
->n_field
== NULL
) {
420 count
= count_messages(node
->n_field
);
423 printf("%-*s %6d.%c %s\n",
424 (int) folder_len
, node
->n_name
,
426 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
431 /* If we're fnext, we haven't checked the last node yet. If it's the
432 * current folder, return the first node. */
433 if (run_mode
== RM_FNEXT
) {
434 assert(last
!= NULL
);
435 if (strcmp(last
->n_name
, cur
) == 0) {
440 if (run_mode
== RM_NEW
) {
441 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
448 main(int argc
, char **argv
)
450 char **ap
, *cp
, **argp
, **arguments
;
452 char *folders
= NULL
;
453 svector_t sequences
= svector_create (0);
458 if (nmh_init(argv
[0], true, true)) { return 1; }
460 arguments
= getarguments (invo_name
, argc
, argv
, 1);
466 while ((cp
= *argp
++)) {
468 switch (smatch (++cp
, switches
)) {
470 ambigsw (cp
, switches
);
473 die("-%s unknown", cp
);
476 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
478 print_help (help
, switches
, 1);
481 print_version(invo_name
);
485 if (!(folders
= *argp
++) || *folders
== '-')
486 die("missing argument to %s", argp
[-2]);
489 if (!(invo_name
= *argp
++) || *invo_name
== '-')
490 die("missing argument to %s", argp
[-2]);
491 invo_name
= r1bindex(invo_name
, '/');
495 /* have a sequence argument */
496 if (!seq_in_list(cp
, svector_strs (sequences
))) {
497 svector_push_back (sequences
, cp
);
502 if (strcmp(invo_name
, "fnext") == 0) {
504 } else if (strcmp(invo_name
, "fprev") == 0) {
506 } else if (strcmp(invo_name
, "unseen") == 0) {
507 run_mode
= RM_UNSEEN
;
510 if (folders
== NULL
) {
513 if (folders
[0] != '/') {
514 folders
= m_maildir(folders
);
519 /* no sequence arguments; use unseen */
520 unseen
= context_find(usequence
);
521 if (unseen
== NULL
|| unseen
[0] == '\0') {
522 die("must specify sequences or set %s", usequence
);
524 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
525 svector_push_back (sequences
, *ap
);
530 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
531 if (folder
== NULL
) {
536 if (run_mode
== RM_UNSEEN
) {
537 /* All the scan(1)s it runs change the current folder, so we
538 * need to put it back. Unfortunately, context_replace lamely
539 * ignores the new value you give it if it is the same one it
540 * has in memory. So, we'll be lame, too. I'm not sure if i
541 * should just change context_replace... */
542 context_replace(pfolder
, "defeat_context_replace_optimization");
545 /* update current folder */
546 context_replace(pfolder
, folder
->n_name
);
548 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
549 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
554 svector_free (sequences
);