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 void mutt_buffy_cleanup (const char *buf, struct stat *st)
142 if (option(OPTCHECKMBOXSIZE))
144 tmp = mutt_find_mailbox (buf);
145 if (tmp && !tmp->new)
146 mutt_update_mailbox (tmp);
150 /* fix up the times so buffy won't get confused */
151 if (st->st_mtime > st->st_atime)
153 ut.actime = st->st_atime;
154 ut.modtime = time (NULL);
162 BUFFY *mutt_find_mailbox (const char *path)
168 if (stat (path,&sb) != 0)
171 for (tmp = Incoming; tmp; tmp = tmp->next)
173 if (stat (tmp->path,&tmp_sb) ==0 &&
174 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
180 void mutt_update_mailbox (BUFFY * b)
187 if (stat (b->path, &sb) == 0)
188 b->size = (off_t) sb.st_size;
194 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
197 char buf[_POSIX_PATH_MAX];
199 char f1[PATH_MAX], f2[PATH_MAX];
204 mutt_extract_token (path, s, 0);
205 strfcpy (buf, path->data, sizeof (buf));
207 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
209 for (tmp = &Incoming; *tmp;)
212 FREE (tmp); /* __FREE_CHECKED__ */
218 mutt_expand_path (buf, sizeof (buf));
220 /* Skip empty tokens. */
223 /* avoid duplicates */
224 p = realpath (buf, f1);
225 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
227 q = realpath ((*tmp)->path, f2);
228 if (mutt_strcmp (p ? p : buf, q ? q : (*tmp)->path) == 0)
230 dprint(3,(debugfile,"mailbox '%s' already registered as '%s'\n", buf, (*tmp)->path));
235 if(data == M_UNMAILBOXES)
239 FREE (&((*tmp)->path));
241 FREE (tmp); /* __FREE_CHECKED__ */
249 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
250 strfcpy ((*tmp)->path, buf, sizeof ((*tmp)->path));
252 /* it is tempting to set magic right here */
258 (*tmp)->notified = 1;
259 (*tmp)->newly_created = 0;
261 /* for check_mbox_size, it is important that if the folder is new (tested by
262 * reading it), the size is set to 0 so that later when we check we see
263 * that it increased . without check_mbox_size we probably don't care.
265 if (option(OPTCHECKMBOXSIZE) &&
266 stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
268 /* some systems out there don't have an off_t type */
269 (*tmp)->size = (off_t) sb.st_size;
277 /* people use check_mbox_size on systems where modified time attributes are
278 * BADLY broken. Ignore them.
280 #define STAT_CHECK_SIZE (sb.st_size > tmp->size)
281 #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))
282 #define STAT_CHECK (option(OPTCHECKMBOXSIZE) ? STAT_CHECK_SIZE : STAT_CHECK_TIME)
284 int mutt_buffy_check (int force)
290 char path[_POSIX_PATH_MAX];
291 struct stat contex_sb;
299 /* update postponed count as well, on force */
301 mutt_update_num_postponed ();
304 /* fastest return if there are no mailboxes */
308 if (!force && (t - BuffyTime < BuffyTimeout))
316 BuffyCount += imap_buffy_check (force);
318 if (!Context || Context->magic != M_IMAP)
321 if (!Context || Context->magic != M_POP)
323 /* check device ID and serial number instead of comparing paths */
324 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
330 for (tmp = Incoming; tmp; tmp = tmp->next)
333 if (tmp->magic != M_IMAP)
338 if (tmp->magic != M_IMAP)
342 if (mx_is_pop (tmp->path))
346 if (stat (tmp->path, &sb) != 0 || (S_ISREG(sb.st_mode) && sb.st_size == 0) ||
347 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
349 /* if the mailbox still doesn't exist, set the newly created flag to
350 * be ready for when it does. */
351 tmp->newly_created = 1;
360 /* check to see if the folder is the currently selected folder
362 if (!Context || !Context->path ||
363 #if defined USE_IMAP || defined USE_POP
374 ) ? mutt_strcmp (tmp->path, Context->path) :
376 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
377 #if defined USE_IMAP || defined USE_POP
393 else if (option(OPTCHECKMBOXSIZE))
395 /* some other program has deleted mail from the folder */
396 tmp->size = (off_t) sb.st_size;
398 if (tmp->newly_created &&
399 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
400 tmp->newly_created = 0;
406 snprintf (path, sizeof (path), "%s/new", tmp->path);
407 if ((dirp = opendir (path)) == NULL)
412 while ((de = readdir (dirp)) != NULL)
415 if (*de->d_name != '.' &&
416 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
418 /* one new and undeleted message is enough */
428 if ((tmp->new = mh_buffy (tmp->path)) > 0)
433 else if (option(OPTCHECKMBOXSIZE) && Context && Context->path)
434 tmp->size = (off_t) sb.st_size; /* update the size of current folder */
438 else if (!tmp->notified)
442 BuffyDoneTime = BuffyTime;
446 int mutt_buffy_list (void)
449 char path[_POSIX_PATH_MAX];
450 char buffylist[2*STRING];
454 int have_unnotified = BuffyNotify;
459 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
460 for (tmp = Incoming; tmp; tmp = tmp->next)
462 /* Is there new mail in this mailbox? */
463 if (!tmp->new || (have_unnotified && tmp->notified))
466 strfcpy (path, tmp->path, sizeof (path));
467 mutt_pretty_mailbox (path, sizeof (path));
469 if (!first && pos + strlen (path) >= COLS - 7)
473 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
475 /* Prepend an asterisk to mailboxes not already notified */
478 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
482 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
487 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
491 mutt_message ("%s", buffylist);
494 /* there were no mailboxes needing to be notified, so clean up since
495 * BuffyNotify has somehow gotten out of sync
501 int mutt_buffy_notify (void)
503 if (mutt_buffy_check (0) && BuffyNotify)
505 return (mutt_buffy_list ());
511 * mutt_buffy() -- incoming folders completion routine
513 * given a folder name, this routine gives the next incoming folder with new
516 void mutt_buffy (char *s, size_t slen)
518 BUFFY *tmp = Incoming;
521 mutt_expand_path (s, slen);
523 if (mutt_buffy_check (0))
525 for (pass = 0; pass < 2; pass++)
526 for (tmp = Incoming; tmp; tmp = tmp->next)
528 mutt_expand_path (tmp->path, sizeof (tmp->path));
529 if ((found || pass) && tmp->new)
531 strfcpy (s, tmp->path, slen);
532 mutt_pretty_mailbox (s, slen);
535 if (mutt_strcmp (s, tmp->path) == 0)
539 mutt_buffy_check (1); /* buffy was wrong - resync things */
542 /* no folders with new mail */