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 inline void buffy_free (BUFFY** p) {
156 p_delete(&(*p)->path);
160 /* Return the index number of path in Incoming list */
161 int buffy_lookup (const char* path) {
164 if (list_empty(Incoming) || !path || !*path)
167 for (i = 0; i < Incoming->length; i++) {
168 if (!m_strcmp(((BUFFY *)Incoming->data[i])->path, path))
175 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
176 BUFFER * err __attribute__ ((unused)))
179 char buf[_POSIX_PATH_MAX];
183 #endif /* BUFFY_SIZE */
185 while (MoreArgs (s)) {
186 mutt_extract_token (path, s, 0);
187 m_strcpy(buf, sizeof(buf), path->data);
189 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
190 list_del (&Incoming, (list_del_t*) buffy_free);
194 /* Skip empty tokens. */
198 mutt_expand_path (buf, sizeof (buf));
199 i = buffy_lookup (buf);
201 if (data == M_UNMAILBOXES) {
203 tmp = (BUFFY*) list_pop_idx (Incoming, i);
210 tmp = p_new(BUFFY, 1);
211 tmp->path = m_strdup(buf);
213 list_push_back (&Incoming, tmp);
214 i = Incoming->length-1;
216 tmp = (BUFFY*) Incoming->data[i];
220 tmp->newly_created = 0;
223 /* for buffy_size, it is important that if the folder is new (tested by
224 * reading it), the size is set to 0 so that later when we check we see
225 * that it increased . without buffy_size we probably don't care.
227 if (stat (tmp->path, &sb) == 0 && !test_new_folder (tmp->path)) {
228 /* some systems out there don't have an off_t type */
229 tmp->size = (long) sb.st_size;
233 #endif /* BUFFY_SIZE */
239 /* people use buffy_size on systems where modified time attributes are BADLY
240 * broken. Ignore them.
242 #define STAT_CHECK (sb.st_size > tmp->size)
244 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
245 #endif /* BUFFY_SIZE */
248 * 0 don't force any checks + update sidebar
249 * 1 force all checks + update sidebar
250 * 2 don't force any checks + _don't_ update sidebar
252 int buffy_check (int force)
258 char path[_POSIX_PATH_MAX];
259 struct stat contex_sb;
263 int local = 0, count = 0;
266 /* update postponed count as well, on force */
268 mutt_update_num_postponed ();
270 /* fastest return if there are no mailboxes */
271 if (list_empty(Incoming))
274 if (force == 0 && (now - BuffyTime < BuffyTimeout)
275 && (now - ImapBuffyTime < ImapBuffyTimeout))
279 if (force == 1 || now - BuffyTime >= BuffyTimeout)
281 last2 = ImapBuffyTime;
282 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
287 count = sidebar_need_count ();
289 if (!Context || !Context->path ||
290 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
291 /* check device ID and serial number instead of comparing paths */
292 contex_sb.st_dev = 0;
293 contex_sb.st_ino = 0;
296 for (i = 0; i < Incoming->length; i++) {
297 tmp = (BUFFY*) Incoming->data[i];
298 tmp->magic = mx_get_magic (tmp->path);
299 local = mx_is_local (tmp->magic-1);
300 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
301 /* if the mailbox still doesn't exist, set the newly created flag to
302 * be ready for when it does. */
303 tmp->newly_created = 1;
311 /* check to see if the folder is the currently selected folder
313 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
314 sb.st_ino != contex_sb.st_ino) :
315 m_strcmp(tmp->path, Context->path))) {
316 switch (tmp->magic) {
319 /* only check on force or $mail_check reached */
320 if (force == 1 || (now - last1 >= BuffyTimeout)) {
328 /* some other program has deleted mail from the folder */
329 tmp->size = (long) sb.st_size;
333 else if (STAT_CHECK || tmp->msgcount == 0) {
334 /* sidebar visible */
337 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
339 tmp->msgcount = ctx->msgcount;
341 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
342 tmp->msg_flagged = ctx->flagged;
343 mx_close_mailbox (ctx, 0);
346 if (tmp->newly_created &&
347 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
348 tmp->newly_created = 0;
350 else if (tmp->new > 0)
355 /* only check on force or $mail_check reached */
356 if (force == 1 || (now - last1 >= BuffyTimeout)) {
357 snprintf (path, sizeof (path), "%s/new", tmp->path);
358 if ((dirp = opendir (path)) == NULL) {
365 while ((de = readdir (dirp)) != NULL) {
368 if (*de->d_name != '.' &&
369 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
370 /* one new and undeleted message is enough */
374 /* if sidebar invisible -> done */
387 /* only count total mail if sidebar visible */
388 snprintf (path, sizeof (path), "%s/cur", tmp->path);
389 if ((dirp = opendir (path)) == NULL) {
393 tmp->msg_flagged = 0;
394 while ((de = readdir (dirp)) != NULL) {
397 if (*de->d_name != '.'
398 && (p = strstr (de->d_name, ":2,")) != NULL) {
399 if (!strchr (p + 3, 'T'))
401 if (strchr (p + 3, 'F'))
408 else if (tmp->new > 0)
409 /* keep current stats if !force and !$mail_check reached */
414 /* only check on force or $mail_check reached */
415 if (force == 1 || (now - last1 >= BuffyTimeout)) {
416 if ((tmp->new = mh_buffy (tmp->path)) > 0)
421 if ((dp = opendir (path)) == NULL)
426 while ((de = readdir (dp))) {
427 if (mh_valid_message (de->d_name)) {
436 else if (tmp->new > 0)
437 /* keep current stats if !force and !$mail_check reached */
442 /* only check on force or $imap_mail_check reached */
443 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
444 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
445 tmp->new = imap_mailbox_check (tmp->path, 1);
446 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
451 if (tmp->msg_unread < 0)
454 else if (tmp->new > 0)
455 /* keep current stats if !force and !$imap_mail_check reached */
462 else if (Context && Context->path)
463 tmp->size = (long) sb.st_size; /* update the size */
468 else if (!tmp->notified)
470 tmp->has_new = tmp->new > 0;
472 if (BuffyCount > 0 && force != 2)
477 int buffy_list (void)
480 char path[_POSIX_PATH_MAX];
484 int have_unnotified = BuffyNotify;
490 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
492 for (i = 0; i < Incoming->length; i++) {
493 tmp = Incoming->data[i];
494 /* Is there new mail in this mailbox? */
495 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
498 m_strcpy(path, sizeof(path), tmp->path);
499 mutt_pretty_mailbox (path);
501 if (!first && pos + m_strlen(path) >= COLS - 7)
505 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
510 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
514 if (!first && i < Incoming->length) {
515 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
518 /* on new mail: redraw sidebar */
520 mutt_message ("%s", buffylist);
523 /* there were no mailboxes needing to be notified, so clean up since
524 * BuffyNotify has somehow gotten out of sync
530 int buffy_notify (void)
532 if (buffy_check (0) && BuffyNotify) {
533 return (buffy_list ());
539 * buffy_next() -- incoming folders completion routine
541 * given a folder name, this routine gives the next incoming folder with new
544 void buffy_next (char *s, size_t slen)
549 mutt_expand_path (s, _POSIX_PATH_MAX);
550 if (!buffy_check (0)) {
556 * buffy_lookup returns the index,
557 * or -1 if not found (-1..Incoming->length-1);
558 * plus one --> (0..Incoming->length).
559 * Modulo mapps it into the correct range.
561 i = 1 + buffy_lookup (s);
562 for (l = 0; l < Incoming->length; l++) {
563 c = (l+i) % Incoming->length;
565 if (m_strcmp(((BUFFY*) Incoming->data[c])->path, s) &&
566 ((BUFFY*) Incoming->data[c])->new > 0)
569 if (l >= Incoming->length) {
571 /* something went wrong since we're here when buffy_check
572 * reported new mail */
575 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
576 mutt_pretty_mailbox (s);