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)
177 BUFFY **tmp,*tmp1,*last;
178 char buf[_POSIX_PATH_MAX];
182 #endif /* BUFFY_SIZE */
186 * to get rid of correcting the ->prev pointers in sidebar.c,
187 * correct them right here
192 * if we really want to make the sort order of the sidebar
193 * configurable, this has to go right here
198 mutt_extract_token (path, s, 0);
199 strfcpy (buf, path->data, sizeof (buf));
201 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
203 for (tmp = &Incoming; *tmp;)
205 FREE (&((*tmp)->path));
213 mutt_expand_path (buf, sizeof (buf));
215 /* Skip empty tokens. */
218 /* simple check to avoid duplicates */
220 for (tmp = &Incoming; *tmp && dup == 0; tmp = &((*tmp)->next))
222 if (mutt_strcmp (buf, (*tmp)->path) == 0) {
232 if(data == M_UNMAILBOXES)
236 FREE (&((*tmp)->path));
244 /* loop over list while it's sorted */
247 for (tmp = &Incoming; *tmp ; tmp = &((*tmp)->next)) {
250 * change this to get whatever sorting order
252 if (mutt_strcmp (buf, (*tmp)->path) < 0) {
259 /* we want: last -> tmp -> tmp1 */
260 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
261 (*tmp)->path = safe_strdup (buf);
264 /* correct pointers */
271 (*tmp)->notified = 1;
272 (*tmp)->newly_created = 0;
275 /* for buffy_size, it is important that if the folder is new (tested by
276 * reading it), the size is set to 0 so that later when we check we see
277 * that it increased . without buffy_size we probably don't care.
279 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
281 /* some systems out there don't have an off_t type */
282 (*tmp)->size = (long) sb.st_size;
286 #endif /* BUFFY_SIZE */
292 /* people use buffy_size on systems where modified time attributes are BADLY
293 * broken. Ignore them.
295 #define STAT_CHECK (sb.st_size > tmp->size)
297 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
298 #endif /* BUFFY_SIZE */
301 * 0 don't force any checks + update sidebar
302 * 1 force all checks + update sidebar
303 * 2 force all checks + _don't_ update sidebar
305 int mutt_buffy_check (int force)
311 char path[_POSIX_PATH_MAX];
312 struct stat contex_sb;
313 time_t now, last1, last2;
316 /* update postponed count as well, on force */
318 mutt_update_num_postponed ();
321 /* fastest return if there are no mailboxes */
325 if (force == 0 && (now - BuffyTime < BuffyTimeout)
327 && (now - ImapBuffyTime < ImapBuffyTimeout))
334 if (force != 0 || now - BuffyTime >= BuffyTimeout)
337 last2 = ImapBuffyTime;
338 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
345 if (!Context || Context->magic != M_IMAP)
348 if (!Context || Context->magic != M_POP)
351 if (!Context || Context->magic != M_NNTP)
353 /* check device ID and serial number instead of comparing paths */
354 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
360 for (tmp = Incoming; tmp; tmp = tmp->next)
363 if (mx_is_imap (tmp->path))
368 if (mx_is_pop (tmp->path))
373 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
377 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
378 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
380 /* if the mailbox still doesn't exist, set the newly created flag to
381 * be ready for when it does. */
382 tmp->newly_created = 1;
390 /* check to see if the folder is the currently selected folder
392 if (!Context || !Context->path ||
396 || tmp->magic == M_IMAP
399 || tmp->magic == M_POP
402 || tmp->magic == M_NNTP
404 ) ? mutt_strcmp (tmp->path, Context->path) :
405 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
413 /* only check on force or $mail_check reached */
414 if (force != 0 || (now - last1 >= BuffyTimeout)) {
415 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
423 /* some other program has deleted mail from the folder */
424 tmp->size = (long) sb.st_size;
427 } else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
428 (STAT_CHECK || tmp->msgcount == 0)) {
429 /* sidebar visible */
431 if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
432 tmp->msgcount = ctx->msgcount;
434 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
435 tmp->msg_flagged = ctx->flagged;
436 mx_close_mailbox (ctx, 0);
439 if (tmp->newly_created &&
440 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
441 tmp->newly_created = 0;
442 } else if (tmp->new > 0)
447 /* only check on force or $mail_check reached */
448 if (force != 0 || (now - last1 >= BuffyTimeout)) {
449 snprintf (path, sizeof (path), "%s/new", tmp->path);
450 if ((dirp = opendir (path)) == NULL)
458 while ((de = readdir (dirp)) != NULL)
461 if (*de->d_name != '.' &&
462 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
464 /* one new and undeleted message is enough */
469 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
470 /* if sidebar invisible -> done */
480 if (SidebarWidth > 0 && option (OPTMBOXPANE))
482 /* only count total mail if sidebar visible */
483 snprintf (path, sizeof (path), "%s/cur", tmp->path);
484 if ((dirp = opendir (path)) == NULL)
489 tmp->msg_flagged = 0;
490 while ((de = readdir (dirp)) != NULL)
493 if (*de->d_name != '.' && (p = strstr (de->d_name, ":2,")) != NULL) {
494 if (!strchr (p + 3, 'T'))
496 if (strchr (p + 3, 'F'))
502 } else if (tmp->new > 0)
503 /* keep current stats if !force and !$mail_check reached */
508 /* only check on force or $mail_check reached */
509 if (force != 0 || (now - last1 >= BuffyTimeout)) {
510 if ((tmp->new = mh_buffy (tmp->path)) > 0)
512 if (SidebarWidth > 0 && option (OPTMBOXPANE))
516 if ((dp = opendir (path)) == NULL)
521 while ((de = readdir (dp)))
523 if (mh_valid_message (de->d_name))
532 } else if (tmp->new > 0)
533 /* keep current stats if !force and !$mail_check reached */
539 /* only check on force or $imap_mail_check reached */
540 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
541 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
542 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
544 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
550 } else if (tmp->new > 0)
551 /* keep current stats if !force and !$imap_mail_check reached */
568 else if (Context && Context->path)
569 tmp->size = (long) sb.st_size; /* update the size */
574 else if (!tmp->notified)
576 tmp->has_new = tmp->new > 0;
578 if (BuffyCount > 0 && force != 2)
579 draw_sidebar (CurrentMenu);
583 int mutt_buffy_list (void)
586 char path[_POSIX_PATH_MAX];
590 int have_unnotified = BuffyNotify;
592 if (option (OPTFORCEBUFFYCHECK))
593 mutt_buffy_check (1);
598 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
599 for (tmp = Incoming; tmp; tmp = tmp->next)
601 /* Is there new mail in this mailbox? */
602 if (!tmp->new || (have_unnotified && tmp->notified))
605 strfcpy (path, tmp->path, sizeof (path));
606 mutt_pretty_mailbox (path);
608 if (!first && pos + strlen (path) >= COLS - 7)
612 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
614 /* Prepend an asterisk to mailboxes not already notified */
617 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
621 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
626 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
630 /* on new mail: redraw sidebar */
631 draw_sidebar (CurrentMenu);
632 mutt_message ("%s", buffylist);
635 /* there were no mailboxes needing to be notified, so clean up since
636 * BuffyNotify has somehow gotten out of sync
642 int mutt_buffy_notify (void)
644 if (mutt_buffy_check (0) && BuffyNotify)
646 return (mutt_buffy_list ());
652 * mutt_buffy() -- incoming folders completion routine
654 * given a folder name, this routine gives the next incoming folder with new
657 void mutt_buffy (char *s, size_t slen)
660 BUFFY *tmp = Incoming;
662 mutt_expand_path (s, _POSIX_PATH_MAX);
663 switch (mutt_buffy_check (0))
672 while (tmp && !tmp->new)
677 mutt_buffy_check (1); /* buffy was wrong - resync things */
680 strfcpy (s, tmp->path, slen);
681 mutt_pretty_mailbox (s);
689 if (mutt_strcmp (s, tmp->path) == 0)
691 else if (count && tmp->new)
703 mutt_buffy_check (1); /* buffy was wrong - resync things */
706 strfcpy (s, tmp->path, slen);
707 mutt_pretty_mailbox (s);