NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
mbox_check_stats()

Check the Mailbox statistics. More...

+ Collaboration diagram for mbox_check_stats():

Functions

static enum MxStatus imap_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 
static enum MxStatus maildir_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 
static enum MxStatus mh_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 
static enum MxStatus mbox_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 
static enum MxStatus nm_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 

Detailed Description

Check the Mailbox statistics.

Parameters
mMailbox to check
flagsFunction flags
Return values
enumMxStatus

Contract

Function Documentation

◆ imap_mbox_check_stats()

static enum MxStatus imap_mbox_check_stats ( struct Mailbox m,
uint8_t  flags 
)
static

Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.

Definition at line 1219 of file imap.c.

1220 {
1221  const int new_msgs = imap_mailbox_status(m, true);
1222  if (new_msgs == -1)
1223  return MX_STATUS_ERROR;
1224  if (new_msgs == 0)
1225  return MX_STATUS_OK;
1226  return MX_STATUS_NEW_MAIL;
1227 }
New mail received in Mailbox.
Definition: mxapi.h:79
No changes.
Definition: mxapi.h:78
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition: imap.c:1270
An error occurred.
Definition: mxapi.h:77
+ Here is the call graph for this function:

◆ maildir_mbox_check_stats()

static enum MxStatus maildir_mbox_check_stats ( struct Mailbox m,
uint8_t  flags 
)
static

Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.

Definition at line 1343 of file maildir.c.

1344 {
1345  bool check_stats = flags;
1346  bool check_new = true;
1347 
1348  if (check_stats)
1349  {
1350  m->msg_count = 0;
1351  m->msg_unread = 0;
1352  m->msg_flagged = 0;
1353  m->msg_new = 0;
1354  }
1355 
1356  maildir_check_dir(m, "new", check_new, check_stats);
1357 
1358  const bool c_maildir_check_cur =
1359  cs_subset_bool(NeoMutt->sub, "maildir_check_cur");
1360  check_new = !m->has_new && c_maildir_check_cur;
1361  if (check_new || check_stats)
1362  maildir_check_dir(m, "cur", check_new, check_stats);
1363 
1364  return m->msg_new ? MX_STATUS_NEW_MAIL : MX_STATUS_OK;
1365 }
int msg_count
Total number of messages.
Definition: mailbox.h:91
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
New mail received in Mailbox.
Definition: mxapi.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:36
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
static void maildir_check_dir(struct Mailbox *m, const char *dir_name, bool check_new, bool check_stats)
Check for new mail / mail counts.
Definition: maildir.c:85
No changes.
Definition: mxapi.h:78
int msg_new
Number of new messages.
Definition: mailbox.h:95
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:

◆ mh_mbox_check_stats()

static enum MxStatus mh_mbox_check_stats ( struct Mailbox m,
uint8_t  flags 
)
static

Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.

Definition at line 189 of file mh.c.

190 {
191  struct MhSequences mhs = { 0 };
192  DIR *dirp = NULL;
193  struct dirent *de = NULL;
194 
195  /* when $mail_check_recent is set and the .mh_sequences file hasn't changed
196  * since the last m visit, there is no "new mail" */
197  const bool c_mail_check_recent =
198  cs_subset_bool(NeoMutt->sub, "mail_check_recent");
199  if (c_mail_check_recent && (mh_seq_changed(m) <= 0))
200  {
201  return MX_STATUS_OK;
202  }
203 
204  if (mh_seq_read(&mhs, mailbox_path(m)) < 0)
205  return MX_STATUS_ERROR;
206 
207  m->msg_count = 0;
208  m->msg_unread = 0;
209  m->msg_flagged = 0;
210 
211  enum MxStatus rc = MX_STATUS_OK;
212  bool check_new = true;
213  for (int i = mhs.max; i > 0; i--)
214  {
215  if ((mh_seq_check(&mhs, i) & MH_SEQ_FLAGGED))
216  m->msg_flagged++;
217  if (mh_seq_check(&mhs, i) & MH_SEQ_UNSEEN)
218  {
219  m->msg_unread++;
220  if (check_new)
221  {
222  /* if the first unseen message we encounter was in the m during the
223  * last visit, don't notify about it */
224  if (!c_mail_check_recent || (mh_already_notified(m, i) == 0))
225  {
226  m->has_new = true;
227  rc = MX_STATUS_NEW_MAIL;
228  }
229  /* Because we are traversing from high to low, we can stop
230  * checking for new mail after the first unseen message.
231  * Whether it resulted in "new mail" or not. */
232  check_new = false;
233  }
234  }
235  }
236 
237  mh_seq_free(&mhs);
238 
239  dirp = opendir(mailbox_path(m));
240  if (dirp)
241  {
242  while ((de = readdir(dirp)))
243  {
244  if (*de->d_name == '.')
245  continue;
246  if (mh_valid_message(de->d_name))
247  m->msg_count++;
248  }
249  closedir(dirp);
250  }
251 
252  return rc;
253 }
MhSeqFlags mh_seq_check(struct MhSequences *mhs, int i)
Get the flags for a given sequence.
Definition: sequence.c:78
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
int msg_count
Total number of messages.
Definition: mailbox.h:91
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
void mh_seq_free(struct MhSequences *mhs)
Free some sequences.
Definition: sequence.c:67
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
static int mh_already_notified(struct Mailbox *m, int msgno)
Has the message changed.
Definition: mh.c:125
int mh_seq_changed(struct Mailbox *m)
Has the mailbox changed.
Definition: sequence.c:445
New mail received in Mailbox.
Definition: mxapi.h:79
int max
Number of flags stored.
Definition: sequence.h:42
int mh_seq_read(struct MhSequences *mhs, const char *path)
Read a set of MH sequences.
Definition: sequence.c:381
Set of MH sequence numbers.
Definition: sequence.h:40
Container for Accounts, Notifications.
Definition: neomutt.h:36
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
bool mh_valid_message(const char *s)
Is this a valid MH message filename.
Definition: mh.c:148
#define MH_SEQ_FLAGGED
Email is flagged.
Definition: sequence.h:35
No changes.
Definition: mxapi.h:78
#define MH_SEQ_UNSEEN
Email hasn&#39;t been read.
Definition: sequence.h:33
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_snc(), and mbox_close() ...
Definition: mxapi.h:75
An error occurred.
Definition: mxapi.h:77
+ Here is the call graph for this function:

◆ mbox_mbox_check_stats()

static enum MxStatus mbox_mbox_check_stats ( struct Mailbox m,
uint8_t  flags 
)
static

Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.

Definition at line 1773 of file mbox.c.

1774 {
1775  struct stat sb = { 0 };
1776  if (stat(mailbox_path(m), &sb) != 0)
1777  return MX_STATUS_ERROR;
1778 
1779  bool new_or_changed;
1780 
1781  const bool c_check_mbox_size =
1782  cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1783  if (c_check_mbox_size)
1784  new_or_changed = (sb.st_size > m->size);
1785  else
1786  {
1787  new_or_changed =
1789  (m->newly_created &&
1792  }
1793 
1794  if (new_or_changed)
1795  {
1796  const bool c_mail_check_recent =
1797  cs_subset_bool(NeoMutt->sub, "mail_check_recent");
1798  if (!c_mail_check_recent ||
1800  {
1801  m->has_new = true;
1802  }
1803  }
1804  else if (c_check_mbox_size)
1805  {
1806  /* some other program has deleted mail from the folder */
1807  m->size = (off_t) sb.st_size;
1808  }
1809 
1810  if (m->newly_created && ((sb.st_ctime != sb.st_mtime) || (sb.st_ctime != sb.st_atime)))
1811  m->newly_created = false;
1812 
1813  if ((flags != 0) && mutt_file_stat_timespec_compare(&sb, MUTT_STAT_MTIME,
1814  &m->stats_last_checked) > 0)
1815  {
1816  bool old_peek = m->peekonly;
1818  mx_mbox_close(m);
1819  m->peekonly = old_peek;
1820  }
1821 
1822  if (m->has_new || m->msg_new)
1823  return MX_STATUS_NEW_MAIL;
1824  return MX_STATUS_OK;
1825 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
#define MUTT_PEEK
Revert atime back after taking a look (if applicable)
Definition: mxapi.h:68
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:304
File/dir&#39;s ctime - creation time.
Definition: file.h:64
bool peekonly
Just taking a glance, revert atime.
Definition: mailbox.h:117
New mail received in Mailbox.
Definition: mxapi.h:79
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:610
Container for Accounts, Notifications.
Definition: neomutt.h:36
bool has_new
Mailbox has new mail.
Definition: mailbox.h:88
struct timespec last_visited
Time of last exit from this mailbox.
Definition: mailbox.h:108
File/dir&#39;s mtime - last modified time.
Definition: file.h:63
struct timespec stats_last_checked
Mtime of mailbox the last time stats where checked.
Definition: mailbox.h:109
No changes.
Definition: mxapi.h:78
#define MUTT_QUIET
Do not print any messages.
Definition: mxapi.h:64
int mutt_file_stat_timespec_compare(struct stat *sba, enum MuttStatType type, struct timespec *b)
Compare stat info with a time value.
Definition: file.c:1580
bool newly_created
Mbox or mmdf just popped into existence.
Definition: mailbox.h:106
int mutt_file_stat_compare(struct stat *sba, enum MuttStatType sba_type, struct stat *sbb, enum MuttStatType sbb_type)
Compare two stat infos.
Definition: file.c:1602
int msg_new
Number of new messages.
Definition: mailbox.h:95
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define MUTT_NOSORT
Do not sort the mailbox after opening it.
Definition: mxapi.h:61
File/dir&#39;s atime - last accessed time.
Definition: file.h:62
An error occurred.
Definition: mxapi.h:77
+ Here is the call graph for this function:

◆ nm_mbox_check_stats()

static enum MxStatus nm_mbox_check_stats ( struct Mailbox m,
uint8_t  flags 
)
static

Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.

Definition at line 1772 of file notmuch.c.

1773 {
1774  struct UrlQuery *item = NULL;
1775  struct Url *url = NULL;
1776  const char *db_filename = NULL;
1777  char *db_query = NULL;
1778  notmuch_database_t *db = NULL;
1779  enum MxStatus rc = MX_STATUS_ERROR;
1780  const short c_nm_db_limit = cs_subset_number(NeoMutt->sub, "nm_db_limit");
1781  int limit = c_nm_db_limit;
1782  mutt_debug(LL_DEBUG1, "nm: count\n");
1783 
1784  url = url_parse(mailbox_path(m));
1785  if (!url)
1786  {
1787  mutt_error(_("failed to parse notmuch url: %s"), mailbox_path(m));
1788  goto done;
1789  }
1790 
1791  STAILQ_FOREACH(item, &url->query_strings, entries)
1792  {
1793  if (item->value && (strcmp(item->name, "query") == 0))
1794  db_query = item->value;
1795  else if (item->value && (strcmp(item->name, "limit") == 0))
1796  {
1797  // Try to parse the limit
1798  if (mutt_str_atoi(item->value, &limit) != 0)
1799  {
1800  mutt_error(_("failed to parse limit: %s"), item->value);
1801  goto done;
1802  }
1803  }
1804  }
1805 
1806  if (!db_query)
1807  goto done;
1808 
1809  db_filename = url->path;
1810  if (!db_filename)
1811  {
1812  const char *const c_nm_default_url =
1813  cs_subset_string(NeoMutt->sub, "nm_default_url");
1814  const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
1815  if (c_nm_default_url)
1816  {
1817  if (nm_path_probe(c_nm_default_url, NULL) == MUTT_NOTMUCH)
1818  db_filename = c_nm_default_url + NmUrlProtocolLen;
1819  else
1820  db_filename = c_nm_default_url;
1821  }
1822  else if (c_folder)
1823  db_filename = c_folder;
1824  }
1825 
1826  /* don't be verbose about connection, as we're called from
1827  * sidebar/mailbox very often */
1828  db = nm_db_do_open(db_filename, false, false);
1829  if (!db)
1830  goto done;
1831 
1832  /* all emails */
1833  m->msg_count = count_query(db, db_query, limit);
1834  while (m->email_max < m->msg_count)
1835  mx_alloc_memory(m);
1836 
1837  // holder variable for extending query to unread/flagged
1838  char *qstr = NULL;
1839 
1840  // unread messages
1841  const char *const c_nm_unread_tag =
1842  cs_subset_string(NeoMutt->sub, "nm_unread_tag");
1843  mutt_str_asprintf(&qstr, "( %s ) tag:%s", db_query, c_nm_unread_tag);
1844  m->msg_unread = count_query(db, qstr, limit);
1845  FREE(&qstr);
1846 
1847  // flagged messages
1848  const char *const c_nm_flagged_tag =
1849  cs_subset_string(NeoMutt->sub, "nm_flagged_tag");
1850  mutt_str_asprintf(&qstr, "( %s ) tag:%s", db_query, c_nm_flagged_tag);
1851  m->msg_flagged = count_query(db, qstr, limit);
1852  FREE(&qstr);
1853 
1854  rc = (m->msg_new > 0) ? MX_STATUS_NEW_MAIL : MX_STATUS_OK;
1855 done:
1856  if (db)
1857  {
1858  nm_db_free(db);
1859  mutt_debug(LL_DEBUG1, "nm: count close DB\n");
1860  }
1861  url_free(&url);
1862 
1863  mutt_debug(LL_DEBUG1, "nm: count done [rc=%d]\n", rc);
1864  return rc;
1865 }
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:90
char * name
Query name.
Definition: url.h:59
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
int msg_count
Total number of messages.
Definition: mailbox.h:91
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:252
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
#define mutt_error(...)
Definition: logging.h:88
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:68
New mail received in Mailbox.
Definition: mxapi.h:79
Parsed Query String.
Definition: url.h:57
void mx_alloc_memory(struct Mailbox *m)
Create storage for the emails.
Definition: mx.c:1212
#define _(a)
Definition: message.h:28
char * value
Query value.
Definition: url.h:60
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition: url.c:123
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
struct UrlQueryList query_strings
List of query strings.
Definition: url.h:76
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
No changes.
Definition: mxapi.h:78
void nm_db_free(notmuch_database_t *db)
decoupled way to close a Notmuch database
Definition: db.c:213
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
static unsigned int count_query(notmuch_database_t *db, const char *qstr, int limit)
Count the results of a query.
Definition: notmuch.c:1425
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
char * path
Path.
Definition: url.h:75
&#39;Notmuch&#39; (virtual) Mailbox type
Definition: mailbox.h:54
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox? - Implements MxOps::path_probe() -.
Definition: notmuch.c:2458
Log at debug level 1.
Definition: logging.h:40
int msg_new
Number of new messages.
Definition: mailbox.h:95
#define FREE(x)
Definition: memory.h:40
const int NmUrlProtocolLen
Definition: notmuch.c:94
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1128
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_snc(), and mbox_close() ...
Definition: mxapi.h:75
An error occurred.
Definition: mxapi.h:77
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:234
+ Here is the call graph for this function: