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 <lib-ui/curses.h>
23 #include <imap/imap.h>
41 static time_t BuffyTime = 0; /* last time we started checking for mail */
43 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
44 static short BuffyCount = 0; /* how many boxes with new mail */
45 static short BuffyNotify = 0; /* # of unnotified new boxes */
49 /* Find the last message in the file.
50 * upon success return 0. If no message found - return -1 */
52 static int fseeko_last_message (FILE * f)
55 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
57 int i; /* Index into `buffer' for scanning. */
60 fseeko (f, 0, SEEK_END);
63 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
64 * `bytes_read' <= `BUFSIZ'. */
65 bytes_read = pos % BUFSIZ;
68 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
69 * reads will be on block boundaries, which might increase efficiency. */
70 while ((pos -= bytes_read) >= 0) {
71 /* we save in the buffer at the end the first 7 chars from the last read */
72 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * m_strlen(CRLF) */
73 fseeko (f, pos, SEEK_SET);
74 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
77 for (i = bytes_read; --i >= 0;)
78 if (!m_strncmp(buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
79 fseeko (f, pos + i + 2, SEEK_SET);
85 /* here we are at the beginning of the file */
86 if (!m_strncmp("From ", buffer, 5)) {
94 /* Return 1 if the last message is new */
95 static int test_last_status_new (FILE * f)
98 ENVELOPE *tmp_envelope;
101 if (fseeko_last_message (f) == -1)
105 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
106 if (!(hdr->read || hdr->old))
109 envelope_delete(&tmp_envelope);
115 static int test_new_folder (const char *path)
121 typ = mx_get_magic (path);
123 if (typ != M_MBOX && typ != M_MMDF)
126 if ((f = fopen (path, "rb"))) {
127 rc = test_last_status_new (f);
134 BUFFY *buffy_find_mailbox (const char *path)
140 if (stat (path, &sb) != 0)
143 if (!list_empty(Incoming)) {
144 for (i = 0; i < Incoming->length; i++) {
145 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
146 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
147 return ((BUFFY*) Incoming->data[i]);
153 void buffy_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 (BUFFY** p) {
170 p_delete(&(*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 (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) )
185 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
186 BUFFER * err __attribute__ ((unused)))
189 char buf[_POSIX_PATH_MAX];
193 #endif /* BUFFY_SIZE */
195 while (MoreArgs (s)) {
196 mutt_extract_token (path, s, 0);
197 m_strcpy(buf, sizeof(buf), path->data);
199 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
200 list_del (&Incoming, (list_del_t*) 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 = p_new(BUFFY, 1);
221 tmp->path = m_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 don't force any checks + _don't_ update sidebar
262 int buffy_check (int force)
268 char path[_POSIX_PATH_MAX];
269 struct stat contex_sb;
273 int local = 0, count = 0;
276 /* update postponed count as well, on force */
278 mutt_update_num_postponed ();
280 /* fastest return if there are no mailboxes */
281 if (list_empty(Incoming))
284 if (force == 0 && (now - BuffyTime < BuffyTimeout)
285 && (now - ImapBuffyTime < ImapBuffyTimeout))
289 if (force == 1 || now - BuffyTime >= BuffyTimeout)
291 last2 = ImapBuffyTime;
292 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
297 count = sidebar_need_count ();
299 if (!Context || !Context->path ||
300 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
301 /* check device ID and serial number instead of comparing paths */
302 contex_sb.st_dev = 0;
303 contex_sb.st_ino = 0;
306 for (i = 0; i < Incoming->length; i++) {
307 tmp = (BUFFY*) Incoming->data[i];
308 tmp->magic = mx_get_magic (tmp->path);
309 local = mx_is_local (tmp->magic-1);
310 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
311 /* if the mailbox still doesn't exist, set the newly created flag to
312 * be ready for when it does. */
313 tmp->newly_created = 1;
321 /* check to see if the folder is the currently selected folder
323 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
324 sb.st_ino != contex_sb.st_ino) :
325 m_strcmp(tmp->path, Context->path))) {
326 switch (tmp->magic) {
329 /* only check on force or $mail_check reached */
330 if (force == 1 || (now - last1 >= BuffyTimeout)) {
338 /* some other program has deleted mail from the folder */
339 tmp->size = (long) sb.st_size;
343 else if (STAT_CHECK || tmp->msgcount == 0) {
344 /* sidebar visible */
347 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
349 tmp->msgcount = ctx->msgcount;
351 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
352 tmp->msg_flagged = ctx->flagged;
353 mx_close_mailbox (ctx, 0);
356 if (tmp->newly_created &&
357 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
358 tmp->newly_created = 0;
360 else if (tmp->new > 0)
365 /* only check on force or $mail_check reached */
366 if (force == 1 || (now - last1 >= BuffyTimeout)) {
367 snprintf (path, sizeof (path), "%s/new", tmp->path);
368 if ((dirp = opendir (path)) == NULL) {
375 while ((de = readdir (dirp)) != NULL) {
378 if (*de->d_name != '.' &&
379 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
380 /* one new and undeleted message is enough */
384 /* if sidebar invisible -> done */
397 /* only count total mail if sidebar visible */
398 snprintf (path, sizeof (path), "%s/cur", tmp->path);
399 if ((dirp = opendir (path)) == NULL) {
403 tmp->msg_flagged = 0;
404 while ((de = readdir (dirp)) != NULL) {
407 if (*de->d_name != '.'
408 && (p = strstr (de->d_name, ":2,")) != NULL) {
409 if (!strchr (p + 3, 'T'))
411 if (strchr (p + 3, 'F'))
418 else if (tmp->new > 0)
419 /* keep current stats if !force and !$mail_check reached */
424 /* only check on force or $mail_check reached */
425 if (force == 1 || (now - last1 >= BuffyTimeout)) {
426 if ((tmp->new = mh_buffy (tmp->path)) > 0)
431 if ((dp = opendir (path)) == NULL)
436 while ((de = readdir (dp))) {
437 if (mh_valid_message (de->d_name)) {
446 else if (tmp->new > 0)
447 /* keep current stats if !force and !$mail_check reached */
452 /* only check on force or $imap_mail_check reached */
453 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
454 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
455 tmp->new = imap_mailbox_check (tmp->path, 1);
456 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
461 if (tmp->msg_unread < 0)
464 else if (tmp->new > 0)
465 /* keep current stats if !force and !$imap_mail_check reached */
472 else if (Context && Context->path)
473 tmp->size = (long) sb.st_size; /* update the size */
478 else if (!tmp->notified)
480 tmp->has_new = tmp->new > 0;
482 if (BuffyCount > 0 && force != 2)
483 sidebar_draw (CurrentMenu);
487 int buffy_list (void)
490 char path[_POSIX_PATH_MAX];
494 int have_unnotified = BuffyNotify;
500 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
502 for (i = 0; i < Incoming->length; i++) {
503 tmp = (BUFFY*) Incoming->data[i];
504 /* Is there new mail in this mailbox? */
505 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
508 m_strcpy(path, sizeof(path), tmp->path);
509 mutt_pretty_mailbox (path);
511 if (!first && pos + m_strlen(path) >= COLS - 7)
515 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
517 /* Prepend an asterisk to mailboxes not already notified */
518 if (!tmp->notified) {
519 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
523 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
527 if (!first && i < Incoming->length) {
528 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
531 /* on new mail: redraw sidebar */
532 sidebar_draw (CurrentMenu);
533 mutt_message ("%s", buffylist);
536 /* there were no mailboxes needing to be notified, so clean up since
537 * BuffyNotify has somehow gotten out of sync
543 int buffy_notify (void)
545 if (buffy_check (0) && BuffyNotify) {
546 return (buffy_list ());
552 * mutt_buffy() -- incoming folders completion routine
554 * given a folder name, this routine gives the next incoming folder with new
557 void buffy_next (char *s, size_t slen)
562 if (list_empty(Incoming))
565 mutt_expand_path (s, _POSIX_PATH_MAX);
566 if (buffy_check (0) == 0) {
572 * If buffy_lookup returns the index,
573 * or -1 if not found (-1..Incoming->length-1);
574 * plus one --> (0..Incoming->length).
575 * Modulo mapps it into the correct range.
577 i = 1 + buffy_lookup (s);
578 for (l=0; l < Incoming->length; l++) {
579 c = (l+i) % Incoming->length;
580 if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) &&
581 ((BUFFY*) Incoming->data[c])->new > 0)
584 if (l >= Incoming->length) {
586 /* something went wrong since we're here when buffy_check
587 * reported new mail */
590 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
591 mutt_pretty_mailbox (s);