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.
24 #include "mutt_curses.h"
39 static time_t BuffyTime = 0; /* last time we started checking for mail */
40 time_t BuffyDoneTime = 0; /* last time we knew for sure how much mail there was. */
41 static short BuffyCount = 0; /* how many boxes with new mail */
42 static short BuffyNotify = 0; /* # of unnotified new boxes */
46 /* Find the last message in the file.
47 * upon success return 0. If no message found - return -1 */
49 int fseek_last_message (FILE * f)
52 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
54 int i; /* Index into `buffer' for scanning. */
56 memset (buffer, 0, sizeof(buffer));
57 fseek (f, 0, SEEK_END);
60 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
61 * `bytes_read' <= `BUFSIZ'. */
62 bytes_read = pos % BUFSIZ;
65 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
66 * reads will be on block boundaries, which might increase efficiency. */
67 while ((pos -= bytes_read) >= 0)
69 /* we save in the buffer at the end the first 7 chars from the last read */
70 strncpy (buffer + BUFSIZ, buffer, 5+2); /* 2 == 2 * mutt_strlen(CRLF) */
71 fseek (f, pos, SEEK_SET);
72 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
75 for (i = bytes_read; --i >= 0;)
76 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
77 { /* found it - go to the beginning of the From */
78 fseek (f, pos + i + 2, SEEK_SET);
84 /* here we are at the beginning of the file */
85 if (!mutt_strncmp ("From ", buffer, 5))
94 /* Return 1 if the last message is new */
95 int test_last_status_new (FILE * f)
98 ENVELOPE* tmp_envelope;
101 if (fseek_last_message (f) == -1)
104 hdr = mutt_new_header ();
105 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
106 if (!(hdr->read || hdr->old))
109 mutt_free_envelope(&tmp_envelope);
110 mutt_free_header (&hdr);
115 int test_new_folder (const char *path)
121 typ = mx_get_magic (path);
123 if (typ != M_MBOX && typ != M_MMDF)
126 if ((f = fopen (path, "rb")))
128 rc = test_last_status_new (f);
135 BUFFY *mutt_find_mailbox (const char *path)
141 if (stat (path,&sb) != 0)
144 for (tmp = Incoming; tmp; tmp = tmp->next)
146 if (stat (tmp->path,&tmp_sb) ==0 &&
147 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
153 void mutt_update_mailbox (BUFFY * b)
160 if (stat (b->path, &sb) == 0)
161 b->size = (long) sb.st_size;
168 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
171 char buf[_POSIX_PATH_MAX];
174 #endif /* BUFFY_SIZE */
178 mutt_extract_token (path, s, 0);
179 strfcpy (buf, path->data, sizeof (buf));
181 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
183 for (tmp = &Incoming; *tmp;)
185 FREE (&((*tmp)->path));
193 mutt_expand_path (buf, sizeof (buf));
195 /* Skip empty tokens. */
198 /* simple check to avoid duplicates */
199 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
201 if (mutt_strcmp (buf, (*tmp)->path) == 0)
205 if(data == M_UNMAILBOXES)
209 FREE (&((*tmp)->path));
219 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
220 (*tmp)->path = safe_strdup (buf);
222 /* it is tempting to set magic right here */
228 (*tmp)->notified = 1;
229 (*tmp)->newly_created = 0;
232 /* for buffy_size, it is important that if the folder is new (tested by
233 * reading it), the size is set to 0 so that later when we check we see
234 * that it increased . without buffy_size we probably don't care.
236 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
238 /* some systems out there don't have an off_t type */
239 (*tmp)->size = (long) sb.st_size;
243 #endif /* BUFFY_SIZE */
249 /* people use buffy_size on systems where modified time attributes are BADLY
250 * broken. Ignore them.
252 #define STAT_CHECK (sb.st_size > tmp->size)
254 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
255 #endif /* BUFFY_SIZE */
257 int mutt_buffy_check (int force)
263 char path[_POSIX_PATH_MAX];
264 struct stat contex_sb;
268 /* update postponed count as well, on force */
270 mutt_update_num_postponed ();
273 /* fastest return if there are no mailboxes */
277 if (!force && (t - BuffyTime < BuffyTimeout))
285 if (!Context || Context->magic != M_IMAP)
288 if (!Context || Context->magic != M_POP)
291 if (!Context || Context->magic != M_NNTP)
293 /* check device ID and serial number instead of comparing paths */
294 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
300 for (tmp = Incoming; tmp; tmp = tmp->next)
307 if (mx_is_imap (tmp->path))
312 if (mx_is_pop (tmp->path))
317 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
321 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
322 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
324 /* if the mailbox still doesn't exist, set the newly created flag to
325 * be ready for when it does. */
326 tmp->newly_created = 1;
334 /* check to see if the folder is the currently selected folder
336 if (!Context || !Context->path ||
340 || tmp->magic == M_IMAP
343 || tmp->magic == M_POP
346 || tmp->magic == M_NNTP
348 ) ? mutt_strcmp (tmp->path, Context->path) :
349 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
359 if (STAT_CHECK || tmp->msgcount == 0)
365 /* parse the mailbox, to see how much mail there is */
366 ctx = mx_open_mailbox( tmp->path, M_READONLY | M_QUIET | M_NOSORT,
370 msgcount = ctx->msgcount;
371 msg_unread = ctx->unread;
372 mx_close_mailbox(ctx, 0);
375 tmp->msgcount = msgcount;
376 tmp->msg_unread = msg_unread;
378 tmp->has_new = tmp->new = 1;
383 /* some other program has deleted mail from the folder */
384 tmp->size = (long) sb.st_size;
387 if (tmp->newly_created &&
388 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
389 tmp->newly_created = 0;
395 snprintf (path, sizeof (path), "%s/new", tmp->path);
396 if ((dirp = opendir (path)) == NULL)
403 while ((de = readdir (dirp)) != NULL)
406 if (*de->d_name != '.' &&
407 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
409 /* one new and undeleted message is enough */
411 tmp->has_new = tmp->new = 1;
417 snprintf (path, sizeof (path), "%s/cur", tmp->path);
418 if ((dirp = opendir (path)) == NULL)
423 while ((de = readdir (dirp)) != NULL)
426 if (*de->d_name != '.' &&
427 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
429 /* one new and undeleted message is enough */
431 tmp->has_new = tmp->new = 1;
442 if ((tmp->new = mh_buffy (tmp->path)) > 0)
445 if ((dp = opendir (path)) == NULL)
448 while ((de = readdir (dp)))
450 if (mh_valid_message (de->d_name))
453 tmp->has_new = tmp->new = 1;
462 tmp->msgcount = imap_mailbox_check(tmp->path, 0);
463 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
484 else if (Context && Context->path)
485 tmp->size = (long) sb.st_size; /* update the size */
490 else if (!tmp->notified)
494 BuffyDoneTime = BuffyTime;
498 int mutt_buffy_list (void)
501 char path[_POSIX_PATH_MAX];
506 int have_unnotified = BuffyNotify;
511 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos));
512 for (tmp = Incoming; tmp; tmp = tmp->next)
514 /* Is there new mail in this mailbox? */
515 if (!tmp->new || (have_unnotified && tmp->notified))
518 strfcpy (path, tmp->path, sizeof (path));
519 mutt_pretty_mailbox (path);
521 if (!first && pos + strlen (path) >= COLS - 7)
525 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos));
527 /* Prepend an asterisk to mailboxes not already notified */
530 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); */
534 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos));
539 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos);
543 mutt_message ("%s", buffylist);
546 /* there were no mailboxes needing to be notified, so clean up since
547 * BuffyNotify has somehow gotten out of sync
553 int mutt_buffy_notify (void)
555 if (mutt_buffy_check (0) && BuffyNotify)
557 return (mutt_buffy_list ());
563 * mutt_buffy() -- incoming folders completion routine
565 * given a folder name, this routine gives the next incoming folder with new
568 void mutt_buffy (char *s, size_t slen)
571 BUFFY *tmp = Incoming;
573 mutt_expand_path (s, _POSIX_PATH_MAX);
574 switch (mutt_buffy_check (0))
583 while (tmp && !tmp->new)
588 mutt_buffy_check (1); /* buffy was wrong - resync things */
591 strfcpy (s, tmp->path, slen);
592 mutt_pretty_mailbox (s);
600 if (mutt_strcmp (s, tmp->path) == 0)
602 else if (count && tmp->new)
614 mutt_buffy_check (1); /* buffy was wrong - resync things */
617 strfcpy (s, tmp->path, slen);
618 mutt_pretty_mailbox (s);