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.
24 #include "mutt_curses.h"
42 static time_t BuffyTime = 0; /* last time we started checking for mail */
45 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
47 static short BuffyCount = 0; /* how many boxes with new mail */
48 static short BuffyNotify = 0; /* # of unnotified new boxes */
52 /* Find the last message in the file.
53 * upon success return 0. If no message found - return -1 */
55 static int fseeko_last_message (FILE * f)
58 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
60 int i; /* Index into `buffer' for scanning. */
62 memset (buffer, 0, sizeof (buffer));
63 fseeko (f, 0, SEEK_END);
66 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
67 * `bytes_read' <= `BUFSIZ'. */
68 bytes_read = pos % BUFSIZ;
71 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
72 * reads will be on block boundaries, which might increase efficiency. */
73 while ((pos -= bytes_read) >= 0) {
74 /* we save in the buffer at the end the first 7 chars from the last read */
75 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * str_len(CRLF) */
76 fseeko (f, pos, SEEK_SET);
77 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
80 for (i = bytes_read; --i >= 0;)
81 if (!str_ncmp (buffer + i, "\n\nFrom ", str_len ("\n\nFrom "))) { /* found it - go to the beginning of the From */
82 fseeko (f, pos + i + 2, SEEK_SET);
88 /* here we are at the beginning of the file */
89 if (!str_ncmp ("From ", buffer, 5)) {
97 /* Return 1 if the last message is new */
98 static int test_last_status_new (FILE * f)
101 ENVELOPE *tmp_envelope;
104 if (fseeko_last_message (f) == -1)
107 hdr = mutt_new_header ();
108 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
109 if (!(hdr->read || hdr->old))
112 mutt_free_envelope (&tmp_envelope);
113 mutt_free_header (&hdr);
118 static int test_new_folder (const char *path)
124 typ = mx_get_magic (path);
126 if (typ != M_MBOX && typ != M_MMDF)
129 if ((f = fopen (path, "rb"))) {
130 rc = test_last_status_new (f);
137 BUFFY *buffy_find_mailbox (const char *path)
143 if (stat (path, &sb) != 0)
146 if (!list_empty(Incoming)) {
147 for (i = 0; i < Incoming->length; i++) {
148 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
149 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
150 return ((BUFFY*) Incoming->data[i]);
156 void buffy_update_mailbox (BUFFY * b)
163 if (stat (b->path, &sb) == 0)
164 b->size = (long) sb.st_size;
171 /* func to free buffy for list_del() */
172 static void buffy_free (BUFFY** p) {
173 mem_free(&(*p)->path);
177 int buffy_lookup (const char* path) {
179 if (list_empty(Incoming) || !path || !*path)
181 for (i = 0; i < Incoming->length; i++) {
182 if (str_eq (((BUFFY*) Incoming->data[i])->path, path) )
188 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
192 char buf[_POSIX_PATH_MAX];
196 #endif /* BUFFY_SIZE */
198 while (MoreArgs (s)) {
199 mutt_extract_token (path, s, 0);
200 strfcpy (buf, path->data, sizeof (buf));
202 if (data == M_UNMAILBOXES && str_eq (buf, "*")) {
203 list_del (&Incoming, (list_del_t*) buffy_free);
207 /* Skip empty tokens. */
211 mutt_expand_path (buf, sizeof (buf));
212 i = buffy_lookup (buf);
214 if (data == M_UNMAILBOXES) {
216 tmp = (BUFFY*) list_pop_idx (Incoming, i);
223 tmp = mem_calloc (1, sizeof (BUFFY));
224 tmp->path = str_dup (buf);
226 list_push_back (&Incoming, tmp);
227 i = Incoming->length-1;
229 tmp = (BUFFY*) Incoming->data[i];
233 tmp->newly_created = 0;
236 /* for buffy_size, it is important that if the folder is new (tested by
237 * reading it), the size is set to 0 so that later when we check we see
238 * that it increased . without buffy_size we probably don't care.
240 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
241 /* some systems out there don't have an off_t type */
242 tmp->size = (long) sb.st_size;
246 #endif /* BUFFY_SIZE */
252 /* people use buffy_size on systems where modified time attributes are BADLY
253 * broken. Ignore them.
255 #define STAT_CHECK (sb.st_size > tmp->size)
257 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
258 #endif /* BUFFY_SIZE */
261 * 0 don't force any checks + update sidebar
262 * 1 force all checks + update sidebar
263 * 2 don't force any checks + _don't_ update sidebar
265 int buffy_check (int force)
271 char path[_POSIX_PATH_MAX];
272 struct stat contex_sb;
275 int i = 0, local = 0, count = 0;
279 /* update postponed count as well, on force */
281 mutt_update_num_postponed ();
284 /* fastest return if there are no mailboxes */
285 if (list_empty(Incoming))
288 if (force == 0 && (now - BuffyTime < BuffyTimeout)
290 && (now - ImapBuffyTime < ImapBuffyTimeout))
297 if (force == 1 || now - BuffyTime >= BuffyTimeout)
300 last2 = ImapBuffyTime;
301 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
307 count = sidebar_need_count ();
309 if (!Context || !Context->path ||
310 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
311 /* check device ID and serial number instead of comparing paths */
312 contex_sb.st_dev = 0;
313 contex_sb.st_ino = 0;
316 for (i = 0; i < Incoming->length; i++) {
317 tmp = (BUFFY*) Incoming->data[i];
318 tmp->magic = mx_get_magic (tmp->path);
319 local = mx_is_local (tmp->magic-1);
320 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
321 /* if the mailbox still doesn't exist, set the newly created flag to
322 * be ready for when it does. */
323 tmp->newly_created = 1;
331 /* check to see if the folder is the currently selected folder
333 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
334 sb.st_ino != contex_sb.st_ino) :
335 !str_eq (tmp->path, Context->path))) {
336 switch (tmp->magic) {
339 /* only check on force or $mail_check reached */
340 if (force == 1 || (now - last1 >= BuffyTimeout)) {
348 /* some other program has deleted mail from the folder */
349 tmp->size = (long) sb.st_size;
353 else if (STAT_CHECK || tmp->msgcount == 0) {
354 /* sidebar visible */
357 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
359 tmp->msgcount = ctx->msgcount;
361 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
362 tmp->msg_flagged = ctx->flagged;
363 mx_close_mailbox (ctx, 0);
366 if (tmp->newly_created &&
367 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
368 tmp->newly_created = 0;
370 else if (tmp->new > 0)
375 /* only check on force or $mail_check reached */
376 if (force == 1 || (now - last1 >= BuffyTimeout)) {
377 snprintf (path, sizeof (path), "%s/new", tmp->path);
378 if ((dirp = opendir (path)) == NULL) {
385 while ((de = readdir (dirp)) != NULL) {
388 if (*de->d_name != '.' &&
389 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
390 /* one new and undeleted message is enough */
395 /* if sidebar invisible -> done */
406 /* only count total mail if sidebar visible */
407 snprintf (path, sizeof (path), "%s/cur", tmp->path);
408 if ((dirp = opendir (path)) == NULL) {
412 tmp->msg_flagged = 0;
413 while ((de = readdir (dirp)) != NULL) {
416 if (*de->d_name != '.'
417 && (p = strstr (de->d_name, ":2,")) != NULL) {
418 if (!strchr (p + 3, 'T'))
420 if (strchr (p + 3, 'F'))
427 else if (tmp->new > 0)
428 /* keep current stats if !force and !$mail_check reached */
433 /* only check on force or $mail_check reached */
434 if (force == 1 || (now - last1 >= BuffyTimeout)) {
435 if ((tmp->new = mh_buffy (tmp->path)) > 0)
441 if ((dp = opendir (path)) == NULL)
446 while ((de = readdir (dp))) {
447 if (mh_valid_message (de->d_name)) {
456 else if (tmp->new > 0)
457 /* keep current stats if !force and !$mail_check reached */
463 /* only check on force or $imap_mail_check reached */
464 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
465 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
466 tmp->new = imap_mailbox_check (tmp->path, 1);
467 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
472 if (tmp->msg_unread < 0)
475 else if (tmp->new > 0)
476 /* keep current stats if !force and !$imap_mail_check reached */
484 else if (Context && Context->path)
485 tmp->size = (long) sb.st_size; /* update the size */
490 else if (!tmp->notified)
492 tmp->has_new = tmp->new > 0;
494 if (BuffyCount > 0 && force != 2)
495 sidebar_draw (CurrentMenu);
499 int buffy_list (void)
502 char path[_POSIX_PATH_MAX];
506 int have_unnotified = BuffyNotify;
512 pos += str_len (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
514 for (i = 0; i < Incoming->length; i++) {
515 tmp = (BUFFY*) Incoming->data[i];
516 /* Is there new mail in this mailbox? */
517 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
520 strfcpy (path, tmp->path, sizeof (path));
521 mutt_pretty_mailbox (path);
523 if (!first && pos + str_len (path) >= COLS - 7)
527 pos += str_len (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
529 /* Prepend an asterisk to mailboxes not already notified */
530 if (!tmp->notified) {
531 /* pos += str_len (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
535 pos += str_len (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
539 if (!first && i < Incoming->length) {
540 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
543 /* on new mail: redraw sidebar */
544 sidebar_draw (CurrentMenu);
545 mutt_message ("%s", buffylist);
548 /* there were no mailboxes needing to be notified, so clean up since
549 * BuffyNotify has somehow gotten out of sync
555 int buffy_notify (void)
557 if (buffy_check (0) && BuffyNotify) {
558 return (buffy_list ());
564 * mutt_buffy() -- incoming folders completion routine
566 * given a folder name, this routine gives the next incoming folder with new
569 void buffy_next (char *s, size_t slen)
571 int i = 0, c = 0, l = 0;
573 if (list_empty(Incoming))
576 mutt_expand_path (s, _POSIX_PATH_MAX);
577 if (buffy_check (0) == 0) {
583 * If buffy_lookup returns the index,
584 * or -1 if not found (-1..Incoming->length-1);
585 * plus one --> (0..Incoming->length).
586 * Modulo mapps it into the correct range.
588 i = 1 + buffy_lookup (s);
589 for (l=0; l < Incoming->length; l++) {
590 c = (l+i) % Incoming->length;
591 if ((!Context || !Context->path || !str_eq (((BUFFY*) Incoming->data[c])->path, Context->path)) &&
592 ((BUFFY*) Incoming->data[c])->new > 0)
595 if (l >= Incoming->length) {
597 /* something went wrong since we're here when buffy_check
598 * reported new mail */
601 strfcpy (s, ((BUFFY*) Incoming->data[c])->path, slen);
602 mutt_pretty_mailbox (s);