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 */
45 time_t BuffyDoneTime = 0; /* last time we knew for sure how much mail there was. */
46 static short BuffyCount = 0; /* how many boxes with new mail */
47 static short BuffyNotify = 0; /* # of unnotified new boxes */
51 /* Find the last message in the file.
52 * upon success return 0. If no message found - return -1 */
54 int fseek_last_message (FILE * f)
57 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
59 int i; /* Index into `buffer' for scanning. */
61 memset (buffer, 0, sizeof(buffer));
62 fseek (f, 0, SEEK_END);
65 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
66 * `bytes_read' <= `BUFSIZ'. */
67 bytes_read = pos % BUFSIZ;
70 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
71 * reads will be on block boundaries, which might increase efficiency. */
72 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 * mutt_strlen(CRLF) */
76 fseek (f, pos, SEEK_SET);
77 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
80 for (i = bytes_read; --i >= 0;)
81 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
82 { /* found it - go to the beginning of the From */
83 fseek (f, pos + i + 2, SEEK_SET);
89 /* here we are at the beginning of the file */
90 if (!mutt_strncmp ("From ", buffer, 5))
99 /* Return 1 if the last message is new */
100 int test_last_status_new (FILE * f)
103 ENVELOPE* tmp_envelope;
106 if (fseek_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 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")))
133 rc = test_last_status_new (f);
140 BUFFY *mutt_find_mailbox (const char *path)
146 if (stat (path,&sb) != 0)
149 for (tmp = Incoming; tmp; tmp = tmp->next)
151 if (stat (tmp->path,&tmp_sb) ==0 &&
152 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
158 void mutt_update_mailbox (BUFFY * b)
165 if (stat (b->path, &sb) == 0)
166 b->size = (long) sb.st_size;
173 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
175 BUFFY **tmp,*tmp1,*last;
176 char buf[_POSIX_PATH_MAX];
180 #endif /* BUFFY_SIZE */
184 * to get rid of correcting the ->prev pointers in sidebar.c,
185 * correct them right here
190 * if we really want to make the sort order of the sidebar
191 * configurable, this has to go right here
196 mutt_extract_token (path, s, 0);
197 strfcpy (buf, path->data, sizeof (buf));
199 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
201 for (tmp = &Incoming; *tmp;)
203 FREE (&((*tmp)->path));
211 mutt_expand_path (buf, sizeof (buf));
213 /* Skip empty tokens. */
216 /* simple check to avoid duplicates */
218 for (tmp = &Incoming; *tmp && dup == 0; tmp = &((*tmp)->next))
220 if (mutt_strcmp (buf, (*tmp)->path) == 0) {
230 if(data == M_UNMAILBOXES)
234 FREE (&((*tmp)->path));
242 /* loop over list while it's sorted */
245 for (tmp = &Incoming; *tmp ; tmp = &((*tmp)->next)) {
248 * change this to get whatever sorting order
250 if (mutt_strcmp (buf, (*tmp)->path) < 0) {
257 /* we want: last -> tmp -> tmp1 */
258 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
259 (*tmp)->path = safe_strdup (buf);
262 /* correct pointers */
269 (*tmp)->notified = 1;
270 (*tmp)->newly_created = 0;
273 /* for buffy_size, it is important that if the folder is new (tested by
274 * reading it), the size is set to 0 so that later when we check we see
275 * that it increased . without buffy_size we probably don't care.
277 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
279 /* some systems out there don't have an off_t type */
280 (*tmp)->size = (long) sb.st_size;
284 #endif /* BUFFY_SIZE */
290 /* people use buffy_size on systems where modified time attributes are BADLY
291 * broken. Ignore them.
293 #define STAT_CHECK (sb.st_size > tmp->size)
295 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
296 #endif /* BUFFY_SIZE */
298 int mutt_buffy_check (int force)
304 char path[_POSIX_PATH_MAX];
305 struct stat contex_sb;
309 /* update postponed count as well, on force */
311 mutt_update_num_postponed ();
314 /* fastest return if there are no mailboxes */
318 if (!force && (t - BuffyTime < BuffyTimeout))
326 if (!Context || Context->magic != M_IMAP)
329 if (!Context || Context->magic != M_POP)
332 if (!Context || Context->magic != M_NNTP)
334 /* check device ID and serial number instead of comparing paths */
335 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
341 for (tmp = Incoming; tmp; tmp = tmp->next)
348 if (mx_is_imap (tmp->path))
353 if (mx_is_pop (tmp->path))
358 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
362 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
363 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
365 /* if the mailbox still doesn't exist, set the newly created flag to
366 * be ready for when it does. */
367 tmp->newly_created = 1;
375 /* check to see if the folder is the currently selected folder
377 if (!Context || !Context->path ||
381 || tmp->magic == M_IMAP
384 || tmp->magic == M_POP
387 || tmp->magic == M_NNTP
389 ) ? mutt_strcmp (tmp->path, Context->path) :
390 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
400 if (STAT_CHECK || tmp->msgcount == 0)
403 /* only do complete count if sidebar visible */
404 if (SidebarWidth > 0)
410 /* parse the mailbox, to see how much mail there is */
411 ctx = mx_open_mailbox( tmp->path, M_READONLY | M_QUIET | M_NOSORT,
415 msgcount = ctx->msgcount;
416 msg_unread = ctx->unread;
417 mx_close_mailbox(ctx, 0);
420 tmp->msgcount = msgcount;
421 tmp->msg_unread = msg_unread;
423 tmp->has_new = tmp->new = 1;
427 /* sidebar invisible -> done */
434 /* some other program has deleted mail from the folder */
435 tmp->size = (long) sb.st_size;
438 if (tmp->newly_created &&
439 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
440 tmp->newly_created = 0;
446 snprintf (path, sizeof (path), "%s/new", tmp->path);
447 if ((dirp = opendir (path)) == NULL)
454 while ((de = readdir (dirp)) != NULL)
457 if (*de->d_name != '.' &&
458 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
460 /* one new and undeleted message is enough */
464 tmp->has_new = tmp->new = 1;
465 if (SidebarWidth == 0)
467 /* if sidebar invisible -> done */
477 if (SidebarWidth > 0)
479 /* only count total mail if sidebar visible */
480 snprintf (path, sizeof (path), "%s/cur", tmp->path);
481 if ((dirp = opendir (path)) == NULL)
486 while ((de = readdir (dirp)) != NULL)
489 if (*de->d_name != '.' &&
490 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
500 if ((tmp->new = mh_buffy (tmp->path)) > 0)
502 if (SidebarWidth > 0)
506 if ((dp = opendir (path)) == NULL)
509 while ((de = readdir (dp)))
511 if (mh_valid_message (de->d_name))
514 tmp->has_new = tmp->new = 1;
523 tmp->msgcount = imap_mailbox_check(tmp->path, 0);
524 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
544 else if (Context && Context->path)
545 tmp->size = (long) sb.st_size; /* update the size */
550 else if (!tmp->notified)
554 BuffyDoneTime = BuffyTime;
558 int mutt_buffy_list (void)
561 char path[_POSIX_PATH_MAX];
566 int have_unnotified = BuffyNotify;
571 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
572 for (tmp = Incoming; tmp; tmp = tmp->next)
574 /* Is there new mail in this mailbox? */
575 if (!tmp->new || (have_unnotified && tmp->notified))
578 strfcpy (path, tmp->path, sizeof (path));
579 mutt_pretty_mailbox (path);
581 if (!first && pos + strlen (path) >= COLS - 7)
585 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
587 /* Prepend an asterisk to mailboxes not already notified */
590 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
594 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
599 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
603 /* on new mail: redraw sidebar */
604 draw_sidebar (CurrentMenu);
605 mutt_message ("%s", buffylist);
608 /* there were no mailboxes needing to be notified, so clean up since
609 * BuffyNotify has somehow gotten out of sync
615 int mutt_buffy_notify (void)
617 if (mutt_buffy_check (0) && BuffyNotify)
619 return (mutt_buffy_list ());
625 * mutt_buffy() -- incoming folders completion routine
627 * given a folder name, this routine gives the next incoming folder with new
630 void mutt_buffy (char *s, size_t slen)
633 BUFFY *tmp = Incoming;
635 mutt_expand_path (s, _POSIX_PATH_MAX);
636 switch (mutt_buffy_check (0))
645 while (tmp && !tmp->new)
650 mutt_buffy_check (1); /* buffy was wrong - resync things */
653 strfcpy (s, tmp->path, slen);
654 mutt_pretty_mailbox (s);
662 if (mutt_strcmp (s, tmp->path) == 0)
664 else if (count && tmp->new)
676 mutt_buffy_check (1); /* buffy was wrong - resync things */
679 strfcpy (s, tmp->path, slen);
680 mutt_pretty_mailbox (s);