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 */
46 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
48 static short BuffyCount = 0; /* how many boxes with new mail */
49 static short BuffyNotify = 0; /* # of unnotified new boxes */
53 /* Find the last message in the file.
54 * upon success return 0. If no message found - return -1 */
56 int fseek_last_message (FILE * f)
59 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
61 int i; /* Index into `buffer' for scanning. */
63 memset (buffer, 0, sizeof(buffer));
64 fseek (f, 0, SEEK_END);
67 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
68 * `bytes_read' <= `BUFSIZ'. */
69 bytes_read = pos % BUFSIZ;
72 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
73 * reads will be on block boundaries, which might increase efficiency. */
74 while ((pos -= bytes_read) >= 0)
76 /* we save in the buffer at the end the first 7 chars from the last read */
77 strncpy (buffer + BUFSIZ, buffer, 5+2); /* 2 == 2 * mutt_strlen(CRLF) */
78 fseek (f, pos, SEEK_SET);
79 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
82 for (i = bytes_read; --i >= 0;)
83 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
84 { /* found it - go to the beginning of the From */
85 fseek (f, pos + i + 2, SEEK_SET);
91 /* here we are at the beginning of the file */
92 if (!mutt_strncmp ("From ", buffer, 5))
101 /* Return 1 if the last message is new */
102 int test_last_status_new (FILE * f)
105 ENVELOPE* tmp_envelope;
108 if (fseek_last_message (f) == -1)
111 hdr = mutt_new_header ();
112 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
113 if (!(hdr->read || hdr->old))
116 mutt_free_envelope(&tmp_envelope);
117 mutt_free_header (&hdr);
122 int test_new_folder (const char *path)
128 typ = mx_get_magic (path);
130 if (typ != M_MBOX && typ != M_MMDF)
133 if ((f = fopen (path, "rb")))
135 rc = test_last_status_new (f);
142 BUFFY *mutt_find_mailbox (const char *path)
148 if (stat (path,&sb) != 0)
151 for (tmp = Incoming; tmp; tmp = tmp->next)
153 if (stat (tmp->path,&tmp_sb) ==0 &&
154 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
160 void mutt_update_mailbox (BUFFY * b)
167 if (stat (b->path, &sb) == 0)
168 b->size = (long) sb.st_size;
175 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
178 char buf[_POSIX_PATH_MAX];
181 #endif /* BUFFY_SIZE */
185 mutt_extract_token (path, s, 0);
186 strfcpy (buf, path->data, sizeof (buf));
188 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
190 for (tmp = &Incoming; *tmp;)
192 FREE (&((*tmp)->path));
200 mutt_expand_path (buf, sizeof (buf));
202 /* Skip empty tokens. */
205 /* simple check to avoid duplicates */
206 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next))
208 if (mutt_strcmp (buf, (*tmp)->path) == 0)
212 if(data == M_UNMAILBOXES)
216 FREE (&((*tmp)->path));
226 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
227 (*tmp)->path = safe_strdup (buf);
229 /* it is tempting to set magic right here */
235 (*tmp)->notified = 1;
236 (*tmp)->newly_created = 0;
239 /* for buffy_size, it is important that if the folder is new (tested by
240 * reading it), the size is set to 0 so that later when we check we see
241 * that it increased . without buffy_size we probably don't care.
243 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
245 /* some systems out there don't have an off_t type */
246 (*tmp)->size = (long) sb.st_size;
250 #endif /* BUFFY_SIZE */
256 /* people use buffy_size on systems where modified time attributes are BADLY
257 * broken. Ignore them.
259 #define STAT_CHECK (sb.st_size > tmp->size)
261 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
262 #endif /* BUFFY_SIZE */
265 * 0 don't force any checks + update sidebar
266 * 1 force all checks + update sidebar
267 * 2 force all checks + _don't_ update sidebar
269 int mutt_buffy_check (int force)
275 char path[_POSIX_PATH_MAX];
276 struct stat contex_sb;
281 /* update postponed count as well, on force */
283 mutt_update_num_postponed ();
286 /* fastest return if there are no mailboxes */
290 if (force == 0 && (now - BuffyTime < BuffyTimeout)
292 && (now - ImapBuffyTime < ImapBuffyTimeout))
299 if (force != 0 || now - BuffyTime >= BuffyTimeout)
302 last2 = ImapBuffyTime;
303 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
310 if (!Context || Context->magic != M_IMAP)
313 if (!Context || Context->magic != M_POP)
316 if (!Context || Context->magic != M_NNTP)
318 /* check device ID and serial number instead of comparing paths */
319 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
325 for (tmp = Incoming; tmp; tmp = tmp->next)
328 if (mx_is_imap (tmp->path))
333 if (mx_is_pop (tmp->path))
338 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
342 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
343 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
345 /* if the mailbox still doesn't exist, set the newly created flag to
346 * be ready for when it does. */
347 tmp->newly_created = 1;
355 /* check to see if the folder is the currently selected folder
357 if (!Context || !Context->path ||
361 || tmp->magic == M_IMAP
364 || tmp->magic == M_POP
367 || tmp->magic == M_NNTP
369 ) ? mutt_strcmp (tmp->path, Context->path) :
370 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
378 /* only check on force or $mail_check reached */
379 if (force != 0 || (now - last1 >= BuffyTimeout)) {
380 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
388 /* some other program has deleted mail from the folder */
389 tmp->size = (long) sb.st_size;
392 } else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
393 (STAT_CHECK || tmp->msgcount == 0)) {
394 /* sidebar visible */
396 if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
397 tmp->msgcount = ctx->msgcount;
399 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
400 tmp->msg_flagged = ctx->flagged;
401 mx_close_mailbox (ctx, 0);
404 if (tmp->newly_created &&
405 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
406 tmp->newly_created = 0;
407 } else if (tmp->new > 0)
412 /* only check on force or $mail_check reached */
413 if (force != 0 || (now - last1 >= BuffyTimeout)) {
414 snprintf (path, sizeof (path), "%s/new", tmp->path);
415 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 */
434 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
435 /* if sidebar invisible -> done */
445 if (SidebarWidth > 0 && option (OPTMBOXPANE))
447 /* only count total mail if sidebar visible */
448 snprintf (path, sizeof (path), "%s/cur", tmp->path);
449 if ((dirp = opendir (path)) == NULL)
454 tmp->msg_flagged = 0;
455 while ((de = readdir (dirp)) != NULL)
458 if (*de->d_name != '.' && (p = strstr (de->d_name, ":2,")) != NULL) {
459 if (!strchr (p + 3, 'T'))
461 if (strchr (p + 3, 'F'))
467 } else if (tmp->new > 0)
468 /* keep current stats if !force and !$mail_check reached */
473 /* only check on force or $mail_check reached */
474 if (force != 0 || (now - last1 >= BuffyTimeout)) {
475 if ((tmp->new = mh_buffy (tmp->path)) > 0)
477 if (SidebarWidth > 0 && option (OPTMBOXPANE))
481 if ((dp = opendir (path)) == NULL)
486 while ((de = readdir (dp)))
488 if (mh_valid_message (de->d_name))
497 } else if (tmp->new > 0)
498 /* keep current stats if !force and !$mail_check reached */
504 /* only check on force or $imap_mail_check reached */
505 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
506 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
507 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
509 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
515 } else if (tmp->new > 0)
516 /* keep current stats if !force and !$imap_mail_check reached */
533 else if (Context && Context->path)
534 tmp->size = (long) sb.st_size; /* update the size */
539 else if (!tmp->notified)
541 tmp->has_new = tmp->new > 0;
543 if (BuffyCount > 0 && force != 2)
544 draw_sidebar (CurrentMenu);
548 int mutt_buffy_list (void)
551 char path[_POSIX_PATH_MAX];
555 int have_unnotified = BuffyNotify;
557 if (option (OPTFORCEBUFFYCHECK))
558 mutt_buffy_check (1);
563 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
564 for (tmp = Incoming; tmp; tmp = tmp->next)
566 /* Is there new mail in this mailbox? */
567 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
570 strfcpy (path, tmp->path, sizeof (path));
571 mutt_pretty_mailbox (path);
573 if (!first && pos + strlen (path) >= COLS - 7)
577 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
579 /* Prepend an asterisk to mailboxes not already notified */
582 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
586 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
591 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
595 /* on new mail: redraw sidebar */
596 draw_sidebar (CurrentMenu);
597 mutt_message ("%s", buffylist);
600 /* there were no mailboxes needing to be notified, so clean up since
601 * BuffyNotify has somehow gotten out of sync
607 int mutt_buffy_notify (void)
609 if (mutt_buffy_check (0) && BuffyNotify)
611 return (mutt_buffy_list ());
617 * mutt_buffy() -- incoming folders completion routine
619 * given a folder name, this routine gives the next incoming folder with new
622 void mutt_buffy (char *s, size_t slen)
625 BUFFY *tmp = Incoming;
627 mutt_expand_path (s, _POSIX_PATH_MAX);
628 switch (mutt_buffy_check (0))
637 while (tmp && tmp->new <= 0)
642 mutt_buffy_check (1); /* buffy was wrong - resync things */
645 strfcpy (s, tmp->path, slen);
646 mutt_pretty_mailbox (s);
654 if (mutt_strcmp (s, tmp->path) == 0)
656 else if (count && tmp->new > 0)
668 mutt_buffy_check (1); /* buffy was wrong - resync things */
671 strfcpy (s, tmp->path, slen);
672 mutt_pretty_mailbox (s);