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.
13 #include <lib-lib/lib-lib.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/sidebar.h>
20 #include <imap/imap.h>
27 static time_t BuffyTime = 0; /* last time we started checking for mail */
29 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
30 static short BuffyCount = 0; /* how many boxes with new mail */
31 static short BuffyNotify = 0; /* # of unnotified new boxes */
35 /* Find the last message in the file.
36 * upon success return 0. If no message found - return -1 */
38 static int fseeko_last_message (FILE * f)
41 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
43 int i; /* Index into `buffer' for scanning. */
46 fseeko (f, 0, SEEK_END);
49 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
50 * `bytes_read' <= `BUFSIZ'. */
51 bytes_read = pos % BUFSIZ;
54 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
55 * reads will be on block boundaries, which might increase efficiency. */
56 while ((pos -= bytes_read) >= 0) {
57 /* we save in the buffer at the end the first 7 chars from the last read */
58 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * m_strlen(CRLF) */
59 fseeko (f, pos, SEEK_SET);
60 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
63 for (i = bytes_read; --i >= 0;)
64 if (!m_strncmp(buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
65 fseeko (f, pos + i + 2, SEEK_SET);
71 /* here we are at the beginning of the file */
72 if (!m_strncmp("From ", buffer, 5)) {
80 /* Return 1 if the last message is new */
81 static int test_last_status_new (FILE * f)
84 ENVELOPE *tmp_envelope;
87 if (fseeko_last_message (f) == -1)
91 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
92 if (!(hdr->read || hdr->old))
95 envelope_delete(&tmp_envelope);
101 static int test_new_folder (const char *path)
107 typ = mx_get_magic (path);
109 if (typ != M_MBOX && typ != M_MMDF)
112 if ((f = fopen (path, "rb"))) {
113 rc = test_last_status_new (f);
120 BUFFY *buffy_find_mailbox (const char *path)
126 if (stat (path, &sb) != 0)
129 if (!list_empty(Incoming)) {
130 for (i = 0; i < Incoming->length; i++) {
131 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
132 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
133 return ((BUFFY*) Incoming->data[i]);
139 void buffy_update_mailbox (BUFFY * b)
146 if (stat (b->path, &sb) == 0)
147 b->size = (long) sb.st_size;
154 /* func to free buffy for list_del() */
155 static void buffy_free (BUFFY** p) {
156 p_delete(&(*p)->path);
160 int buffy_lookup (const char* path) {
162 if (list_empty(Incoming) || !path || !*path)
164 for (i = 0; i < Incoming->length; i++) {
165 if (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) )
171 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
172 BUFFER * err __attribute__ ((unused)))
175 char buf[_POSIX_PATH_MAX];
179 #endif /* BUFFY_SIZE */
181 while (MoreArgs (s)) {
182 mutt_extract_token (path, s, 0);
183 m_strcpy(buf, sizeof(buf), path->data);
185 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
186 list_del (&Incoming, (list_del_t*) buffy_free);
190 /* Skip empty tokens. */
194 mutt_expand_path (buf, sizeof (buf));
195 i = buffy_lookup (buf);
197 if (data == M_UNMAILBOXES) {
199 tmp = (BUFFY*) list_pop_idx (Incoming, i);
206 tmp = p_new(BUFFY, 1);
207 tmp->path = m_strdup(buf);
209 list_push_back (&Incoming, tmp);
210 i = Incoming->length-1;
212 tmp = (BUFFY*) Incoming->data[i];
216 tmp->newly_created = 0;
219 /* for buffy_size, it is important that if the folder is new (tested by
220 * reading it), the size is set to 0 so that later when we check we see
221 * that it increased . without buffy_size we probably don't care.
223 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
224 /* some systems out there don't have an off_t type */
225 tmp->size = (long) sb.st_size;
229 #endif /* BUFFY_SIZE */
235 /* people use buffy_size on systems where modified time attributes are BADLY
236 * broken. Ignore them.
238 #define STAT_CHECK (sb.st_size > tmp->size)
240 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
241 #endif /* BUFFY_SIZE */
244 * 0 don't force any checks + update sidebar
245 * 1 force all checks + update sidebar
246 * 2 don't force any checks + _don't_ update sidebar
248 int buffy_check (int force)
254 char path[_POSIX_PATH_MAX];
255 struct stat contex_sb;
259 int local = 0, count = 0;
262 /* update postponed count as well, on force */
264 mutt_update_num_postponed ();
266 /* fastest return if there are no mailboxes */
267 if (list_empty(Incoming))
270 if (force == 0 && (now - BuffyTime < BuffyTimeout)
271 && (now - ImapBuffyTime < ImapBuffyTimeout))
275 if (force == 1 || now - BuffyTime >= BuffyTimeout)
277 last2 = ImapBuffyTime;
278 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
283 count = sidebar_need_count ();
285 if (!Context || !Context->path ||
286 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
287 /* check device ID and serial number instead of comparing paths */
288 contex_sb.st_dev = 0;
289 contex_sb.st_ino = 0;
292 for (i = 0; i < Incoming->length; i++) {
293 tmp = (BUFFY*) Incoming->data[i];
294 tmp->magic = mx_get_magic (tmp->path);
295 local = mx_is_local (tmp->magic-1);
296 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
297 /* if the mailbox still doesn't exist, set the newly created flag to
298 * be ready for when it does. */
299 tmp->newly_created = 1;
307 /* check to see if the folder is the currently selected folder
309 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
310 sb.st_ino != contex_sb.st_ino) :
311 m_strcmp(tmp->path, Context->path))) {
312 switch (tmp->magic) {
315 /* only check on force or $mail_check reached */
316 if (force == 1 || (now - last1 >= BuffyTimeout)) {
324 /* some other program has deleted mail from the folder */
325 tmp->size = (long) sb.st_size;
329 else if (STAT_CHECK || tmp->msgcount == 0) {
330 /* sidebar visible */
333 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
335 tmp->msgcount = ctx->msgcount;
337 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
338 tmp->msg_flagged = ctx->flagged;
339 mx_close_mailbox (ctx, 0);
342 if (tmp->newly_created &&
343 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
344 tmp->newly_created = 0;
346 else if (tmp->new > 0)
351 /* only check on force or $mail_check reached */
352 if (force == 1 || (now - last1 >= BuffyTimeout)) {
353 snprintf (path, sizeof (path), "%s/new", tmp->path);
354 if ((dirp = opendir (path)) == NULL) {
361 while ((de = readdir (dirp)) != NULL) {
364 if (*de->d_name != '.' &&
365 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
366 /* one new and undeleted message is enough */
370 /* if sidebar invisible -> done */
383 /* only count total mail if sidebar visible */
384 snprintf (path, sizeof (path), "%s/cur", tmp->path);
385 if ((dirp = opendir (path)) == NULL) {
389 tmp->msg_flagged = 0;
390 while ((de = readdir (dirp)) != NULL) {
393 if (*de->d_name != '.'
394 && (p = strstr (de->d_name, ":2,")) != NULL) {
395 if (!strchr (p + 3, 'T'))
397 if (strchr (p + 3, 'F'))
404 else if (tmp->new > 0)
405 /* keep current stats if !force and !$mail_check reached */
410 /* only check on force or $mail_check reached */
411 if (force == 1 || (now - last1 >= BuffyTimeout)) {
412 if ((tmp->new = mh_buffy (tmp->path)) > 0)
417 if ((dp = opendir (path)) == NULL)
422 while ((de = readdir (dp))) {
423 if (mh_valid_message (de->d_name)) {
432 else if (tmp->new > 0)
433 /* keep current stats if !force and !$mail_check reached */
438 /* only check on force or $imap_mail_check reached */
439 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
440 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
441 tmp->new = imap_mailbox_check (tmp->path, 1);
442 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
447 if (tmp->msg_unread < 0)
450 else if (tmp->new > 0)
451 /* keep current stats if !force and !$imap_mail_check reached */
458 else if (Context && Context->path)
459 tmp->size = (long) sb.st_size; /* update the size */
464 else if (!tmp->notified)
466 tmp->has_new = tmp->new > 0;
468 if (BuffyCount > 0 && force != 2)
469 sidebar_draw (CurrentMenu);
473 int buffy_list (void)
476 char path[_POSIX_PATH_MAX];
480 int have_unnotified = BuffyNotify;
486 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
488 for (i = 0; i < Incoming->length; i++) {
489 tmp = (BUFFY*) Incoming->data[i];
490 /* Is there new mail in this mailbox? */
491 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
494 m_strcpy(path, sizeof(path), tmp->path);
495 mutt_pretty_mailbox (path);
497 if (!first && pos + m_strlen(path) >= COLS - 7)
501 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
503 /* Prepend an asterisk to mailboxes not already notified */
504 if (!tmp->notified) {
505 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
509 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
513 if (!first && i < Incoming->length) {
514 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
517 /* on new mail: redraw sidebar */
518 sidebar_draw (CurrentMenu);
519 mutt_message ("%s", buffylist);
522 /* there were no mailboxes needing to be notified, so clean up since
523 * BuffyNotify has somehow gotten out of sync
529 int buffy_notify (void)
531 if (buffy_check (0) && BuffyNotify) {
532 return (buffy_list ());
538 * mutt_buffy() -- incoming folders completion routine
540 * given a folder name, this routine gives the next incoming folder with new
543 void buffy_next (char *s, size_t slen)
548 if (list_empty(Incoming))
551 mutt_expand_path (s, _POSIX_PATH_MAX);
552 if (buffy_check (0) == 0) {
558 * If buffy_lookup returns the index,
559 * or -1 if not found (-1..Incoming->length-1);
560 * plus one --> (0..Incoming->length).
561 * Modulo mapps it into the correct range.
563 i = 1 + buffy_lookup (s);
564 for (l=0; l < Incoming->length; l++) {
565 c = (l+i) % Incoming->length;
566 if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) &&
567 ((BUFFY*) Incoming->data[c])->new > 0)
570 if (l >= Incoming->length) {
572 /* something went wrong since we're here when buffy_check
573 * reported new mail */
576 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
577 mutt_pretty_mailbox (s);