2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
20 #include "mutt_curses.h"
35 static time_t BuffyTime = 0; /* last time we started checking for mail */
38 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
40 static short BuffyCount = 0; /* how many boxes with new mail */
41 static short BuffyNotify = 0; /* # of unnotified new boxes */
45 /* Find the last message in the file.
46 * upon success return 0. If no message found - return -1 */
48 int fseek_last_message (FILE * f)
51 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
53 int i; /* Index into `buffer' for scanning. */
55 memset (buffer, 0, sizeof (buffer));
56 fseek (f, 0, SEEK_END);
59 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
60 * `bytes_read' <= `BUFSIZ'. */
61 bytes_read = pos % BUFSIZ;
64 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
65 * reads will be on block boundaries, which might increase efficiency. */
66 while ((pos -= bytes_read) >= 0) {
67 /* we save in the buffer at the end the first 7 chars from the last read */
68 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * mutt_strlen(CRLF) */
69 fseek (f, pos, SEEK_SET);
70 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
73 for (i = bytes_read; --i >= 0;)
74 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom "))) { /* found it - go to the beginning of the From */
75 fseek (f, pos + i + 2, SEEK_SET);
81 /* here we are at the beginning of the file */
82 if (!mutt_strncmp ("From ", buffer, 5)) {
90 /* Return 1 if the last message is new */
91 int test_last_status_new (FILE * f)
94 ENVELOPE *tmp_envelope;
97 if (fseek_last_message (f) == -1)
100 hdr = mutt_new_header ();
101 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
102 if (!(hdr->read || hdr->old))
105 mutt_free_envelope (&tmp_envelope);
106 mutt_free_header (&hdr);
111 int test_new_folder (const char *path)
117 typ = mx_get_magic (path);
119 if (typ != M_MBOX && typ != M_MMDF)
122 if ((f = fopen (path, "rb"))) {
123 rc = test_last_status_new (f);
130 BUFFY *mutt_find_mailbox (const char *path)
136 if (stat (path, &sb) != 0)
139 for (tmp = Incoming; tmp; tmp = tmp->next) {
140 if (stat (tmp->path, &tmp_sb) == 0 &&
141 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
147 void mutt_update_mailbox (BUFFY * b)
154 if (stat (b->path, &sb) == 0)
155 b->size = (long) sb.st_size;
162 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
166 char buf[_POSIX_PATH_MAX];
170 #endif /* BUFFY_SIZE */
172 while (MoreArgs (s)) {
173 mutt_extract_token (path, s, 0);
174 strfcpy (buf, path->data, sizeof (buf));
176 if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
177 for (tmp = &Incoming; *tmp;) {
178 FREE (&((*tmp)->path));
186 mutt_expand_path (buf, sizeof (buf));
188 /* Skip empty tokens. */
192 /* simple check to avoid duplicates */
193 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next)) {
194 if (mutt_strcmp (buf, (*tmp)->path) == 0)
198 if (data == M_UNMAILBOXES) {
200 FREE (&((*tmp)->path));
209 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
210 (*tmp)->path = safe_strdup (buf);
212 /* it is tempting to set magic right here */
218 (*tmp)->notified = 1;
219 (*tmp)->newly_created = 0;
222 /* for buffy_size, it is important that if the folder is new (tested by
223 * reading it), the size is set to 0 so that later when we check we see
224 * that it increased . without buffy_size we probably don't care.
226 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path)) {
227 /* some systems out there don't have an off_t type */
228 (*tmp)->size = (long) sb.st_size;
232 #endif /* BUFFY_SIZE */
238 /* people use buffy_size on systems where modified time attributes are BADLY
239 * broken. Ignore them.
241 #define STAT_CHECK (sb.st_size > tmp->size)
243 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
244 #endif /* BUFFY_SIZE */
247 * 0 don't force any checks + update sidebar
248 * 1 force all checks + update sidebar
249 * 2 force all checks + _don't_ update sidebar
251 int mutt_buffy_check (int force)
257 char path[_POSIX_PATH_MAX];
258 struct stat contex_sb;
265 /* update postponed count as well, on force */
267 mutt_update_num_postponed ();
270 /* fastest return if there are no mailboxes */
274 if (force == 0 && (now - BuffyTime < BuffyTimeout)
276 && (now - ImapBuffyTime < ImapBuffyTimeout))
283 if (force != 0 || now - BuffyTime >= BuffyTimeout)
286 last2 = ImapBuffyTime;
287 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
294 if (!Context || Context->magic != M_IMAP)
297 if (!Context || Context->magic != M_POP)
300 if (!Context || Context->magic != M_NNTP)
302 /* check device ID and serial number instead of comparing paths */
303 if (!Context || !Context->path
304 || stat (Context->path, &contex_sb) != 0) {
305 contex_sb.st_dev = 0;
306 contex_sb.st_ino = 0;
309 for (tmp = Incoming; tmp; tmp = tmp->next) {
311 if (mx_is_imap (tmp->path))
316 if (mx_is_pop (tmp->path))
321 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
325 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
326 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
327 /* if the mailbox still doesn't exist, set the newly created flag to
328 * be ready for when it does. */
329 tmp->newly_created = 1;
337 /* check to see if the folder is the currently selected folder
339 if (!Context || !Context->path || ((0
341 || tmp->magic == M_IMAP
344 || tmp->magic == M_POP
347 || tmp->magic == M_NNTP
349 )? mutt_strcmp (tmp->path,
350 Context->path) : (sb.
363 switch (tmp->magic) {
366 /* only check on force or $mail_check reached */
367 if (force != 0 || (now - last1 >= BuffyTimeout)) {
368 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
375 /* some other program has deleted mail from the folder */
376 tmp->size = (long) sb.st_size;
380 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
381 (STAT_CHECK || tmp->msgcount == 0)) {
382 /* sidebar visible */
385 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
387 tmp->msgcount = ctx->msgcount;
389 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
390 tmp->msg_flagged = ctx->flagged;
391 mx_close_mailbox (ctx, 0);
394 if (tmp->newly_created &&
395 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
396 tmp->newly_created = 0;
398 else if (tmp->new > 0)
403 /* only check on force or $mail_check reached */
404 if (force != 0 || (now - last1 >= BuffyTimeout)) {
405 snprintf (path, sizeof (path), "%s/new", tmp->path);
406 if ((dirp = opendir (path)) == NULL) {
413 while ((de = readdir (dirp)) != NULL) {
416 if (*de->d_name != '.' &&
417 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
418 /* one new and undeleted message is enough */
422 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
423 /* if sidebar invisible -> done */
433 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
434 /* only count total mail if sidebar visible */
435 snprintf (path, sizeof (path), "%s/cur", tmp->path);
436 if ((dirp = opendir (path)) == NULL) {
440 tmp->msg_flagged = 0;
441 while ((de = readdir (dirp)) != NULL) {
444 if (*de->d_name != '.'
445 && (p = strstr (de->d_name, ":2,")) != NULL) {
446 if (!strchr (p + 3, 'T'))
448 if (strchr (p + 3, 'F'))
455 else if (tmp->new > 0)
456 /* keep current stats if !force and !$mail_check reached */
461 /* only check on force or $mail_check reached */
462 if (force != 0 || (now - last1 >= BuffyTimeout)) {
463 if ((tmp->new = mh_buffy (tmp->path)) > 0)
465 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
469 if ((dp = opendir (path)) == NULL)
474 while ((de = readdir (dp))) {
475 if (mh_valid_message (de->d_name)) {
484 else if (tmp->new > 0)
485 /* keep current stats if !force and !$mail_check reached */
491 /* only check on force or $imap_mail_check reached */
492 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
493 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
494 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
496 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
503 else if (tmp->new > 0)
504 /* keep current stats if !force and !$imap_mail_check reached */
521 else if (Context && Context->path)
522 tmp->size = (long) sb.st_size; /* update the size */
527 else if (!tmp->notified)
529 tmp->has_new = tmp->new > 0;
531 if (BuffyCount > 0 && force != 2)
532 draw_sidebar (CurrentMenu);
536 int mutt_buffy_list (void)
539 char path[_POSIX_PATH_MAX];
543 int have_unnotified = BuffyNotify;
545 if (option (OPTFORCEBUFFYCHECK))
546 mutt_buffy_check (1);
551 pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
552 for (tmp = Incoming; tmp; tmp = tmp->next) {
553 /* Is there new mail in this mailbox? */
554 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
557 strfcpy (path, tmp->path, sizeof (path));
558 mutt_pretty_mailbox (path);
560 if (!first && pos + strlen (path) >= COLS - 7)
564 pos += strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
566 /* Prepend an asterisk to mailboxes not already notified */
567 if (!tmp->notified) {
568 /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
572 pos += strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
576 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
579 /* on new mail: redraw sidebar */
580 draw_sidebar (CurrentMenu);
581 mutt_message ("%s", buffylist);
584 /* there were no mailboxes needing to be notified, so clean up since
585 * BuffyNotify has somehow gotten out of sync
591 int mutt_buffy_notify (void)
593 if (mutt_buffy_check (0) && BuffyNotify) {
594 return (mutt_buffy_list ());
600 * mutt_buffy() -- incoming folders completion routine
602 * given a folder name, this routine gives the next incoming folder with new
605 void mutt_buffy (char *s, size_t slen)
608 BUFFY *tmp = Incoming;
610 mutt_expand_path (s, _POSIX_PATH_MAX);
611 switch (mutt_buffy_check (0)) {
619 while (tmp && tmp->new <= 0)
623 mutt_buffy_check (1); /* buffy was wrong - resync things */
626 strfcpy (s, tmp->path, slen);
627 mutt_pretty_mailbox (s);
634 if (mutt_strcmp (s, tmp->path) == 0)
636 else if (count && tmp->new > 0)
646 mutt_buffy_check (1); /* buffy was wrong - resync things */
649 strfcpy (s, tmp->path, slen);
650 mutt_pretty_mailbox (s);