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.
28 #include "mutt_curses.h"
43 static time_t BuffyTime = 0; /* last time we started checking for mail */
44 time_t BuffyDoneTime = 0; /* last time we knew for sure how much mail there was. */
45 static short BuffyCount = 0; /* how many boxes with new mail */
46 static short BuffyNotify = 0; /* # of unnotified new boxes */
50 /* Find the last message in the file.
51 * upon success return 0. If no message found - return -1 */
53 int fseek_last_message (FILE * f)
56 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
58 int i; /* Index into `buffer' for scanning. */
60 memset (buffer, 0, sizeof(buffer));
61 fseek (f, 0, SEEK_END);
64 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
65 * `bytes_read' <= `BUFSIZ'. */
66 bytes_read = pos % BUFSIZ;
69 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
70 * reads will be on block boundaries, which might increase efficiency. */
71 while ((pos -= bytes_read) >= 0)
73 /* we save in the buffer at the end the first 7 chars from the last read */
74 strncpy (buffer + BUFSIZ, buffer, 5+2); /* 2 == 2 * mutt_strlen(CRLF) */
75 fseek (f, pos, SEEK_SET);
76 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
79 for (i = bytes_read; --i >= 0;)
80 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
81 { /* found it - go to the beginning of the From */
82 fseek (f, pos + i + 2, SEEK_SET);
88 /* here we are at the beginning of the file */
89 if (!mutt_strncmp ("From ", buffer, 5))
98 /* Return 1 if the last message is new */
99 int test_last_status_new (FILE * f)
102 ENVELOPE* tmp_envelope;
105 if (fseek_last_message (f) == -1)
108 hdr = mutt_new_header ();
109 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
110 if (!(hdr->read || hdr->old))
113 mutt_free_envelope(&tmp_envelope);
114 mutt_free_header (&hdr);
119 int test_new_folder (const char *path)
125 typ = mx_get_magic (path);
127 if (typ != M_MBOX && typ != M_MMDF)
130 if ((f = fopen (path, "rb")))
132 rc = test_last_status_new (f);
139 BUFFY *mutt_find_mailbox (const char *path)
145 if (stat (path,&sb) != 0)
148 for (tmp = Incoming; tmp; tmp = tmp->next)
150 if (stat (tmp->path,&tmp_sb) ==0 &&
151 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
157 void mutt_update_mailbox (BUFFY * b)
164 if (stat (b->path, &sb) == 0)
165 b->size = (long) sb.st_size;
172 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
175 char buf[_POSIX_PATH_MAX];
178 #endif /* BUFFY_SIZE */
182 mutt_extract_token (path, s, 0);
183 strfcpy (buf, path->data, sizeof (buf));
185 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
187 for (tmp = &Incoming; *tmp;)
189 FREE (&((*tmp)->path));
197 mutt_expand_path (buf, sizeof (buf));
199 /* Skip empty tokens. */
202 /* simple check to avoid duplicates */
203 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
205 if (mutt_strcmp (buf, (*tmp)->path) == 0)
209 if(data == M_UNMAILBOXES)
213 FREE (&((*tmp)->path));
223 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
224 (*tmp)->path = safe_strdup (buf);
226 /* it is tempting to set magic right here */
232 (*tmp)->notified = 1;
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))
242 /* some systems out there don't have an off_t type */
243 (*tmp)->size = (long) sb.st_size;
247 #endif /* BUFFY_SIZE */
253 /* people use buffy_size on systems where modified time attributes are BADLY
254 * broken. Ignore them.
256 #define STAT_CHECK (sb.st_size > tmp->size)
258 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
259 #endif /* BUFFY_SIZE */
261 int mutt_buffy_check (int force)
267 char path[_POSIX_PATH_MAX];
268 struct stat contex_sb;
272 /* update postponed count as well, on force */
274 mutt_update_num_postponed ();
277 /* fastest return if there are no mailboxes */
281 if (!force && (t - BuffyTime < BuffyTimeout))
289 if (!Context || Context->magic != M_IMAP)
292 if (!Context || Context->magic != M_POP)
295 if (!Context || Context->magic != M_NNTP)
297 /* check device ID and serial number instead of comparing paths */
298 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
304 for (tmp = Incoming; tmp; tmp = tmp->next)
311 if (mx_is_imap (tmp->path))
316 if (mx_is_pop (tmp->path))
321 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
325 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
326 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
328 /* if the mailbox still doesn't exist, set the newly created flag to
329 * be ready for when it does. */
330 tmp->newly_created = 1;
338 /* check to see if the folder is the currently selected folder
340 if (!Context || !Context->path ||
344 || tmp->magic == M_IMAP
347 || tmp->magic == M_POP
350 || tmp->magic == M_NNTP
352 ) ? mutt_strcmp (tmp->path, Context->path) :
353 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
363 if (STAT_CHECK || tmp->msgcount == 0)
369 /* parse the mailbox, to see how much mail there is */
370 ctx = mx_open_mailbox( tmp->path, M_READONLY | M_QUIET | M_NOSORT,
374 msgcount = ctx->msgcount;
375 msg_unread = ctx->unread;
376 mx_close_mailbox(ctx, 0);
379 tmp->msgcount = msgcount;
380 tmp->msg_unread = msg_unread;
382 tmp->has_new = tmp->new = 1;
387 /* some other program has deleted mail from the folder */
388 tmp->size = (long) sb.st_size;
391 if (tmp->newly_created &&
392 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
393 tmp->newly_created = 0;
399 snprintf (path, sizeof (path), "%s/new", tmp->path);
400 if ((dirp = opendir (path)) == NULL)
407 while ((de = readdir (dirp)) != NULL)
410 if (*de->d_name != '.' &&
411 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
413 /* one new and undeleted message is enough */
417 tmp->has_new = tmp->new = 1;
425 /* I commented this out because it led to an infite "New mail in ..." loop,
426 * and when looking at the code, the check seems to be overly eager.
429 snprintf (path, sizeof (path), "%s/cur", tmp->path);
430 if ((dirp = opendir (path)) == NULL)
435 while ((de = readdir (dirp)) != NULL)
438 if (*de->d_name != '.' &&
439 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
441 /* one new and undeleted message is enough */
444 /* we're checking for read and not new mail;
445 * seems like copy'n'paste error
447 tmp->has_new = tmp->new = 1;
460 if ((tmp->new = mh_buffy (tmp->path)) > 0)
463 if ((dp = opendir (path)) == NULL)
466 while ((de = readdir (dp)))
468 if (mh_valid_message (de->d_name))
471 tmp->has_new = tmp->new = 1;
480 tmp->msgcount = imap_mailbox_check(tmp->path, 0);
481 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
502 else if (Context && Context->path)
503 tmp->size = (long) sb.st_size; /* update the size */
508 else if (!tmp->notified)
512 BuffyDoneTime = BuffyTime;
516 int mutt_buffy_list (void)
519 char path[_POSIX_PATH_MAX];
524 int have_unnotified = BuffyNotify;
529 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
530 for (tmp = Incoming; tmp; tmp = tmp->next)
532 /* Is there new mail in this mailbox? */
533 if (!tmp->new || (have_unnotified && tmp->notified))
536 strfcpy (path, tmp->path, sizeof (path));
537 mutt_pretty_mailbox (path);
539 if (!first && pos + strlen (path) >= COLS - 7)
543 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
545 /* Prepend an asterisk to mailboxes not already notified */
548 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
552 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
557 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
561 mutt_message ("%s", buffylist);
564 /* there were no mailboxes needing to be notified, so clean up since
565 * BuffyNotify has somehow gotten out of sync
571 int mutt_buffy_notify (void)
573 if (mutt_buffy_check (0) && BuffyNotify)
575 return (mutt_buffy_list ());
581 * mutt_buffy() -- incoming folders completion routine
583 * given a folder name, this routine gives the next incoming folder with new
586 void mutt_buffy (char *s, size_t slen)
589 BUFFY *tmp = Incoming;
591 mutt_expand_path (s, _POSIX_PATH_MAX);
592 switch (mutt_buffy_check (0))
601 while (tmp && !tmp->new)
606 mutt_buffy_check (1); /* buffy was wrong - resync things */
609 strfcpy (s, tmp->path, slen);
610 mutt_pretty_mailbox (s);
618 if (mutt_strcmp (s, tmp->path) == 0)
620 else if (count && tmp->new)
632 mutt_buffy_check (1); /* buffy was wrong - resync things */
635 strfcpy (s, tmp->path, slen);
636 mutt_pretty_mailbox (s);