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/buffer.h>
19 #include <lib-lib/macros.h>
21 #include <imap/imap.h>
29 #include "mutt_curses.h"
40 static time_t BuffyTime = 0; /* last time we started checking for mail */
42 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 static int fseeko_last_message (FILE * f)
54 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
56 int i; /* Index into `buffer' for scanning. */
59 fseeko (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 * m_strlen(CRLF) */
72 fseeko (f, pos, SEEK_SET);
73 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
76 for (i = bytes_read; --i >= 0;)
77 if (!m_strncmp(buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
78 fseeko (f, pos + i + 2, SEEK_SET);
84 /* here we are at the beginning of the file */
85 if (!m_strncmp("From ", buffer, 5)) {
93 /* Return 1 if the last message is new */
94 static int test_last_status_new (FILE * f)
97 ENVELOPE *tmp_envelope;
100 if (fseeko_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 static 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 *buffy_find_mailbox (const char *path)
139 if (stat (path, &sb) != 0)
142 if (!list_empty(Incoming)) {
143 for (i = 0; i < Incoming->length; i++) {
144 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
145 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
146 return ((BUFFY*) Incoming->data[i]);
152 void buffy_update_mailbox (BUFFY * b)
159 if (stat (b->path, &sb) == 0)
160 b->size = (long) sb.st_size;
167 /* func to free buffy for list_del() */
168 static void buffy_free (BUFFY** p) {
169 p_delete(&(*p)->path);
173 int buffy_lookup (const char* path) {
175 if (list_empty(Incoming) || !path || !*path)
177 for (i = 0; i < Incoming->length; i++) {
178 if (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) )
184 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
185 BUFFER * err __attribute__ ((unused)))
188 char buf[_POSIX_PATH_MAX];
192 #endif /* BUFFY_SIZE */
194 while (MoreArgs (s)) {
195 mutt_extract_token (path, s, 0);
196 m_strcpy(buf, sizeof(buf), path->data);
198 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
199 list_del (&Incoming, (list_del_t*) buffy_free);
203 /* Skip empty tokens. */
207 mutt_expand_path (buf, sizeof (buf));
208 i = buffy_lookup (buf);
210 if (data == M_UNMAILBOXES) {
212 tmp = (BUFFY*) list_pop_idx (Incoming, i);
219 tmp = p_new(BUFFY, 1);
220 tmp->path = m_strdup(buf);
222 list_push_back (&Incoming, tmp);
223 i = Incoming->length-1;
225 tmp = (BUFFY*) Incoming->data[i];
229 tmp->newly_created = 0;
232 /* for buffy_size, it is important that if the folder is new (tested by
233 * reading it), the size is set to 0 so that later when we check we see
234 * that it increased . without buffy_size we probably don't care.
236 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
237 /* some systems out there don't have an off_t type */
238 tmp->size = (long) sb.st_size;
242 #endif /* BUFFY_SIZE */
248 /* people use buffy_size on systems where modified time attributes are BADLY
249 * broken. Ignore them.
251 #define STAT_CHECK (sb.st_size > tmp->size)
253 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
254 #endif /* BUFFY_SIZE */
257 * 0 don't force any checks + update sidebar
258 * 1 force all checks + update sidebar
259 * 2 don't force any checks + _don't_ update sidebar
261 int buffy_check (int force)
267 char path[_POSIX_PATH_MAX];
268 struct stat contex_sb;
272 int local = 0, count = 0;
275 /* update postponed count as well, on force */
277 mutt_update_num_postponed ();
279 /* fastest return if there are no mailboxes */
280 if (list_empty(Incoming))
283 if (force == 0 && (now - BuffyTime < BuffyTimeout)
284 && (now - ImapBuffyTime < ImapBuffyTimeout))
288 if (force == 1 || now - BuffyTime >= BuffyTimeout)
290 last2 = ImapBuffyTime;
291 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
296 count = sidebar_need_count ();
298 if (!Context || !Context->path ||
299 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
300 /* check device ID and serial number instead of comparing paths */
301 contex_sb.st_dev = 0;
302 contex_sb.st_ino = 0;
305 for (i = 0; i < Incoming->length; i++) {
306 tmp = (BUFFY*) Incoming->data[i];
307 tmp->magic = mx_get_magic (tmp->path);
308 local = mx_is_local (tmp->magic-1);
309 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
310 /* if the mailbox still doesn't exist, set the newly created flag to
311 * be ready for when it does. */
312 tmp->newly_created = 1;
320 /* check to see if the folder is the currently selected folder
322 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
323 sb.st_ino != contex_sb.st_ino) :
324 !m_strcmp(tmp->path, Context->path))) {
325 switch (tmp->magic) {
328 /* only check on force or $mail_check reached */
329 if (force == 1 || (now - last1 >= BuffyTimeout)) {
337 /* some other program has deleted mail from the folder */
338 tmp->size = (long) sb.st_size;
342 else if (STAT_CHECK || tmp->msgcount == 0) {
343 /* sidebar visible */
346 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
348 tmp->msgcount = ctx->msgcount;
350 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
351 tmp->msg_flagged = ctx->flagged;
352 mx_close_mailbox (ctx, 0);
355 if (tmp->newly_created &&
356 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
357 tmp->newly_created = 0;
359 else if (tmp->new > 0)
364 /* only check on force or $mail_check reached */
365 if (force == 1 || (now - last1 >= BuffyTimeout)) {
366 snprintf (path, sizeof (path), "%s/new", tmp->path);
367 if ((dirp = opendir (path)) == NULL) {
374 while ((de = readdir (dirp)) != NULL) {
377 if (*de->d_name != '.' &&
378 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
379 /* one new and undeleted message is enough */
383 /* if sidebar invisible -> done */
396 /* only count total mail if sidebar visible */
397 snprintf (path, sizeof (path), "%s/cur", tmp->path);
398 if ((dirp = opendir (path)) == NULL) {
402 tmp->msg_flagged = 0;
403 while ((de = readdir (dirp)) != NULL) {
406 if (*de->d_name != '.'
407 && (p = strstr (de->d_name, ":2,")) != NULL) {
408 if (!strchr (p + 3, 'T'))
410 if (strchr (p + 3, 'F'))
417 else if (tmp->new > 0)
418 /* keep current stats if !force and !$mail_check reached */
423 /* only check on force or $mail_check reached */
424 if (force == 1 || (now - last1 >= BuffyTimeout)) {
425 if ((tmp->new = mh_buffy (tmp->path)) > 0)
430 if ((dp = opendir (path)) == NULL)
435 while ((de = readdir (dp))) {
436 if (mh_valid_message (de->d_name)) {
445 else if (tmp->new > 0)
446 /* keep current stats if !force and !$mail_check reached */
451 /* only check on force or $imap_mail_check reached */
452 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
453 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
454 tmp->new = imap_mailbox_check (tmp->path, 1);
455 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
460 if (tmp->msg_unread < 0)
463 else if (tmp->new > 0)
464 /* keep current stats if !force and !$imap_mail_check reached */
471 else if (Context && Context->path)
472 tmp->size = (long) sb.st_size; /* update the size */
477 else if (!tmp->notified)
479 tmp->has_new = tmp->new > 0;
481 if (BuffyCount > 0 && force != 2)
482 sidebar_draw (CurrentMenu);
486 int buffy_list (void)
489 char path[_POSIX_PATH_MAX];
493 int have_unnotified = BuffyNotify;
499 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
501 for (i = 0; i < Incoming->length; i++) {
502 tmp = (BUFFY*) Incoming->data[i];
503 /* Is there new mail in this mailbox? */
504 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
507 m_strcpy(path, sizeof(path), tmp->path);
508 mutt_pretty_mailbox (path);
510 if (!first && pos + m_strlen(path) >= COLS - 7)
514 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
516 /* Prepend an asterisk to mailboxes not already notified */
517 if (!tmp->notified) {
518 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
522 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
526 if (!first && i < Incoming->length) {
527 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
530 /* on new mail: redraw sidebar */
531 sidebar_draw (CurrentMenu);
532 mutt_message ("%s", buffylist);
535 /* there were no mailboxes needing to be notified, so clean up since
536 * BuffyNotify has somehow gotten out of sync
542 int buffy_notify (void)
544 if (buffy_check (0) && BuffyNotify) {
545 return (buffy_list ());
551 * mutt_buffy() -- incoming folders completion routine
553 * given a folder name, this routine gives the next incoming folder with new
556 void buffy_next (char *s, size_t slen)
561 if (list_empty(Incoming))
564 mutt_expand_path (s, _POSIX_PATH_MAX);
565 if (buffy_check (0) == 0) {
571 * If buffy_lookup returns the index,
572 * or -1 if not found (-1..Incoming->length-1);
573 * plus one --> (0..Incoming->length).
574 * Modulo mapps it into the correct range.
576 i = 1 + buffy_lookup (s);
577 for (l=0; l < Incoming->length; l++) {
578 c = (l+i) % Incoming->length;
579 if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) &&
580 ((BUFFY*) Incoming->data[c])->new > 0)
583 if (l >= Incoming->length) {
585 /* something went wrong since we're here when buffy_check
586 * reported new mail */
589 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
590 mutt_pretty_mailbox (s);