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 /* func to free buffy for list_del() */
169 static void buffy_free (void** p) {
170 FREE(&(*((BUFFY**) p))->path);
174 int buffy_lookup (const char* path) {
176 if (list_empty(Incoming) || !path || !*path)
178 for (i = 0; i < Incoming->length; i++) {
179 if (mutt_strcmp (((BUFFY*) Incoming->data[i])->path, path) == 0)
185 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
189 char buf[_POSIX_PATH_MAX];
193 #endif /* BUFFY_SIZE */
195 while (MoreArgs (s)) {
196 mutt_extract_token (path, s, 0);
197 strfcpy (buf, path->data, sizeof (buf));
199 if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
200 list_del (&Incoming, buffy_free);
204 /* Skip empty tokens. */
208 mutt_expand_path (buf, sizeof (buf));
209 i = buffy_lookup (buf);
211 if (data == M_UNMAILBOXES) {
213 tmp = (BUFFY*) list_pop_idx (Incoming, i);
220 tmp = safe_calloc (1, sizeof (BUFFY));
221 tmp->path = safe_strdup (buf);
223 list_push_back (&Incoming, tmp);
224 i = Incoming->length-1;
226 tmp = (BUFFY*) Incoming->data[i];
230 tmp->newly_created = 0;
233 /* for buffy_size, it is important that if the folder is new (tested by
234 * reading it), the size is set to 0 so that later when we check we see
235 * that it increased . without buffy_size we probably don't care.
237 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
238 /* some systems out there don't have an off_t type */
239 tmp->size = (long) sb.st_size;
243 #endif /* BUFFY_SIZE */
249 /* people use buffy_size on systems where modified time attributes are BADLY
250 * broken. Ignore them.
252 #define STAT_CHECK (sb.st_size > tmp->size)
254 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
255 #endif /* BUFFY_SIZE */
258 * 0 don't force any checks + update sidebar
259 * 1 force all checks + update sidebar
260 * 2 force all checks + _don't_ update sidebar
262 int mutt_buffy_check (int force)
268 char path[_POSIX_PATH_MAX];
269 struct stat contex_sb;
276 /* update postponed count as well, on force */
278 mutt_update_num_postponed ();
281 /* fastest return if there are no mailboxes */
282 if (list_empty(Incoming))
285 if (force == 0 && (now - BuffyTime < BuffyTimeout)
287 && (now - ImapBuffyTime < ImapBuffyTimeout))
294 if (force != 0 || now - BuffyTime >= BuffyTimeout)
297 last2 = ImapBuffyTime;
298 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
305 if (!Context || Context->magic != M_IMAP)
308 if (!Context || Context->magic != M_POP)
311 if (!Context || Context->magic != M_NNTP)
313 /* check device ID and serial number instead of comparing paths */
314 if (!Context || !Context->path
315 || stat (Context->path, &contex_sb) != 0) {
316 contex_sb.st_dev = 0;
317 contex_sb.st_ino = 0;
320 for (i = 0; i < Incoming->length; i++) {
321 tmp = (BUFFY*) Incoming->data[i];
323 if (mx_is_imap (tmp->path))
328 if (mx_is_pop (tmp->path))
333 if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
337 if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
338 (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
339 /* if the mailbox still doesn't exist, set the newly created flag to
340 * be ready for when it does. */
341 tmp->newly_created = 1;
349 /* check to see if the folder is the currently selected folder
351 if (!Context || !Context->path || ((0
353 || tmp->magic == M_IMAP
356 || tmp->magic == M_POP
359 || tmp->magic == M_NNTP
361 )? mutt_strcmp (tmp->path,
362 Context->path) : (sb.
375 switch (tmp->magic) {
378 /* only check on force or $mail_check reached */
379 if (force != 0 || (now - last1 >= BuffyTimeout)) {
380 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
387 /* some other program has deleted mail from the folder */
388 tmp->size = (long) sb.st_size;
392 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
393 (STAT_CHECK || tmp->msgcount == 0)) {
394 /* sidebar visible */
397 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
399 tmp->msgcount = ctx->msgcount;
401 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
402 tmp->msg_flagged = ctx->flagged;
403 mx_close_mailbox (ctx, 0);
406 if (tmp->newly_created &&
407 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
408 tmp->newly_created = 0;
410 else if (tmp->new > 0)
415 /* only check on force or $mail_check reached */
416 if (force != 0 || (now - last1 >= BuffyTimeout)) {
417 snprintf (path, sizeof (path), "%s/new", tmp->path);
418 if ((dirp = opendir (path)) == NULL) {
425 while ((de = readdir (dirp)) != NULL) {
428 if (*de->d_name != '.' &&
429 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
430 /* one new and undeleted message is enough */
434 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
435 /* if sidebar invisible -> done */
445 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
446 /* only count total mail if sidebar visible */
447 snprintf (path, sizeof (path), "%s/cur", tmp->path);
448 if ((dirp = opendir (path)) == NULL) {
452 tmp->msg_flagged = 0;
453 while ((de = readdir (dirp)) != NULL) {
456 if (*de->d_name != '.'
457 && (p = strstr (de->d_name, ":2,")) != NULL) {
458 if (!strchr (p + 3, 'T'))
460 if (strchr (p + 3, 'F'))
467 else if (tmp->new > 0)
468 /* keep current stats if !force and !$mail_check reached */
473 /* only check on force or $mail_check reached */
474 if (force != 0 || (now - last1 >= BuffyTimeout)) {
475 if ((tmp->new = mh_buffy (tmp->path)) > 0)
477 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
481 if ((dp = opendir (path)) == NULL)
486 while ((de = readdir (dp))) {
487 if (mh_valid_message (de->d_name)) {
496 else if (tmp->new > 0)
497 /* keep current stats if !force and !$mail_check reached */
503 /* only check on force or $imap_mail_check reached */
504 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
505 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
506 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
508 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
515 else if (tmp->new > 0)
516 /* keep current stats if !force and !$imap_mail_check reached */
533 else if (Context && Context->path)
534 tmp->size = (long) sb.st_size; /* update the size */
539 else if (!tmp->notified)
541 tmp->has_new = tmp->new > 0;
543 if (BuffyCount > 0 && force != 2)
544 sidebar_draw (CurrentMenu);
548 int mutt_buffy_list (void)
551 char path[_POSIX_PATH_MAX];
555 int have_unnotified = BuffyNotify;
558 if (option (OPTFORCEBUFFYCHECK))
559 mutt_buffy_check (1);
564 pos += mutt_strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
566 for (i = 0; i < Incoming->length; i++) {
567 tmp = (BUFFY*) Incoming->data[i];
568 /* Is there new mail in this mailbox? */
569 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
572 strfcpy (path, tmp->path, sizeof (path));
573 mutt_pretty_mailbox (path);
575 if (!first && pos + mutt_strlen (path) >= COLS - 7)
579 pos += mutt_strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
581 /* Prepend an asterisk to mailboxes not already notified */
582 if (!tmp->notified) {
583 /* pos += mutt_strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
587 pos += mutt_strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
591 if (!first && i < Incoming->length) {
592 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
595 /* on new mail: redraw sidebar */
596 sidebar_draw (CurrentMenu);
597 mutt_message ("%s", buffylist);
600 /* there were no mailboxes needing to be notified, so clean up since
601 * BuffyNotify has somehow gotten out of sync
607 int mutt_buffy_notify (void)
609 if (mutt_buffy_check (0) && BuffyNotify) {
610 return (mutt_buffy_list ());
616 * mutt_buffy() -- incoming folders completion routine
618 * given a folder name, this routine gives the next incoming folder with new
621 void mutt_buffy (char *s, size_t slen)
625 if (list_empty(Incoming))
628 mutt_expand_path (s, _POSIX_PATH_MAX);
629 if (mutt_buffy_check (0) == 0) {
634 i = buffy_lookup (s);
635 c = i == Incoming->length-1 ? 0 : i+1;
636 while (((BUFFY*) Incoming->data[c])->new <= 0) {
637 c = (c+1) % Incoming->length;
639 break; /* tried all once */
643 /* something went wrong since we're here when mutt_buffy_check
644 * reported new mail */
645 mutt_buffy_check (0);
647 strfcpy (s, ((BUFFY*) Incoming->data[c])->path, slen);
648 mutt_pretty_mailbox (s);