]>
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 /* 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
[BUFSIZ
];
103 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
104 int failed_to_lock
= 0;
105 m_getfld_state_t gstate
= 0;
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 if (failed_to_lock
) {
125 adios (seqfile
, "failed to lock");
132 /* Use m_getfld to scan sequence file */
134 int fieldsz
= sizeof field
;
135 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
)) {
138 if (state
== FLDPLUS
) {
140 while (state
== FLDPLUS
) {
141 fieldsz
= sizeof field
;
142 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
);
143 cp
= add (field
, cp
);
146 /* Here's where we differ from seq_public: if it's in a
147 * sequence we want, save the list of messages. */
148 if (seq_in_list(name
, sequences
)) {
149 this_msgnums
= trimcpy(cp
);
150 if (msgnums
== NULL
) {
151 msgnums
= this_msgnums
;
153 old_msgnums
= msgnums
;
154 msgnums
= concat(old_msgnums
, " ",
155 this_msgnums
, (void *)NULL
);
163 if (seq_in_list(name
, sequences
)) {
164 this_msgnums
= trimcpy(field
);
165 if (msgnums
== NULL
) {
166 msgnums
= this_msgnums
;
168 old_msgnums
= msgnums
;
169 msgnums
= concat(old_msgnums
, " ",
170 this_msgnums
, (void *)NULL
);
180 adios (NULL
, "no blank lines are permitted in %s", seqfile
);
187 adios (NULL
, "%s is poorly formatted", seqfile
);
189 break; /* break from for loop */
191 m_getfld_state_destroy (&gstate
);
193 lkfclosedata (fp
, seqfile
);
200 /* Check `folder' (of length `len') for interesting messages, filling in the
203 check_folder(char *folder
, size_t len
, struct list_state
*b
)
205 char *msgnums
= get_msgnums(folder
, b
->sequences
);
206 int is_cur
= strcmp(folder
, b
->cur
) == 0;
208 if (is_cur
|| msgnums
!= NULL
) {
209 if (*b
->first
== NULL
) {
213 NEW(b
->node
->n_next
);
214 b
->node
= b
->node
->n_next
;
216 b
->node
->n_name
= folder
;
217 b
->node
->n_field
= msgnums
;
219 if (*b
->maxlen
< len
) {
224 /* Save the node for the current folder, so we can fall back to it. */
226 *b
->cur_node
= b
->node
;
231 crawl_callback(char *folder
, void *baton
)
233 check_folder(folder
, strlen(folder
), baton
);
237 /* Scan folders, returning:
238 * first -- list of nodes for all folders which have desired messages;
239 * if the current folder is listed in .folders, it is also in
240 * the list regardless of whether it has any desired messages
241 * last -- last node in list
242 * cur_node -- node of current folder, if listed in .folders
243 * maxlen -- length of longest folder name
245 * `cur' points to the name of the current folder, `folders' points to the
246 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
247 * the array of sequences for which to look.
249 * An empty list is returned as first=last=NULL.
252 check_folders(struct node
**first
, struct node
**last
,
253 struct node
**cur_node
, size_t *maxlen
,
254 char *cur
, char *folders
, char *sequences
[])
261 *first
= *last
= *cur_node
= NULL
;
265 b
.cur_node
= cur_node
;
268 b
.sequences
= sequences
;
270 if (folders
== NULL
) {
271 if (chdir(m_maildir("")) < 0) {
272 advise (m_maildir(""), "chdir");
274 crawl_folders(".", crawl_callback
, &b
);
276 fp
= fopen(folders
, "r");
278 adios(NULL
, "failed to read %s", folders
);
280 while (vfgets(fp
, &line
) == OK
) {
281 len
= strlen(line
) - 1;
283 check_folder(getcpy(line
), len
, &b
);
288 if (*first
!= NULL
) {
289 b
.node
->n_next
= NULL
;
294 /* Return a single string of the `sequences' joined by a space (' '). */
296 join_sequences(char *sequences
[])
302 for (i
= 0; sequences
[i
] != NULL
; i
++) {
303 len
+= strlen(sequences
[i
]) + 1;
305 result
= mh_xmalloc(len
+ 1);
307 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
308 len
= strlen(sequences
[i
]);
309 memcpy(cp
, sequences
[i
], len
);
312 /* -1 to overwrite the last delimiter */
318 /* Return a struct node for the folder to change to. This is the next
319 * (previous, if RM_FPREV mode) folder with desired messages, or the current
320 * folder if no folders have desired. If RM_NEW or RM_UNSEEN mode, print the
321 * output but don't change folders.
323 * n_name is the folder to change to, and n_field is the string list of
324 * desired message numbers.
327 doit(char *cur
, char *folders
, char *sequences
[])
329 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
331 int count
, total
= 0;
332 char *command
= NULL
, *sequences_s
= NULL
;
334 if (cur
== NULL
|| cur
[0] == '\0') {
338 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
341 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
343 /* No folders at all... */
345 } else if (first
->n_next
== NULL
) {
346 /* We have only one node; any desired messages in it? */
347 if (first
->n_field
== NULL
) {
352 } else 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
379 } else if (run_mode
== RM_UNSEEN
) {
382 if (node
->n_field
== NULL
) {
386 printf("\n%d %s messages in %s",
387 count_messages(node
->n_field
),
390 if (strcmp(node
->n_name
, cur
) == 0) {
391 puts(" (*: current folder)");
397 /* TODO: Split enough of scan.c out so that we can call it here. */
398 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
400 status
= system(command
);
401 if (! WIFEXITED (status
)) {
402 adios (command
, "system");
406 if (node
->n_field
== NULL
) {
410 count
= count_messages(node
->n_field
);
413 printf("%-*s %6d.%c %s\n",
414 (int) folder_len
, node
->n_name
,
416 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
421 /* If we're fnext, we haven't checked the last node yet. If it's the
422 * current folder, return the first node. */
423 if (run_mode
== RM_FNEXT
) {
424 assert(last
!= NULL
);
425 if (strcmp(last
->n_name
, cur
) == 0) {
430 if (run_mode
== RM_NEW
) {
431 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
438 main(int argc
, char **argv
)
440 char **ap
, *cp
, **argp
, **arguments
;
442 char *folders
= NULL
;
443 svector_t sequences
= svector_create (0);
448 if (nmh_init(argv
[0], 1)) { return 1; }
450 arguments
= getarguments (invo_name
, argc
, argv
, 1);
456 while ((cp
= *argp
++)) {
458 switch (smatch (++cp
, switches
)) {
460 ambigsw (cp
, switches
);
463 adios (NULL
, "-%s unknown", cp
);
466 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
468 print_help (help
, switches
, 1);
471 print_version(invo_name
);
475 if (!(folders
= *argp
++) || *folders
== '-')
476 adios(NULL
, "missing argument to %s", argp
[-2]);
479 if (!(invo_name
= *argp
++) || *invo_name
== '-')
480 adios(NULL
, "missing argument to %s", argp
[-2]);
481 invo_name
= r1bindex(invo_name
, '/');
485 /* have a sequence argument */
486 if (!seq_in_list(cp
, svector_strs (sequences
))) {
487 svector_push_back (sequences
, cp
);
492 if (strcmp(invo_name
, "fnext") == 0) {
494 } else if (strcmp(invo_name
, "fprev") == 0) {
496 } else if (strcmp(invo_name
, "unseen") == 0) {
497 run_mode
= RM_UNSEEN
;
500 if (folders
== NULL
) {
503 if (folders
[0] != '/') {
504 folders
= m_maildir(folders
);
509 /* no sequence arguments; use unseen */
510 unseen
= context_find(usequence
);
511 if (unseen
== NULL
|| unseen
[0] == '\0') {
512 adios(NULL
, "must specify sequences or set %s", usequence
);
514 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
515 svector_push_back (sequences
, *ap
);
520 folder
= doit(context_find(pfolder
), folders
, svector_strs (sequences
));
521 if (folder
== NULL
) {
526 if (run_mode
== RM_UNSEEN
) {
527 /* All the scan(1)s it runs change the current folder, so we
528 * need to put it back. Unfortunately, context_replace lamely
529 * ignores the new value you give it if it is the same one it
530 * has in memory. So, we'll be lame, too. I'm not sure if i
531 * should just change context_replace... */
532 context_replace(pfolder
, "defeat_context_replace_optimization");
535 /* update current folder */
536 context_replace(pfolder
, folder
->n_name
);
538 if (run_mode
== RM_FNEXT
|| run_mode
== RM_FPREV
) {
539 printf("%s %s\n", folder
->n_name
, folder
->n_field
);
544 svector_free (sequences
);