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 < mod_buffy.mail_check)
136 && (now - ImapBuffyTime < ImapBuffyTimeout))
140 if (force == 1 || now - BuffyTime >= mod_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) {
176 /* only check on force or $mail_check reached */
177 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
184 else if (STAT_CHECK || tmp->msgcount == 0) {
185 /* sidebar visible */
188 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
190 tmp->msgcount = ctx->msgcount;
192 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
193 tmp->msg_flagged = ctx->flagged;
194 mx_close_mailbox (ctx, 0);
197 if (tmp->newly_created &&
198 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
199 tmp->newly_created = 0;
201 else if (tmp->new > 0)
206 /* only check on force or $mail_check reached */
207 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
208 snprintf (path, sizeof (path), "%s/new", tmp->path);
209 if ((dirp = opendir (path)) == NULL) {
216 while ((de = readdir (dirp)) != NULL) {
219 if (*de->d_name != '.' &&
220 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
221 /* one new and undeleted message is enough */
225 /* if sidebar invisible -> done */
238 /* only count total mail if sidebar visible */
239 snprintf (path, sizeof (path), "%s/cur", tmp->path);
240 if ((dirp = opendir (path)) == NULL) {
244 tmp->msg_flagged = 0;
245 while ((de = readdir (dirp)) != NULL) {
248 if (*de->d_name != '.'
249 && (p = strstr (de->d_name, ":2,")) != NULL) {
250 if (!strchr (p + 3, 'T'))
252 if (strchr (p + 3, 'F'))
259 else if (tmp->new > 0)
260 /* keep current stats if !force and !$mail_check reached */
265 /* only check on force or $mail_check reached */
266 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
267 if ((tmp->new = mh_buffy (tmp->path)) > 0)
272 if ((dp = opendir (path)) == NULL)
277 while ((de = readdir (dp))) {
278 if (mh_valid_message (de->d_name)) {
287 else if (tmp->new > 0)
288 /* keep current stats if !force and !$mail_check reached */
293 /* only check on force or $imap_mail_check reached */
294 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
295 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
296 tmp->new = imap_mailbox_check (tmp->path, 1);
297 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
302 if (tmp->msg_unread < 0)
305 else if (tmp->new > 0)
306 /* keep current stats if !force and !$imap_mail_check reached */
315 else if (!tmp->notified)
317 tmp->has_new = tmp->new > 0;
319 if (BuffyCount > 0 && force != 2)
324 int buffy_list (void)
326 char buffylist[LONG_STRING];
327 int have_unnotified = BuffyNotify;
328 int pos = 0, first = 1, i;
330 pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
332 for (i = 0; i < Incoming.len; i++) {
333 char path[_POSIX_PATH_MAX];
334 BUFFY *tmp = Incoming.arr[i];
336 /* Is there new mail in this mailbox? */
337 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
340 m_strcpy(path, sizeof(path), tmp->path);
341 mutt_pretty_mailbox(path);
344 if (pos + m_strlen(path) >= COLS - 7)
346 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, ", ");
352 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
357 /* on new mail: redraw sidebar */
360 if (i < Incoming.len) {
361 mutt_message("%s, ...", buffylist);
363 mutt_message("%s", buffylist);
368 /* there were no mailboxes needing to be notified, so clean up since
369 BuffyNotify has somehow gotten out of sync */
374 int buffy_notify(void)
376 return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
380 * buffy_next() -- incoming folders completion routine
382 * given a folder name, this routine gives the next incoming folder with new
385 void buffy_next(char *s, size_t slen)
390 mutt_expand_path(s, _POSIX_PATH_MAX);
391 if (!buffy_check(0)) {
397 * buffy_lookup returns the index,
398 * or -1 if not found (-1..Incoming.len-1);
399 * plus one --> (0..Incoming.len).
400 * Modulo mapps it into the correct range.
402 i = 1 + buffy_lookup(s);
403 for (l = 0; l < Incoming.len; l++) {
404 c = (l+i) % Incoming.len;
406 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
410 if (l >= Incoming.len) {
412 /* something went wrong since we're here when buffy_check
413 * reported new mail */
416 m_strcpy(s, slen, Incoming.arr[c]->path);
417 mutt_pretty_mailbox(s);