]>
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 static struct swit switches
[] = {
36 static enum { NEW
, FNEXT
, FPREV
, UNSEEN
} run_mode
= NEW
;
38 /* check_folders uses this to maintain state with both .folders list of
39 * folders and with crawl_folders. */
41 struct node
**first
, **cur_node
;
48 /* Return the number of messages in a string list of message numbers. */
50 count_messages(char *field
)
56 field
= getcpy(field
);
58 /* copied from seq_read.c:seq_init */
59 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
60 if ((cp
= strchr(*ap
, '-')))
62 if ((j
= m_atoi (*ap
)) > 0) {
63 k
= cp
? m_atoi (cp
) : j
;
74 /* Return TRUE if the sequence 'name' is in 'sequences'. */
76 seq_in_list(char *name
, char *sequences
[])
80 for (i
= 0; sequences
[i
] != NULL
; i
++) {
81 if (strcmp(name
, sequences
[i
]) == 0) {
89 /* Return the string list of message numbers from the sequences file, or NULL
92 get_msgnums(char *folder
, char *sequences
[])
94 char *seqfile
= concat(m_maildir(folder
), "/", mh_seq
, (void *)NULL
);
95 FILE *fp
= fopen(seqfile
, "r");
97 char name
[NAMESZ
], field
[BUFSIZ
];
99 char *msgnums
= NULL
, *this_msgnums
, *old_msgnums
;
101 /* no sequences file -> no messages */
106 /* copied from seq_read.c:seq_public */
107 for (state
= FLD
;;) {
108 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), fp
)) {
112 if (state
== FLDPLUS
) {
114 while (state
== FLDPLUS
) {
115 state
= m_getfld (state
, name
, field
,
117 cp
= add (field
, cp
);
120 /* Here's where we differ from seq_public: if it's in a
121 * sequence we want, save the list of messages. */
122 if (seq_in_list(name
, sequences
)) {
123 this_msgnums
= trimcpy(cp
);
124 if (msgnums
== NULL
) {
125 msgnums
= this_msgnums
;
127 old_msgnums
= msgnums
;
128 msgnums
= concat(old_msgnums
, " ",
129 this_msgnums
, (void *)NULL
);
137 if (seq_in_list(name
, sequences
)) {
138 this_msgnums
= trimcpy(field
);
139 if (msgnums
== NULL
) {
140 msgnums
= this_msgnums
;
142 old_msgnums
= msgnums
;
143 msgnums
= concat(old_msgnums
, " ",
144 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 */
174 /* Check `folder' (of length `len') for interesting messages, filling in the
177 check_folder(char *folder
, size_t len
, struct list_state
*b
)
179 char *msgnums
= get_msgnums(folder
, b
->sequences
);
180 int is_cur
= strcmp(folder
, b
->cur
) == 0;
182 if (is_cur
|| msgnums
!= NULL
) {
183 if (*b
->first
== NULL
) {
184 *b
->first
= b
->node
= mh_xmalloc(sizeof(*b
->node
));
186 b
->node
->n_next
= mh_xmalloc(sizeof(*b
->node
));
187 b
->node
= b
->node
->n_next
;
189 b
->node
->n_name
= folder
;
190 b
->node
->n_field
= msgnums
;
192 if (*b
->maxlen
< len
) {
197 /* Save the node for the current folder, so we can fall back to it. */
199 *b
->cur_node
= b
->node
;
204 crawl_callback(char *folder
, void *baton
)
206 check_folder(folder
, strlen(folder
), baton
);
210 /* Scan folders, returning:
211 * first -- list of nodes for all folders which have desired messages;
212 * if the current folder is listed in .folders, it is also in
213 * the list regardless of whether it has any desired messages
214 * last -- last node in list
215 * cur_node -- node of current folder, if listed in .folders
216 * maxlen -- length of longest folder name
218 * `cur' points to the name of the current folder, `folders' points to the
219 * name of a .folder (if NULL, crawl all folders), and `sequences' points to
220 * the array of sequences for which to look.
223 check_folders(struct node
**first
, struct node
**last
,
224 struct node
**cur_node
, size_t *maxlen
,
225 char *cur
, char *folders
, char *sequences
[])
232 *first
= *cur_node
= NULL
;
236 b
.cur_node
= cur_node
;
239 b
.sequences
= sequences
;
241 if (folders
== NULL
) {
242 chdir(m_maildir(""));
243 crawl_folders(".", crawl_callback
, &b
);
245 fp
= fopen(folders
, "r");
247 adios(NULL
, "failed to read %s", folders
);
249 while (vfgets(fp
, &line
) == OK
) {
250 len
= strlen(line
) - 1;
252 check_folder(getcpy(line
), len
, &b
);
257 if (*first
!= NULL
) {
258 b
.node
->n_next
= NULL
;
263 /* Return a single string of the `sequences' joined by a space (' '). */
265 join_sequences(char *sequences
[])
271 for (i
= 0; sequences
[i
] != NULL
; i
++) {
272 len
+= strlen(sequences
[i
]) + 1;
274 result
= mh_xmalloc(len
+ 1);
276 for (i
= 0, cp
= result
; sequences
[i
] != NULL
; i
++, cp
+= len
+ 1) {
277 len
= strlen(sequences
[i
]);
278 memcpy(cp
, sequences
[i
], len
);
281 /* -1 to overwrite the last delimiter */
287 /* Return a struct node for the folder to change to. This is the next
288 * (previous, if FPREV mode) folder with desired messages, or the current
289 * folder if no folders have desired. If NEW or UNSEEN mode, print the
290 * output but don't change folders.
292 * n_name is the folder to change to, and n_field is the string list of
293 * desired message numbers.
296 doit(char *cur
, char *folders
, char *sequences
[])
298 struct node
*first
, *cur_node
, *node
, *last
, *prev
;
300 int count
, total
= 0;
301 char *command
= NULL
, *sequences_s
= NULL
;
303 if (cur
== NULL
|| cur
[0] == '\0') {
307 check_folders(&first
, &last
, &cur_node
, &folder_len
, cur
,
310 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
311 if (first
->n_next
== NULL
) {
312 /* We have only one node; any desired messages in it? */
313 if (first
->n_field
== NULL
) {
318 } else if (cur_node
== NULL
) {
319 /* Current folder is not listed in .folders, return first. */
322 } else if (run_mode
== UNSEEN
) {
323 sequences_s
= join_sequences(sequences
);
326 for (node
= first
, prev
= NULL
;
328 prev
= node
, node
= node
->n_next
) {
329 if (run_mode
== FNEXT
) {
330 /* If we have a previous node and it is the current
331 * folder, return this node. */
332 if (prev
!= NULL
&& strcmp(prev
->n_name
, cur
) == 0) {
335 } else if (run_mode
== FPREV
) {
336 if (strcmp(node
->n_name
, cur
) == 0) {
337 /* Found current folder in fprev mode; if we have a
338 * previous node in the list, return it; else return
345 } else if (run_mode
== UNSEEN
) {
346 if (node
->n_field
== NULL
) {
350 printf("\n%d %s messages in %s",
351 count_messages(node
->n_field
),
354 if (strcmp(node
->n_name
, cur
) == 0) {
355 puts(" (*: current folder)");
361 /* TODO: Split enough of scan.c out so that we can call it here. */
362 command
= concat("scan +", node
->n_name
, " ", sequences_s
,
367 if (node
->n_field
== NULL
) {
371 count
= count_messages(node
->n_field
);
374 printf("%-*s %6d.%c %s\n",
375 (int) folder_len
, node
->n_name
,
377 (strcmp(node
->n_name
, cur
) == 0 ? '*' : ' '),
382 /* If we're fnext, we haven't checked the last node yet. If it's the
383 * current folder, return the first node. */
384 if (run_mode
== FNEXT
&& strcmp(last
->n_name
, cur
) == 0) {
388 if (run_mode
== NEW
) {
389 printf("%-*s %6d.\n", (int) folder_len
, " total", total
);
396 main(int argc
, char **argv
)
398 char **ap
, *cp
, **argp
, **arguments
;
400 char *folders
= NULL
;
401 char *sequences
[NUMATTRS
+ 1];
407 setlocale(LC_ALL
, "");
409 invo_name
= r1bindex(argv
[0], '/');
411 /* read user profile/context */
414 arguments
= getarguments (invo_name
, argc
, argv
, 1);
420 while ((cp
= *argp
++)) {
422 switch (smatch (++cp
, switches
)) {
424 ambigsw (cp
, switches
);
427 adios (NULL
, "-%s unknown", cp
);
430 snprintf (help
, sizeof(help
), "%s [switches] [sequences]",
432 print_help (help
, switches
, 1);
435 print_version(invo_name
);
439 if (!(folders
= *argp
++) || *folders
== '-')
440 adios(NULL
, "missing argument to %s", argp
[-2]);
443 if (!(invo_name
= *argp
++) || *invo_name
== '-')
444 adios(NULL
, "missing argument to %s", argp
[-2]);
445 invo_name
= r1bindex(invo_name
, '/');
449 /* have a sequence argument */
450 if (!seq_in_list(cp
, sequences
)) {
455 if (strcmp(invo_name
, "fnext") == 0) {
457 } else if (strcmp(invo_name
, "fprev") == 0) {
459 } else if (strcmp(invo_name
, "unseen") == 0) {
463 if (folders
== NULL
) {
466 if (folders
[0] != '/') {
467 folders
= m_maildir(folders
);
472 /* no sequence arguments; use unseen */
473 unseen
= context_find(usequence
);
474 if (unseen
== NULL
|| unseen
[0] == '\0') {
475 adios(NULL
, "must specify sequences or set %s", usequence
);
477 for (ap
= brkstring(unseen
, " ", "\n"); *ap
; ap
++) {
478 sequences
[i
++] = *ap
;
483 folder
= doit(context_find(pfolder
), folders
, sequences
);
484 if (folder
== NULL
) {
489 if (run_mode
== UNSEEN
) {
490 /* All the scan(1)s it runs change the current folder, so we
491 * need to put it back. Unfortunately, context_replace lamely
492 * ignores the new value you give it if it is the same one it
493 * has in memory. So, we'll be lame, too. I'm not sure if i
494 * should just change context_replace... */
495 context_replace(pfolder
, "defeat_context_replace_optimization");
498 /* update current folder */
499 context_replace(pfolder
, folder
->n_name
);
501 if (run_mode
== FNEXT
|| run_mode
== FPREV
) {
502 printf("%s %s\n", folder
->n_name
, folder
->n_field
);