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/lib-lib.h>
27 #include <lib-ui/curses.h>
28 #include <lib-ui/sidebar.h>
30 #include <imap/imap.h>
37 static time_t BuffyTime = 0; /* last time we started checking for mail */
39 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
40 static short BuffyCount = 0; /* how many boxes with new mail */
41 static short BuffyNotify = 0; /* # of unnotified new boxes */
45 /* Find the last message in the file.
46 * upon success return 0. If no message found - return -1 */
48 static int fseeko_last_message (FILE * f)
51 char buffer[BUFSIZ + 9]; /* 7 for "\n\nFrom " */
53 int i; /* Index into `buffer' for scanning. */
56 fseeko (f, 0, SEEK_END);
59 /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
60 * `bytes_read' <= `BUFSIZ'. */
61 bytes_read = pos % BUFSIZ;
64 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
65 * reads will be on block boundaries, which might increase efficiency. */
66 while ((pos -= bytes_read) >= 0) {
67 /* we save in the buffer at the end the first 7 chars from the last read */
68 strncpy (buffer + BUFSIZ, buffer, 5 + 2); /* 2 == 2 * m_strlen(CRLF) */
69 fseeko (f, pos, SEEK_SET);
70 bytes_read = fread (buffer, sizeof (char), bytes_read, f);
73 for (i = bytes_read; --i >= 0;)
74 if (!m_strncmp(buffer + i, "\n\nFrom ", m_strlen("\n\nFrom "))) { /* found it - go to the beginning of the From */
75 fseeko (f, pos + i + 2, SEEK_SET);
81 /* here we are at the beginning of the file */
82 if (!m_strncmp("From ", buffer, 5)) {
90 /* Return 1 if the last message is new */
91 static int test_last_status_new (FILE * f)
94 ENVELOPE *tmp_envelope;
97 if (fseeko_last_message (f) == -1)
101 tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
102 if (!(hdr->read || hdr->old))
105 envelope_delete(&tmp_envelope);
111 static int test_new_folder (const char *path)
117 typ = mx_get_magic (path);
119 if (typ != M_MBOX && typ != M_MMDF)
122 if ((f = fopen (path, "rb"))) {
123 rc = test_last_status_new (f);
130 BUFFY *buffy_find_mailbox (const char *path)
136 if (stat (path, &sb) != 0)
139 if (!list_empty(Incoming)) {
140 for (i = 0; i < Incoming->length; i++) {
141 if (stat (Incoming->data[i], &tmp_sb) == 0 &&
142 sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
143 return ((BUFFY*) Incoming->data[i]);
149 void buffy_update_mailbox (BUFFY * b)
156 if (stat (b->path, &sb) == 0)
157 b->size = (long) sb.st_size;
164 /* func to free buffy for list_del() */
165 static void buffy_free (BUFFY** p) {
166 p_delete(&(*p)->path);
170 int buffy_lookup (const char* path) {
172 if (list_empty(Incoming) || !path || !*path)
174 for (i = 0; i < Incoming->length; i++) {
175 if (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) )
181 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
182 BUFFER * err __attribute__ ((unused)))
185 char buf[_POSIX_PATH_MAX];
189 #endif /* BUFFY_SIZE */
191 while (MoreArgs (s)) {
192 mutt_extract_token (path, s, 0);
193 m_strcpy(buf, sizeof(buf), path->data);
195 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
196 list_del (&Incoming, (list_del_t*) buffy_free);
200 /* Skip empty tokens. */
204 mutt_expand_path (buf, sizeof (buf));
205 i = buffy_lookup (buf);
207 if (data == M_UNMAILBOXES) {
209 tmp = (BUFFY*) list_pop_idx (Incoming, i);
216 tmp = p_new(BUFFY, 1);
217 tmp->path = m_strdup(buf);
219 list_push_back (&Incoming, tmp);
220 i = Incoming->length-1;
222 tmp = (BUFFY*) Incoming->data[i];
226 tmp->newly_created = 0;
229 /* for buffy_size, it is important that if the folder is new (tested by
230 * reading it), the size is set to 0 so that later when we check we see
231 * that it increased . without buffy_size we probably don't care.
233 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
234 /* some systems out there don't have an off_t type */
235 tmp->size = (long) sb.st_size;
239 #endif /* BUFFY_SIZE */
245 /* people use buffy_size on systems where modified time attributes are BADLY
246 * broken. Ignore them.
248 #define STAT_CHECK (sb.st_size > tmp->size)
250 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
251 #endif /* BUFFY_SIZE */
254 * 0 don't force any checks + update sidebar
255 * 1 force all checks + update sidebar
256 * 2 don't force any checks + _don't_ update sidebar
258 int buffy_check (int force)
264 char path[_POSIX_PATH_MAX];
265 struct stat contex_sb;
269 int local = 0, count = 0;
272 /* update postponed count as well, on force */
274 mutt_update_num_postponed ();
276 /* fastest return if there are no mailboxes */
277 if (list_empty(Incoming))
280 if (force == 0 && (now - BuffyTime < BuffyTimeout)
281 && (now - ImapBuffyTime < ImapBuffyTimeout))
285 if (force == 1 || now - BuffyTime >= BuffyTimeout)
287 last2 = ImapBuffyTime;
288 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
293 count = sidebar_need_count ();
295 if (!Context || !Context->path ||
296 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
297 /* check device ID and serial number instead of comparing paths */
298 contex_sb.st_dev = 0;
299 contex_sb.st_ino = 0;
302 for (i = 0; i < Incoming->length; i++) {
303 tmp = (BUFFY*) Incoming->data[i];
304 tmp->magic = mx_get_magic (tmp->path);
305 local = mx_is_local (tmp->magic-1);
306 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
307 /* if the mailbox still doesn't exist, set the newly created flag to
308 * be ready for when it does. */
309 tmp->newly_created = 1;
317 /* check to see if the folder is the currently selected folder
319 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
320 sb.st_ino != contex_sb.st_ino) :
321 m_strcmp(tmp->path, Context->path))) {
322 switch (tmp->magic) {
325 /* only check on force or $mail_check reached */
326 if (force == 1 || (now - last1 >= BuffyTimeout)) {
334 /* some other program has deleted mail from the folder */
335 tmp->size = (long) sb.st_size;
339 else if (STAT_CHECK || tmp->msgcount == 0) {
340 /* sidebar visible */
343 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
345 tmp->msgcount = ctx->msgcount;
347 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
348 tmp->msg_flagged = ctx->flagged;
349 mx_close_mailbox (ctx, 0);
352 if (tmp->newly_created &&
353 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
354 tmp->newly_created = 0;
356 else if (tmp->new > 0)
361 /* only check on force or $mail_check reached */
362 if (force == 1 || (now - last1 >= BuffyTimeout)) {
363 snprintf (path, sizeof (path), "%s/new", tmp->path);
364 if ((dirp = opendir (path)) == NULL) {
371 while ((de = readdir (dirp)) != NULL) {
374 if (*de->d_name != '.' &&
375 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
376 /* one new and undeleted message is enough */
380 /* if sidebar invisible -> done */
393 /* only count total mail if sidebar visible */
394 snprintf (path, sizeof (path), "%s/cur", tmp->path);
395 if ((dirp = opendir (path)) == NULL) {
399 tmp->msg_flagged = 0;
400 while ((de = readdir (dirp)) != NULL) {
403 if (*de->d_name != '.'
404 && (p = strstr (de->d_name, ":2,")) != NULL) {
405 if (!strchr (p + 3, 'T'))
407 if (strchr (p + 3, 'F'))
414 else if (tmp->new > 0)
415 /* keep current stats if !force and !$mail_check reached */
420 /* only check on force or $mail_check reached */
421 if (force == 1 || (now - last1 >= BuffyTimeout)) {
422 if ((tmp->new = mh_buffy (tmp->path)) > 0)
427 if ((dp = opendir (path)) == NULL)
432 while ((de = readdir (dp))) {
433 if (mh_valid_message (de->d_name)) {
442 else if (tmp->new > 0)
443 /* keep current stats if !force and !$mail_check reached */
448 /* only check on force or $imap_mail_check reached */
449 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
450 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
451 tmp->new = imap_mailbox_check (tmp->path, 1);
452 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
457 if (tmp->msg_unread < 0)
460 else if (tmp->new > 0)
461 /* keep current stats if !force and !$imap_mail_check reached */
468 else if (Context && Context->path)
469 tmp->size = (long) sb.st_size; /* update the size */
474 else if (!tmp->notified)
476 tmp->has_new = tmp->new > 0;
478 if (BuffyCount > 0 && force != 2)
479 sidebar_draw (CurrentMenu);
483 int buffy_list (void)
486 char path[_POSIX_PATH_MAX];
490 int have_unnotified = BuffyNotify;
496 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
498 for (i = 0; i < Incoming->length; i++) {
499 tmp = (BUFFY*) Incoming->data[i];
500 /* Is there new mail in this mailbox? */
501 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
504 m_strcpy(path, sizeof(path), tmp->path);
505 mutt_pretty_mailbox (path);
507 if (!first && pos + m_strlen(path) >= COLS - 7)
511 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
513 /* Prepend an asterisk to mailboxes not already notified */
514 if (!tmp->notified) {
515 /* pos += m_strlen(strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); __STRNCAT_CHECKED__ */
519 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
523 if (!first && i < Incoming->length) {
524 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
527 /* on new mail: redraw sidebar */
528 sidebar_draw (CurrentMenu);
529 mutt_message ("%s", buffylist);
532 /* there were no mailboxes needing to be notified, so clean up since
533 * BuffyNotify has somehow gotten out of sync
539 int buffy_notify (void)
541 if (buffy_check (0) && BuffyNotify) {
542 return (buffy_list ());
548 * mutt_buffy() -- incoming folders completion routine
550 * given a folder name, this routine gives the next incoming folder with new
553 void buffy_next (char *s, size_t slen)
558 if (list_empty(Incoming))
561 mutt_expand_path (s, _POSIX_PATH_MAX);
562 if (buffy_check (0) == 0) {
568 * If buffy_lookup returns the index,
569 * or -1 if not found (-1..Incoming->length-1);
570 * plus one --> (0..Incoming->length).
571 * Modulo mapps it into the correct range.
573 i = 1 + buffy_lookup (s);
574 for (l=0; l < Incoming->length; l++) {
575 c = (l+i) % Incoming->length;
576 if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) &&
577 ((BUFFY*) Incoming->data[c])->new > 0)
580 if (l >= Incoming->length) {
582 /* something went wrong since we're here when buffy_check
583 * reported new mail */
586 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
587 mutt_pretty_mailbox (s);