]>
diplodocus.org Git - nmh/blob - uip/new.c
5de05d4862c4d1d825e224285f41a86cebd9bba0
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/m_atoi.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/brkstring.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
24 #include "h/crawl_folders.h"
27 #include "sbr/lock_file.h"
28 #include "sbr/m_maildir.h"
30 #define NEW_SWITCHES \
31 X("mode", 1, MODESW) \
32 X("folders", 1, FOLDERSSW) \
33 X("version", 1, VERSIONSW) \
34 X("help", 1, HELPSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(NEW
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(NEW
, switches
);
44 /* What to do, based on argv[0]. */
52 /* check_folders uses this to maintain state with both .folders list of
53 * folders and with crawl_folders. */
55 struct node
**first
, **cur_node
;
62 /* Return the number of messages in a string list of message numbers. */
64 count_messages(char *field
)
70 field
= getcpy(field
);
72 /* copied from seq_read.c:seq_init */
73 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
74 if ((cp
= strchr(*ap
, '-')))
76 if ((j
= m_atoi (*ap
)) > 0) {
77 k
= cp
? m_atoi (cp
) : j
;
88 /* Return true if the sequence 'name' is in 'sequences'. */
90 seq_in_list(char *name
, char *sequences
[])
94 for (i
= 0; sequences
[i
] != NULL
; i
++) {
95 if (strcmp(name
, sequences
[i
]) == 0) {
103 /* Return the string list of message numbers from the sequences file, or NULL
106 get_msgnums(char *folder
, char *sequences
[])
108 char *seqfile
= NULL
;
111 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
113 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
114 int failed_to_lock
= 0;
115 m_getfld_state_t gstate
;
117 /* copied from seq_read.c:seq_public */
119 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
120 * the "mh-sequences" profile entry, but left it empty),
121 * then just return, and do not initialize any public sequences.
123 if (mh_seq
== NULL
|| *mh_seq
== '\0')
126 /* get filename of sequence file */
127 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, NULL
);
132 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
134 adios (seqfile
, "failed to lock");
139 /* Use m_getfld2 to scan sequence file */
140 gstate
= m_getfld_state_init(fp
);
142 int fieldsz
= sizeof field
;
143 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
146 if (state
== FLDPLUS
) {
148 while (state
== FLDPLUS
) {
149 fieldsz
= sizeof field
;
150 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
151 cp
= add (field
, cp
);
154 /* Here's where we differ from seq_public: if it's in a
155 * sequence we want, save the list of messages. */
156 if (seq_in_list(name
, sequences
)) {
157 this_msgnums
= trimcpy(cp
);
158 if (msgnums
== NULL
) {
159 msgnums
= this_msgnums
;
161 old_msgnums
= msgnums
;
162 msgnums
= concat(old_msgnums
, " ",
171 if (seq_in_list(name
, sequences
)) {
172 this_msgnums
= trimcpy(field
);
173 if (msgnums
== NULL
) {
174 msgnums
= this_msgnums
;
176 old_msgnums
= msgnums
;
177 msgnums
= concat(old_msgnums
, " ",
188 die("no blank lines are permitted in %s", seqfile
);
195 die("%s is poorly formatted", seqfile
);
197 break; /* break from for loop */
199 m_getfld_state_destroy (&gstate
);
201 lkfclosedata (fp
, seqfile
);
208 /* Check `folder' (of length `len') for interesting messages, filling in the
211 check_folder(char *folder
, size_t len
, struct list_state
*b
)
213 char *msgnums
= get_msgnums(folder
, b
->sequences
);
214 int is_cur
= strcmp(folder
, b
->cur
) == 0;
216 if (is_cur
|| msgnums
!= NULL
) {
217 if (*b
->first
== NULL
) {
221 NEW(b
->node
->n_next
);
222 b
->node
= b
->node
->n_next
;
224 b
->node
->n_name
= folder
;
225 b
->node
->n_field
= msgnums
;
227 if (*b
->maxlen
< len
) {
232 /* Save the node for the current folder, so we can fall back to it. */
234 *b
->cur_node
= b
->node
;
239 crawl_callback(char *folder
, void *baton
)
241 check_folder(folder
, strlen(folder
), baton
);
245 /* Scan folders, returning:
246 * first -- list of nodes for all folders which have desired messages;
247 * if the current folder is listed in .folders, it is also in
248 * the list regardless of whether it has any desired messages
249 * last -- last node in list
250 * cur_node -- node of current folder, if listed in .folders
251 * maxlen -- length of longest folder name
253 * `cur' points to the name of the current folder, `folders' points to the
254 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
255 * the array of sequences for which to look.
257 * An empty list is returned as first=last=NULL.
260 check_folders(struct node
**first
, struct node
**last
,
261 struct node
**cur_node
, size_t *maxlen
,
262 char *cur
, char *folders
, char *sequences
[])
269 *first
= *last
= *cur_node
= NULL
;
273 b
.cur_node
= cur_node
;
276 b
.sequences
= sequences
;
278 if (folders
== NULL
) {
279 if (chdir(m_maildir("")) < 0) {
280 advise (m_maildir(""), "chdir");
282 crawl_folders(".", crawl_callback
, &b
);
284 fp
= fopen(folders
, "r");
286 die("failed to read %s", folders
);
288 while (vfgets(fp
, &line
) == OK
) {
289 len
= strlen(line
) - 1;
291 check_folder(mh_xstrdup(line
), len
, &b
);
296 if (*first
!= NULL
) {
297 b
.node
->n_next
= NULL
;
302 /* Return a single string of the `sequences' joined by a space (' '). */
304 join_sequences(char *sequences
[])
310 for (i
= 0; sequences
[i
] != NULL
; i
++) {
311 len
+= strlen(sequences
[i
]) + 1;
313 result
= mh_xmalloc(len
+ 1);
315 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
316 len
= strlen(sequences
[i
]);
317 memcpy(cp
, sequences
[i
], len
);
320 /* -1 to overwrite the last delimiter */
326 /* Return a struct node for the folder to change to. This is the next
327 * (previous, if RM_FPREV mode) folder with desired messages, or the current
328 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
329 * output but don't change folders.
331 * n_name is the folder to change to, and n_field is the string list of
332 * desired message numbers.
335 doit(char *cur
, char *folders
, char *sequences
[])
337 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
339 int count
, total
= 0;
340 char *command
= NULL
, *sequences_s
= NULL
;
342 if (cur
== NULL
|| cur
[0] == '\0') {
346 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
349 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
351 /* No folders at all... */
354 if (first
->n_next
== NULL
) {
355 /* We have only one node; any desired messages in it? */
356 if (first
->n_field
== NULL
) {
361 if (cur_node
== NULL
) {
362 /* Current folder is not listed in .folders, return first. */
365 } else if (run_mode
== RM_UNSEEN
) {
366 sequences_s
= join_sequences(sequences
);
369 for (node
= first
, prev
= NULL
;
371 prev
= node
, node
= node
->n_next
) {
372 if (run_mode
== RM_FNEXT
) {
373 /* If we have a previous node and it is the current
374 * folder, return this node. */
375 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
378 } else if (run_mode
== RM_FPREV
) {
379 if (strcmp(node
->n_name
, cur
) == 0) {
380 /* Found current folder in fprev mode; if we have a
381 * previous node in the list, return it; else return
387 } else if (run_mode
== RM_UNSEEN
) {
390 if (node
->n_field
== NULL
) {
394 printf("\n%d %s messages in %s",
395 count_messages(node
->n_field
),
398 if (strcmp(node
->n_name
, cur
) == 0) {
399 puts(" (*: current folder)");
405 /* TODO: Split enough of scan.c out so that we can call it here. */
406 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
408 status
= system(command
);
409 if (! WIFEXITED (status
)) {
410 adios (command
, "system");
414 if (node
->n_field
== NULL
) {
418 count
= count_messages(node
->n_field
);
421 printf("%-*s %6d.%c %s\n",
422 (int) folder_len
, node
->n_name
,
424 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
429 /* If we're fnext, we haven't checked the last node yet. If it's the
430 * current folder, return the first node. */
431 if (run_mode
== RM_FNEXT
) {
432 assert(last
!= NULL
);
433 if (strcmp(last
->n_name
, cur
) == 0) {
438 if (run_mode
== RM_NEW
) {
439 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
446 main(int argc
, char **argv
)
448 char **ap
, *cp
, **argp
, **arguments
;
450 char *folders
= NULL
;
451 svector_t sequences
= svector_create (0);
456 if (nmh_init(argv
[0], true, true)) { return 1; }
458 arguments
= getarguments (invo_name
, argc
, argv
, 1);
464 while ((cp
= *argp
++)) {
466 switch (smatch (++cp
, switches
)) {
468 ambigsw (cp
, switches
);
471 die("-%s unknown", cp
);
474 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
476 print_help (help
, switches
, 1);
479 print_version(invo_name
);
483 if (!(folders
= *argp
++) || *folders
== '-')
484 die("missing argument to %s", argp
[-2]);
487 if (!(invo_name
= *argp
++) || *invo_name
== '-')
488 die("missing argument to %s", argp
[-2]);
489 invo_name
= r1bindex(invo_name
, '/');
493 /* have a sequence argument */
494 if (!seq_in_list(cp
, svector_strs (sequences
))) {
495 svector_push_back (sequences
, cp
);
500 if (strcmp(invo_name
, "fnext") == 0) {
502 } else if (strcmp(invo_name
, "fprev") == 0) {
504 } else if (strcmp(invo_name
, "unseen") == 0) {
505 run_mode
= RM_UNSEEN
;
508 if (folders
== NULL
) {
511 if (folders
[0] != '/') {
512 folders
= m_maildir(folders
);
517 /* no sequence arguments; use unseen */
518 unseen
= context_find(usequence
);
519 if (unseen
== NULL
|| unseen
[0] == '\0') {
520 die("must specify sequences or set %s", usequence
);
522 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
523 svector_push_back (sequences
, *ap
);
528 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
529 if (folder
== NULL
) {
534 if (run_mode
== RM_UNSEEN
) {
535 /* All the scan(1)s it runs change the current folder, so we
536 * need to put it back. Unfortunately, context_replace lamely
537 * ignores the new value you give it if it is the same one it
538 * has in memory. So, we'll be lame, too. I'm not sure if i
539 * should just change context_replace... */
540 context_replace(pfolder
, "defeat_context_replace_optimization");
543 /* update current folder */
544 context_replace(pfolder
, folder
->n_name
);
546 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
547 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
552 svector_free (sequences
);