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.
24 #include "mutt_curses.h"
42 static time_t BuffyTime = 0; /* last time we started checking for mail */
45 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
47 static short BuffyCount = 0; /* how many boxes with new mail */
48 static short BuffyNotify = 0; /* # of unnotified new boxes */
52 /* Find the last message in the file.
53 * upon success return 0. If no message found - return -1 */
55 static int fseek_last_message (FILE * f)
58 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
60 int i; /* Index into `buffer' for scanning. */
62 memset (buffer, 0, sizeof (buffer));
63 fseek (f, 0, SEEK_END);
66 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
67 * `bytes_read' <= `BUFSIZ'. */
68 bytes_read = pos % BUFSIZ;
71 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
72 * reads will be on block boundaries, which might increase efficiency. */
73 while ((pos -= bytes_read) >= 0) {
74 /* we save in the buffer at the end the first 7 chars from the last read */
75 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * str_len(CRLF) */
76 fseek (f, pos, SEEK_SET);
77 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
80 for (i = bytes_read; --i >= 0;)
81 if (!str_ncmp (buffer + i, "\n\nFrom ", str_len ("\n\nFrom "))) { /* found it - go to the beginning of the From */
82 fseek (f, pos + i + 2, SEEK_SET);
88 /* here we are at the beginning of the file */
89 if (!str_ncmp ("From ", buffer, 5)) {
97 /* Return 1 if the last message is new */
98 static int test_last_status_new (FILE * f)
101 ENVELOPE *tmp_envelope;
104 if (fseek_last_message (f) == -1)
107 hdr = mutt_new_header ();
108 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
109 if (!(hdr->read || hdr->old))
112 mutt_free_envelope (&tmp_envelope);
113 mutt_free_header (&hdr);
118 static int test_new_folder (const char *path)
124 typ = mx_get_magic (path);
126 if (typ != M_MBOX && typ != M_MMDF)
129 if ((f = fopen (path, "rb"))) {
130 rc = test_last_status_new (f);
137 BUFFY *buffy_find_mailbox (const char *path)
143 if (stat (path, &sb) != 0)
146 if (!list_empty(Incoming)) {
147 for (i = 0; i < Incoming->length; i++) {
148 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
149 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
150 return ((BUFFY*) Incoming->data[i]);
156 void buffy_update_mailbox (BUFFY * b)
163 if (stat (b->path, &sb) == 0)
164 b->size = (long) sb.st_size;
171 /* func to free buffy for list_del() */
172 static void buffy_free (BUFFY** p) {
173 mem_free(&(*p)->path);
177 int buffy_lookup (const char* path) {
179 if (list_empty(Incoming) || !path || !*path)
181 for (i = 0; i < Incoming->length; i++) {
182 if (str_eq (((BUFFY*) Incoming->data[i])->path, path) )
188 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
192 char buf[_POSIX_PATH_MAX];
196 #endif /* BUFFY_SIZE */
198 while (MoreArgs (s)) {
199 mutt_extract_token (path, s, 0);
200 strfcpy (buf, path->data, sizeof (buf));
202 if (data == M_UNMAILBOXES && str_eq (buf, "*")) {
203 list_del (&Incoming, (list_del_t*) buffy_free);
204 sidebar_draw (CurrentMenu);
208 /* Skip empty tokens. */
212 mutt_expand_path (buf, sizeof (buf));
213 i = buffy_lookup (buf);
215 if (data == M_UNMAILBOXES) {
217 tmp = (BUFFY*) list_pop_idx (Incoming, i);
224 tmp = mem_calloc (1, sizeof (BUFFY));
225 tmp->path = str_dup (buf);
227 list_push_back (&Incoming, tmp);
228 i = Incoming->length-1;
230 tmp = (BUFFY*) Incoming->data[i];
234 tmp->newly_created = 0;
237 /* for buffy_size, it is important that if the folder is new (tested by
238 * reading it), the size is set to 0 so that later when we check we see
239 * that it increased . without buffy_size we probably don't care.
241 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
242 /* some systems out there don't have an off_t type */
243 tmp->size = (long) sb.st_size;
247 #endif /* BUFFY_SIZE */
249 sidebar_draw (CurrentMenu);
254 /* people use buffy_size on systems where modified time attributes are BADLY
255 * broken. Ignore them.
257 #define STAT_CHECK (sb.st_size > tmp->size)
259 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
260 #endif /* BUFFY_SIZE */
263 * 0 don't force any checks + update sidebar
264 * 1 force all checks + update sidebar
265 * 2 force all checks + _don't_ update sidebar
267 int buffy_check (int force)
273 char path[_POSIX_PATH_MAX];
274 struct stat contex_sb;
277 int i = 0, local = 0, count = 0;
281 /* update postponed count as well, on force */
283 mutt_update_num_postponed ();
286 /* fastest return if there are no mailboxes */
287 if (list_empty(Incoming))
290 if (force == 0 && (now - BuffyTime < BuffyTimeout)
292 && (now - ImapBuffyTime < ImapBuffyTimeout))
299 if (force != 0 || now - BuffyTime >= BuffyTimeout)
302 last2 = ImapBuffyTime;
303 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
309 count = sidebar_need_count ();
311 if (!Context || !Context->path ||
312 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
313 /* check device ID and serial number instead of comparing paths */
314 contex_sb.st_dev = 0;
315 contex_sb.st_ino = 0;
318 for (i = 0; i < Incoming->length; i++) {
319 tmp = (BUFFY*) Incoming->data[i];
320 tmp->magic = mx_get_magic (tmp->path);
321 local = mx_is_local (tmp->magic-1);
322 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
323 /* if the mailbox still doesn't exist, set the newly created flag to
324 * be ready for when it does. */
325 tmp->newly_created = 1;
333 /* check to see if the folder is the currently selected folder
335 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
336 sb.st_ino != contex_sb.st_ino) :
337 !str_eq (tmp->path, Context->path))) {
338 switch (tmp->magic) {
341 /* only check on force or $mail_check reached */
342 if (force != 0 || (now - last1 >= BuffyTimeout)) {
350 /* some other program has deleted mail from the folder */
351 tmp->size = (long) sb.st_size;
355 else if (STAT_CHECK || tmp->msgcount == 0) {
356 /* sidebar visible */
359 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
361 tmp->msgcount = ctx->msgcount;
363 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
364 tmp->msg_flagged = ctx->flagged;
365 mx_close_mailbox (ctx, 0);
368 if (tmp->newly_created &&
369 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
370 tmp->newly_created = 0;
372 else if (tmp->new > 0)
377 /* only check on force or $mail_check reached */
378 if (force != 0 || (now - last1 >= BuffyTimeout)) {
379 snprintf (path, sizeof (path), "%s/new", tmp->path);
380 if ((dirp = opendir (path)) == NULL) {
387 while ((de = readdir (dirp)) != NULL) {
390 if (*de->d_name != '.' &&
391 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
392 /* one new and undeleted message is enough */
397 /* if sidebar invisible -> done */
408 /* only count total mail if sidebar visible */
409 snprintf (path, sizeof (path), "%s/cur", tmp->path);
410 if ((dirp = opendir (path)) == NULL) {
414 tmp->msg_flagged = 0;
415 while ((de = readdir (dirp)) != NULL) {
418 if (*de->d_name != '.'
419 && (p = strstr (de->d_name, ":2,")) != NULL) {
420 if (!strchr (p + 3, 'T'))
422 if (strchr (p + 3, 'F'))
429 else if (tmp->new > 0)
430 /* keep current stats if !force and !$mail_check reached */
435 /* only check on force or $mail_check reached */
436 if (force != 0 || (now - last1 >= BuffyTimeout)) {
437 if ((tmp->new = mh_buffy (tmp->path)) > 0)
443 if ((dp = opendir (path)) == NULL)
448 while ((de = readdir (dp))) {
449 if (mh_valid_message (de->d_name)) {
458 else if (tmp->new > 0)
459 /* keep current stats if !force and !$mail_check reached */
465 /* only check on force or $imap_mail_check reached */
466 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
467 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
468 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
470 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
477 else if (tmp->new > 0)
478 /* keep current stats if !force and !$imap_mail_check reached */
486 else if (Context && Context->path)
487 tmp->size = (long) sb.st_size; /* update the size */
492 else if (!tmp->notified)
494 tmp->has_new = tmp->new > 0;
496 if (BuffyCount > 0 && force != 2)
497 sidebar_draw (CurrentMenu);
501 int buffy_list (void)
504 char path[_POSIX_PATH_MAX];
508 int have_unnotified = BuffyNotify;
514 pos += str_len (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
516 for (i = 0; i < Incoming->length; i++) {
517 tmp = (BUFFY*) Incoming->data[i];
518 /* Is there new mail in this mailbox? */
519 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
522 strfcpy (path, tmp->path, sizeof (path));
523 mutt_pretty_mailbox (path);
525 if (!first && pos + str_len (path) >= COLS - 7)
529 pos += str_len (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
531 /* Prepend an asterisk to mailboxes not already notified */
532 if (!tmp->notified) {
533 /* pos += str_len (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
537 pos += str_len (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
541 if (!first && i < Incoming->length) {
542 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
545 /* on new mail: redraw sidebar */
546 sidebar_draw (CurrentMenu);
547 mutt_message ("%s", buffylist);
550 /* there were no mailboxes needing to be notified, so clean up since
551 * BuffyNotify has somehow gotten out of sync
557 int buffy_notify (void)
559 if (buffy_check (0) && BuffyNotify) {
560 return (buffy_list ());
566 * mutt_buffy() -- incoming folders completion routine
568 * given a folder name, this routine gives the next incoming folder with new
571 void buffy_next (char *s, size_t slen)
573 int i = 0, c = 0, l = 0;
575 if (list_empty(Incoming))
578 mutt_expand_path (s, _POSIX_PATH_MAX);
579 if (buffy_check (0) == 0) {
585 * If buffy_lookup returns the index,
586 * or -1 if not found (-1..Incoming->length-1);
587 * plus one --> (0..Incoming->length).
588 * Modulo mapps it into the correct range.
590 i = 1 + buffy_lookup (s);
591 for (l=0; l < Incoming->length; l++) {
592 c = (l+i) % Incoming->length;
593 if ((!Context || !Context->path || !str_eq (((BUFFY*) Incoming->data[c])->path, Context->path)) &&
594 ((BUFFY*) Incoming->data[c])->new > 0)
597 if (l >= Incoming->length) {
599 /* something went wrong since we're here when buffy_check
600 * reported new mail */
603 strfcpy (s, ((BUFFY*) Incoming->data[c])->path, slen);
604 mutt_pretty_mailbox (s);