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/lib-ui.h>
19 #include <lib-mx/mx.h>
20 #include <lib-mx/mh.h>
22 #include <imap/imap.h>
26 @import "lib-lua/base.cpkg"
28 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 */
37 ** This variable configures how often (in seconds) Madmutt should look for
40 ** \fBNote:\fP This does not apply to IMAP mailboxes, see $$imap_mail_check.
44 void mailboxes(const string_t s) {
45 buffy_do_mailboxes(s, 1);
49 void unmailboxes(const string_t s) {
50 buffy_do_mailboxes(s, 0);
55 void buffy_do_mailboxes(const char *s, int add)
57 char buf[_POSIX_PATH_MAX];
61 if (!m_strcmp(s, "*")) {
62 buffy_array_wipe(&Incoming);
69 _mutt_expand_path(buf, sizeof(buf), s, 0);
70 i = buffy_lookup(buf);
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;
91 /* Return the index number of path in Incoming list */
92 int buffy_lookup(const char* path)
96 if (m_strisempty(path))
99 for (i = 0; i < Incoming.len; i++) {
100 if (!m_strcmp(Incoming.arr[i]->path, path))
107 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
110 * 0 don't force any checks + update sidebar
111 * 1 force all checks + update sidebar
112 * 2 don't force any checks + _don't_ update sidebar
114 int buffy_check(int force)
119 char path[_POSIX_PATH_MAX];
120 struct stat contex_sb;
121 time_t now, last1, last2;
123 int i = 0, local = 0;
125 /* update postponed count as well, on force */
127 mutt_update_num_postponed ();
129 /* fastest return if there are no mailboxes */
134 if (force == 0 && (now - BuffyTime < mod_buffy.mail_check)
135 && (now - ImapBuffyTime < ImapBuffyTimeout))
139 if (force == 1 || now - BuffyTime >= mod_buffy.mail_check)
141 last2 = ImapBuffyTime;
142 if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
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 BUFFY *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) {
173 /* only check on force or $mail_check reached */
174 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
181 else if (STAT_CHECK || tmp->msgcount == 0) {
182 /* sidebar visible */
185 mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
187 tmp->msgcount = ctx->msgcount;
189 tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
190 tmp->msg_flagged = ctx->flagged;
191 mx_close_mailbox (ctx, 0);
194 if (tmp->newly_created &&
195 (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
196 tmp->newly_created = 0;
198 else if (tmp->new > 0)
203 /* only check on force or $mail_check reached */
204 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
205 snprintf (path, sizeof (path), "%s/new", tmp->path);
206 if ((dirp = opendir (path)) == NULL) {
213 while ((de = readdir (dirp)) != NULL) {
216 if (*de->d_name != '.' &&
217 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
218 /* one new and undeleted message is enough */
222 /* if sidebar invisible -> done */
235 /* only count total mail if sidebar visible */
236 snprintf (path, sizeof (path), "%s/cur", tmp->path);
237 if ((dirp = opendir (path)) == NULL) {
241 tmp->msg_flagged = 0;
242 while ((de = readdir (dirp)) != NULL) {
245 if (*de->d_name != '.'
246 && (p = strstr (de->d_name, ":2,")) != NULL) {
247 if (!strchr (p + 3, 'T'))
249 if (strchr (p + 3, 'F'))
256 else if (tmp->new > 0)
257 /* keep current stats if !force and !$mail_check reached */
262 /* only check on force or $mail_check reached */
263 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
264 if ((tmp->new = mh_buffy (tmp->path)) > 0)
269 if ((dp = opendir (path)) == NULL)
274 while ((de = readdir (dp))) {
275 if (mh_valid_message (de->d_name)) {
284 else if (tmp->new > 0)
285 /* keep current stats if !force and !$mail_check reached */
290 /* only check on force or $imap_mail_check reached */
291 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
292 tmp->msgcount = imap_mailbox_check (tmp->path, 0);
293 tmp->new = imap_mailbox_check (tmp->path, 1);
294 tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
299 if (tmp->msg_unread < 0)
302 else if (tmp->new > 0)
303 /* keep current stats if !force and !$imap_mail_check reached */
312 else if (!tmp->notified)
314 tmp->has_new = tmp->new > 0;
316 if (BuffyCount > 0 && force != 2)
321 int buffy_list (void)
323 char buffylist[LONG_STRING];
324 int have_unnotified = BuffyNotify;
325 int pos = 0, first = 1, i;
327 pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
329 for (i = 0; i < Incoming.len; i++) {
330 char path[_POSIX_PATH_MAX];
331 BUFFY *tmp = Incoming.arr[i];
333 /* Is there new mail in this mailbox? */
334 if (tmp->new <= 0 || (have_unnotified && tmp->notified))
337 m_strcpy(path, sizeof(path), tmp->path);
338 mutt_pretty_mailbox(path);
341 if (pos + m_strlen(path) >= COLS - 7)
343 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, ", ");
349 pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
354 /* on new mail: redraw sidebar */
357 if (i < Incoming.len) {
358 mutt_message("%s, ...", buffylist);
360 mutt_message("%s", buffylist);
365 /* there were no mailboxes needing to be notified, so clean up since
366 BuffyNotify has somehow gotten out of sync */
371 int buffy_notify(void)
373 return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
377 * buffy_next() -- incoming folders completion routine
379 * given a folder name, this routine gives the next incoming folder with new
382 void buffy_next(char *s, size_t slen)
387 mutt_expand_path(s, _POSIX_PATH_MAX);
388 if (!buffy_check(0)) {
394 * buffy_lookup returns the index,
395 * or -1 if not found (-1..Incoming.len-1);
396 * plus one --> (0..Incoming.len).
397 * Modulo mapps it into the correct range.
399 i = 1 + buffy_lookup(s);
400 for (l = 0; l < Incoming.len; l++) {
401 c = (l+i) % Incoming.len;
403 if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
407 if (l >= Incoming.len) {
409 /* something went wrong since we're here when buffy_check
410 * reported new mail */
413 m_strcpy(s, slen, Incoming.arr[c]->path);
414 mutt_pretty_mailbox(s);