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 */
47 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
49 static short BuffyCount = 0; /* how many boxes with new mail */
50 static short BuffyNotify = 0; /* # of unnotified new boxes */
54 /* Find the last message in the file.
55 * upon success return 0. If no message found - return -1 */
57 int fseek_last_message (FILE * f)
60 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
62 int i; /* Index into `buffer' for scanning. */
64 memset (buffer, 0, sizeof (buffer));
65 fseek (f, 0, SEEK_END);
68 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
69 * `bytes_read' <= `BUFSIZ'. */
70 bytes_read = pos % BUFSIZ;
73 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
74 * reads will be on block boundaries, which might increase efficiency. */
75 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 "))) { /* found it - go to the beginning of the From */
84 fseek (f, pos + i + 2, SEEK_SET);
90 /* here we are at the beginning of the file */
91 if (!mutt_strncmp ("From ", buffer, 5)) {
99 /* Return 1 if the last message is new */
100 int test_last_status_new (FILE * f)
103 ENVELOPE *tmp_envelope;
106 if (fseek_last_message (f) == -1)
109 hdr = mutt_new_header ();
110 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
111 if (!(hdr->read || hdr->old))
114 mutt_free_envelope (&tmp_envelope);
115 mutt_free_header (&hdr);
120 int test_new_folder (const char *path)
126 typ = mx_get_magic (path);
128 if (typ != M_MBOX && typ != M_MMDF)
131 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) {
149 if (stat (tmp->path, &tmp_sb) == 0 &&
150 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
156 void mutt_update_mailbox (BUFFY * b)
163 if (stat (b->path, &sb) == 0)
164 b->size = (long) sb.st_size;
171 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
175 char buf[_POSIX_PATH_MAX];
179 #endif /* BUFFY_SIZE */
181 while (MoreArgs (s)) {
182 mutt_extract_token (path, s, 0);
183 strfcpy (buf, path->data, sizeof (buf));
185 if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
186 for (tmp = &Incoming; *tmp;) {
187 FREE (&((*tmp)->path));
195 mutt_expand_path (buf, sizeof (buf));
197 /* Skip empty tokens. */
201 /* simple check to avoid duplicates */
202 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next)) {
203 if (mutt_strcmp (buf, (*tmp)->path) == 0)
207 if (data == M_UNMAILBOXES) {
209 FREE (&((*tmp)->path));
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;
231 /* for buffy_size, it is important that if the folder is new (tested by
232 * reading it), the size is set to 0 so that later when we check we see
233 * that it increased . without buffy_size we probably don't care.
235 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path)) {
236 /* some systems out there don't have an off_t type */
237 (*tmp)->size = (long) sb.st_size;
241 #endif /* BUFFY_SIZE */
247 /* people use buffy_size on systems where modified time attributes are BADLY
248 * broken. Ignore them.
250 #define STAT_CHECK (sb.st_size > tmp->size)
252 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
253 #endif /* BUFFY_SIZE */
256 * 0 don't force any checks + update sidebar
257 * 1 force all checks + update sidebar
258 * 2 force all checks + _don't_ update sidebar
260 int mutt_buffy_check (int force)
266 char path[_POSIX_PATH_MAX];
267 struct stat contex_sb;
274 /* update postponed count as well, on force */
276 mutt_update_num_postponed ();
279 /* fastest return if there are no mailboxes */
283 if (force == 0 && (now - BuffyTime < BuffyTimeout)
285 && (now - ImapBuffyTime < ImapBuffyTimeout))
292 if (force != 0 || now - BuffyTime >= BuffyTimeout)
295 last2 = ImapBuffyTime;
296 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
303 if (!Context || Context->magic != M_IMAP)
306 if (!Context || Context->magic != M_POP)
309 if (!Context || Context->magic != M_NNTP)
311 /* check device ID and serial number instead of comparing paths */
312 if (!Context || !Context->path
313 || stat (Context->path, &contex_sb) != 0) {
314 contex_sb.st_dev = 0;
315 contex_sb.st_ino = 0;
318 for (tmp = Incoming; tmp; tmp = tmp->next) {
320 if (mx_is_imap (tmp->path))
325 if (mx_is_pop (tmp->path))
330 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
334 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
335 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
336 /* if the mailbox still doesn't exist, set the newly created flag to
337 * be ready for when it does. */
338 tmp->newly_created = 1;
346 /* check to see if the folder is the currently selected folder
348 if (!Context || !Context->path || ((0
350 || tmp->magic == M_IMAP
353 || tmp->magic == M_POP
356 || tmp->magic == M_NNTP
358 )? mutt_strcmp (tmp->path,
359 Context->path) : (sb.
372 switch (tmp->magic) {
375 /* only check on force or $mail_check reached */
376 if (force != 0 || (now - last1 >= BuffyTimeout)) {
377 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
384 /* some other program has deleted mail from the folder */
385 tmp->size = (long) sb.st_size;
389 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
390 (STAT_CHECK || tmp->msgcount == 0)) {
391 /* sidebar visible */
394 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
396 tmp->msgcount = ctx->msgcount;
398 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
399 tmp->msg_flagged = ctx->flagged;
400 mx_close_mailbox (ctx, 0);
403 if (tmp->newly_created &&
404 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
405 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) {
422 while ((de = readdir (dirp)) != NULL) {
425 if (*de->d_name != '.' &&
426 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
427 /* one new and undeleted message is enough */
431 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
432 /* if sidebar invisible -> done */
442 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
443 /* only count total mail if sidebar visible */
444 snprintf (path, sizeof (path), "%s/cur", tmp->path);
445 if ((dirp = opendir (path)) == NULL) {
449 tmp->msg_flagged = 0;
450 while ((de = readdir (dirp)) != NULL) {
453 if (*de->d_name != '.'
454 && (p = strstr (de->d_name, ":2,")) != NULL) {
455 if (!strchr (p + 3, 'T'))
457 if (strchr (p + 3, 'F'))
464 else if (tmp->new > 0)
465 /* keep current stats if !force and !$mail_check reached */
470 /* only check on force or $mail_check reached */
471 if (force != 0 || (now - last1 >= BuffyTimeout)) {
472 if ((tmp->new = mh_buffy (tmp->path)) > 0)
474 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
478 if ((dp = opendir (path)) == NULL)
483 while ((de = readdir (dp))) {
484 if (mh_valid_message (de->d_name)) {
493 else if (tmp->new > 0)
494 /* keep current stats if !force and !$mail_check reached */
500 /* only check on force or $imap_mail_check reached */
501 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
502 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
503 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
505 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
512 else if (tmp->new > 0)
513 /* keep current stats if !force and !$imap_mail_check reached */
530 else if (Context && Context->path)
531 tmp->size = (long) sb.st_size; /* update the size */
536 else if (!tmp->notified)
538 tmp->has_new = tmp->new > 0;
540 if (BuffyCount > 0 && force != 2)
541 draw_sidebar (CurrentMenu);
545 int mutt_buffy_list (void)
548 char path[_POSIX_PATH_MAX];
552 int have_unnotified = BuffyNotify;
554 if (option (OPTFORCEBUFFYCHECK))
555 mutt_buffy_check (1);
560 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
561 for (tmp = Incoming; tmp; tmp = tmp->next) {
562 /* Is there new mail in this mailbox? */
563 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
566 strfcpy (path, tmp->path, sizeof (path));
567 mutt_pretty_mailbox (path);
569 if (!first && pos + strlen (path) >= COLS - 7)
573 pos += strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
575 /* Prepend an asterisk to mailboxes not already notified */
576 if (!tmp->notified) {
577 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
581 pos += strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
585 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
588 /* on new mail: redraw sidebar */
589 draw_sidebar (CurrentMenu);
590 mutt_message ("%s", buffylist);
593 /* there were no mailboxes needing to be notified, so clean up since
594 * BuffyNotify has somehow gotten out of sync
600 int mutt_buffy_notify (void)
602 if (mutt_buffy_check (0) && BuffyNotify) {
603 return (mutt_buffy_list ());
609 * mutt_buffy() -- incoming folders completion routine
611 * given a folder name, this routine gives the next incoming folder with new
614 void mutt_buffy (char *s, size_t slen)
617 BUFFY *tmp = Incoming;
619 mutt_expand_path (s, _POSIX_PATH_MAX);
620 switch (mutt_buffy_check (0)) {
628 while (tmp && tmp->new <= 0)
632 mutt_buffy_check (1); /* buffy was wrong - resync things */
635 strfcpy (s, tmp->path, slen);
636 mutt_pretty_mailbox (s);
643 if (mutt_strcmp (s, tmp->path) == 0)
645 else if (count && tmp->new > 0)
655 mutt_buffy_check (1); /* buffy was wrong - resync things */
658 strfcpy (s, tmp->path, slen);
659 mutt_pretty_mailbox (s);