]>
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>
21 #include <h/crawl_folders.h>
24 #define NEW_SWITCHES \
25 X("mode", 1, MODESW) \
26 X("folders", 1, FOLDERSSW) \
27 X("version", 1, VERSIONSW) \
28 X("help", 1, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(NEW
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(NEW
, switches
);
38 static enum { NEW
, FNEXT
, FPREV
, UNSEEN
} run_mode
= NEW
;
40 /* check_folders uses this to maintain state with both .folders list of
41 * folders and with crawl_folders. */
43 struct node
**first
, **cur_node
;
50 /* Return the number of messages in a string list of message numbers. */
52 count_messages(char *field
)
58 field
= getcpy(field
);
60 /* copied from seq_read.c:seq_init */
61 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
62 if ((cp
= strchr(*ap
, '-')))
64 if ((j
= m_atoi (*ap
)) > 0) {
65 k
= cp
? m_atoi (cp
) : j
;
76 /* Return TRUE if the sequence 'name' is in 'sequences'. */
78 seq_in_list(char *name
, char *sequences
[])
82 for (i
= 0; sequences
[i
] != NULL
; i
++) {
83 if (strcmp(name
, sequences
[i
]) == 0) {
91 /* Return the string list of message numbers from the sequences file, or NULL
94 get_msgnums(char *folder
, char *sequences
[])
96 char *seqfile
= concat(m_maildir(folder
), "/", mh_seq
, (void *)NULL
);
97 FILE *fp
= fopen(seqfile
, "r");
99 char name
[NAMESZ
], field
[BUFSIZ
];
101 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
102 m_getfld_state_t gstate
= 0;
104 /* no sequences file -> no messages */
109 /* copied from seq_read.c:seq_public */
111 int fieldsz
= sizeof field
;
112 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
)) {
115 if (state
== FLDPLUS
) {
117 while (state
== FLDPLUS
) {
118 fieldsz
= sizeof field
;
119 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, fp
);
120 cp
= add (field
, cp
);
123 /* Here's where we differ from seq_public: if it's in a
124 * sequence we want, save the list of messages. */
125 if (seq_in_list(name
, sequences
)) {
126 this_msgnums
= trimcpy(cp
);
127 if (msgnums
== NULL
) {
128 msgnums
= this_msgnums
;
130 old_msgnums
= msgnums
;
131 msgnums
= concat(old_msgnums
, " ",
132 this_msgnums
, (void *)NULL
);
140 if (seq_in_list(name
, sequences
)) {
141 this_msgnums
= trimcpy(field
);
142 if (msgnums
== NULL
) {
143 msgnums
= this_msgnums
;
145 old_msgnums
= msgnums
;
146 msgnums
= concat(old_msgnums
, " ",
147 this_msgnums
, (void *)NULL
);
157 adios (NULL
, "no blank lines are permitted in %s", seqfile
);
164 adios (NULL
, "%s is poorly formatted", seqfile
);
166 break; /* break from for loop */
168 m_getfld_state_destroy (&gstate
);
175 /* Check `folder' (of length `len') for interesting messages, filling in the
178 check_folder(char *folder
, size_t len
, struct list_state
*b
)
180 char *msgnums
= get_msgnums(folder
, b
->sequences
);
181 int is_cur
= strcmp(folder
, b
->cur
) == 0;
183 if (is_cur
|| msgnums
!= NULL
) {
184 if (*b
->first
== NULL
) {
185 *b
->first
= b
->node
= mh_xmalloc(sizeof(*b
->node
));
187 b
->node
->n_next
= mh_xmalloc(sizeof(*b
->node
));
188 b
->node
= b
->node
->n_next
;
190 b
->node
->n_name
= folder
;
191 b
->node
->n_field
= msgnums
;
193 if (*b
->maxlen
< len
) {
198 /* Save the node for the current folder, so we can fall back to it. */
200 *b
->cur_node
= b
->node
;
205 crawl_callback(char *folder
, void *baton
)
207 check_folder(folder
, strlen(folder
), baton
);
211 /* Scan folders, returning:
212 * first -- list of nodes for all folders which have desired messages;
213 * if the current folder is listed in .folders, it is also in
214 * the list regardless of whether it has any desired messages
215 * last -- last node in list
216 * cur_node -- node of current folder, if listed in .folders
217 * maxlen -- length of longest folder name
219 * `cur' points to the name of the current folder, `folders' points to the
220 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
221 * the array of sequences for which to look.
223 * An empty list is returned as first=last=NULL.
226 check_folders(struct node
**first
, struct node
**last
,
227 struct node
**cur_node
, size_t *maxlen
,
228 char *cur
, char *folders
, char *sequences
[])
235 *first
= *last
= *cur_node
= NULL
;
239 b
.cur_node
= cur_node
;
242 b
.sequences
= sequences
;
244 if (folders
== NULL
) {
245 chdir(m_maildir(""));
246 crawl_folders(".", crawl_callback
, &b
);
248 fp
= fopen(folders
, "r");
250 adios(NULL
, "failed to read %s", folders
);
252 while (vfgets(fp
, &line
) == OK
) {
253 len
= strlen(line
) - 1;
255 check_folder(getcpy(line
), len
, &b
);
260 if (*first
!= NULL
) {
261 b
.node
->n_next
= NULL
;
266 /* Return a single string of the `sequences' joined by a space (' '). */
268 join_sequences(char *sequences
[])
274 for (i
= 0; sequences
[i
] != NULL
; i
++) {
275 len
+= strlen(sequences
[i
]) + 1;
277 result
= mh_xmalloc(len
+ 1);
279 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
280 len
= strlen(sequences
[i
]);
281 memcpy(cp
, sequences
[i
], len
);
284 /* -1 to overwrite the last delimiter */
290 /* Return a struct node for the folder to change to. This is the next
291 * (previous, if FPREV mode) folder with desired messages, or the current
292 * folder if no folders have desired. If NEW or UNSEEN mode, print the
293 * output but don't change folders.
295 * n_name is the folder to change to, and n_field is the string list of
296 * desired message numbers.
299 doit(char *cur
, char *folders
, char *sequences
[])
301 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
303 int count
, total
= 0;
304 char *command
= NULL
, *sequences_s
= NULL
;
306 if (cur
== NULL
|| cur
[0] == '\0') {
310 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
313 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
315 /* No folders at all... */
317 } else if (first
->n_next
== NULL
) {
318 /* We have only one node; any desired messages in it? */
319 if (first
->n_field
== NULL
) {
324 } else if (cur_node
== NULL
) {
325 /* Current folder is not listed in .folders, return first. */
328 } else if (run_mode
== UNSEEN
) {
329 sequences_s
= join_sequences(sequences
);
332 for (node
= first
, prev
= NULL
;
334 prev
= node
, node
= node
->n_next
) {
335 if (run_mode
== FNEXT
) {
336 /* If we have a previous node and it is the current
337 * folder, return this node. */
338 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
341 } else if (run_mode
== FPREV
) {
342 if (strcmp(node
->n_name
, cur
) == 0) {
343 /* Found current folder in fprev mode; if we have a
344 * previous node in the list, return it; else return
351 } else if (run_mode
== UNSEEN
) {
352 if (node
->n_field
== NULL
) {
356 printf("\n%d %s messages in %s",
357 count_messages(node
->n_field
),
360 if (strcmp(node
->n_name
, cur
) == 0) {
361 puts(" (*: current folder)");
367 /* TODO: Split enough of scan.c out so that we can call it here. */
368 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
373 if (node
->n_field
== NULL
) {
377 count
= count_messages(node
->n_field
);
380 printf("%-*s %6d.%c %s\n",
381 (int) folder_len
, node
->n_name
,
383 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
388 /* If we're fnext, we haven't checked the last node yet. If it's the
389 * current folder, return the first node. */
390 if (run_mode
== FNEXT
&& strcmp(last
->n_name
, cur
) == 0) {
394 if (run_mode
== NEW
) {
395 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
402 main(int argc
, char **argv
)
404 char **ap
, *cp
, **argp
, **arguments
;
406 char *folders
= NULL
;
407 char *sequences
[NUMATTRS
+ 1];
416 setlocale(LC_ALL
, "");
418 invo_name
= r1bindex(argv
[0], '/');
420 /* read user profile/context */
423 arguments
= getarguments (invo_name
, argc
, argv
, 1);
429 while ((cp
= *argp
++)) {
431 switch (smatch (++cp
, switches
)) {
433 ambigsw (cp
, switches
);
436 adios (NULL
, "-%s unknown", cp
);
439 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
441 print_help (help
, switches
, 1);
444 print_version(invo_name
);
448 if (!(folders
= *argp
++) || *folders
== '-')
449 adios(NULL
, "missing argument to %s", argp
[-2]);
452 if (!(invo_name
= *argp
++) || *invo_name
== '-')
453 adios(NULL
, "missing argument to %s", argp
[-2]);
454 invo_name
= r1bindex(invo_name
, '/');
458 /* have a sequence argument */
459 if (!seq_in_list(cp
, sequences
)) {
465 if (strcmp(invo_name
, "fnext") == 0) {
467 } else if (strcmp(invo_name
, "fprev") == 0) {
469 } else if (strcmp(invo_name
, "unseen") == 0) {
473 if (folders
== NULL
) {
476 if (folders
[0] != '/') {
477 folders
= m_maildir(folders
);
482 /* no sequence arguments; use unseen */
483 unseen
= context_find(usequence
);
484 if (unseen
== NULL
|| unseen
[0] == '\0') {
485 adios(NULL
, "must specify sequences or set %s", usequence
);
487 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
488 sequences
[i
++] = *ap
;
493 folder
= doit(context_find(pfolder
), folders
, sequences
);
494 if (folder
== NULL
) {
499 if (run_mode
== UNSEEN
) {
500 /* All the scan(1)s it runs change the current folder, so we
501 * need to put it back. Unfortunately, context_replace lamely
502 * ignores the new value you give it if it is the same one it
503 * has in memory. So, we'll be lame, too. I'm not sure if i
504 * should just change context_replace... */
505 context_replace(pfolder
, "defeat_context_replace_optimization");
508 /* update current folder */
509 context_replace(pfolder
, folder
->n_name
);
511 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
512 printf("%s %s\n", folder
->n_name
, folder
->n_field
);