2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Rocco Rutte <pdmef@cs.tu-berlin.de>
8 * This file is part of mutt-ng, see http://www.muttng.org/.
9 * It's licensed under the GNU General Public License,
10 * please see the file GPL in the top level source directory.
23 #include "mutt_curses.h"
41 static time_t BuffyTime = 0; /* last time we started checking for mail */
44 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
46 static short BuffyCount = 0; /* how many boxes with new mail */
47 static short BuffyNotify = 0; /* # of unnotified new boxes */
51 /* Find the last message in the file.
52 * upon success return 0. If no message found - return -1 */
54 int fseek_last_message (FILE * f)
57 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
59 int i; /* Index into `buffer' for scanning. */
61 memset (buffer, 0, sizeof (buffer));
62 fseek (f, 0, SEEK_END);
65 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
66 * `bytes_read' <= `BUFSIZ'. */
67 bytes_read = pos % BUFSIZ;
70 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
71 * reads will be on block boundaries, which might increase efficiency. */
72 while ((pos -= bytes_read) >= 0) {
73 /* we save in the buffer at the end the first 7 chars from the last read */
74 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * mutt_strlen(CRLF) */
75 fseek (f, pos, SEEK_SET);
76 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
79 for (i = bytes_read; --i >= 0;)
80 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom "))) { /* found it - go to the beginning of the From */
81 fseek (f, pos + i + 2, SEEK_SET);
87 /* here we are at the beginning of the file */
88 if (!mutt_strncmp ("From ", buffer, 5)) {
96 /* Return 1 if the last message is new */
97 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 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"))) {
129 rc = test_last_status_new (f);
136 BUFFY *mutt_find_mailbox (const char *path)
142 if (stat (path, &sb) != 0)
145 for (tmp = Incoming; tmp; tmp = tmp->next) {
146 if (stat (tmp->path, &tmp_sb) == 0 &&
147 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
153 void mutt_update_mailbox (BUFFY * b)
160 if (stat (b->path, &sb) == 0)
161 b->size = (long) sb.st_size;
168 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
172 char buf[_POSIX_PATH_MAX];
176 #endif /* BUFFY_SIZE */
178 while (MoreArgs (s)) {
179 mutt_extract_token (path, s, 0);
180 strfcpy (buf, path->data, sizeof (buf));
182 if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
183 for (tmp = &Incoming; *tmp;) {
184 FREE (&((*tmp)->path));
192 mutt_expand_path (buf, sizeof (buf));
194 /* Skip empty tokens. */
198 /* simple check to avoid duplicates */
199 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next)) {
200 if (mutt_strcmp (buf, (*tmp)->path) == 0)
204 if (data == M_UNMAILBOXES) {
206 FREE (&((*tmp)->path));
215 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
216 (*tmp)->path = safe_strdup (buf);
218 /* it is tempting to set magic right here */
224 (*tmp)->notified = 1;
225 (*tmp)->newly_created = 0;
228 /* for buffy_size, it is important that if the folder is new (tested by
229 * reading it), the size is set to 0 so that later when we check we see
230 * that it increased . without buffy_size we probably don't care.
232 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path)) {
233 /* some systems out there don't have an off_t type */
234 (*tmp)->size = (long) sb.st_size;
238 #endif /* BUFFY_SIZE */
244 /* people use buffy_size on systems where modified time attributes are BADLY
245 * broken. Ignore them.
247 #define STAT_CHECK (sb.st_size > tmp->size)
249 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
250 #endif /* BUFFY_SIZE */
253 * 0 don't force any checks + update sidebar
254 * 1 force all checks + update sidebar
255 * 2 force all checks + _don't_ update sidebar
257 int mutt_buffy_check (int force)
263 char path[_POSIX_PATH_MAX];
264 struct stat contex_sb;
271 /* update postponed count as well, on force */
273 mutt_update_num_postponed ();
276 /* fastest return if there are no mailboxes */
280 if (force == 0 && (now - BuffyTime < BuffyTimeout)
282 && (now - ImapBuffyTime < ImapBuffyTimeout))
289 if (force != 0 || now - BuffyTime >= BuffyTimeout)
292 last2 = ImapBuffyTime;
293 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
300 if (!Context || Context->magic != M_IMAP)
303 if (!Context || Context->magic != M_POP)
306 if (!Context || Context->magic != M_NNTP)
308 /* check device ID and serial number instead of comparing paths */
309 if (!Context || !Context->path
310 || stat (Context->path, &contex_sb) != 0) {
311 contex_sb.st_dev = 0;
312 contex_sb.st_ino = 0;
315 for (tmp = Incoming; tmp; tmp = tmp->next) {
317 if (mx_is_imap (tmp->path))
322 if (mx_is_pop (tmp->path))
327 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
331 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
332 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
333 /* if the mailbox still doesn't exist, set the newly created flag to
334 * be ready for when it does. */
335 tmp->newly_created = 1;
343 /* check to see if the folder is the currently selected folder
345 if (!Context || !Context->path || ((0
347 || tmp->magic == M_IMAP
350 || tmp->magic == M_POP
353 || tmp->magic == M_NNTP
355 )? mutt_strcmp (tmp->path,
356 Context->path) : (sb.
369 switch (tmp->magic) {
372 /* only check on force or $mail_check reached */
373 if (force != 0 || (now - last1 >= BuffyTimeout)) {
374 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
381 /* some other program has deleted mail from the folder */
382 tmp->size = (long) sb.st_size;
386 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
387 (STAT_CHECK || tmp->msgcount == 0)) {
388 /* sidebar visible */
391 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
393 tmp->msgcount = ctx->msgcount;
395 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
396 tmp->msg_flagged = ctx->flagged;
397 mx_close_mailbox (ctx, 0);
400 if (tmp->newly_created &&
401 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
402 tmp->newly_created = 0;
404 else if (tmp->new > 0)
409 /* only check on force or $mail_check reached */
410 if (force != 0 || (now - last1 >= BuffyTimeout)) {
411 snprintf (path, sizeof (path), "%s/new", tmp->path);
412 if ((dirp = opendir (path)) == NULL) {
419 while ((de = readdir (dirp)) != NULL) {
422 if (*de->d_name != '.' &&
423 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
424 /* one new and undeleted message is enough */
428 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
429 /* if sidebar invisible -> done */
439 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
440 /* only count total mail if sidebar visible */
441 snprintf (path, sizeof (path), "%s/cur", tmp->path);
442 if ((dirp = opendir (path)) == NULL) {
446 tmp->msg_flagged = 0;
447 while ((de = readdir (dirp)) != NULL) {
450 if (*de->d_name != '.'
451 && (p = strstr (de->d_name, ":2,")) != NULL) {
452 if (!strchr (p + 3, 'T'))
454 if (strchr (p + 3, 'F'))
461 else if (tmp->new > 0)
462 /* keep current stats if !force and !$mail_check reached */
467 /* only check on force or $mail_check reached */
468 if (force != 0 || (now - last1 >= BuffyTimeout)) {
469 if ((tmp->new = mh_buffy (tmp->path)) > 0)
471 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
475 if ((dp = opendir (path)) == NULL)
480 while ((de = readdir (dp))) {
481 if (mh_valid_message (de->d_name)) {
490 else if (tmp->new > 0)
491 /* keep current stats if !force and !$mail_check reached */
497 /* only check on force or $imap_mail_check reached */
498 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
499 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
500 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
502 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
509 else if (tmp->new > 0)
510 /* keep current stats if !force and !$imap_mail_check reached */
527 else if (Context && Context->path)
528 tmp->size = (long) sb.st_size; /* update the size */
533 else if (!tmp->notified)
535 tmp->has_new = tmp->new > 0;
537 if (BuffyCount > 0 && force != 2)
538 draw_sidebar (CurrentMenu);
542 int mutt_buffy_list (void)
545 char path[_POSIX_PATH_MAX];
549 int have_unnotified = BuffyNotify;
551 if (option (OPTFORCEBUFFYCHECK))
552 mutt_buffy_check (1);
557 pos += mutt_strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
558 for (tmp = Incoming; tmp; tmp = tmp->next) {
559 /* Is there new mail in this mailbox? */
560 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
563 strfcpy (path, tmp->path, sizeof (path));
564 mutt_pretty_mailbox (path);
566 if (!first && pos + mutt_strlen (path) >= COLS - 7)
570 pos += mutt_strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
572 /* Prepend an asterisk to mailboxes not already notified */
573 if (!tmp->notified) {
574 /* pos += mutt_strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
578 pos += mutt_strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
582 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
585 /* on new mail: redraw sidebar */
586 draw_sidebar (CurrentMenu);
587 mutt_message ("%s", buffylist);
590 /* there were no mailboxes needing to be notified, so clean up since
591 * BuffyNotify has somehow gotten out of sync
597 int mutt_buffy_notify (void)
599 if (mutt_buffy_check (0) && BuffyNotify) {
600 return (mutt_buffy_list ());
606 * mutt_buffy() -- incoming folders completion routine
608 * given a folder name, this routine gives the next incoming folder with new
611 void mutt_buffy (char *s, size_t slen)
614 BUFFY *tmp = Incoming;
616 mutt_expand_path (s, _POSIX_PATH_MAX);
617 switch (mutt_buffy_check (0)) {
625 while (tmp && tmp->new <= 0)
629 mutt_buffy_check (1); /* buffy was wrong - resync things */
632 strfcpy (s, tmp->path, slen);
633 mutt_pretty_mailbox (s);
640 if (mutt_strcmp (s, tmp->path) == 0)
642 else if (count && tmp->new > 0)
652 mutt_buffy_check (1); /* buffy was wrong - resync things */
655 strfcpy (s, tmp->path, slen);
656 mutt_pretty_mailbox (s);