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 static inline void buffy_delete(BUFFY** p)
34 p_delete(&(*p)->path);
37 DO_ARRAY_FUNCS(BUFFY, buffy, buffy_delete);
39 /* Return the index number of path in Incoming list */
40 int buffy_lookup(const char* path)
47 for (i = 0; i < Incoming.len; i++) {
48 if (!m_strcmp(Incoming.arr[i]->path, path))
55 int buffy_parse_mailboxes(BUFFER *path, BUFFER *s, unsigned long data,
56 BUFFER *err __attribute__ ((unused)))
59 char buf[_POSIX_PATH_MAX];
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);
88 buffy_array_append(&Incoming, tmp);
90 tmp = Incoming.arr[i];
95 tmp->newly_created = 0;
101 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
104 * 0 don't force any checks + update sidebar
105 * 1 force all checks + update sidebar
106 * 2 don't force any checks + _don't_ update sidebar
108 int buffy_check(int force)
113 char path[_POSIX_PATH_MAX];
114 struct stat contex_sb;
115 time_t now, last1, last2;
117 int i = 0, local = 0, count = 0;
119 /* update postponed count as well, on force */
121 mutt_update_num_postponed ();
123 /* fastest return if there are no mailboxes */
128 if (force == 0 && (now - BuffyTime < BuffyTimeout)
129 && (now - ImapBuffyTime < ImapBuffyTimeout))
133 if (force == 1 || now - BuffyTime >= BuffyTimeout)
135 last2 = ImapBuffyTime;
136 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
141 count = sidebar_need_count();
143 if (!Context || !Context->path ||
144 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
145 /* check device ID and serial number instead of comparing paths */
146 contex_sb.st_dev = 0;
147 contex_sb.st_ino = 0;
150 for (i = 0; i < Incoming.len; i++) {
151 BUFFY *tmp = Incoming.arr[i];
152 tmp->magic = mx_get_magic (tmp->path);
153 local = mx_is_local (tmp->magic-1);
154 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
155 /* if the mailbox still doesn't exist, set the newly created flag to
156 * be ready for when it does. */
157 tmp->newly_created = 1;
162 /* check to see if the folder is the currently selected folder
164 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
165 sb.st_ino != contex_sb.st_ino) :
166 m_strcmp(tmp->path, Context->path))) {
167 switch (tmp->magic) {
170 /* only check on force or $mail_check reached */
171 if (force == 1 || (now - last1 >= BuffyTimeout)) {
178 else if (STAT_CHECK || tmp->msgcount == 0) {
179 /* sidebar visible */
182 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
184 tmp->msgcount = ctx->msgcount;
186 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
187 tmp->msg_flagged = ctx->flagged;
188 mx_close_mailbox (ctx, 0);
191 if (tmp->newly_created &&
192 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
193 tmp->newly_created = 0;
195 else if (tmp->new > 0)
200 /* only check on force or $mail_check reached */
201 if (force == 1 || (now - last1 >= BuffyTimeout)) {
202 snprintf (path, sizeof (path), "%s/new", tmp->path);
203 if ((dirp = opendir (path)) == NULL) {
210 while ((de = readdir (dirp)) != NULL) {
213 if (*de->d_name != '.' &&
214 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
215 /* one new and undeleted message is enough */
219 /* if sidebar invisible -> done */
232 /* only count total mail if sidebar visible */
233 snprintf (path, sizeof (path), "%s/cur", tmp->path);
234 if ((dirp = opendir (path)) == NULL) {
238 tmp->msg_flagged = 0;
239 while ((de = readdir (dirp)) != NULL) {
242 if (*de->d_name != '.'
243 && (p = strstr (de->d_name, ":2,")) != NULL) {
244 if (!strchr (p + 3, 'T'))
246 if (strchr (p + 3, 'F'))
253 else if (tmp->new > 0)
254 /* keep current stats if !force and !$mail_check reached */
259 /* only check on force or $mail_check reached */
260 if (force == 1 || (now - last1 >= BuffyTimeout)) {
261 if ((tmp->new = mh_buffy (tmp->path)) > 0)
266 if ((dp = opendir (path)) == NULL)
271 while ((de = readdir (dp))) {
272 if (mh_valid_message (de->d_name)) {
281 else if (tmp->new > 0)
282 /* keep current stats if !force and !$mail_check reached */
287 /* only check on force or $imap_mail_check reached */
288 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
289 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
290 tmp->new = imap_mailbox_check (tmp->path, 1);
291 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
296 if (tmp->msg_unread < 0)
299 else if (tmp->new > 0)
300 /* keep current stats if !force and !$imap_mail_check reached */
309 else if (!tmp->notified)
311 tmp->has_new = tmp->new > 0;
313 if (BuffyCount > 0 && force != 2)
318 int buffy_list (void)
320 char buffylist[LONG_STRING];
321 int have_unnotified = BuffyNotify;
322 int pos = 0, first = 1, i;
324 pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
326 for (i = 0; i < Incoming.len; i++) {
327 char path[_POSIX_PATH_MAX];
328 BUFFY *tmp = Incoming.arr[i];
330 /* Is there new mail in this mailbox? */
331 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
334 m_strcpy(path, sizeof(path), tmp->path);
335 mutt_pretty_mailbox(path);
338 if (pos + m_strlen(path) >= COLS - 7)
340 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, ", ");
346 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
351 /* on new mail: redraw sidebar */
354 if (i < Incoming.len) {
355 mutt_message("%s, ...", buffylist);
357 mutt_message("%s", buffylist);
362 /* there were no mailboxes needing to be notified, so clean up since
363 BuffyNotify has somehow gotten out of sync */
368 int buffy_notify(void)
370 return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
374 * buffy_next() -- incoming folders completion routine
376 * given a folder name, this routine gives the next incoming folder with new
379 void buffy_next(char *s, size_t slen)
384 mutt_expand_path(s, _POSIX_PATH_MAX);
385 if (!buffy_check(0)) {
391 * buffy_lookup returns the index,
392 * or -1 if not found (-1..Incoming.len-1);
393 * plus one --> (0..Incoming.len).
394 * Modulo mapps it into the correct range.
396 i = 1 + buffy_lookup(s);
397 for (l = 0; l < Incoming.len; l++) {
398 c = (l+i) % Incoming.len;
400 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
404 if (l >= Incoming.len) {
406 /* something went wrong since we're here when buffy_check
407 * reported new mail */
410 m_strcpy(s, slen, Incoming.arr[c]->path);
411 mutt_pretty_mailbox(s);