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>
19 #include <lib-mx/mx.h>
20 #include <lib-mx/mh.h>
22 #include <imap/imap.h>
27 static time_t BuffyTime = 0; /* last time we started checking for mail */
28 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
29 static short BuffyCount = 0; /* how many boxes with new mail */
30 static short BuffyNotify = 0; /* # of unnotified new boxes */
32 /* Return the index number of path in Incoming list */
33 int buffy_lookup(const char* path)
37 if (m_strisempty(path))
40 for (i = 0; i < Incoming.len; i++) {
41 if (!m_strcmp(Incoming.arr[i]->path, path))
48 int buffy_parse_mailboxes(BUFFER *path, BUFFER *s, unsigned long data,
49 BUFFER *err __attribute__ ((unused)))
52 char buf[_POSIX_PATH_MAX];
57 mutt_extract_token(path, s, 0);
58 m_strcpy(buf, sizeof(buf), path->data);
60 if (data == M_UNMAILBOXES && !m_strcmp(buf, "*")) {
61 buffy_array_wipe(&Incoming);
65 /* Skip empty tokens. */
69 mutt_expand_path(buf, sizeof (buf));
70 i = buffy_lookup(buf);
72 if (data == M_UNMAILBOXES) {
73 tmp = buffy_array_take(&Incoming, i);
79 tmp = p_new(BUFFY, 1);
80 tmp->path = m_strdup(buf);
81 buffy_array_append(&Incoming, tmp);
83 tmp = Incoming.arr[i];
88 tmp->newly_created = 0;
94 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
97 * 0 don't force any checks + update sidebar
98 * 1 force all checks + update sidebar
99 * 2 don't force any checks + _don't_ update sidebar
101 int buffy_check(int force)
106 char path[_POSIX_PATH_MAX];
107 struct stat contex_sb;
108 time_t now, last1, last2;
110 int i = 0, local = 0, count = 0;
112 /* update postponed count as well, on force */
114 mutt_update_num_postponed ();
116 /* fastest return if there are no mailboxes */
121 if (force == 0 && (now - BuffyTime < BuffyTimeout)
122 && (now - ImapBuffyTime < ImapBuffyTimeout))
126 if (force == 1 || now - BuffyTime >= BuffyTimeout)
128 last2 = ImapBuffyTime;
129 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
134 count = sidebar_need_count();
136 if (!Context || !Context->path ||
137 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
138 /* check device ID and serial number instead of comparing paths */
139 contex_sb.st_dev = 0;
140 contex_sb.st_ino = 0;
143 for (i = 0; i < Incoming.len; i++) {
144 BUFFY *tmp = Incoming.arr[i];
145 tmp->magic = mx_get_magic (tmp->path);
146 local = mx_is_local (tmp->magic-1);
147 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
148 /* if the mailbox still doesn't exist, set the newly created flag to
149 * be ready for when it does. */
150 tmp->newly_created = 1;
155 /* check to see if the folder is the currently selected folder
157 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
158 sb.st_ino != contex_sb.st_ino) :
159 m_strcmp(tmp->path, Context->path))) {
160 switch (tmp->magic) {
163 /* only check on force or $mail_check reached */
164 if (force == 1 || (now - last1 >= BuffyTimeout)) {
171 else if (STAT_CHECK || tmp->msgcount == 0) {
172 /* sidebar visible */
175 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
177 tmp->msgcount = ctx->msgcount;
179 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
180 tmp->msg_flagged = ctx->flagged;
181 mx_close_mailbox (ctx, 0);
184 if (tmp->newly_created &&
185 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
186 tmp->newly_created = 0;
188 else if (tmp->new > 0)
193 /* only check on force or $mail_check reached */
194 if (force == 1 || (now - last1 >= BuffyTimeout)) {
195 snprintf (path, sizeof (path), "%s/new", tmp->path);
196 if ((dirp = opendir (path)) == NULL) {
203 while ((de = readdir (dirp)) != NULL) {
206 if (*de->d_name != '.' &&
207 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
208 /* one new and undeleted message is enough */
212 /* if sidebar invisible -> done */
225 /* only count total mail if sidebar visible */
226 snprintf (path, sizeof (path), "%s/cur", tmp->path);
227 if ((dirp = opendir (path)) == NULL) {
231 tmp->msg_flagged = 0;
232 while ((de = readdir (dirp)) != NULL) {
235 if (*de->d_name != '.'
236 && (p = strstr (de->d_name, ":2,")) != NULL) {
237 if (!strchr (p + 3, 'T'))
239 if (strchr (p + 3, 'F'))
246 else if (tmp->new > 0)
247 /* keep current stats if !force and !$mail_check reached */
252 /* only check on force or $mail_check reached */
253 if (force == 1 || (now - last1 >= BuffyTimeout)) {
254 if ((tmp->new = mh_buffy (tmp->path)) > 0)
259 if ((dp = opendir (path)) == NULL)
264 while ((de = readdir (dp))) {
265 if (mh_valid_message (de->d_name)) {
274 else if (tmp->new > 0)
275 /* keep current stats if !force and !$mail_check reached */
280 /* only check on force or $imap_mail_check reached */
281 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
282 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
283 tmp->new = imap_mailbox_check (tmp->path, 1);
284 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
289 if (tmp->msg_unread < 0)
292 else if (tmp->new > 0)
293 /* keep current stats if !force and !$imap_mail_check reached */
302 else if (!tmp->notified)
304 tmp->has_new = tmp->new > 0;
306 if (BuffyCount > 0 && force != 2)
311 int buffy_list (void)
313 char buffylist[LONG_STRING];
314 int have_unnotified = BuffyNotify;
315 int pos = 0, first = 1, i;
317 pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
319 for (i = 0; i < Incoming.len; i++) {
320 char path[_POSIX_PATH_MAX];
321 BUFFY *tmp = Incoming.arr[i];
323 /* Is there new mail in this mailbox? */
324 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
327 m_strcpy(path, sizeof(path), tmp->path);
328 mutt_pretty_mailbox(path);
331 if (pos + m_strlen(path) >= COLS - 7)
333 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, ", ");
339 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
344 /* on new mail: redraw sidebar */
347 if (i < Incoming.len) {
348 mutt_message("%s, ...", buffylist);
350 mutt_message("%s", buffylist);
355 /* there were no mailboxes needing to be notified, so clean up since
356 BuffyNotify has somehow gotten out of sync */
361 int buffy_notify(void)
363 return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
367 * buffy_next() -- incoming folders completion routine
369 * given a folder name, this routine gives the next incoming folder with new
372 void buffy_next(char *s, size_t slen)
377 mutt_expand_path(s, _POSIX_PATH_MAX);
378 if (!buffy_check(0)) {
384 * buffy_lookup returns the index,
385 * or -1 if not found (-1..Incoming.len-1);
386 * plus one --> (0..Incoming.len).
387 * Modulo mapps it into the correct range.
389 i = 1 + buffy_lookup(s);
390 for (l = 0; l < Incoming.len; l++) {
391 c = (l+i) % Incoming.len;
393 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
397 if (l >= Incoming.len) {
399 /* something went wrong since we're here when buffy_check
400 * reported new mail */
403 m_strcpy(s, slen, Incoming.arr[c]->path);
404 mutt_pretty_mailbox(s);