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"
38 static time_t BuffyTime = 0; /* last time we started checking for mail */
41 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
43 static short BuffyCount = 0; /* how many boxes with new mail */
44 static short BuffyNotify = 0; /* # of unnotified new boxes */
48 /* Find the last message in the file.
49 * upon success return 0. If no message found - return -1 */
51 int fseek_last_message (FILE * f)
54 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
56 int i; /* Index into `buffer' for scanning. */
58 memset (buffer, 0, sizeof (buffer));
59 fseek (f, 0, SEEK_END);
62 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
63 * `bytes_read' <= `BUFSIZ'. */
64 bytes_read = pos % BUFSIZ;
67 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
68 * reads will be on block boundaries, which might increase efficiency. */
69 while ((pos -= bytes_read) >= 0) {
70 /* we save in the buffer at the end the first 7 chars from the last read */
71 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * mutt_strlen(CRLF) */
72 fseek (f, pos, SEEK_SET);
73 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
76 for (i = bytes_read; --i >= 0;)
77 if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom "))) { /* found it - go to the beginning of the From */
78 fseek (f, pos + i + 2, SEEK_SET);
84 /* here we are at the beginning of the file */
85 if (!mutt_strncmp ("From ", buffer, 5)) {
93 /* Return 1 if the last message is new */
94 int test_last_status_new (FILE * f)
97 ENVELOPE *tmp_envelope;
100 if (fseek_last_message (f) == -1)
103 hdr = mutt_new_header ();
104 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
105 if (!(hdr->read || hdr->old))
108 mutt_free_envelope (&tmp_envelope);
109 mutt_free_header (&hdr);
114 int test_new_folder (const char *path)
120 typ = mx_get_magic (path);
122 if (typ != M_MBOX && typ != M_MMDF)
125 if ((f = fopen (path, "rb"))) {
126 rc = test_last_status_new (f);
133 BUFFY *mutt_find_mailbox (const char *path)
139 if (stat (path, &sb) != 0)
142 for (tmp = Incoming; tmp; tmp = tmp->next) {
143 if (stat (tmp->path, &tmp_sb) == 0 &&
144 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
150 void mutt_update_mailbox (BUFFY * b)
157 if (stat (b->path, &sb) == 0)
158 b->size = (long) sb.st_size;
165 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
169 char buf[_POSIX_PATH_MAX];
173 #endif /* BUFFY_SIZE */
175 while (MoreArgs (s)) {
176 mutt_extract_token (path, s, 0);
177 strfcpy (buf, path->data, sizeof (buf));
179 if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
180 for (tmp = &Incoming; *tmp;) {
181 FREE (&((*tmp)->path));
189 mutt_expand_path (buf, sizeof (buf));
191 /* Skip empty tokens. */
195 /* simple check to avoid duplicates */
196 for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next)) {
197 if (mutt_strcmp (buf, (*tmp)->path) == 0)
201 if (data == M_UNMAILBOXES) {
203 FREE (&((*tmp)->path));
212 *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
213 (*tmp)->path = safe_strdup (buf);
215 /* it is tempting to set magic right here */
221 (*tmp)->notified = 1;
222 (*tmp)->newly_created = 0;
225 /* for buffy_size, it is important that if the folder is new (tested by
226 * reading it), the size is set to 0 so that later when we check we see
227 * that it increased . without buffy_size we probably don't care.
229 if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path)) {
230 /* some systems out there don't have an off_t type */
231 (*tmp)->size = (long) sb.st_size;
235 #endif /* BUFFY_SIZE */
241 /* people use buffy_size on systems where modified time attributes are BADLY
242 * broken. Ignore them.
244 #define STAT_CHECK (sb.st_size > tmp->size)
246 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
247 #endif /* BUFFY_SIZE */
250 * 0 don't force any checks + update sidebar
251 * 1 force all checks + update sidebar
252 * 2 force all checks + _don't_ update sidebar
254 int mutt_buffy_check (int force)
260 char path[_POSIX_PATH_MAX];
261 struct stat contex_sb;
268 /* update postponed count as well, on force */
270 mutt_update_num_postponed ();
273 /* fastest return if there are no mailboxes */
277 if (force == 0 && (now - BuffyTime < BuffyTimeout)
279 && (now - ImapBuffyTime < ImapBuffyTimeout))
286 if (force != 0 || now - BuffyTime >= BuffyTimeout)
289 last2 = ImapBuffyTime;
290 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
297 if (!Context || Context->magic != M_IMAP)
300 if (!Context || Context->magic != M_POP)
303 if (!Context || Context->magic != M_NNTP)
305 /* check device ID and serial number instead of comparing paths */
306 if (!Context || !Context->path
307 || stat (Context->path, &contex_sb) != 0) {
308 contex_sb.st_dev = 0;
309 contex_sb.st_ino = 0;
312 for (tmp = Incoming; tmp; tmp = tmp->next) {
314 if (mx_is_imap (tmp->path))
319 if (mx_is_pop (tmp->path))
324 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
328 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
329 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
330 /* if the mailbox still doesn't exist, set the newly created flag to
331 * be ready for when it does. */
332 tmp->newly_created = 1;
340 /* check to see if the folder is the currently selected folder
342 if (!Context || !Context->path || ((0
344 || tmp->magic == M_IMAP
347 || tmp->magic == M_POP
350 || tmp->magic == M_NNTP
352 )? mutt_strcmp (tmp->path,
353 Context->path) : (sb.
366 switch (tmp->magic) {
369 /* only check on force or $mail_check reached */
370 if (force != 0 || (now - last1 >= BuffyTimeout)) {
371 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
378 /* some other program has deleted mail from the folder */
379 tmp->size = (long) sb.st_size;
383 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
384 (STAT_CHECK || tmp->msgcount == 0)) {
385 /* sidebar visible */
388 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
390 tmp->msgcount = ctx->msgcount;
392 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
393 tmp->msg_flagged = ctx->flagged;
394 mx_close_mailbox (ctx, 0);
397 if (tmp->newly_created &&
398 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
399 tmp->newly_created = 0;
401 else if (tmp->new > 0)
406 /* only check on force or $mail_check reached */
407 if (force != 0 || (now - last1 >= BuffyTimeout)) {
408 snprintf (path, sizeof (path), "%s/new", tmp->path);
409 if ((dirp = opendir (path)) == NULL) {
416 while ((de = readdir (dirp)) != NULL) {
419 if (*de->d_name != '.' &&
420 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
421 /* one new and undeleted message is enough */
425 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
426 /* if sidebar invisible -> done */
436 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
437 /* only count total mail if sidebar visible */
438 snprintf (path, sizeof (path), "%s/cur", tmp->path);
439 if ((dirp = opendir (path)) == NULL) {
443 tmp->msg_flagged = 0;
444 while ((de = readdir (dirp)) != NULL) {
447 if (*de->d_name != '.'
448 && (p = strstr (de->d_name, ":2,")) != NULL) {
449 if (!strchr (p + 3, 'T'))
451 if (strchr (p + 3, 'F'))
458 else if (tmp->new > 0)
459 /* keep current stats if !force and !$mail_check reached */
464 /* only check on force or $mail_check reached */
465 if (force != 0 || (now - last1 >= BuffyTimeout)) {
466 if ((tmp->new = mh_buffy (tmp->path)) > 0)
468 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
472 if ((dp = opendir (path)) == NULL)
477 while ((de = readdir (dp))) {
478 if (mh_valid_message (de->d_name)) {
487 else if (tmp->new > 0)
488 /* keep current stats if !force and !$mail_check reached */
494 /* only check on force or $imap_mail_check reached */
495 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
496 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
497 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
499 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
506 else if (tmp->new > 0)
507 /* keep current stats if !force and !$imap_mail_check reached */
524 else if (Context && Context->path)
525 tmp->size = (long) sb.st_size; /* update the size */
530 else if (!tmp->notified)
532 tmp->has_new = tmp->new > 0;
534 if (BuffyCount > 0 && force != 2)
535 draw_sidebar (CurrentMenu);
539 int mutt_buffy_list (void)
542 char path[_POSIX_PATH_MAX];
546 int have_unnotified = BuffyNotify;
548 if (option (OPTFORCEBUFFYCHECK))
549 mutt_buffy_check (1);
554 pos += mutt_strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
555 for (tmp = Incoming; tmp; tmp = tmp->next) {
556 /* Is there new mail in this mailbox? */
557 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
560 strfcpy (path, tmp->path, sizeof (path));
561 mutt_pretty_mailbox (path);
563 if (!first && pos + mutt_strlen (path) >= COLS - 7)
567 pos += mutt_strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
569 /* Prepend an asterisk to mailboxes not already notified */
570 if (!tmp->notified) {
571 /* pos += mutt_strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
575 pos += mutt_strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
579 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
582 /* on new mail: redraw sidebar */
583 draw_sidebar (CurrentMenu);
584 mutt_message ("%s", buffylist);
587 /* there were no mailboxes needing to be notified, so clean up since
588 * BuffyNotify has somehow gotten out of sync
594 int mutt_buffy_notify (void)
596 if (mutt_buffy_check (0) && BuffyNotify) {
597 return (mutt_buffy_list ());
603 * mutt_buffy() -- incoming folders completion routine
605 * given a folder name, this routine gives the next incoming folder with new
608 void mutt_buffy (char *s, size_t slen)
611 BUFFY *tmp = Incoming;
613 mutt_expand_path (s, _POSIX_PATH_MAX);
614 switch (mutt_buffy_check (0)) {
622 while (tmp && tmp->new <= 0)
626 mutt_buffy_check (1); /* buffy was wrong - resync things */
629 strfcpy (s, tmp->path, slen);
630 mutt_pretty_mailbox (s);
637 if (mutt_strcmp (s, tmp->path) == 0)
639 else if (count && tmp->new > 0)
649 mutt_buffy_check (1); /* buffy was wrong - resync things */
652 strfcpy (s, tmp->path, slen);
653 mutt_pretty_mailbox (s);