2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Rocco Rutte <pdmef@cs.tu-berlin.de>
8 * This file is part of mutt-ng, see http://www.muttng.org/.
9 * It's licensed under the GNU General Public License,
10 * please see the file GPL in the top level source directory.
17 #include <lib-lib/mem.h>
26 #include "mutt_curses.h"
44 static time_t BuffyTime = 0; /* last time we started checking for mail */
47 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
49 static short BuffyCount = 0; /* how many boxes with new mail */
50 static short BuffyNotify = 0; /* # of unnotified new boxes */
54 /* Find the last message in the file.
55 * upon success return 0. If no message found - return -1 */
57 static int fseeko_last_message (FILE * f)
60 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
62 int i; /* Index into `buffer' for scanning. */
64 memset (buffer, 0, sizeof (buffer));
65 fseeko (f, 0, SEEK_END);
68 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
69 * `bytes_read' <= `BUFSIZ'. */
70 bytes_read = pos % BUFSIZ;
73 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
74 * reads will be on block boundaries, which might increase efficiency. */
75 while ((pos -= bytes_read) >= 0) {
76 /* we save in the buffer at the end the first 7 chars from the last read */
77 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * str_len(CRLF) */
78 fseeko (f, pos, SEEK_SET);
79 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
82 for (i = bytes_read; --i >= 0;)
83 if (!str_ncmp (buffer + i, "\n\nFrom ", str_len ("\n\nFrom "))) { /* found it - go to the beginning of the From */
84 fseeko (f, pos + i + 2, SEEK_SET);
90 /* here we are at the beginning of the file */
91 if (!str_ncmp ("From ", buffer, 5)) {
99 /* Return 1 if the last message is new */
100 static int test_last_status_new (FILE * f)
103 ENVELOPE *tmp_envelope;
106 if (fseeko_last_message (f) == -1)
109 hdr = mutt_new_header ();
110 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
111 if (!(hdr->read || hdr->old))
114 mutt_free_envelope (&tmp_envelope);
115 mutt_free_header (&hdr);
120 static int test_new_folder (const char *path)
126 typ = mx_get_magic (path);
128 if (typ != M_MBOX && typ != M_MMDF)
131 if ((f = fopen (path, "rb"))) {
132 rc = test_last_status_new (f);
139 BUFFY *buffy_find_mailbox (const char *path)
145 if (stat (path, &sb) != 0)
148 if (!list_empty(Incoming)) {
149 for (i = 0; i < Incoming->length; i++) {
150 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
151 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
152 return ((BUFFY*) Incoming->data[i]);
158 void buffy_update_mailbox (BUFFY * b)
165 if (stat (b->path, &sb) == 0)
166 b->size = (long) sb.st_size;
173 /* func to free buffy for list_del() */
174 static void buffy_free (BUFFY** p) {
175 p_delete(&(*p)->path);
179 int buffy_lookup (const char* path) {
181 if (list_empty(Incoming) || !path || !*path)
183 for (i = 0; i < Incoming->length; i++) {
184 if (str_eq (((BUFFY*) Incoming->data[i])->path, path) )
190 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
194 char buf[_POSIX_PATH_MAX];
198 #endif /* BUFFY_SIZE */
200 while (MoreArgs (s)) {
201 mutt_extract_token (path, s, 0);
202 strfcpy (buf, path->data, sizeof (buf));
204 if (data == M_UNMAILBOXES && str_eq (buf, "*")) {
205 list_del (&Incoming, (list_del_t*) buffy_free);
209 /* Skip empty tokens. */
213 mutt_expand_path (buf, sizeof (buf));
214 i = buffy_lookup (buf);
216 if (data == M_UNMAILBOXES) {
218 tmp = (BUFFY*) list_pop_idx (Incoming, i);
225 tmp = p_new(BUFFY, 1);
226 tmp->path = str_dup (buf);
228 list_push_back (&Incoming, tmp);
229 i = Incoming->length-1;
231 tmp = (BUFFY*) Incoming->data[i];
235 tmp->newly_created = 0;
238 /* for buffy_size, it is important that if the folder is new (tested by
239 * reading it), the size is set to 0 so that later when we check we see
240 * that it increased . without buffy_size we probably don't care.
242 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
243 /* some systems out there don't have an off_t type */
244 tmp->size = (long) sb.st_size;
248 #endif /* BUFFY_SIZE */
254 /* people use buffy_size on systems where modified time attributes are BADLY
255 * broken. Ignore them.
257 #define STAT_CHECK (sb.st_size > tmp->size)
259 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
260 #endif /* BUFFY_SIZE */
263 * 0 don't force any checks + update sidebar
264 * 1 force all checks + update sidebar
265 * 2 don't force any checks + _don't_ update sidebar
267 int buffy_check (int force)
273 char path[_POSIX_PATH_MAX];
274 struct stat contex_sb;
277 int i = 0, local = 0, count = 0;
281 /* update postponed count as well, on force */
283 mutt_update_num_postponed ();
286 /* fastest return if there are no mailboxes */
287 if (list_empty(Incoming))
290 if (force == 0 && (now - BuffyTime < BuffyTimeout)
292 && (now - ImapBuffyTime < ImapBuffyTimeout))
299 if (force == 1 || now - BuffyTime >= BuffyTimeout)
302 last2 = ImapBuffyTime;
303 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
309 count = sidebar_need_count ();
311 if (!Context || !Context->path ||
312 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
313 /* check device ID and serial number instead of comparing paths */
314 contex_sb.st_dev = 0;
315 contex_sb.st_ino = 0;
318 for (i = 0; i < Incoming->length; i++) {
319 tmp = (BUFFY*) Incoming->data[i];
320 tmp->magic = mx_get_magic (tmp->path);
321 local = mx_is_local (tmp->magic-1);
322 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
323 /* if the mailbox still doesn't exist, set the newly created flag to
324 * be ready for when it does. */
325 tmp->newly_created = 1;
333 /* check to see if the folder is the currently selected folder
335 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
336 sb.st_ino != contex_sb.st_ino) :
337 !str_eq (tmp->path, Context->path))) {
338 switch (tmp->magic) {
341 /* only check on force or $mail_check reached */
342 if (force == 1 || (now - last1 >= BuffyTimeout)) {
350 /* some other program has deleted mail from the folder */
351 tmp->size = (long) sb.st_size;
355 else if (STAT_CHECK || tmp->msgcount == 0) {
356 /* sidebar visible */
359 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
361 tmp->msgcount = ctx->msgcount;
363 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
364 tmp->msg_flagged = ctx->flagged;
365 mx_close_mailbox (ctx, 0);
368 if (tmp->newly_created &&
369 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
370 tmp->newly_created = 0;
372 else if (tmp->new > 0)
377 /* only check on force or $mail_check reached */
378 if (force == 1 || (now - last1 >= BuffyTimeout)) {
379 snprintf (path, sizeof (path), "%s/new", tmp->path);
380 if ((dirp = opendir (path)) == NULL) {
387 while ((de = readdir (dirp)) != NULL) {
390 if (*de->d_name != '.' &&
391 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
392 /* one new and undeleted message is enough */
396 /* if sidebar invisible -> done */
409 /* only count total mail if sidebar visible */
410 snprintf (path, sizeof (path), "%s/cur", tmp->path);
411 if ((dirp = opendir (path)) == NULL) {
415 tmp->msg_flagged = 0;
416 while ((de = readdir (dirp)) != NULL) {
419 if (*de->d_name != '.'
420 && (p = strstr (de->d_name, ":2,")) != NULL) {
421 if (!strchr (p + 3, 'T'))
423 if (strchr (p + 3, 'F'))
430 else if (tmp->new > 0)
431 /* keep current stats if !force and !$mail_check reached */
436 /* only check on force or $mail_check reached */
437 if (force == 1 || (now - last1 >= BuffyTimeout)) {
438 if ((tmp->new = mh_buffy (tmp->path)) > 0)
444 if ((dp = opendir (path)) == NULL)
449 while ((de = readdir (dp))) {
450 if (mh_valid_message (de->d_name)) {
459 else if (tmp->new > 0)
460 /* keep current stats if !force and !$mail_check reached */
466 /* only check on force or $imap_mail_check reached */
467 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
468 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
469 tmp->new = imap_mailbox_check (tmp->path, 1);
470 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
475 if (tmp->msg_unread < 0)
478 else if (tmp->new > 0)
479 /* keep current stats if !force and !$imap_mail_check reached */
487 else if (Context && Context->path)
488 tmp->size = (long) sb.st_size; /* update the size */
493 else if (!tmp->notified)
495 tmp->has_new = tmp->new > 0;
497 if (BuffyCount > 0 && force != 2)
498 sidebar_draw (CurrentMenu);
502 int buffy_list (void)
505 char path[_POSIX_PATH_MAX];
509 int have_unnotified = BuffyNotify;
515 pos += str_len (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
517 for (i = 0; i < Incoming->length; i++) {
518 tmp = (BUFFY*) Incoming->data[i];
519 /* Is there new mail in this mailbox? */
520 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
523 strfcpy (path, tmp->path, sizeof (path));
524 mutt_pretty_mailbox (path);
526 if (!first && pos + str_len (path) >= COLS - 7)
530 pos += str_len (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
532 /* Prepend an asterisk to mailboxes not already notified */
533 if (!tmp->notified) {
534 /* pos += str_len (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
538 pos += str_len (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
542 if (!first && i < Incoming->length) {
543 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
546 /* on new mail: redraw sidebar */
547 sidebar_draw (CurrentMenu);
548 mutt_message ("%s", buffylist);
551 /* there were no mailboxes needing to be notified, so clean up since
552 * BuffyNotify has somehow gotten out of sync
558 int buffy_notify (void)
560 if (buffy_check (0) && BuffyNotify) {
561 return (buffy_list ());
567 * mutt_buffy() -- incoming folders completion routine
569 * given a folder name, this routine gives the next incoming folder with new
572 void buffy_next (char *s, size_t slen)
574 int i = 0, c = 0, l = 0;
576 if (list_empty(Incoming))
579 mutt_expand_path (s, _POSIX_PATH_MAX);
580 if (buffy_check (0) == 0) {
586 * If buffy_lookup returns the index,
587 * or -1 if not found (-1..Incoming->length-1);
588 * plus one --> (0..Incoming->length).
589 * Modulo mapps it into the correct range.
591 i = 1 + buffy_lookup (s);
592 for (l=0; l < Incoming->length; l++) {
593 c = (l+i) % Incoming->length;
594 if ((!Context || !Context->path || !str_eq (((BUFFY*) Incoming->data[c])->path, Context->path)) &&
595 ((BUFFY*) Incoming->data[c])->new > 0)
598 if (l >= Incoming->length) {
600 /* something went wrong since we're here when buffy_check
601 * reported new mail */
604 strfcpy (s, ((BUFFY*) Incoming->data[c])->path, slen);
605 mutt_pretty_mailbox (s);