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.
17 #include <lib-lib/mem.h>
18 #include <lib-lib/macros.h>
27 #include "mutt_curses.h"
43 static time_t BuffyTime = 0; /* last time we started checking for mail */
46 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
48 static short BuffyCount = 0; /* how many boxes with new mail */
49 static short BuffyNotify = 0; /* # of unnotified new boxes */
53 /* Find the last message in the file.
54 * upon success return 0. If no message found - return -1 */
56 static int fseeko_last_message (FILE * f)
59 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
61 int i; /* Index into `buffer' for scanning. */
63 memset (buffer, 0, sizeof (buffer));
64 fseeko (f, 0, SEEK_END);
67 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
68 * `bytes_read' <= `BUFSIZ'. */
69 bytes_read = pos % BUFSIZ;
72 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
73 * reads will be on block boundaries, which might increase efficiency. */
74 while ((pos -= bytes_read) >= 0) {
75 /* we save in the buffer at the end the first 7 chars from the last read */
76 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * m_strlen(CRLF) */
77 fseeko (f, pos, SEEK_SET);
78 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
81 for (i = bytes_read; --i >= 0;)
82 if (!str_ncmp (buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
83 fseeko (f, pos + i + 2, SEEK_SET);
89 /* here we are at the beginning of the file */
90 if (!str_ncmp ("From ", buffer, 5)) {
98 /* Return 1 if the last message is new */
99 static int test_last_status_new (FILE * f)
102 ENVELOPE *tmp_envelope;
105 if (fseeko_last_message (f) == -1)
108 hdr = mutt_new_header ();
109 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
110 if (!(hdr->read || hdr->old))
113 mutt_free_envelope (&tmp_envelope);
114 mutt_free_header (&hdr);
119 static int test_new_folder (const char *path)
125 typ = mx_get_magic (path);
127 if (typ != M_MBOX && typ != M_MMDF)
130 if ((f = fopen (path, "rb"))) {
131 rc = test_last_status_new (f);
138 BUFFY *buffy_find_mailbox (const char *path)
144 if (stat (path, &sb) != 0)
147 if (!list_empty(Incoming)) {
148 for (i = 0; i < Incoming->length; i++) {
149 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
150 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
151 return ((BUFFY*) Incoming->data[i]);
157 void buffy_update_mailbox (BUFFY * b)
164 if (stat (b->path, &sb) == 0)
165 b->size = (long) sb.st_size;
172 /* func to free buffy for list_del() */
173 static void buffy_free (BUFFY** p) {
174 p_delete(&(*p)->path);
178 int buffy_lookup (const char* path) {
180 if (list_empty(Incoming) || !path || !*path)
182 for (i = 0; i < Incoming->length; i++) {
183 if (str_eq (((BUFFY*) Incoming->data[i])->path, path) )
189 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
193 char buf[_POSIX_PATH_MAX];
197 #endif /* BUFFY_SIZE */
199 while (MoreArgs (s)) {
200 mutt_extract_token (path, s, 0);
201 strfcpy (buf, path->data, sizeof (buf));
203 if (data == M_UNMAILBOXES && str_eq (buf, "*")) {
204 list_del (&Incoming, (list_del_t*) buffy_free);
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 = p_new(BUFFY, 1);
225 tmp->path = m_strdup(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 */
253 /* people use buffy_size on systems where modified time attributes are BADLY
254 * broken. Ignore them.
256 #define STAT_CHECK (sb.st_size > tmp->size)
258 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
259 #endif /* BUFFY_SIZE */
262 * 0 don't force any checks + update sidebar
263 * 1 force all checks + update sidebar
264 * 2 don't force any checks + _don't_ update sidebar
266 int buffy_check (int force)
272 char path[_POSIX_PATH_MAX];
273 struct stat contex_sb;
276 int i = 0, local = 0, count = 0;
280 /* update postponed count as well, on force */
282 mutt_update_num_postponed ();
285 /* fastest return if there are no mailboxes */
286 if (list_empty(Incoming))
289 if (force == 0 && (now - BuffyTime < BuffyTimeout)
291 && (now - ImapBuffyTime < ImapBuffyTimeout))
298 if (force == 1 || now - BuffyTime >= BuffyTimeout)
301 last2 = ImapBuffyTime;
302 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
308 count = sidebar_need_count ();
310 if (!Context || !Context->path ||
311 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
312 /* check device ID and serial number instead of comparing paths */
313 contex_sb.st_dev = 0;
314 contex_sb.st_ino = 0;
317 for (i = 0; i < Incoming->length; i++) {
318 tmp = (BUFFY*) Incoming->data[i];
319 tmp->magic = mx_get_magic (tmp->path);
320 local = mx_is_local (tmp->magic-1);
321 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
322 /* if the mailbox still doesn't exist, set the newly created flag to
323 * be ready for when it does. */
324 tmp->newly_created = 1;
332 /* check to see if the folder is the currently selected folder
334 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
335 sb.st_ino != contex_sb.st_ino) :
336 !str_eq (tmp->path, Context->path))) {
337 switch (tmp->magic) {
340 /* only check on force or $mail_check reached */
341 if (force == 1 || (now - last1 >= BuffyTimeout)) {
349 /* some other program has deleted mail from the folder */
350 tmp->size = (long) sb.st_size;
354 else if (STAT_CHECK || tmp->msgcount == 0) {
355 /* sidebar visible */
358 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
360 tmp->msgcount = ctx->msgcount;
362 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
363 tmp->msg_flagged = ctx->flagged;
364 mx_close_mailbox (ctx, 0);
367 if (tmp->newly_created &&
368 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
369 tmp->newly_created = 0;
371 else if (tmp->new > 0)
376 /* only check on force or $mail_check reached */
377 if (force == 1 || (now - last1 >= BuffyTimeout)) {
378 snprintf (path, sizeof (path), "%s/new", tmp->path);
379 if ((dirp = opendir (path)) == NULL) {
386 while ((de = readdir (dirp)) != NULL) {
389 if (*de->d_name != '.' &&
390 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
391 /* one new and undeleted message is enough */
395 /* 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 == 1 || (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 == 1 || (now - last2 >= ImapBuffyTimeout)) {
467 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
468 tmp->new = imap_mailbox_check (tmp->path, 1);
469 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
474 if (tmp->msg_unread < 0)
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 += m_strlen(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 + m_strlen(path) >= COLS - 7)
529 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
531 /* Prepend an asterisk to mailboxes not already notified */
532 if (!tmp->notified) {
533 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
537 pos += m_strlen(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);