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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 */
48 /* Find the last message in the file.
49 * upon success return 0. If no message found - return -1 */
51 static int fseek_last_message (FILE * f)
54 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
56 int i; /* Index into `buffer' for scanning. */
58 memset (buffer, 0, sizeof(buffer));
59 fseek (f, 0, SEEK_END);
62 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
63 * `bytes_read' <= `BUFSIZ'. */
64 bytes_read = pos % BUFSIZ;
67 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
68 * reads will be on block boundaries, which might increase efficiency. */
69 while ((pos -= bytes_read) >= 0)
71 /* we save in the buffer at the end the first 7 chars from the last read */
72 strncpy (buffer + BUFSIZ, buffer, 5+2); /* 2 == 2 * mutt_strlen(CRLF) */
73 fseeko (f, pos, SEEK_SET);
74 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
77 for (i = bytes_read; --i >= 0;)
78 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
79 { /* found it - go to the beginning of the From */
80 fseeko (f, pos + i + 2, SEEK_SET);
86 /* here we are at the beginning of the file */
87 if (!mutt_strncmp ("From ", buffer, 5))
96 /* Return 1 if the last message is new */
97 static int test_last_status_new (FILE * f)
100 ENVELOPE* tmp_envelope;
103 if (fseek_last_message (f) == -1)
106 hdr = mutt_new_header ();
107 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
108 if (!(hdr->read || hdr->old))
111 mutt_free_envelope(&tmp_envelope);
112 mutt_free_header (&hdr);
117 static int test_new_folder (const char *path)
123 typ = mx_get_magic (path);
125 if (typ != M_MBOX && typ != M_MMDF)
128 if ((f = fopen (path, "rb")))
130 rc = test_last_status_new (f);
137 BUFFY *mutt_find_mailbox (const char *path)
143 if (stat (path,&sb) != 0)
146 for (tmp = Incoming; tmp; tmp = tmp->next)
148 if (stat (tmp->path,&tmp_sb) ==0 &&
149 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
155 void mutt_update_mailbox (BUFFY * b)
162 if (stat (b->path, &sb) == 0)
163 b->size = (off_t) sb.st_size;
169 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
172 char buf[_POSIX_PATH_MAX];
177 mutt_extract_token (path, s, 0);
178 strfcpy (buf, path->data, sizeof (buf));
180 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
182 for (tmp = &Incoming; *tmp;)
184 FREE (&((*tmp)->path));
186 FREE (tmp); /* __FREE_CHECKED__ */
192 mutt_expand_path (buf, sizeof (buf));
194 /* Skip empty tokens. */
197 /* simple check to avoid duplicates */
198 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
200 if (mutt_strcmp (buf, (*tmp)->path) == 0)
204 if(data == M_UNMAILBOXES)
208 FREE (&((*tmp)->path));
210 FREE (tmp); /* __FREE_CHECKED__ */
218 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
219 (*tmp)->path = safe_strdup (buf);
221 /* it is tempting to set magic right here */
227 (*tmp)->notified = 1;
228 (*tmp)->newly_created = 0;
230 /* for check_mbox_size, it is important that if the folder is new (tested by
231 * reading it), the size is set to 0 so that later when we check we see
232 * that it increased . without check_mbox_size we probably don't care.
234 if (option(OPTCHECKMBOXSIZE) &&
235 stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
237 /* some systems out there don't have an off_t type */
238 (*tmp)->size = (off_t) sb.st_size;
246 /* people use check_mbox_size on systems where modified time attributes are
247 * BADLY broken. Ignore them.
249 #define STAT_CHECK_SIZE (sb.st_size > tmp->size)
250 #define STAT_CHECK_TIME (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
251 #define STAT_CHECK (option(OPTCHECKMBOXSIZE) ? STAT_CHECK_SIZE : STAT_CHECK_TIME)
253 int mutt_buffy_check (int force)
259 char path[_POSIX_PATH_MAX];
260 struct stat contex_sb;
264 /* update postponed count as well, on force */
266 mutt_update_num_postponed ();
269 /* fastest return if there are no mailboxes */
273 if (!force && (t - BuffyTime < BuffyTimeout))
281 BuffyCount += imap_buffy_check (force);
283 if (!Context || Context->magic != M_IMAP)
286 if (!Context || Context->magic != M_POP)
288 /* check device ID and serial number instead of comparing paths */
289 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
295 for (tmp = Incoming; tmp; tmp = tmp->next)
298 if (tmp->magic != M_IMAP)
303 if (tmp->magic != M_IMAP)
307 if (mx_is_pop (tmp->path))
311 if (stat (tmp->path, &sb) != 0 || (S_ISREG(sb.st_mode) && sb.st_size == 0) ||
312 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
314 /* if the mailbox still doesn't exist, set the newly created flag to
315 * be ready for when it does. */
316 tmp->newly_created = 1;
325 /* check to see if the folder is the currently selected folder
327 if (!Context || !Context->path ||
328 #if defined USE_IMAP || defined USE_POP
339 ) ? mutt_strcmp (tmp->path, Context->path) :
341 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
342 #if defined USE_IMAP || defined USE_POP
358 else if (option(OPTCHECKMBOXSIZE))
360 /* some other program has deleted mail from the folder */
361 tmp->size = (off_t) sb.st_size;
363 if (tmp->newly_created &&
364 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
365 tmp->newly_created = 0;
371 snprintf (path, sizeof (path), "%s/new", tmp->path);
372 if ((dirp = opendir (path)) == NULL)
377 while ((de = readdir (dirp)) != NULL)
380 if (*de->d_name != '.' &&
381 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
383 /* one new and undeleted message is enough */
393 if ((tmp->new = mh_buffy (tmp->path)) > 0)
398 else if (option(OPTCHECKMBOXSIZE) && Context && Context->path)
399 tmp->size = (off_t) sb.st_size; /* update the size of current folder */
403 else if (!tmp->notified)
407 BuffyDoneTime = BuffyTime;
411 int mutt_buffy_list (void)
414 char path[_POSIX_PATH_MAX];
415 char buffylist[2*STRING];
419 int have_unnotified = BuffyNotify;
424 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
425 for (tmp = Incoming; tmp; tmp = tmp->next)
427 /* Is there new mail in this mailbox? */
428 if (!tmp->new || (have_unnotified && tmp->notified))
431 strfcpy (path, tmp->path, sizeof (path));
432 mutt_pretty_mailbox (path, sizeof (path));
434 if (!first && pos + strlen (path) >= COLS - 7)
438 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
440 /* Prepend an asterisk to mailboxes not already notified */
443 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
447 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
452 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
456 mutt_message ("%s", buffylist);
459 /* there were no mailboxes needing to be notified, so clean up since
460 * BuffyNotify has somehow gotten out of sync
466 int mutt_buffy_notify (void)
468 if (mutt_buffy_check (0) && BuffyNotify)
470 return (mutt_buffy_list ());
476 * mutt_buffy() -- incoming folders completion routine
478 * given a folder name, this routine gives the next incoming folder with new
481 void mutt_buffy (char *s, size_t slen)
484 BUFFY *tmp = Incoming;
486 mutt_expand_path (s, _POSIX_PATH_MAX);
487 switch (mutt_buffy_check (0))
496 while (tmp && !tmp->new)
501 mutt_buffy_check (1); /* buffy was wrong - resync things */
504 strfcpy (s, tmp->path, slen);
505 mutt_pretty_mailbox (s, slen);
513 if (mutt_strcmp (s, tmp->path) == 0)
515 else if (count && tmp->new)
527 mutt_buffy_check (1); /* buffy was wrong - resync things */
530 strfcpy (s, tmp->path, slen);
531 mutt_pretty_mailbox (s, slen);