NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
mutt_mailbox.h File Reference

Mailbox helper functions. More...

#include <stdbool.h>
#include "core/lib.h"
+ Include dependency graph for mutt_mailbox.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int mutt_mailbox_check (struct Mailbox *m_cur, CheckStatsFlags flags)
 Check all all Mailboxes for new mail. More...
 
void mutt_mailbox_cleanup (const char *path, struct stat *st)
 Restore the timestamp of a mailbox. More...
 
bool mutt_mailbox_list (void)
 List the mailboxes with new mail. More...
 
struct Mailboxmutt_mailbox_next (struct Mailbox *m_cur, struct Buffer *s)
 Incoming folders completion routine. More...
 
struct Mailboxmutt_mailbox_next_unread (struct Mailbox *m_cur, struct Buffer *s)
 Find next mailbox with unread mail. More...
 
bool mutt_mailbox_notify (struct Mailbox *m_cur)
 Notify the user if there's new mail. More...
 
void mutt_mailbox_set_notified (struct Mailbox *m)
 Note when the user was last notified of new mail. More...
 

Detailed Description

Mailbox helper functions.

  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file mutt_mailbox.h.

Function Documentation

◆ mutt_mailbox_check()

int mutt_mailbox_check ( struct Mailbox m_cur,
CheckStatsFlags  flags 
)

Check all all Mailboxes for new mail.

Parameters
m_curCurrent Mailbox
flagsFlags, e.g. MUTT_MAILBOX_CHECK_FORCE
Return values
numNumber of mailboxes with new mail

Check all all Mailboxes for new mail and total/new/flagged messages

Definition at line 158 of file mutt_mailbox.c.

159{
160 if (TAILQ_EMPTY(&NeoMutt->accounts)) // fast return if there are no mailboxes
161 return 0;
162
163#ifdef USE_IMAP
164 if (flags & MUTT_MAILBOX_CHECK_FORCE)
166#endif
167
168 const short c_mail_check = cs_subset_number(NeoMutt->sub, "mail_check");
169 const bool c_mail_check_stats = cs_subset_bool(NeoMutt->sub, "mail_check_stats");
170 const short c_mail_check_stats_interval = cs_subset_number(NeoMutt->sub, "mail_check_stats_interval");
171
172 time_t t = mutt_date_now();
173 if ((flags == MUTT_MAILBOX_CHECK_NO_FLAGS) && (t - MailboxTime < c_mail_check))
174 return MailboxCount;
175
176 if ((flags & MUTT_MAILBOX_CHECK_FORCE_STATS) ||
177 (c_mail_check_stats && ((t - MailboxStatsTime) >= c_mail_check_stats_interval)))
178 {
181 }
182
183 MailboxTime = t;
184 MailboxCount = 0;
185 MailboxNotify = 0;
186
187 /* check device ID and serial number instead of comparing paths */
188 struct stat st_cur = { 0 };
189 if (!m_cur || (m_cur->type == MUTT_IMAP) || (m_cur->type == MUTT_POP)
190#ifdef USE_NNTP
191 || (m_cur->type == MUTT_NNTP)
192#endif
193 || stat(mailbox_path(m_cur), &st_cur) != 0)
194 {
195 st_cur.st_dev = 0;
196 st_cur.st_ino = 0;
197 }
198
199 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
201 struct MailboxNode *np = NULL;
202 STAILQ_FOREACH(np, &ml, entries)
203 {
204 if (!np->mailbox->visible)
205 continue;
206
207 CheckStatsFlags m_flags = flags;
208 if (!np->mailbox->first_check_stats_done && c_mail_check_stats)
209 {
211 }
212 mailbox_check(m_cur, np->mailbox, &st_cur, m_flags);
213 if (np->mailbox->has_new)
214 MailboxCount++;
216 }
218
219 return MailboxCount;
220}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:209
@ MUTT_POP
'POP3' Mailbox type
Definition: mailbox.h:52
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
@ MUTT_MAILBOX_ANY
Match any Mailbox type.
Definition: mailbox.h:42
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
static time_t MailboxStatsTime
last time we check performed mail_check_stats
Definition: mutt_mailbox.c:43
static short MailboxCount
how many boxes with new mail
Definition: mutt_mailbox.c:44
static short MailboxNotify
Definition: mutt_mailbox.c:45
static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check, struct stat *st_cur, CheckStatsFlags flags)
Check a mailbox for new mail.
Definition: mutt_mailbox.c:81
static time_t MailboxTime
last time we started checking for mail
Definition: mutt_mailbox.c:42
#define MUTT_MAILBOX_CHECK_NO_FLAGS
No flags are set.
Definition: mxapi.h:74
#define MUTT_MAILBOX_CHECK_FORCE_STATS
Ignore MailboxType and calculate statistics.
Definition: mxapi.h:76
#define MUTT_MAILBOX_CHECK_FORCE
Ignore MailboxTime and check for new mail.
Definition: mxapi.h:75
uint8_t CheckStatsFlags
Flags for mutt_mailbox_check.
Definition: mxapi.h:73
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:141
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:164
void mutt_update_num_postponed(void)
Force the update of the number of postponed messages.
Definition: postpone.c:185
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define TAILQ_EMPTY(head)
Definition: queue.h:721
List of Mailboxes.
Definition: mailbox.h:152
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:153
bool has_new
Mailbox has new mail.
Definition: mailbox.h:85
bool first_check_stats_done
True when the check have been done at least one time.
Definition: mailbox.h:112
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
bool visible
True if a result of "mailboxes".
Definition: mailbox.h:130
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct AccountList accounts
List of all Accounts.
Definition: neomutt.h:40
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_cleanup()

void mutt_mailbox_cleanup ( const char *  path,
struct stat *  st 
)

Restore the timestamp of a mailbox.

Parameters
pathPath to the mailbox
stTimestamp info from stat()

Fix up the atime and mtime after mbox/mmdf mailbox was modified according to stat() info taken before a modification.

Definition at line 419 of file mutt_mailbox.c.

420{
421#ifdef HAVE_UTIMENSAT
422 struct timespec ts[2];
423#else
424 struct utimbuf ut;
425#endif
426
427 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
428 if (c_check_mbox_size)
429 {
430 struct Mailbox *m = mailbox_find(path);
431 if (m && !m->has_new)
433 }
434 else
435 {
436 /* fix up the times so mailbox won't get confused */
437 if (st->st_mtime > st->st_atime)
438 {
439#ifdef HAVE_UTIMENSAT
440 ts[0].tv_sec = 0;
441 ts[0].tv_nsec = UTIME_OMIT;
442 ts[1].tv_sec = 0;
443 ts[1].tv_nsec = UTIME_NOW;
444 utimensat(AT_FDCWD, buf, ts, 0);
445#else
446 ut.actime = st->st_atime;
447 ut.modtime = mutt_date_now();
448 utime(path, &ut);
449#endif
450 }
451 else
452 {
453#ifdef HAVE_UTIMENSAT
454 ts[0].tv_sec = 0;
455 ts[0].tv_nsec = UTIME_NOW;
456 ts[1].tv_sec = 0;
457 ts[1].tv_nsec = UTIME_NOW;
458 utimensat(AT_FDCWD, buf, ts, 0);
459#else
460 utime(path, NULL);
461#endif
462 }
463 }
464}
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:205
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:140
A mailbox.
Definition: mailbox.h:79
Time value with nanosecond precision.
Definition: file.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_list()

bool mutt_mailbox_list ( void  )

List the mailboxes with new mail.

Return values
trueThere is new mail

Definition at line 240 of file mutt_mailbox.c.

241{
242 char mailboxlist[512] = { 0 };
243 size_t pos = 0;
244 int first = 1;
245
246 int have_unnotified = MailboxNotify;
247
248 struct Buffer *path = buf_pool_get();
249
250 mailboxlist[0] = '\0';
251 pos += strlen(strncat(mailboxlist, _("New mail in "), sizeof(mailboxlist) - 1 - pos));
252 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
254 struct MailboxNode *np = NULL;
255 STAILQ_FOREACH(np, &ml, entries)
256 {
257 /* Is there new mail in this mailbox? */
258 if (!np->mailbox->has_new || (have_unnotified && np->mailbox->notified))
259 continue;
260
261 buf_strcpy(path, mailbox_path(np->mailbox));
262 buf_pretty_mailbox(path);
263
264 const size_t width = msgwin_get_width();
265 if (!first && (width >= 7) && ((pos + buf_len(path)) >= (width - 7)))
266 {
267 break;
268 }
269
270 if (!first)
271 pos += strlen(strncat(mailboxlist + pos, ", ", sizeof(mailboxlist) - 1 - pos));
272
273 /* Prepend an asterisk to mailboxes not already notified */
274 if (!np->mailbox->notified)
275 {
276 np->mailbox->notified = true;
278 }
279 pos += strlen(strncat(mailboxlist + pos, buf_string(path), sizeof(mailboxlist) - 1 - pos));
280 first = 0;
281 }
283
284 if (!first && np)
285 {
286 strncat(mailboxlist + pos, ", ...", sizeof(mailboxlist) - 1 - pos);
287 }
288
289 buf_pool_release(&path);
290
291 if (!first)
292 {
293 mutt_message("%s", mailboxlist);
294 return true;
295 }
296
297 /* there were no mailboxes needing to be notified, so clean up since
298 * MailboxNotify has somehow gotten out of sync */
299 MailboxNotify = 0;
300 return false;
301}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:460
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:401
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
#define mutt_message(...)
Definition: logging2.h:89
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:270
#define _(a)
Definition: message.h:28
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:560
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:34
bool notified
User has been notified.
Definition: mailbox.h:101
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_next()

struct Mailbox * mutt_mailbox_next ( struct Mailbox m_cur,
struct Buffer s 
)

Incoming folders completion routine.

Parameters
m_curCurrent Mailbox
sBuffer containing name of current mailbox
Return values
ptrMailbox

Given a folder name, find the next incoming folder with new mail. The Mailbox will be returned and a pretty version of the path put into s.

Definition at line 373 of file mutt_mailbox.c.

374{
376
378 {
379 struct Mailbox *m_res = find_next_mailbox(s, true);
380 if (m_res)
381 return m_res;
382
383 mutt_mailbox_check(m_cur, MUTT_MAILBOX_CHECK_FORCE); /* mailbox was wrong - resync things */
384 }
385
386 buf_reset(s); // no folders with new mail
387 return NULL;
388}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
int mutt_mailbox_check(struct Mailbox *m_cur, CheckStatsFlags flags)
Check all all Mailboxes for new mail.
Definition: mutt_mailbox.c:158
static struct Mailbox * find_next_mailbox(struct Buffer *s, bool find_new)
Find the next mailbox with new or unread mail.
Definition: mutt_mailbox.c:330
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:333
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_next_unread()

struct Mailbox * mutt_mailbox_next_unread ( struct Mailbox m_cur,
struct Buffer s 
)

Find next mailbox with unread mail.

Parameters
m_curCurrent Mailbox
sBuffer containing name of current mailbox
Return values
ptrMailbox

Given a folder name, find the next mailbox with unread mail. The Mailbox will be returned and a pretty version of the path put into s.

Definition at line 399 of file mutt_mailbox.c.

400{
402
403 struct Mailbox *m_res = find_next_mailbox(s, false);
404 if (m_res)
405 return m_res;
406
407 buf_reset(s); // no folders with new mail
408 return NULL;
409}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_notify()

bool mutt_mailbox_notify ( struct Mailbox m_cur)

Notify the user if there's new mail.

Parameters
m_curCurrent Mailbox
Return values
trueThere is new mail

Definition at line 227 of file mutt_mailbox.c.

228{
230 {
231 return mutt_mailbox_list();
232 }
233 return false;
234}
bool mutt_mailbox_list(void)
List the mailboxes with new mail.
Definition: mutt_mailbox.c:240
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_mailbox_set_notified()

void mutt_mailbox_set_notified ( struct Mailbox m)

Note when the user was last notified of new mail.

Parameters
mMailbox

Definition at line 307 of file mutt_mailbox.c.

308{
309 if (!m)
310 return;
311
312 m->notified = true;
313#ifdef HAVE_CLOCK_GETTIME
314 clock_gettime(CLOCK_REALTIME, &m->last_visited);
315#else
317 m->last_visited.tv_nsec = 0;
318#endif
319}
struct timespec last_visited
Time of last exit from this mailbox.
Definition: mailbox.h:105
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:52
time_t tv_sec
Number of seconds since the epoch.
Definition: file.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function: