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 */
33 /* func to free buffy for list_del() */
34 static inline void buffy_free (BUFFY** p) {
35 p_delete(&(*p)->path);
39 /* Return the index number of path in Incoming list */
40 int buffy_lookup (const char* path) {
43 if (list_empty(Incoming) || !path || !*path)
46 for (i = 0; i < Incoming->length; i++) {
47 if (!m_strcmp(((BUFFY *)Incoming->data[i])->path, path))
54 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
55 BUFFER * err __attribute__ ((unused)))
58 char buf[_POSIX_PATH_MAX];
61 while (MoreArgs (s)) {
62 mutt_extract_token (path, s, 0);
63 m_strcpy(buf, sizeof(buf), path->data);
65 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
66 list_del (&Incoming, (list_del_t*) buffy_free);
70 /* Skip empty tokens. */
74 mutt_expand_path (buf, sizeof (buf));
75 i = buffy_lookup (buf);
77 if (data == M_UNMAILBOXES) {
79 tmp = (BUFFY*) list_pop_idx (Incoming, i);
86 tmp = p_new(BUFFY, 1);
87 tmp->path = m_strdup(buf);
89 list_push_back (&Incoming, tmp);
90 i = Incoming->length-1;
92 tmp = (BUFFY*) Incoming->data[i];
96 tmp->newly_created = 0;
102 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
105 * 0 don't force any checks + update sidebar
106 * 1 force all checks + update sidebar
107 * 2 don't force any checks + _don't_ update sidebar
109 int buffy_check (int force)
115 char path[_POSIX_PATH_MAX];
116 struct stat contex_sb;
120 int local = 0, count = 0;
123 /* update postponed count as well, on force */
125 mutt_update_num_postponed ();
127 /* fastest return if there are no mailboxes */
128 if (list_empty(Incoming))
131 if (force == 0 && (now - BuffyTime < BuffyTimeout)
132 && (now - ImapBuffyTime < ImapBuffyTimeout))
136 if (force == 1 || now - BuffyTime >= BuffyTimeout)
138 last2 = ImapBuffyTime;
139 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
144 count = sidebar_need_count ();
146 if (!Context || !Context->path ||
147 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
148 /* check device ID and serial number instead of comparing paths */
149 contex_sb.st_dev = 0;
150 contex_sb.st_ino = 0;
153 for (i = 0; i < Incoming->length; i++) {
154 tmp = (BUFFY*) Incoming->data[i];
155 tmp->magic = mx_get_magic (tmp->path);
156 local = mx_is_local (tmp->magic-1);
157 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
158 /* if the mailbox still doesn't exist, set the newly created flag to
159 * be ready for when it does. */
160 tmp->newly_created = 1;
165 /* check to see if the folder is the currently selected folder
167 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
168 sb.st_ino != contex_sb.st_ino) :
169 m_strcmp(tmp->path, Context->path))) {
170 switch (tmp->magic) {
173 /* only check on force or $mail_check reached */
174 if (force == 1 || (now - last1 >= BuffyTimeout)) {
181 else if (STAT_CHECK || tmp->msgcount == 0) {
182 /* sidebar visible */
185 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
187 tmp->msgcount = ctx->msgcount;
189 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
190 tmp->msg_flagged = ctx->flagged;
191 mx_close_mailbox (ctx, 0);
194 if (tmp->newly_created &&
195 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
196 tmp->newly_created = 0;
198 else if (tmp->new > 0)
203 /* only check on force or $mail_check reached */
204 if (force == 1 || (now - last1 >= BuffyTimeout)) {
205 snprintf (path, sizeof (path), "%s/new", tmp->path);
206 if ((dirp = opendir (path)) == NULL) {
213 while ((de = readdir (dirp)) != NULL) {
216 if (*de->d_name != '.' &&
217 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
218 /* one new and undeleted message is enough */
222 /* if sidebar invisible -> done */
235 /* only count total mail if sidebar visible */
236 snprintf (path, sizeof (path), "%s/cur", tmp->path);
237 if ((dirp = opendir (path)) == NULL) {
241 tmp->msg_flagged = 0;
242 while ((de = readdir (dirp)) != NULL) {
245 if (*de->d_name != '.'
246 && (p = strstr (de->d_name, ":2,")) != NULL) {
247 if (!strchr (p + 3, 'T'))
249 if (strchr (p + 3, 'F'))
256 else if (tmp->new > 0)
257 /* keep current stats if !force and !$mail_check reached */
262 /* only check on force or $mail_check reached */
263 if (force == 1 || (now - last1 >= BuffyTimeout)) {
264 if ((tmp->new = mh_buffy (tmp->path)) > 0)
269 if ((dp = opendir (path)) == NULL)
274 while ((de = readdir (dp))) {
275 if (mh_valid_message (de->d_name)) {
284 else if (tmp->new > 0)
285 /* keep current stats if !force and !$mail_check reached */
290 /* only check on force or $imap_mail_check reached */
291 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
292 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
293 tmp->new = imap_mailbox_check (tmp->path, 1);
294 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
299 if (tmp->msg_unread < 0)
302 else if (tmp->new > 0)
303 /* keep current stats if !force and !$imap_mail_check reached */
312 else if (!tmp->notified)
314 tmp->has_new = tmp->new > 0;
316 if (BuffyCount > 0 && force != 2)
321 int buffy_list (void)
324 char path[_POSIX_PATH_MAX];
328 int have_unnotified = BuffyNotify;
334 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
336 for (i = 0; i < Incoming->length; i++) {
337 tmp = Incoming->data[i];
338 /* Is there new mail in this mailbox? */
339 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
342 m_strcpy(path, sizeof(path), tmp->path);
343 mutt_pretty_mailbox (path);
345 if (!first && pos + m_strlen(path) >= COLS - 7)
349 pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
354 pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
358 if (!first && i < Incoming->length) {
359 strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
362 /* on new mail: redraw sidebar */
364 mutt_message ("%s", buffylist);
367 /* there were no mailboxes needing to be notified, so clean up since
368 * BuffyNotify has somehow gotten out of sync
374 int buffy_notify (void)
376 if (buffy_check (0) && BuffyNotify) {
377 return (buffy_list ());
383 * buffy_next() -- incoming folders completion routine
385 * given a folder name, this routine gives the next incoming folder with new
388 void buffy_next (char *s, size_t slen)
393 mutt_expand_path (s, _POSIX_PATH_MAX);
394 if (!buffy_check (0)) {
400 * buffy_lookup returns the index,
401 * or -1 if not found (-1..Incoming->length-1);
402 * plus one --> (0..Incoming->length).
403 * Modulo mapps it into the correct range.
405 i = 1 + buffy_lookup (s);
406 for (l = 0; l < Incoming->length; l++) {
407 c = (l+i) % Incoming->length;
409 if (m_strcmp(((BUFFY*) Incoming->data[c])->path, s) &&
410 ((BUFFY*) Incoming->data[c])->new > 0)
413 if (l >= Incoming->length) {
415 /* something went wrong since we're here when buffy_check
416 * reported new mail */
419 m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
420 mutt_pretty_mailbox (s);