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-lua/lib-lua.h>
18 #include <lib-ui/curses.h>
19 #include <lib-ui/sidebar.h>
20 #include <lib-mx/mx.h>
21 #include <lib-mx/mh.h>
23 #include <imap/imap.h>
27 @import "lib-lua/base.cpkg"
29 static time_t BuffyTime = 0; /* last time we started checking for mail */
30 static time_t ImapBuffyTime = 0; /* last time we started checking for mail */
31 static short BuffyCount = 0; /* how many boxes with new mail */
32 static short BuffyNotify = 0; /* # of unnotified new boxes */
38 ** This variable configures how often (in seconds) Madmutt should look for
41 ** \fBNote:\fP This does not apply to IMAP mailboxes, see $$imap_mail_check.
45 void mailboxes(const string_t s) {
46 buffy_do_mailboxes(s, 1);
50 void unmailboxes(const string_t s) {
51 buffy_do_mailboxes(s, 0);
56 void buffy_do_mailboxes(const char *s, int add)
58 char buf[_POSIX_PATH_MAX];
62 if (!m_strcmp(s, "*")) {
63 buffy_array_wipe(&Incoming);
70 _mutt_expand_path(buf, sizeof(buf), s, 0);
71 i = buffy_lookup(buf);
74 tmp = buffy_array_take(&Incoming, i);
80 tmp = p_new(BUFFY, 1);
81 tmp->path = m_strdup(buf);
82 buffy_array_append(&Incoming, tmp);
84 tmp = Incoming.arr[i];
89 tmp->newly_created = 0;
92 /* Return the index number of path in Incoming list */
93 int buffy_lookup(const char* path)
97 if (m_strisempty(path))
100 for (i = 0; i < Incoming.len; i++) {
101 if (!m_strcmp(Incoming.arr[i]->path, path))
108 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
111 * 0 don't force any checks + update sidebar
112 * 1 force all checks + update sidebar
113 * 2 don't force any checks + _don't_ update sidebar
115 int buffy_check(int force)
120 char path[_POSIX_PATH_MAX];
121 struct stat contex_sb;
122 time_t now, last1, last2;
124 int i = 0, local = 0, count = 0;
126 /* update postponed count as well, on force */
128 mutt_update_num_postponed ();
130 /* fastest return if there are no mailboxes */
135 if (force == 0 && (now - BuffyTime < Buffy.mail_check)
136 && (now - ImapBuffyTime < ImapBuffyTimeout))
140 if (force == 1 || now - BuffyTime >= Buffy.mail_check)
142 last2 = ImapBuffyTime;
143 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
148 count = sidebar_need_count();
150 if (!Context || !Context->path ||
151 (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
152 /* check device ID and serial number instead of comparing paths */
153 contex_sb.st_dev = 0;
154 contex_sb.st_ino = 0;
157 for (i = 0; i < Incoming.len; i++) {
158 BUFFY *tmp = Incoming.arr[i];
159 tmp->magic = mx_get_magic (tmp->path);
160 local = mx_is_local (tmp->magic-1);
161 if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
162 /* if the mailbox still doesn't exist, set the newly created flag to
163 * be ready for when it does. */
164 tmp->newly_created = 1;
169 /* check to see if the folder is the currently selected folder
171 if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
172 sb.st_ino != contex_sb.st_ino) :
173 m_strcmp(tmp->path, Context->path))) {
174 switch (tmp->magic) {
177 /* only check on force or $mail_check reached */
178 if (force == 1 || (now - last1 >= Buffy.mail_check)) {
185 else if (STAT_CHECK || tmp->msgcount == 0) {
186 /* sidebar visible */
189 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
191 tmp->msgcount = ctx->msgcount;
193 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
194 tmp->msg_flagged = ctx->flagged;
195 mx_close_mailbox (ctx, 0);
198 if (tmp->newly_created &&
199 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
200 tmp->newly_created = 0;
202 else if (tmp->new > 0)
207 /* only check on force or $mail_check reached */
208 if (force == 1 || (now - last1 >= Buffy.mail_check)) {
209 snprintf (path, sizeof (path), "%s/new", tmp->path);
210 if ((dirp = opendir (path)) == NULL) {
217 while ((de = readdir (dirp)) != NULL) {
220 if (*de->d_name != '.' &&
221 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
222 /* one new and undeleted message is enough */
226 /* if sidebar invisible -> done */
239 /* only count total mail if sidebar visible */
240 snprintf (path, sizeof (path), "%s/cur", tmp->path);
241 if ((dirp = opendir (path)) == NULL) {
245 tmp->msg_flagged = 0;
246 while ((de = readdir (dirp)) != NULL) {
249 if (*de->d_name != '.'
250 && (p = strstr (de->d_name, ":2,")) != NULL) {
251 if (!strchr (p + 3, 'T'))
253 if (strchr (p + 3, 'F'))
260 else if (tmp->new > 0)
261 /* keep current stats if !force and !$mail_check reached */
266 /* only check on force or $mail_check reached */
267 if (force == 1 || (now - last1 >= Buffy.mail_check)) {
268 if ((tmp->new = mh_buffy (tmp->path)) > 0)
273 if ((dp = opendir (path)) == NULL)
278 while ((de = readdir (dp))) {
279 if (mh_valid_message (de->d_name)) {
288 else if (tmp->new > 0)
289 /* keep current stats if !force and !$mail_check reached */
294 /* only check on force or $imap_mail_check reached */
295 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
296 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
297 tmp->new = imap_mailbox_check (tmp->path, 1);
298 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
303 if (tmp->msg_unread < 0)
306 else if (tmp->new > 0)
307 /* keep current stats if !force and !$imap_mail_check reached */
316 else if (!tmp->notified)
318 tmp->has_new = tmp->new > 0;
320 if (BuffyCount > 0 && force != 2)
325 int buffy_list (void)
327 char buffylist[LONG_STRING];
328 int have_unnotified = BuffyNotify;
329 int pos = 0, first = 1, i;
331 pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
333 for (i = 0; i < Incoming.len; i++) {
334 char path[_POSIX_PATH_MAX];
335 BUFFY *tmp = Incoming.arr[i];
337 /* Is there new mail in this mailbox? */
338 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
341 m_strcpy(path, sizeof(path), tmp->path);
342 mutt_pretty_mailbox(path);
345 if (pos + m_strlen(path) >= COLS - 7)
347 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, ", ");
353 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
358 /* on new mail: redraw sidebar */
361 if (i < Incoming.len) {
362 mutt_message("%s, ...", buffylist);
364 mutt_message("%s", buffylist);
369 /* there were no mailboxes needing to be notified, so clean up since
370 BuffyNotify has somehow gotten out of sync */
375 int buffy_notify(void)
377 return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
381 * buffy_next() -- incoming folders completion routine
383 * given a folder name, this routine gives the next incoming folder with new
386 void buffy_next(char *s, size_t slen)
391 mutt_expand_path(s, _POSIX_PATH_MAX);
392 if (!buffy_check(0)) {
398 * buffy_lookup returns the index,
399 * or -1 if not found (-1..Incoming.len-1);
400 * plus one --> (0..Incoming.len).
401 * Modulo mapps it into the correct range.
403 i = 1 + buffy_lookup(s);
404 for (l = 0; l < Incoming.len; l++) {
405 c = (l+i) % Incoming.len;
407 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
411 if (l >= Incoming.len) {
413 /* something went wrong since we're here when buffy_check
414 * reported new mail */
417 m_strcpy(s, slen, Incoming.arr[c]->path);
418 mutt_pretty_mailbox(s);