NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
mutt_mailbox.h File Reference

Mailbox helper functions. More...

#include <stdbool.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.

Macros

#define MUTT_MAILBOX_CHECK_FORCE   (1 << 0)
 
#define MUTT_MAILBOX_CHECK_FORCE_STATS   (1 << 1)
 

Functions

int mutt_mailbox_check (struct Mailbox *m_cur, int force)
 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...
 
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.

Macro Definition Documentation

◆ MUTT_MAILBOX_CHECK_FORCE

#define MUTT_MAILBOX_CHECK_FORCE   (1 << 0)

Definition at line 32 of file mutt_mailbox.h.

◆ MUTT_MAILBOX_CHECK_FORCE_STATS

#define MUTT_MAILBOX_CHECK_FORCE_STATS   (1 << 1)

Definition at line 33 of file mutt_mailbox.h.

Function Documentation

◆ mutt_mailbox_check()

int mutt_mailbox_check ( struct Mailbox m_cur,
int  force 
)

Check all all Mailboxes for new mail.

Parameters
m_curCurrent Mailbox
forceForce flags, see below
Return values
numNumber of mailboxes with new mail

The force argument may be any combination of the following values:

  • MUTT_MAILBOX_CHECK_FORCE ignore MailboxTime and check for new mail
  • MUTT_MAILBOX_CHECK_FORCE_STATS ignore MailboxTime and calculate statistics

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

Definition at line 137 of file mutt_mailbox.c.

138 {
139  struct stat ctx_st;
140  time_t t;
141  bool check_stats = false;
142  ctx_st.st_dev = 0;
143  ctx_st.st_ino = 0;
144 
145 #ifdef USE_IMAP
146  /* update postponed count as well, on force */
147  if (force & MUTT_MAILBOX_CHECK_FORCE)
149 #endif
150 
151  /* fastest return if there are no mailboxes */
153  return 0;
154 
155  const short c_mail_check = cs_subset_number(NeoMutt->sub, "mail_check");
156  const bool c_mail_check_stats =
157  cs_subset_bool(NeoMutt->sub, "mail_check_stats");
158  const short c_mail_check_stats_interval =
159  cs_subset_number(NeoMutt->sub, "mail_check_stats_interval");
160 
161  t = mutt_date_epoch();
162  if (!force && (t - MailboxTime < c_mail_check))
163  return MailboxCount;
164 
165  if ((force & MUTT_MAILBOX_CHECK_FORCE_STATS) ||
166  (c_mail_check_stats && ((t - MailboxStatsTime) >= c_mail_check_stats_interval)))
167  {
168  check_stats = true;
169  MailboxStatsTime = t;
170  }
171 
172  MailboxTime = t;
173  MailboxCount = 0;
174  MailboxNotify = 0;
175 
176  /* check device ID and serial number instead of comparing paths */
177  if (!m_cur || (m_cur->type == MUTT_IMAP) || (m_cur->type == MUTT_POP)
178 #ifdef USE_NNTP
179  || (m_cur->type == MUTT_NNTP)
180 #endif
181  || stat(mailbox_path(m_cur), &ctx_st) != 0)
182  {
183  ctx_st.st_dev = 0;
184  ctx_st.st_ino = 0;
185  }
186 
187  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
189  struct MailboxNode *np = NULL;
190  STAILQ_FOREACH(np, &ml, entries)
191  {
192  if (np->mailbox->flags & MB_HIDDEN)
193  continue;
194 
195  mailbox_check(m_cur, np->mailbox, &ctx_st,
196  check_stats || (!np->mailbox->first_check_stats_done && c_mail_check_stats));
197  np->mailbox->first_check_stats_done = true;
198  }
200 
201  return MailboxCount;
202 }
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
&#39;NNTP&#39; (Usenet) Mailbox type
Definition: mailbox.h:52
struct AccountList accounts
List of all Accounts.
Definition: neomutt.h:40
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:141
Match any Mailbox type.
Definition: mailbox.h:45
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
bool first_check_stats_done
True when the check have been done at least on time.
Definition: mailbox.h:116
static time_t MailboxStatsTime
last time we check performed mail_check_stats
Definition: mutt_mailbox.c:43
static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check, struct stat *ctx_sb, bool check_stats)
Check a mailbox for new mail.
Definition: mutt_mailbox.c:54
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
Container for Accounts, Notifications.
Definition: neomutt.h:36
static short MailboxCount
how many boxes with new mail
Definition: mutt_mailbox.c:44
static short MailboxNotify
of unnotified new boxes
Definition: mutt_mailbox.c:45
#define MB_HIDDEN
Definition: mailbox.h:38
&#39;POP3&#39; Mailbox type
Definition: mailbox.h:55
static time_t MailboxTime
last time we started checking for mail
Definition: mutt_mailbox.c:42
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
void mutt_update_num_postponed(void)
Force the update of the number of postponed messages.
Definition: postpone.c:240
#define MUTT_MAILBOX_CHECK_FORCE
Definition: mutt_mailbox.h:32
uint8_t flags
e.g. MB_NORMAL
Definition: mailbox.h:134
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
List of Mailboxes.
Definition: mailbox.h:156
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define TAILQ_EMPTY(head)
Definition: queue.h:721
&#39;IMAP&#39; Mailbox type
Definition: mailbox.h:53
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:158
#define MUTT_MAILBOX_CHECK_FORCE_STATS
Definition: mutt_mailbox.h:33
+ 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 360 of file mutt_mailbox.c.

361 {
362 #ifdef HAVE_UTIMENSAT
363  struct timespec ts[2];
364 #else
365  struct utimbuf ut;
366 #endif
367 
368  const bool c_check_mbox_size =
369  cs_subset_bool(NeoMutt->sub, "check_mbox_size");
370  if (c_check_mbox_size)
371  {
372  struct Mailbox *m = mailbox_find(path);
373  if (m && !m->has_new)
374  mailbox_update(m);
375  }
376  else
377  {
378  /* fix up the times so mailbox won't get confused */
379  if (st->st_mtime > st->st_atime)
380  {
381 #ifdef HAVE_UTIMENSAT
382  ts[0].tv_sec = 0;
383  ts[0].tv_nsec = UTIME_OMIT;
384  ts[1].tv_sec = 0;
385  ts[1].tv_nsec = UTIME_NOW;
386  utimensat(AT_FDCWD, buf, ts, 0);
387 #else
388  ut.actime = st->st_atime;
389  ut.modtime = mutt_date_epoch();
390  utime(path, &ut);
391 #endif
392  }
393  else
394  {
395 #ifdef HAVE_UTIMENSAT
396  ts[0].tv_sec = 0;
397  ts[0].tv_nsec = UTIME_NOW;
398  ts[1].tv_sec = 0;
399  ts[1].tv_nsec = UTIME_NOW;
400  utimensat(AT_FDCWD, buf, ts, 0);
401 #else
402  utime(path, NULL);
403 #endif
404  }
405  }
406 }
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:125
Container for Accounts, Notifications.
Definition: neomutt.h:36
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
A mailbox.
Definition: mailbox.h:81
void mailbox_update(struct Mailbox *m)
Get the mailbox&#39;s current size.
Definition: mailbox.c:190
Time value with nanosecond precision.
Definition: file.h:48
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_list()

bool mutt_mailbox_list ( void  )

List the mailboxes with new mail.

Return values
trueThere is new mail

Definition at line 222 of file mutt_mailbox.c.

223 {
224  char mailboxlist[512];
225  size_t pos = 0;
226  int first = 1;
227 
228  int have_unnotified = MailboxNotify;
229 
230  struct Buffer *path = mutt_buffer_pool_get();
231 
232  mailboxlist[0] = '\0';
233  pos += strlen(strncat(mailboxlist, _("New mail in "), sizeof(mailboxlist) - 1 - pos));
234  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
236  struct MailboxNode *np = NULL;
237  STAILQ_FOREACH(np, &ml, entries)
238  {
239  /* Is there new mail in this mailbox? */
240  if (!np->mailbox->has_new || (have_unnotified && np->mailbox->notified))
241  continue;
242 
245 
246  const size_t width = msgwin_get_width();
247  if (!first && (width >= 7) && ((pos + mutt_buffer_len(path)) >= (width - 7)))
248  {
249  break;
250  }
251 
252  if (!first)
253  pos += strlen(strncat(mailboxlist + pos, ", ", sizeof(mailboxlist) - 1 - pos));
254 
255  /* Prepend an asterisk to mailboxes not already notified */
256  if (!np->mailbox->notified)
257  {
258  /* pos += strlen (strncat(mailboxlist + pos, "*", sizeof(mailboxlist)-1-pos)); */
259  np->mailbox->notified = true;
260  MailboxNotify--;
261  }
262  pos += strlen(strncat(mailboxlist + pos, mutt_buffer_string(path),
263  sizeof(mailboxlist) - 1 - pos));
264  first = 0;
265  }
267 
268  if (!first && np)
269  {
270  strncat(mailboxlist + pos, ", ...", sizeof(mailboxlist) - 1 - pos);
271  }
272 
274 
275  if (!first)
276  {
277  mutt_message("%s", mailboxlist);
278  return true;
279  }
280 
281  /* there were no mailboxes needing to be notified, so clean up since
282  * MailboxNotify has somehow gotten out of sync */
283  MailboxNotify = 0;
284  return false;
285 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:141
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
Match any Mailbox type.
Definition: mailbox.h:45
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
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:262
String manipulation buffer.
Definition: buffer.h:33
#define _(a)
Definition: message.h:28
Container for Accounts, Notifications.
Definition: neomutt.h:36
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:603
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
static short MailboxNotify
of unnotified new boxes
Definition: mutt_mailbox.c:45
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:356
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
bool notified
User has been notified.
Definition: mailbox.h:104
#define mutt_message(...)
Definition: logging.h:87
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
List of Mailboxes.
Definition: mailbox.h:156
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:158
+ 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 314 of file mutt_mailbox.c.

315 {
317 
318  if (mutt_mailbox_check(m_cur, 0) > 0)
319  {
320  bool found = false;
321  for (int pass = 0; pass < 2; pass++)
322  {
323  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
325  struct MailboxNode *np = NULL;
326  STAILQ_FOREACH(np, &ml, entries)
327  {
328  if (np->mailbox->type == MUTT_NOTMUCH) /* only match real mailboxes */
329  continue;
331  if ((found || (pass > 0)) && np->mailbox->has_new)
332  {
335  struct Mailbox *m_result = np->mailbox;
337  return m_result;
338  }
340  found = true;
341  }
343  }
344 
345  mutt_mailbox_check(m_cur, MUTT_MAILBOX_CHECK_FORCE); /* mailbox was wrong - resync things */
346  }
347 
348  mutt_buffer_reset(s); // no folders with new mail
349  return NULL;
350 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:141
Match any Mailbox type.
Definition: mailbox.h:45
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
Container for Accounts, Notifications.
Definition: neomutt.h:36
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:603
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
A mailbox.
Definition: mailbox.h:81
int mutt_mailbox_check(struct Mailbox *m_cur, int force)
Check all all Mailboxes for new mail.
Definition: mutt_mailbox.c:137
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
#define MUTT_MAILBOX_CHECK_FORCE
Definition: mutt_mailbox.h:32
&#39;Notmuch&#39; (virtual) Mailbox type
Definition: mailbox.h:54
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
List of Mailboxes.
Definition: mailbox.h:156
struct Buffer pathbuf
Definition: mailbox.h:83
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:158
+ 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 209 of file mutt_mailbox.c.

210 {
211  if ((mutt_mailbox_check(m_cur, 0) > 0) && MailboxNotify)
212  {
213  return mutt_mailbox_list();
214  }
215  return false;
216 }
bool mutt_mailbox_list(void)
List the mailboxes with new mail.
Definition: mutt_mailbox.c:222
static short MailboxNotify
of unnotified new boxes
Definition: mutt_mailbox.c:45
int mutt_mailbox_check(struct Mailbox *m_cur, int force)
Check all all Mailboxes for new mail.
Definition: mutt_mailbox.c:137
+ 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 291 of file mutt_mailbox.c.

292 {
293  if (!m)
294  return;
295 
296  m->notified = true;
297 #ifdef HAVE_CLOCK_GETTIME
298  clock_gettime(CLOCK_REALTIME, &m->last_visited);
299 #else
301  m->last_visited.tv_nsec = 0;
302 #endif
303 }
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
time_t tv_sec
Definition: file.h:50
struct timespec last_visited
Time of last exit from this mailbox.
Definition: mailbox.h:108
long tv_nsec
Definition: file.h:51
bool notified
User has been notified.
Definition: mailbox.h:104
+ Here is the call graph for this function:
+ Here is the caller graph for this function: