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 static inline void buffy_delete(BUFFY** p)
35 p_delete(&(*p)->path);
39 DO_ARRAY_FUNCS(BUFFY, buffy, buffy_delete);
41 /* Return the index number of path in Incoming list */
42 int buffy_lookup (const char* path) {
45 if (!Incoming.len || !path || !*path)
48 for (i = 0; i < Incoming.len; i++) {
49 if (!m_strcmp(Incoming.arr[i]->path, path))
56 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
57 BUFFER * err __attribute__ ((unused)))
60 char buf[_POSIX_PATH_MAX];
63 while (MoreArgs (s)) {
64 mutt_extract_token (path, s, 0);
65 m_strcpy(buf, sizeof(buf), path->data);
67 if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
68 buffy_array_wipe(&Incoming);
72 /* Skip empty tokens. */
76 mutt_expand_path (buf, sizeof (buf));
77 i = buffy_lookup (buf);
79 if (data == M_UNMAILBOXES) {
80 tmp = buffy_array_take(&Incoming, i);
86 tmp = p_new(BUFFY, 1);
87 tmp->path = m_strdup(buf);
89 buffy_array_append(&Incoming, tmp);
92 tmp = Incoming.arr[i];
97 tmp->newly_created = 0;
103 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
106 * 0 don't force any checks + update sidebar
107 * 1 force all checks + update sidebar
108 * 2 don't force any checks + _don't_ update sidebar
110 int buffy_check (int force)
116 char path[_POSIX_PATH_MAX];
117 struct stat contex_sb;
121 int local = 0, count = 0;
124 /* update postponed count as well, on force */
126 mutt_update_num_postponed ();
128 /* fastest return if there are no mailboxes */
132 if (force == 0 && (now - BuffyTime < BuffyTimeout)
133 && (now - ImapBuffyTime < ImapBuffyTimeout))
137 if (force == 1 || now - BuffyTime >= BuffyTimeout)
139 last2 = ImapBuffyTime;
140 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
145 count = sidebar_need_count ();
147 if (!Context || !Context->path ||
148 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
149 /* check device ID and serial number instead of comparing paths */
150 contex_sb.st_dev = 0;
151 contex_sb.st_ino = 0;
154 for (i = 0; i < Incoming.len; i++) {
155 tmp = Incoming.arr[i];
156 tmp->magic = mx_get_magic (tmp->path);
157 local = mx_is_local (tmp->magic-1);
158 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
159 /* if the mailbox still doesn't exist, set the newly created flag to
160 * be ready for when it does. */
161 tmp->newly_created = 1;
166 /* check to see if the folder is the currently selected folder
168 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
169 sb.st_ino != contex_sb.st_ino) :
170 m_strcmp(tmp->path, Context->path))) {
171 switch (tmp->magic) {
174 /* only check on force or $mail_check reached */
175 if (force == 1 || (now - last1 >= BuffyTimeout)) {
182 else if (STAT_CHECK || tmp->msgcount == 0) {
183 /* sidebar visible */
186 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
188 tmp->msgcount = ctx->msgcount;
190 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
191 tmp->msg_flagged = ctx->flagged;
192 mx_close_mailbox (ctx, 0);
195 if (tmp->newly_created &&
196 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
197 tmp->newly_created = 0;
199 else if (tmp->new > 0)
204 /* only check on force or $mail_check reached */
205 if (force == 1 || (now - last1 >= BuffyTimeout)) {
206 snprintf (path, sizeof (path), "%s/new", tmp->path);
207 if ((dirp = opendir (path)) == NULL) {
214 while ((de = readdir (dirp)) != NULL) {
217 if (*de->d_name != '.' &&
218 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
219 /* one new and undeleted message is enough */
223 /* if sidebar invisible -> done */
236 /* only count total mail if sidebar visible */
237 snprintf (path, sizeof (path), "%s/cur", tmp->path);
238 if ((dirp = opendir (path)) == NULL) {
242 tmp->msg_flagged = 0;
243 while ((de = readdir (dirp)) != NULL) {
246 if (*de->d_name != '.'
247 && (p = strstr (de->d_name, ":2,")) != NULL) {
248 if (!strchr (p + 3, 'T'))
250 if (strchr (p + 3, 'F'))
257 else if (tmp->new > 0)
258 /* keep current stats if !force and !$mail_check reached */
263 /* only check on force or $mail_check reached */
264 if (force == 1 || (now - last1 >= BuffyTimeout)) {
265 if ((tmp->new = mh_buffy (tmp->path)) > 0)
270 if ((dp = opendir (path)) == NULL)
275 while ((de = readdir (dp))) {
276 if (mh_valid_message (de->d_name)) {
285 else if (tmp->new > 0)
286 /* keep current stats if !force and !$mail_check reached */
291 /* only check on force or $imap_mail_check reached */
292 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
293 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
294 tmp->new = imap_mailbox_check (tmp->path, 1);
295 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
300 if (tmp->msg_unread < 0)
303 else if (tmp->new > 0)
304 /* keep current stats if !force and !$imap_mail_check reached */
313 else if (!tmp->notified)
315 tmp->has_new = tmp->new > 0;
317 if (BuffyCount > 0 && force != 2)
322 int buffy_list (void)
325 char path[_POSIX_PATH_MAX];
329 int have_unnotified = BuffyNotify;
335 pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
336 for (i = 0; i < Incoming.len; i++) {
337 tmp = Incoming.arr[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.len) {
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.len-1);
402 * plus one --> (0..Incoming.len).
403 * Modulo mapps it into the correct range.
405 i = 1 + buffy_lookup (s);
406 for (l = 0; l < Incoming.len; l++) {
407 c = (l+i) % Incoming.len;
409 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
412 if (l >= Incoming.len) {
414 /* something went wrong since we're here when buffy_check
415 * reported new mail */
418 m_strcpy(s, slen, Incoming.arr[c]->path);
419 mutt_pretty_mailbox (s);