2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 #include "mutt_curses.h"
44 static time_t BuffyTime = 0; /* last time we started checking for mail */
46 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
48 static short BuffyCount = 0; /* how many boxes with new mail */
49 static short BuffyNotify = 0; /* # of unnotified new boxes */
53 /* Find the last message in the file.
54 * upon success return 0. If no message found - return -1 */
56 int fseek_last_message (FILE * f)
59 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
61 int i; /* Index into `buffer' for scanning. */
63 memset (buffer, 0, sizeof(buffer));
64 fseek (f, 0, SEEK_END);
67 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
68 * `bytes_read' <= `BUFSIZ'. */
69 bytes_read = pos % BUFSIZ;
72 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
73 * reads will be on block boundaries, which might increase efficiency. */
74 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 * mutt_strlen(CRLF) */
78 fseek (f, pos, SEEK_SET);
79 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
82 for (i = bytes_read; --i >= 0;)
83 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
84 { /* found it - go to the beginning of the From */
85 fseek (f, pos + i + 2, SEEK_SET);
91 /* here we are at the beginning of the file */
92 if (!mutt_strncmp ("From ", buffer, 5))
101 /* Return 1 if the last message is new */
102 int test_last_status_new (FILE * f)
105 ENVELOPE* tmp_envelope;
108 if (fseek_last_message (f) == -1)
111 hdr = mutt_new_header ();
112 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
113 if (!(hdr->read || hdr->old))
116 mutt_free_envelope(&tmp_envelope);
117 mutt_free_header (&hdr);
122 int test_new_folder (const char *path)
128 typ = mx_get_magic (path);
130 if (typ != M_MBOX && typ != M_MMDF)
133 if ((f = fopen (path, "rb")))
135 rc = test_last_status_new (f);
142 BUFFY *mutt_find_mailbox (const char *path)
148 if (stat (path,&sb) != 0)
151 for (tmp = Incoming; tmp; tmp = tmp->next)
153 if (stat (tmp->path,&tmp_sb) ==0 &&
154 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
160 void mutt_update_mailbox (BUFFY * b)
167 if (stat (b->path, &sb) == 0)
168 b->size = (long) sb.st_size;
175 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
178 char buf[_POSIX_PATH_MAX];
181 #endif /* BUFFY_SIZE */
185 mutt_extract_token (path, s, 0);
186 strfcpy (buf, path->data, sizeof (buf));
188 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
190 for (tmp = &Incoming; *tmp;)
192 FREE (&((*tmp)->path));
200 mutt_expand_path (buf, sizeof (buf));
202 /* Skip empty tokens. */
205 /* simple check to avoid duplicates */
206 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
208 if (mutt_strcmp (buf, (*tmp)->path) == 0)
212 if(data == M_UNMAILBOXES)
216 FREE (&((*tmp)->path));
226 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
227 (*tmp)->path = safe_strdup (buf);
229 /* it is tempting to set magic right here */
235 (*tmp)->notified = 1;
236 (*tmp)->newly_created = 0;
239 /* for buffy_size, it is important that if the folder is new (tested by
240 * reading it), the size is set to 0 so that later when we check we see
241 * that it increased . without buffy_size we probably don't care.
243 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
245 /* some systems out there don't have an off_t type */
246 (*tmp)->size = (long) sb.st_size;
250 #endif /* BUFFY_SIZE */
256 /* people use buffy_size on systems where modified time attributes are BADLY
257 * broken. Ignore them.
259 #define STAT_CHECK (sb.st_size > tmp->size)
261 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
262 #endif /* BUFFY_SIZE */
265 * 0 don't force any checks + update sidebar
266 * 1 force all checks + update sidebar
267 * 2 force all checks + _don't_ update sidebar
269 int mutt_buffy_check (int force)
275 char path[_POSIX_PATH_MAX];
276 struct stat contex_sb;
277 time_t now, last1, last2;
280 /* update postponed count as well, on force */
282 mutt_update_num_postponed ();
285 /* fastest return if there are no mailboxes */
289 if (force == 0 && (now - BuffyTime < BuffyTimeout)
291 && (now - ImapBuffyTime < ImapBuffyTimeout))
298 if (force != 0 || now - BuffyTime >= BuffyTimeout)
301 last2 = ImapBuffyTime;
302 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
309 if (!Context || Context->magic != M_IMAP)
312 if (!Context || Context->magic != M_POP)
315 if (!Context || Context->magic != M_NNTP)
317 /* check device ID and serial number instead of comparing paths */
318 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
324 for (tmp = Incoming; tmp; tmp = tmp->next)
327 if (mx_is_imap (tmp->path))
332 if (mx_is_pop (tmp->path))
337 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
341 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
342 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
344 /* if the mailbox still doesn't exist, set the newly created flag to
345 * be ready for when it does. */
346 tmp->newly_created = 1;
354 /* check to see if the folder is the currently selected folder
356 if (!Context || !Context->path ||
360 || tmp->magic == M_IMAP
363 || tmp->magic == M_POP
366 || tmp->magic == M_NNTP
368 ) ? mutt_strcmp (tmp->path, Context->path) :
369 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
377 /* only check on force or $mail_check reached */
378 if (force != 0 || (now - last1 >= BuffyTimeout)) {
379 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
387 /* some other program has deleted mail from the folder */
388 tmp->size = (long) sb.st_size;
391 } else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
392 (STAT_CHECK || tmp->msgcount == 0)) {
393 /* sidebar visible */
395 if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
396 tmp->msgcount = ctx->msgcount;
398 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
399 tmp->msg_flagged = ctx->flagged;
400 mx_close_mailbox (ctx, 0);
403 if (tmp->newly_created &&
404 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
405 tmp->newly_created = 0;
406 } else if (tmp->new > 0)
411 /* only check on force or $mail_check reached */
412 if (force != 0 || (now - last1 >= BuffyTimeout)) {
413 snprintf (path, sizeof (path), "%s/new", tmp->path);
414 if ((dirp = opendir (path)) == NULL)
422 while ((de = readdir (dirp)) != NULL)
425 if (*de->d_name != '.' &&
426 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
428 /* one new and undeleted message is enough */
433 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
434 /* if sidebar invisible -> done */
444 if (SidebarWidth > 0 && option (OPTMBOXPANE))
446 /* only count total mail if sidebar visible */
447 snprintf (path, sizeof (path), "%s/cur", tmp->path);
448 if ((dirp = opendir (path)) == NULL)
453 tmp->msg_flagged = 0;
454 while ((de = readdir (dirp)) != NULL)
457 if (*de->d_name != '.' && (p = strstr (de->d_name, ":2,")) != NULL) {
458 if (!strchr (p + 3, 'T'))
460 if (strchr (p + 3, 'F'))
466 } else if (tmp->new > 0)
467 /* keep current stats if !force and !$mail_check reached */
472 /* only check on force or $mail_check reached */
473 if (force != 0 || (now - last1 >= BuffyTimeout)) {
474 if ((tmp->new = mh_buffy (tmp->path)) > 0)
476 if (SidebarWidth > 0 && option (OPTMBOXPANE))
480 if ((dp = opendir (path)) == NULL)
485 while ((de = readdir (dp)))
487 if (mh_valid_message (de->d_name))
496 } else if (tmp->new > 0)
497 /* keep current stats if !force and !$mail_check reached */
503 /* only check on force or $imap_mail_check reached */
504 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
505 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
506 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
508 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
514 } else if (tmp->new > 0)
515 /* keep current stats if !force and !$imap_mail_check reached */
532 else if (Context && Context->path)
533 tmp->size = (long) sb.st_size; /* update the size */
538 else if (!tmp->notified)
540 tmp->has_new = tmp->new > 0;
542 if (BuffyCount > 0 && force != 2)
543 draw_sidebar (CurrentMenu);
547 int mutt_buffy_list (void)
550 char path[_POSIX_PATH_MAX];
554 int have_unnotified = BuffyNotify;
556 if (option (OPTFORCEBUFFYCHECK))
557 mutt_buffy_check (1);
562 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
563 for (tmp = Incoming; tmp; tmp = tmp->next)
565 /* Is there new mail in this mailbox? */
566 if (!tmp->new || (have_unnotified && tmp->notified))
569 strfcpy (path, tmp->path, sizeof (path));
570 mutt_pretty_mailbox (path);
572 if (!first && pos + strlen (path) >= COLS - 7)
576 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
578 /* Prepend an asterisk to mailboxes not already notified */
581 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
585 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
590 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
594 /* on new mail: redraw sidebar */
595 draw_sidebar (CurrentMenu);
596 mutt_message ("%s", buffylist);
599 /* there were no mailboxes needing to be notified, so clean up since
600 * BuffyNotify has somehow gotten out of sync
606 int mutt_buffy_notify (void)
608 if (mutt_buffy_check (0) && BuffyNotify)
610 return (mutt_buffy_list ());
616 * mutt_buffy() -- incoming folders completion routine
618 * given a folder name, this routine gives the next incoming folder with new
621 void mutt_buffy (char *s, size_t slen)
624 BUFFY *tmp = Incoming;
626 mutt_expand_path (s, _POSIX_PATH_MAX);
627 switch (mutt_buffy_check (0))
636 while (tmp && !tmp->new)
641 mutt_buffy_check (1); /* buffy was wrong - resync things */
644 strfcpy (s, tmp->path, slen);
645 mutt_pretty_mailbox (s);
653 if (mutt_strcmp (s, tmp->path) == 0)
655 else if (count && tmp->new)
667 mutt_buffy_check (1); /* buffy was wrong - resync things */
670 strfcpy (s, tmp->path, slen);
671 mutt_pretty_mailbox (s);