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)
177 BUFFY **tmp,*tmp1,*last;
178 char buf[_POSIX_PATH_MAX];
182 #endif /* BUFFY_SIZE */
186 * to get rid of correcting the ->prev pointers in sidebar.c,
187 * correct them right here
192 * if we really want to make the sort order of the sidebar
193 * configurable, this has to go right here
198 mutt_extract_token (path, s, 0);
199 strfcpy (buf, path->data, sizeof (buf));
201 if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
203 for (tmp = &Incoming; *tmp;)
205 FREE (&((*tmp)->path));
213 mutt_expand_path (buf, sizeof (buf));
215 /* Skip empty tokens. */
218 /* simple check to avoid duplicates */
220 for (tmp = &Incoming; *tmp && dup == 0; tmp = &((*tmp)->next))
222 if (mutt_strcmp (buf, (*tmp)->path) == 0) {
232 if(data == M_UNMAILBOXES)
236 FREE (&((*tmp)->path));
244 /* loop over list while it's sorted */
247 for (tmp = &Incoming; *tmp ; tmp = &((*tmp)->next)) {
250 * change this to get whatever sorting order
252 if (mutt_strcmp (buf, (*tmp)->path) < 0) {
259 /* we want: last -> tmp -> tmp1 */
260 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
261 (*tmp)->path = safe_strdup (buf);
264 /* correct pointers */
271 (*tmp)->notified = 1;
272 (*tmp)->newly_created = 0;
275 /* for buffy_size, it is important that if the folder is new (tested by
276 * reading it), the size is set to 0 so that later when we check we see
277 * that it increased . without buffy_size we probably don't care.
279 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
281 /* some systems out there don't have an off_t type */
282 (*tmp)->size = (long) sb.st_size;
286 #endif /* BUFFY_SIZE */
292 /* people use buffy_size on systems where modified time attributes are BADLY
293 * broken. Ignore them.
295 #define STAT_CHECK (sb.st_size > tmp->size)
297 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
298 #endif /* BUFFY_SIZE */
300 int mutt_buffy_check (int force)
306 char path[_POSIX_PATH_MAX];
307 struct stat contex_sb;
308 time_t now, last1, last2;
311 /* update postponed count as well, on force */
313 mutt_update_num_postponed ();
316 /* fastest return if there are no mailboxes */
320 if (!force && (now - BuffyTime < BuffyTimeout)
322 && (now - ImapBuffyTime < ImapBuffyTimeout))
329 if (force || now - BuffyTime >= BuffyTimeout)
332 last2 = ImapBuffyTime;
333 if (force || now - ImapBuffyTime >= ImapBuffyTimeout)
340 if (!Context || Context->magic != M_IMAP)
343 if (!Context || Context->magic != M_POP)
346 if (!Context || Context->magic != M_NNTP)
348 /* check device ID and serial number instead of comparing paths */
349 if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
355 for (tmp = Incoming; tmp; tmp = tmp->next)
358 if (mx_is_imap (tmp->path))
363 if (mx_is_pop (tmp->path))
368 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
372 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
373 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
375 /* if the mailbox still doesn't exist, set the newly created flag to
376 * be ready for when it does. */
377 tmp->newly_created = 1;
385 /* check to see if the folder is the currently selected folder
387 if (!Context || !Context->path ||
391 || tmp->magic == M_IMAP
394 || tmp->magic == M_POP
397 || tmp->magic == M_NNTP
399 ) ? mutt_strcmp (tmp->path, Context->path) :
400 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
408 /* only check on force or $mail_check reached */
409 if (force || (now - last1 >= BuffyTimeout)) {
412 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
415 tmp->new = tmp->has_new = 1;
420 /* some other program has deleted mail from the folder */
421 tmp->size = (long) sb.st_size;
424 } else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
425 (STAT_CHECK || tmp->msgcount == 0)) {
426 /* sidebar visible */
427 int msg_count = 0, msg_new = 0, msg_unread = 0;
429 if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
430 msg_count = ctx->msgcount;
432 msg_unread = ctx->unread;
433 mx_close_mailbox (ctx, 0);
435 tmp->msgcount = msg_count;
437 tmp->msg_unread = msg_unread;
438 tmp->has_new = msg_new > 0;
440 if (tmp->newly_created &&
441 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
442 tmp->newly_created = 0;
443 tmp->has_new = tmp->new > 0;
444 } else if (tmp->new > 0) {
445 /* keep current stats if !force and !$mail_check reached */
452 /* only check on force or $mail_check reached */
453 if (force || (now - last1 >= BuffyTimeout)) {
454 snprintf (path, sizeof (path), "%s/new", tmp->path);
455 if ((dirp = opendir (path)) == NULL)
463 while ((de = readdir (dirp)) != NULL)
466 if (*de->d_name != '.' &&
467 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
469 /* one new and undeleted message is enough */
473 tmp->has_new = tmp->new = 1;
474 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
475 /* if sidebar invisible -> done */
484 tmp->has_new = tmp->new > 0;
486 if (SidebarWidth > 0 && option (OPTMBOXPANE))
488 /* only count total mail if sidebar visible */
489 snprintf (path, sizeof (path), "%s/cur", tmp->path);
490 if ((dirp = opendir (path)) == NULL)
495 while ((de = readdir (dirp)) != NULL)
498 if (*de->d_name != '.' &&
499 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
506 } else if (tmp->new > 0) {
507 /* keep current stats if !force and !$mail_check reached */
514 /* only check on force or $mail_check reached */
515 if (force || (now - last1 >= BuffyTimeout)) {
518 if ((tmp->new = mh_buffy (tmp->path)) > 0)
520 if (SidebarWidth > 0 && option (OPTMBOXPANE))
524 if ((dp = opendir (path)) == NULL)
527 while ((de = readdir (dp)))
529 if (mh_valid_message (de->d_name))
538 tmp->has_new = tmp->new > 0;
539 } else if (tmp->new > 0) {
540 /* keep current stats if !force and !$mail_check reached */
548 /* only check on force or $imap_mail_check reached */
549 if (force || (now - last2 >= ImapBuffyTimeout)) {
552 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
553 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
555 tmp->has_new = tmp->new > 0;
556 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
560 } else if (tmp->new > 0) {
561 /* keep current stats if !force and !$imap_mail_check reached */
580 else if (Context && Context->path)
581 tmp->size = (long) sb.st_size; /* update the size */
586 else if (!tmp->notified)
588 tmp->has_new = tmp->new > 0;
591 draw_sidebar (CurrentMenu);
595 int mutt_buffy_list (void)
598 char path[_POSIX_PATH_MAX];
602 int have_unnotified = BuffyNotify;
604 if (option (OPTFORCEBUFFYCHECK))
605 mutt_buffy_check (1);
610 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
611 for (tmp = Incoming; tmp; tmp = tmp->next)
613 /* Is there new mail in this mailbox? */
614 if (!tmp->new || (have_unnotified && tmp->notified))
617 strfcpy (path, tmp->path, sizeof (path));
618 mutt_pretty_mailbox (path);
620 if (!first && pos + strlen (path) >= COLS - 7)
624 pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
626 /* Prepend an asterisk to mailboxes not already notified */
629 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
633 pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
638 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
642 /* on new mail: redraw sidebar */
643 draw_sidebar (CurrentMenu);
644 mutt_message ("%s", buffylist);
647 /* there were no mailboxes needing to be notified, so clean up since
648 * BuffyNotify has somehow gotten out of sync
654 int mutt_buffy_notify (void)
656 if (mutt_buffy_check (0) && BuffyNotify)
658 return (mutt_buffy_list ());
664 * mutt_buffy() -- incoming folders completion routine
666 * given a folder name, this routine gives the next incoming folder with new
669 void mutt_buffy (char *s, size_t slen)
672 BUFFY *tmp = Incoming;
674 mutt_expand_path (s, _POSIX_PATH_MAX);
675 switch (mutt_buffy_check (0))
684 while (tmp && !tmp->new)
689 mutt_buffy_check (1); /* buffy was wrong - resync things */
692 strfcpy (s, tmp->path, slen);
693 mutt_pretty_mailbox (s);
701 if (mutt_strcmp (s, tmp->path) == 0)
703 else if (count && tmp->new)
715 mutt_buffy_check (1); /* buffy was wrong - resync things */
718 strfcpy (s, tmp->path, slen);
719 mutt_pretty_mailbox (s);