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 * safe_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 (!safe_strncmp (buffer + i, "\n\nFrom ", safe_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 (!safe_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 if (!list_empty(Incoming)) {
146 for (i = 0; i < Incoming->length; i++) {
147 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
148 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
149 return ((BUFFY*) Incoming->data[i]);
155 void mutt_update_mailbox (BUFFY * b)
162 if (stat (b->path, &sb) == 0)
163 b->size = (long) sb.st_size;
170 /* func to free buffy for list_del() */
171 static void buffy_free (void** p) {
172 FREE(&(*((BUFFY**) p))->path);
176 int buffy_lookup (const char* path) {
178 if (list_empty(Incoming) || !path || !*path)
180 for (i = 0; i < Incoming->length; i++) {
181 if (safe_strcmp (((BUFFY*) Incoming->data[i])->path, path) == 0)
187 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
191 char buf[_POSIX_PATH_MAX];
195 #endif /* BUFFY_SIZE */
197 while (MoreArgs (s)) {
198 mutt_extract_token (path, s, 0);
199 strfcpy (buf, path->data, sizeof (buf));
201 if (data == M_UNMAILBOXES && safe_strcmp (buf, "*") == 0) {
202 list_del (&Incoming, buffy_free);
206 /* Skip empty tokens. */
210 mutt_expand_path (buf, sizeof (buf));
211 i = buffy_lookup (buf);
213 if (data == M_UNMAILBOXES) {
215 tmp = (BUFFY*) list_pop_idx (Incoming, i);
222 tmp = safe_calloc (1, sizeof (BUFFY));
223 tmp->path = safe_strdup (buf);
225 list_push_back (&Incoming, tmp);
226 i = Incoming->length-1;
228 tmp = (BUFFY*) Incoming->data[i];
232 tmp->newly_created = 0;
235 /* for buffy_size, it is important that if the folder is new (tested by
236 * reading it), the size is set to 0 so that later when we check we see
237 * that it increased . without buffy_size we probably don't care.
239 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
240 /* some systems out there don't have an off_t type */
241 tmp->size = (long) sb.st_size;
245 #endif /* BUFFY_SIZE */
251 /* people use buffy_size on systems where modified time attributes are BADLY
252 * broken. Ignore them.
254 #define STAT_CHECK (sb.st_size > tmp->size)
256 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
257 #endif /* BUFFY_SIZE */
260 * 0 don't force any checks + update sidebar
261 * 1 force all checks + update sidebar
262 * 2 force all checks + _don't_ update sidebar
264 int mutt_buffy_check (int force)
270 char path[_POSIX_PATH_MAX];
271 struct stat contex_sb;
274 int i = 0, local = 0;
278 /* update postponed count as well, on force */
280 mutt_update_num_postponed ();
283 /* fastest return if there are no mailboxes */
284 if (list_empty(Incoming))
287 if (force == 0 && (now - BuffyTime < BuffyTimeout)
289 && (now - ImapBuffyTime < ImapBuffyTimeout))
296 if (force != 0 || now - BuffyTime >= BuffyTimeout)
299 last2 = ImapBuffyTime;
300 if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
306 if (!Context || !Context->path ||
307 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
308 /* check device ID and serial number instead of comparing paths */
309 contex_sb.st_dev = 0;
310 contex_sb.st_ino = 0;
313 for (i = 0; i < Incoming->length; i++) {
314 tmp = (BUFFY*) Incoming->data[i];
315 tmp->magic = mx_get_magic (tmp->path);
316 local = mx_is_local (tmp->magic-1);
317 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
318 /* if the mailbox still doesn't exist, set the newly created flag to
319 * be ready for when it does. */
320 tmp->newly_created = 1;
328 /* check to see if the folder is the currently selected folder
330 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
331 sb.st_ino != contex_sb.st_ino) :
332 safe_strcmp (tmp->path, Context->path))) {
333 switch (tmp->magic) {
336 /* only check on force or $mail_check reached */
337 if (force != 0 || (now - last1 >= BuffyTimeout)) {
338 if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
345 /* some other program has deleted mail from the folder */
346 tmp->size = (long) sb.st_size;
350 else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
351 (STAT_CHECK || tmp->msgcount == 0)) {
352 /* sidebar visible */
355 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
357 tmp->msgcount = ctx->msgcount;
359 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
360 tmp->msg_flagged = ctx->flagged;
361 mx_close_mailbox (ctx, 0);
364 if (tmp->newly_created &&
365 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
366 tmp->newly_created = 0;
368 else if (tmp->new > 0)
373 /* only check on force or $mail_check reached */
374 if (force != 0 || (now - last1 >= BuffyTimeout)) {
375 snprintf (path, sizeof (path), "%s/new", tmp->path);
376 if ((dirp = opendir (path)) == NULL) {
383 while ((de = readdir (dirp)) != NULL) {
386 if (*de->d_name != '.' &&
387 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
388 /* one new and undeleted message is enough */
392 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
393 /* if sidebar invisible -> done */
403 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
404 /* only count total mail if sidebar visible */
405 snprintf (path, sizeof (path), "%s/cur", tmp->path);
406 if ((dirp = opendir (path)) == NULL) {
410 tmp->msg_flagged = 0;
411 while ((de = readdir (dirp)) != NULL) {
414 if (*de->d_name != '.'
415 && (p = strstr (de->d_name, ":2,")) != NULL) {
416 if (!strchr (p + 3, 'T'))
418 if (strchr (p + 3, 'F'))
425 else if (tmp->new > 0)
426 /* keep current stats if !force and !$mail_check reached */
431 /* only check on force or $mail_check reached */
432 if (force != 0 || (now - last1 >= BuffyTimeout)) {
433 if ((tmp->new = mh_buffy (tmp->path)) > 0)
435 if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
439 if ((dp = opendir (path)) == NULL)
444 while ((de = readdir (dp))) {
445 if (mh_valid_message (de->d_name)) {
454 else if (tmp->new > 0)
455 /* keep current stats if !force and !$mail_check reached */
461 /* only check on force or $imap_mail_check reached */
462 if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
463 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
464 if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
466 tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
473 else if (tmp->new > 0)
474 /* keep current stats if !force and !$imap_mail_check reached */
482 else if (Context && Context->path)
483 tmp->size = (long) sb.st_size; /* update the size */
488 else if (!tmp->notified)
490 tmp->has_new = tmp->new > 0;
492 if (BuffyCount > 0 && force != 2)
493 sidebar_draw (CurrentMenu);
497 int mutt_buffy_list (void)
500 char path[_POSIX_PATH_MAX];
504 int have_unnotified = BuffyNotify;
507 if (option (OPTFORCEBUFFYCHECK))
508 mutt_buffy_check (1);
513 pos += safe_strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
515 for (i = 0; i < Incoming->length; i++) {
516 tmp = (BUFFY*) Incoming->data[i];
517 /* Is there new mail in this mailbox? */
518 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
521 strfcpy (path, tmp->path, sizeof (path));
522 mutt_pretty_mailbox (path);
524 if (!first && pos + safe_strlen (path) >= COLS - 7)
528 pos += safe_strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
530 /* Prepend an asterisk to mailboxes not already notified */
531 if (!tmp->notified) {
532 /* pos += safe_strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
536 pos += safe_strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
540 if (!first && i < Incoming->length) {
541 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
544 /* on new mail: redraw sidebar */
545 sidebar_draw (CurrentMenu);
546 mutt_message ("%s", buffylist);
549 /* there were no mailboxes needing to be notified, so clean up since
550 * BuffyNotify has somehow gotten out of sync
556 int mutt_buffy_notify (void)
558 if (mutt_buffy_check (0) && BuffyNotify) {
559 return (mutt_buffy_list ());
565 * mutt_buffy() -- incoming folders completion routine
567 * given a folder name, this routine gives the next incoming folder with new
570 void mutt_buffy (char *s, size_t slen)
574 if (list_empty(Incoming))
577 mutt_expand_path (s, _POSIX_PATH_MAX);
578 if (mutt_buffy_check (0) == 0) {
583 i = buffy_lookup (s);
584 c = i == Incoming->length-1 ? 0 : i+1;
585 while (((BUFFY*) Incoming->data[c])->new <= 0) {
586 c = (c+1) % Incoming->length;
588 break; /* tried all once */
592 /* something went wrong since we're here when mutt_buffy_check
593 * reported new mail */
594 mutt_buffy_check (0);
596 strfcpy (s, ((BUFFY*) Incoming->data[c])->path, slen);
597 mutt_pretty_mailbox (s);