-/* values for force:
- * 0 don't force any checks + update sidebar
- * 1 force all checks + update sidebar
- * 2 force all checks + _don't_ update sidebar
- */
-int mutt_buffy_check (int force)
-{
- BUFFY *tmp;
- struct stat sb;
- struct dirent *de;
- DIR *dirp;
- char path[_POSIX_PATH_MAX];
- struct stat contex_sb;
- time_t now, last1, last2;
- CONTEXT *ctx;
-#ifdef USE_IMAP
- /* update postponed count as well, on force */
- if (force != 0)
- mutt_update_num_postponed ();
-#endif
-
- /* fastest return if there are no mailboxes */
- if (!Incoming)
- return 0;
- now = time (NULL);
- if (force == 0 && (now - BuffyTime < BuffyTimeout)
-#ifdef USE_IMAP
- && (now - ImapBuffyTime < ImapBuffyTimeout))
-#else
- )
-#endif
- return BuffyCount;
-
- last1 = BuffyTime;
- if (force != 0 || now - BuffyTime >= BuffyTimeout)
- BuffyTime = now;
-#ifdef USE_IMAP
- last2 = ImapBuffyTime;
- if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
- ImapBuffyTime = now;
-#endif
- BuffyCount = 0;
- BuffyNotify = 0;
-
-#ifdef USE_IMAP
- if (!Context || Context->magic != M_IMAP)
-#endif
-#ifdef USE_POP
- if (!Context || Context->magic != M_POP)
-#endif
-#ifdef USE_NNTP
- if (!Context || Context->magic != M_NNTP)
-#endif
- /* check device ID and serial number instead of comparing paths */
- if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
- {
- contex_sb.st_dev=0;
- contex_sb.st_ino=0;
- }
-
- for (tmp = Incoming; tmp; tmp = tmp->next)
- {
-#ifdef USE_IMAP
- if (mx_is_imap (tmp->path))
- tmp->magic = M_IMAP;
- else
-#endif
-#ifdef USE_POP
- if (mx_is_pop (tmp->path))
- tmp->magic = M_POP;
- else
-#endif
-#ifdef USE_NNTP
- if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
- tmp->magic = M_NNTP;
- else
-#endif
- if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
- (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
- {
- /* if the mailbox still doesn't exist, set the newly created flag to
- * be ready for when it does. */
- tmp->newly_created = 1;
- tmp->magic = 0;
-#ifdef BUFFY_SIZE
- tmp->size = 0;
-#endif
- continue;
- }
-
- /* check to see if the folder is the currently selected folder
- * before polling */
- if (!Context || !Context->path ||
- (
- (0
-#ifdef USE_IMAP
- || tmp->magic == M_IMAP
-#endif
-#ifdef USE_POP
- || tmp->magic == M_POP
-#endif
-#ifdef USE_NNTP
- || tmp->magic == M_NNTP
-#endif
- ) ? mutt_strcmp (tmp->path, Context->path) :
- (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
- )
- )
- {
- switch (tmp->magic)
- {
- case M_MBOX:
- case M_MMDF:
- /* only check on force or $mail_check reached */
- if (force != 0 || (now - last1 >= BuffyTimeout)) {
- if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
- if (STAT_CHECK) {
- BuffyCount++;
- tmp->new = 1;
- }
-#ifdef BUFFY_SIZE
- else
- {
- /* some other program has deleted mail from the folder */
- tmp->size = (long) sb.st_size;
- }
-#endif
- } else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
- (STAT_CHECK || tmp->msgcount == 0)) {
- /* sidebar visible */
- BuffyCount++;
- if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
- tmp->msgcount = ctx->msgcount;
- tmp->new = ctx->new;
- tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
- tmp->msg_flagged = ctx->flagged;
- mx_close_mailbox (ctx, 0);
- }
- }
- if (tmp->newly_created &&
- (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
- tmp->newly_created = 0;
- } else if (tmp->new > 0)
- BuffyCount++;
- break;
-
- case M_MAILDIR:
- /* only check on force or $mail_check reached */
- if (force != 0 || (now - last1 >= BuffyTimeout)) {
- snprintf (path, sizeof (path), "%s/new", tmp->path);
- if ((dirp = opendir (path)) == NULL)
- {
- tmp->magic = 0;
- break;
- }
- tmp->new = 0;
- tmp->msg_unread = 0;
- tmp->msgcount = 0;
- while ((de = readdir (dirp)) != NULL)
- {
- char *p;
- if (*de->d_name != '.' &&
- (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
- {
- /* one new and undeleted message is enough */
- if (tmp->new == 0)
- {
- BuffyCount++;
- tmp->new = 1;
- if (SidebarWidth == 0 || !option (OPTMBOXPANE))
- /* if sidebar invisible -> done */
- break;
- }
- tmp->msgcount++;
- tmp->msg_unread++;
- tmp->new++;
- }
- }
- closedir (dirp);
-
- if (SidebarWidth > 0 && option (OPTMBOXPANE))
- {
- /* only count total mail if sidebar visible */
- snprintf (path, sizeof (path), "%s/cur", tmp->path);
- if ((dirp = opendir (path)) == NULL)
- {
- tmp->magic = 0;
- break;
- }
- tmp->msg_flagged = 0;
- while ((de = readdir (dirp)) != NULL)
- {
- char *p;
- if (*de->d_name != '.' && (p = strstr (de->d_name, ":2,")) != NULL) {
- if (!strchr (p + 3, 'T'))
- tmp->msgcount++;
- if (strchr (p + 3, 'F'))
- tmp->msg_flagged++;
- }
- }
- closedir (dirp);
- }
- } else if (tmp->new > 0)
- /* keep current stats if !force and !$mail_check reached */
- BuffyCount++;
- break;
-
- case M_MH:
- /* only check on force or $mail_check reached */
- if (force != 0 || (now - last1 >= BuffyTimeout)) {
- if ((tmp->new = mh_buffy (tmp->path)) > 0)
- BuffyCount++;
- if (SidebarWidth > 0 && option (OPTMBOXPANE))
- {
- DIR *dp;
- struct dirent *de;
- if ((dp = opendir (path)) == NULL)
- break;
- tmp->new = 0;
- tmp->msgcount = 0;
- tmp->msg_unread = 0;
- while ((de = readdir (dp)))
- {
- if (mh_valid_message (de->d_name))
- {
- tmp->msgcount++;
- tmp->msg_unread++;
- tmp->new++;
- }
- }
- closedir (dp);
- }
- } else if (tmp->new > 0)
- /* keep current stats if !force and !$mail_check reached */
- BuffyCount++;
- break;
-
-#ifdef USE_IMAP
- case M_IMAP:
- /* only check on force or $imap_mail_check reached */
- if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
- tmp->msgcount = imap_mailbox_check (tmp->path, 0);
- if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
- BuffyCount++;
- tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
- }
- else {
- tmp->new = 0;
- tmp->msg_unread = 0;
- }
- } else if (tmp->new > 0)
- /* keep current stats if !force and !$imap_mail_check reached */
- BuffyCount++;
- break;
-#endif
-
-#ifdef USE_POP
- case M_POP:
- break;
-#endif
-
-#ifdef USE_NNTP
- case M_NNTP:
- break;
-#endif
- }