]>
diplodocus.org Git - nmh/blob - uip/new.c
3 * new.c -- as new, list all folders with unseen messages
4 * -- as fnext, move to next folder with unseen messages
5 * -- as fprev, move to previous folder with unseen messages
6 * -- as unseen, scan all unseen messages
7 * This code is Copyright (c) 2008, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
11 * Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
14 #include <sys/types.h>
17 #include <h/crawl_folders.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 static enum { NEW
, FNEXT
, FPREV
, UNSEEN
} run_mode
= NEW
;
36 /* check_folders uses this to maintain state with both .folders list of
37 * folders and with crawl_folders. */
39 struct node
**first
, **cur_node
;
46 /* Return the number of messages in a string list of message numbers. */
48 count_messages(char *field
)
54 field
= getcpy(field
);
56 /* copied from seq_read.c:seq_init */
57 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
58 if ((cp
= strchr(*ap
, '-')))
60 if ((j
= m_atoi (*ap
)) > 0) {
61 k
= cp
? m_atoi (cp
) : j
;
72 /* Return TRUE if the sequence 'name' is in 'sequences'. */
74 seq_in_list(char *name
, char *sequences
[])
78 for (i
= 0; sequences
[i
] != NULL
; i
++) {
79 if (strcmp(name
, sequences
[i
]) == 0) {
87 /* Return the string list of message numbers from the sequences file, or NULL
90 get_msgnums(char *folder
, char *sequences
[])
95 char name
[NAMESZ
], field
[BUFSIZ
];
97 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
98 int failed_to_lock
= 0;
99 m_getfld_state_t gstate
= 0;
101 /* copied from seq_read.c:seq_public */
103 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
104 * the "mh-sequences" profile entry, but left it empty),
105 * then just return, and do not initialize any public sequences.
107 if (mh_seq
== NULL
|| *mh_seq
== '\0')
110 /* get filename of sequence file */
111 seqfile
= concat(m_maildir(folder
), "/", mh_seq
, (void *)NULL
);
116 if ((fp
= lkfopendata (seqfile
, "r", & failed_to_lock
)) == NULL
) {
118 if (failed_to_lock
) {
119 adios (seqfile
, "failed to lock");
126 /* Use m_getfld to scan sequence file */
128 int fieldsz
= sizeof field
;
129 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
)) {
132 if (state
== FLDPLUS
) {
134 while (state
== FLDPLUS
) {
135 fieldsz
= sizeof field
;
136 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
);
137 cp
= add (field
, cp
);
140 /* Here's where we differ from seq_public: if it's in a
141 * sequence we want, save the list of messages. */
142 if (seq_in_list(name
, sequences
)) {
143 this_msgnums
= trimcpy(cp
);
144 if (msgnums
== NULL
) {
145 msgnums
= this_msgnums
;
147 old_msgnums
= msgnums
;
148 msgnums
= concat(old_msgnums
, " ",
149 this_msgnums
, (void *)NULL
);
157 if (seq_in_list(name
, sequences
)) {
158 this_msgnums
= trimcpy(field
);
159 if (msgnums
== NULL
) {
160 msgnums
= this_msgnums
;
162 old_msgnums
= msgnums
;
163 msgnums
= concat(old_msgnums
, " ",
164 this_msgnums
, (void *)NULL
);
174 adios (NULL
, "no blank lines are permitted in %s", seqfile
);
181 adios (NULL
, "%s is poorly formatted", seqfile
);
183 break; /* break from for loop */
185 m_getfld_state_destroy (&gstate
);
187 lkfclosedata (fp
, seqfile
);
194 /* Check `folder' (of length `len') for interesting messages, filling in the
197 check_folder(char *folder
, size_t len
, struct list_state
*b
)
199 char *msgnums
= get_msgnums(folder
, b
->sequences
);
200 int is_cur
= strcmp(folder
, b
->cur
) == 0;
202 if (is_cur
|| msgnums
!= NULL
) {
203 if (*b
->first
== NULL
) {
204 *b
->first
= b
->node
= mh_xmalloc(sizeof(*b
->node
));
206 b
->node
->n_next
= mh_xmalloc(sizeof(*b
->node
));
207 b
->node
= b
->node
->n_next
;
209 b
->node
->n_name
= folder
;
210 b
->node
->n_field
= msgnums
;
212 if (*b
->maxlen
< len
) {
217 /* Save the node for the current folder, so we can fall back to it. */
219 *b
->cur_node
= b
->node
;
224 crawl_callback(char *folder
, void *baton
)
226 check_folder(folder
, strlen(folder
), baton
);
230 /* Scan folders, returning:
231 * first -- list of nodes for all folders which have desired messages;
232 * if the current folder is listed in .folders, it is also in
233 * the list regardless of whether it has any desired messages
234 * last -- last node in list
235 * cur_node -- node of current folder, if listed in .folders
236 * maxlen -- length of longest folder name
238 * `cur' points to the name of the current folder, `folders' points to the
239 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
240 * the array of sequences for which to look.
242 * An empty list is returned as first=last=NULL.
245 check_folders(struct node
**first
, struct node
**last
,
246 struct node
**cur_node
, size_t *maxlen
,
247 char *cur
, char *folders
, char *sequences
[])
254 *first
= *last
= *cur_node
= NULL
;
258 b
.cur_node
= cur_node
;
261 b
.sequences
= sequences
;
263 if (folders
== NULL
) {
264 if (chdir(m_maildir("")) < 0) {
265 advise (m_maildir(""), "chdir");
267 crawl_folders(".", crawl_callback
, &b
);
269 fp
= fopen(folders
, "r");
271 adios(NULL
, "failed to read %s", folders
);
273 while (vfgets(fp
, &line
) == OK
) {
274 len
= strlen(line
) - 1;
276 check_folder(getcpy(line
), len
, &b
);
281 if (*first
!= NULL
) {
282 b
.node
->n_next
= NULL
;
287 /* Return a single string of the `sequences' joined by a space (' '). */
289 join_sequences(char *sequences
[])
295 for (i
= 0; sequences
[i
] != NULL
; i
++) {
296 len
+= strlen(sequences
[i
]) + 1;
298 result
= mh_xmalloc(len
+ 1);
300 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
301 len
= strlen(sequences
[i
]);
302 memcpy(cp
, sequences
[i
], len
);
305 /* -1 to overwrite the last delimiter */
311 /* Return a struct node for the folder to change to. This is the next
312 * (previous, if FPREV mode) folder with desired messages, or the current
313 * folder if no folders have desired. If NEW or UNSEEN mode, print the
314 * output but don't change folders.
316 * n_name is the folder to change to, and n_field is the string list of
317 * desired message numbers.
320 doit(char *cur
, char *folders
, char *sequences
[])
322 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
324 int count
, total
= 0;
325 char *command
= NULL
, *sequences_s
= NULL
;
327 if (cur
== NULL
|| cur
[0] == '\0') {
331 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
334 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
336 /* No folders at all... */
338 } else if (first
->n_next
== NULL
) {
339 /* We have only one node; any desired messages in it? */
340 if (first
->n_field
== NULL
) {
345 } else if (cur_node
== NULL
) {
346 /* Current folder is not listed in .folders, return first. */
349 } else if (run_mode
== UNSEEN
) {
350 sequences_s
= join_sequences(sequences
);
353 for (node
= first
, prev
= NULL
;
355 prev
= node
, node
= node
->n_next
) {
356 if (run_mode
== FNEXT
) {
357 /* If we have a previous node and it is the current
358 * folder, return this node. */
359 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
362 } else if (run_mode
== FPREV
) {
363 if (strcmp(node
->n_name
, cur
) == 0) {
364 /* Found current folder in fprev mode; if we have a
365 * previous node in the list, return it; else return
372 } else if (run_mode
== UNSEEN
) {
375 if (node
->n_field
== NULL
) {
379 printf("\n%d %s messages in %s",
380 count_messages(node
->n_field
),
383 if (strcmp(node
->n_name
, cur
) == 0) {
384 puts(" (*: current folder)");
390 /* TODO: Split enough of scan.c out so that we can call it here. */
391 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
393 status
= system(command
);
394 if (! WIFEXITED (status
)) {
395 adios (command
, "system");
399 if (node
->n_field
== NULL
) {
403 count
= count_messages(node
->n_field
);
406 printf("%-*s %6d.%c %s\n",
407 (int) folder_len
, node
->n_name
,
409 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
414 /* If we're fnext, we haven't checked the last node yet. If it's the
415 * current folder, return the first node. */
416 if (run_mode
== FNEXT
) {
417 assert(last
!= NULL
);
418 if (strcmp(last
->n_name
, cur
) == 0) {
423 if (run_mode
== NEW
) {
424 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
431 main(int argc
, char **argv
)
433 char **ap
, *cp
, **argp
, **arguments
;
435 char *folders
= NULL
;
436 svector_t sequences
= svector_create (0);
441 if (nmh_init(argv
[0], 1)) { return 1; }
443 arguments
= getarguments (invo_name
, argc
, argv
, 1);
449 while ((cp
= *argp
++)) {
451 switch (smatch (++cp
, switches
)) {
453 ambigsw (cp
, switches
);
456 adios (NULL
, "-%s unknown", cp
);
459 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
461 print_help (help
, switches
, 1);
464 print_version(invo_name
);
468 if (!(folders
= *argp
++) || *folders
== '-')
469 adios(NULL
, "missing argument to %s", argp
[-2]);
472 if (!(invo_name
= *argp
++) || *invo_name
== '-')
473 adios(NULL
, "missing argument to %s", argp
[-2]);
474 invo_name
= r1bindex(invo_name
, '/');
478 /* have a sequence argument */
479 if (!seq_in_list(cp
, svector_strs (sequences
))) {
480 svector_push_back (sequences
, cp
);
485 if (strcmp(invo_name
, "fnext") == 0) {
487 } else if (strcmp(invo_name
, "fprev") == 0) {
489 } else if (strcmp(invo_name
, "unseen") == 0) {
493 if (folders
== NULL
) {
496 if (folders
[0] != '/') {
497 folders
= m_maildir(folders
);
502 /* no sequence arguments; use unseen */
503 unseen
= context_find(usequence
);
504 if (unseen
== NULL
|| unseen
[0] == '\0') {
505 adios(NULL
, "must specify sequences or set %s", usequence
);
507 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
508 svector_push_back (sequences
, *ap
);
513 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
514 if (folder
== NULL
) {
519 if (run_mode
== UNSEEN
) {
520 /* All the scan(1)s it runs change the current folder, so we
521 * need to put it back. Unfortunately, context_replace lamely
522 * ignores the new value you give it if it is the same one it
523 * has in memory. So, we'll be lame, too. I'm not sure if i
524 * should just change context_replace... */
525 context_replace(pfolder
, "defeat_context_replace_optimization");
528 /* update current folder */
529 context_replace(pfolder
, folder
->n_name
);
531 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
532 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
537 svector_free (sequences
);