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.
25 #include <lib-lib/mem.h>
26 #include <lib-lib/buffer.h>
27 #include <lib-lib/macros.h>
29 #include <lib-ui/curses.h>
30 #include <lib-ui/sidebar.h>
32 #include <imap/imap.h>
39 static time_t BuffyTime = 0; /* last time we started checking for mail */
41 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
42 static short BuffyCount = 0; /* how many boxes with new mail */
43 static short BuffyNotify = 0; /* # of unnotified new boxes */
47 /* Find the last message in the file.
48 * upon success return 0. If no message found - return -1 */
50 static int fseeko_last_message (FILE * f)
53 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
55 int i; /* Index into `buffer' for scanning. */
58 fseeko (f, 0, SEEK_END);
61 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
62 * `bytes_read' <= `BUFSIZ'. */
63 bytes_read = pos % BUFSIZ;
66 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
67 * reads will be on block boundaries, which might increase efficiency. */
68 while ((pos -= bytes_read) >= 0) {
69 /* we save in the buffer at the end the first 7 chars from the last read */
70 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * m_strlen(CRLF) */
71 fseeko (f, pos, SEEK_SET);
72 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
75 for (i = bytes_read; --i >= 0;)
76 if (!m_strncmp(buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
77 fseeko (f, pos + i + 2, SEEK_SET);
83 /* here we are at the beginning of the file */
84 if (!m_strncmp("From ", buffer, 5)) {
92 /* Return 1 if the last message is new */
93 static int test_last_status_new (FILE * f)
96 ENVELOPE *tmp_envelope;
99 if (fseeko_last_message (f) == -1)
103 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
104 if (!(hdr->read || hdr->old))
107 envelope_delete(&tmp_envelope);
113 static int test_new_folder (const char *path)
119 typ = mx_get_magic (path);
121 if (typ != M_MBOX && typ != M_MMDF)
124 if ((f = fopen (path, "rb"))) {
125 rc = test_last_status_new (f);
132 BUFFY *buffy_find_mailbox (const char *path)
138 if (stat (path, &sb) != 0)
141 if (!list_empty(Incoming)) {
142 for (i = 0; i < Incoming->length; i++) {
143 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
144 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
145 return ((BUFFY*) Incoming->data[i]);
151 void buffy_update_mailbox (BUFFY * b)
158 if (stat (b->path, &sb) == 0)
159 b->size = (long) sb.st_size;
166 /* func to free buffy for list_del() */
167 static void buffy_free (BUFFY** p) {
168 p_delete(&(*p)->path);
172 int buffy_lookup (const char* path) {
174 if (list_empty(Incoming) || !path || !*path)
176 for (i = 0; i < Incoming->length; i++) {
177 if (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) )
183 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
184 BUFFER * err __attribute__ ((unused)))
187 char buf[_POSIX_PATH_MAX];
191 #endif /* BUFFY_SIZE */
193 while (MoreArgs (s)) {
194 mutt_extract_token (path, s, 0);
195 m_strcpy(buf, sizeof(buf), path->data);
197 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
198 list_del (&Incoming, (list_del_t*) buffy_free);
202 /* Skip empty tokens. */
206 mutt_expand_path (buf, sizeof (buf));
207 i = buffy_lookup (buf);
209 if (data == M_UNMAILBOXES) {
211 tmp = (BUFFY*) list_pop_idx (Incoming, i);
218 tmp = p_new(BUFFY, 1);
219 tmp->path = m_strdup(buf);
221 list_push_back (&Incoming, tmp);
222 i = Incoming->length-1;
224 tmp = (BUFFY*) Incoming->data[i];
228 tmp->newly_created = 0;
231 /* for buffy_size, it is important that if the folder is new (tested by
232 * reading it), the size is set to 0 so that later when we check we see
233 * that it increased . without buffy_size we probably don't care.
235 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
236 /* some systems out there don't have an off_t type */
237 tmp->size = (long) sb.st_size;
241 #endif /* BUFFY_SIZE */
247 /* people use buffy_size on systems where modified time attributes are BADLY
248 * broken. Ignore them.
250 #define STAT_CHECK (sb.st_size > tmp->size)
252 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
253 #endif /* BUFFY_SIZE */
256 * 0 don't force any checks + update sidebar
257 * 1 force all checks + update sidebar
258 * 2 don't force any checks + _don't_ update sidebar
260 int buffy_check (int force)
266 char path[_POSIX_PATH_MAX];
267 struct stat contex_sb;
271 int local = 0, count = 0;
274 /* update postponed count as well, on force */
276 mutt_update_num_postponed ();
278 /* fastest return if there are no mailboxes */
279 if (list_empty(Incoming))
282 if (force == 0 && (now - BuffyTime < BuffyTimeout)
283 && (now - ImapBuffyTime < ImapBuffyTimeout))
287 if (force == 1 || now - BuffyTime >= BuffyTimeout)
289 last2 = ImapBuffyTime;
290 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
295 count = sidebar_need_count ();
297 if (!Context || !Context->path ||
298 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
299 /* check device ID and serial number instead of comparing paths */
300 contex_sb.st_dev = 0;
301 contex_sb.st_ino = 0;
304 for (i = 0; i < Incoming->length; i++) {
305 tmp = (BUFFY*) Incoming->data[i];
306 tmp->magic = mx_get_magic (tmp->path);
307 local = mx_is_local (tmp->magic-1);
308 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
309 /* if the mailbox still doesn't exist, set the newly created flag to
310 * be ready for when it does. */
311 tmp->newly_created = 1;
319 /* check to see if the folder is the currently selected folder
321 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
322 sb.st_ino != contex_sb.st_ino) :
323 m_strcmp(tmp->path, Context->path))) {
324 switch (tmp->magic) {
327 /* only check on force or $mail_check reached */
328 if (force == 1 || (now - last1 >= BuffyTimeout)) {
336 /* some other program has deleted mail from the folder */
337 tmp->size = (long) sb.st_size;
341 else if (STAT_CHECK || tmp->msgcount == 0) {
342 /* sidebar visible */
345 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
347 tmp->msgcount = ctx->msgcount;
349 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
350 tmp->msg_flagged = ctx->flagged;
351 mx_close_mailbox (ctx, 0);
354 if (tmp->newly_created &&
355 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
356 tmp->newly_created = 0;
358 else if (tmp->new > 0)
363 /* only check on force or $mail_check reached */
364 if (force == 1 || (now - last1 >= BuffyTimeout)) {
365 snprintf (path, sizeof (path), "%s/new", tmp->path);
366 if ((dirp = opendir (path)) == NULL) {
373 while ((de = readdir (dirp)) != NULL) {
376 if (*de->d_name != '.' &&
377 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
378 /* one new and undeleted message is enough */
382 /* if sidebar invisible -> done */
395 /* only count total mail if sidebar visible */
396 snprintf (path, sizeof (path), "%s/cur", tmp->path);
397 if ((dirp = opendir (path)) == NULL) {
401 tmp->msg_flagged = 0;
402 while ((de = readdir (dirp)) != NULL) {
405 if (*de->d_name != '.'
406 && (p = strstr (de->d_name, ":2,")) != NULL) {
407 if (!strchr (p + 3, 'T'))
409 if (strchr (p + 3, 'F'))
416 else if (tmp->new > 0)
417 /* keep current stats if !force and !$mail_check reached */
422 /* only check on force or $mail_check reached */
423 if (force == 1 || (now - last1 >= BuffyTimeout)) {
424 if ((tmp->new = mh_buffy (tmp->path)) > 0)
429 if ((dp = opendir (path)) == NULL)
434 while ((de = readdir (dp))) {
435 if (mh_valid_message (de->d_name)) {
444 else if (tmp->new > 0)
445 /* keep current stats if !force and !$mail_check reached */
450 /* only check on force or $imap_mail_check reached */
451 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
452 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
453 tmp->new = imap_mailbox_check (tmp->path, 1);
454 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
459 if (tmp->msg_unread < 0)
462 else if (tmp->new > 0)
463 /* keep current stats if !force and !$imap_mail_check reached */
470 else if (Context && Context->path)
471 tmp->size = (long) sb.st_size; /* update the size */
476 else if (!tmp->notified)
478 tmp->has_new = tmp->new > 0;
480 if (BuffyCount > 0 && force != 2)
481 sidebar_draw (CurrentMenu);
485 int buffy_list (void)
488 char path[_POSIX_PATH_MAX];
492 int have_unnotified = BuffyNotify;
498 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
500 for (i = 0; i < Incoming->length; i++) {
501 tmp = (BUFFY*) Incoming->data[i];
502 /* Is there new mail in this mailbox? */
503 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
506 m_strcpy(path, sizeof(path), tmp->path);
507 mutt_pretty_mailbox (path);
509 if (!first && pos + m_strlen(path) >= COLS - 7)
513 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
515 /* Prepend an asterisk to mailboxes not already notified */
516 if (!tmp->notified) {
517 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
521 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
525 if (!first && i < Incoming->length) {
526 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
529 /* on new mail: redraw sidebar */
530 sidebar_draw (CurrentMenu);
531 mutt_message ("%s", buffylist);
534 /* there were no mailboxes needing to be notified, so clean up since
535 * BuffyNotify has somehow gotten out of sync
541 int buffy_notify (void)
543 if (buffy_check (0) && BuffyNotify) {
544 return (buffy_list ());
550 * mutt_buffy() -- incoming folders completion routine
552 * given a folder name, this routine gives the next incoming folder with new
555 void buffy_next (char *s, size_t slen)
560 if (list_empty(Incoming))
563 mutt_expand_path (s, _POSIX_PATH_MAX);
564 if (buffy_check (0) == 0) {
570 * If buffy_lookup returns the index,
571 * or -1 if not found (-1..Incoming->length-1);
572 * plus one --> (0..Incoming->length).
573 * Modulo mapps it into the correct range.
575 i = 1 + buffy_lookup (s);
576 for (l=0; l < Incoming->length; l++) {
577 c = (l+i) % Incoming->length;
578 if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) &&
579 ((BUFFY*) Incoming->data[c])->new > 0)
582 if (l >= Incoming->length) {
584 /* something went wrong since we're here when buffy_check
585 * reported new mail */
588 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
589 mutt_pretty_mailbox (s);