NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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() -.
 
enum MxStatus maildir_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
 
static enum MxStatus mbox_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
 
static enum MxStatus mh_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
 
static enum MxStatus nm_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
 

Detailed Description

Check the Mailbox statistics.

Parameters
mMailbox to check
flagsFunction flags
Return values
enumMxStatus
Precondition
m is not NULL

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 1345 of file imap.c.

1346{
1347 const bool queue = (flags & MUTT_MAILBOX_CHECK_IMMEDIATE) == 0;
1348 const int new_msgs = imap_mailbox_status(m, queue);
1349 if (new_msgs == -1)
1350 return MX_STATUS_ERROR;
1351 if (new_msgs == 0)
1352 return MX_STATUS_OK;
1353 return MX_STATUS_NEW_MAIL;
1354}
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition imap.c:1397
@ MUTT_MAILBOX_CHECK_IMMEDIATE
Don't postpone the actual checking.
Definition mxapi.h:61
@ MX_STATUS_ERROR
An error occurred.
Definition mxapi.h:71
@ MX_STATUS_OK
No changes.
Definition mxapi.h:72
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition mxapi.h:73
+ Here is the call graph for this function:

◆ maildir_mbox_check_stats()

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

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

Definition at line 820 of file mailbox.c.

821{
822 bool check_stats = flags & MUTT_MAILBOX_CHECK_STATS;
823 bool check_new = true;
824
825 if (check_stats)
826 {
827 m->msg_new = 0;
828 m->msg_count = 0;
829 m->msg_unread = 0;
830 m->msg_flagged = 0;
831 }
832
833 maildir_check_dir(m, "new", check_new, check_stats);
834
835 const bool c_maildir_check_cur = cs_subset_bool(NeoMutt->sub, "maildir_check_cur");
836 check_new = !m->has_new && c_maildir_check_cur;
837 if (check_new || check_stats)
838 maildir_check_dir(m, "cur", check_new, check_stats);
839
841}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
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 mailbox.c:392
@ MUTT_MAILBOX_CHECK_STATS
Ignore mail_check_stats and calculate statistics (used by <check-stats>)
Definition mxapi.h:60
bool has_new
Mailbox has new mail.
Definition mailbox.h:87
int msg_new
Number of new messages.
Definition mailbox.h:94
int msg_count
Total number of messages.
Definition mailbox.h:90
int msg_flagged
Number of flagged messages.
Definition mailbox.h:92
int msg_unread
Number of unread messages.
Definition mailbox.h:91
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ 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 1677 of file mbox.c.

1678{
1679 struct stat st = { 0 };
1680 if (stat(mailbox_path(m), &st) != 0)
1681 return MX_STATUS_ERROR;
1682
1683 bool new_or_changed;
1684
1685 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1686 if (c_check_mbox_size)
1687 {
1688 new_or_changed = (st.st_size > m->size);
1689 }
1690 else
1691 {
1692 new_or_changed =
1694 (m->newly_created &&
1697 }
1698
1699 if (new_or_changed)
1700 {
1701 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
1702 if (!c_mail_check_recent ||
1704 {
1705 m->has_new = true;
1706 }
1707 }
1708 else if (c_check_mbox_size)
1709 {
1710 /* some other program has deleted mail from the folder */
1711 m->size = (off_t) st.st_size;
1712 }
1713
1714 if (m->newly_created && ((st.st_ctime != st.st_mtime) || (st.st_ctime != st.st_atime)))
1715 m->newly_created = false;
1716
1717 if (flags & MUTT_MAILBOX_CHECK_STATS)
1718 {
1721 &adata->stats_last_checked) > 0)
1722 {
1723 bool old_peek = m->peekonly;
1725 mx_mbox_close(m);
1726 m->peekonly = old_peek;
1727 mutt_time_now(&adata->stats_last_checked);
1728 }
1729 }
1730
1731 if (m->has_new || m->msg_new)
1732 return MX_STATUS_NEW_MAIL;
1733 return MX_STATUS_OK;
1734}
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:216
int mutt_file_stat_compare(struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type)
Compare two stat infos.
Definition file.c:1536
int mutt_file_stat_timespec_compare(struct stat *st, enum MuttStatType type, struct timespec *b)
Compare stat info with a time value.
Definition file.c:1514
@ MUTT_STAT_CTIME
File/dir's ctime - last status change time.
Definition file.h:60
@ MUTT_STAT_ATIME
File/dir's atime - last accessed time.
Definition file.h:58
@ MUTT_STAT_MTIME
File/dir's mtime - last modified time.
Definition file.h:59
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition mbox.c:121
void mutt_time_now(struct timespec *tp)
Set the provided time field to the current time.
Definition date.c:481
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition mx.c:285
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition mx.c:595
@ MUTT_QUIET
Do not print any messages.
Definition mxapi.h:46
@ MUTT_NOSORT
Do not sort the mailbox after opening it.
Definition mxapi.h:43
@ MUTT_PEEK
Revert atime back after taking a look (if applicable)
Definition mxapi.h:47
void * adata
Private data (for Mailbox backends)
Definition account.h:42
bool newly_created
Mbox or mmdf just popped into existence.
Definition mailbox.h:105
bool peekonly
Just taking a glance, revert atime.
Definition mailbox.h:116
off_t size
Size of the Mailbox.
Definition mailbox.h:86
struct timespec last_visited
Time of last exit from this mailbox.
Definition mailbox.h:106
Mbox-specific Account data -.
Definition lib.h:50
+ 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 140 of file mh.c.

141{
142 struct MhSequences mhs = { 0 };
143 DIR *dir = NULL;
144 struct dirent *de = NULL;
145
146 /* when $mail_check_recent is set and the .mh_sequences file hasn't changed
147 * since the last m visit, there is no "new mail" */
148 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
149 if (c_mail_check_recent && (mh_seq_changed(m) <= 0))
150 {
151 return MX_STATUS_OK;
152 }
153
154 if (mh_seq_read(&mhs, mailbox_path(m)) < 0)
155 return MX_STATUS_ERROR;
156
157 m->msg_count = 0;
158 m->msg_unread = 0;
159 m->msg_flagged = 0;
160
161 enum MxStatus rc = MX_STATUS_OK;
162 bool check_new = true;
163 for (int i = mhs.max; i > 0; i--)
164 {
165 if ((mh_seq_check(&mhs, i) & MH_SEQ_FLAGGED))
166 m->msg_flagged++;
167 if (mh_seq_check(&mhs, i) & MH_SEQ_UNSEEN)
168 {
169 m->msg_unread++;
170 if (check_new)
171 {
172 /* if the first unseen message we encounter was in the m during the
173 * last visit, don't notify about it */
174 if (!c_mail_check_recent || (mh_already_notified(m, i) == 0))
175 {
176 m->has_new = true;
178 }
179 /* Because we are traversing from high to low, we can stop
180 * checking for new mail after the first unseen message.
181 * Whether it resulted in "new mail" or not. */
182 check_new = false;
183 }
184 }
185 }
186
187 mh_seq_free(&mhs);
188
190 if (dir)
191 {
192 while ((de = readdir(dir)))
193 {
194 if (*de->d_name == '.')
195 continue;
196 if (mh_valid_message(de->d_name))
197 m->msg_count++;
198 }
199 closedir(dir);
200 }
201
202 return rc;
203}
DIR * mutt_file_opendir(const char *path, enum MuttOpenDirMode mode)
Open a directory.
Definition file.c:535
@ MUTT_OPENDIR_NONE
Plain opendir()
Definition file.h:68
static bool mh_valid_message(const char *s)
Is this a valid MH message filename.
Definition mh.c:99
static int mh_already_notified(struct Mailbox *m, int msgno)
Has the message changed.
Definition mh.c:77
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_sync(), and mbox_close()
Definition mxapi.h:70
MhSeqFlags mh_seq_check(struct MhSequences *mhs, int i)
Get the flags for a given sequence.
Definition sequence.c:79
void mh_seq_free(struct MhSequences *mhs)
Free some sequences.
Definition sequence.c:68
int mh_seq_changed(struct Mailbox *m)
Has the mailbox changed.
Definition sequence.c:441
int mh_seq_read(struct MhSequences *mhs, const char *path)
Read a set of MH sequences.
Definition sequence.c:380
@ MH_SEQ_UNSEEN
Email hasn't been read.
Definition sequence.h:37
@ MH_SEQ_FLAGGED
Email is flagged.
Definition sequence.h:39
Set of MH sequence numbers.
Definition sequence.h:47
int max
Number of flags stored.
Definition sequence.h:48
+ 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 1836 of file notmuch.c.

1837{
1838 struct UrlQuery *item = NULL;
1839 struct Url *url = NULL;
1840 const char *db_filename = NULL;
1841 char *db_query = NULL;
1842 notmuch_database_t *db = NULL;
1843 enum MxStatus rc = MX_STATUS_ERROR;
1844 const short c_nm_db_limit = cs_subset_number(NeoMutt->sub, "nm_db_limit");
1845 int limit = c_nm_db_limit;
1846 mutt_debug(LL_DEBUG1, "nm: count\n");
1847
1848 url = url_parse(mailbox_path(m));
1849 if (!url)
1850 {
1851 mutt_error(_("failed to parse notmuch url: %s"), mailbox_path(m));
1852 goto done;
1853 }
1854
1855 STAILQ_FOREACH(item, &url->query_strings, entries)
1856 {
1857 if (item->value && (mutt_str_equal(item->name, "query")))
1858 {
1859 db_query = item->value;
1860 }
1861 else if (item->value && (mutt_str_equal(item->name, "limit")))
1862 {
1863 // Try to parse the limit
1864 if (!mutt_str_atoi_full(item->value, &limit))
1865 {
1866 mutt_error(_("failed to parse limit: %s"), item->value);
1867 goto done;
1868 }
1869 }
1870 }
1871
1872 if (!db_query)
1873 goto done;
1874
1875 db_filename = url->path;
1876 if (!db_filename)
1877 db_filename = nm_db_get_filename(m);
1878
1879#if LIBNOTMUCH_CHECK_VERSION(5, 4, 0)
1880 /* don't be verbose about connection, as we're called from
1881 * sidebar/mailbox very often */
1882 db = nm_db_do_open(db_filename, false, false);
1883#else
1884 if (!db_filename)
1885 {
1886 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
1887 db_filename = c_folder;
1888 }
1889 /* don't be verbose about connection, as we're called from
1890 * sidebar/mailbox very often */
1891 if (db_filename)
1892 db = nm_db_do_open(db_filename, false, false);
1893#endif
1894 if (!db)
1895 goto done;
1896
1897 /* all emails */
1898 m->msg_count = count_query(db, db_query, limit);
1900
1901 // holder variable for extending query to unread/flagged
1902 char *qstr = NULL;
1903
1904 // unread messages
1905 const char *const c_nm_unread_tag = cs_subset_string(NeoMutt->sub, "nm_unread_tag");
1906 mutt_str_asprintf(&qstr, "( %s ) tag:%s", db_query, c_nm_unread_tag);
1907 m->msg_unread = count_query(db, qstr, limit);
1908 FREE(&qstr);
1909
1910 // flagged messages
1911 const char *const c_nm_flagged_tag = cs_subset_string(NeoMutt->sub, "nm_flagged_tag");
1912 mutt_str_asprintf(&qstr, "( %s ) tag:%s", db_query, c_nm_flagged_tag);
1913 m->msg_flagged = count_query(db, qstr, limit);
1914 FREE(&qstr);
1915
1916 rc = (m->msg_unread > 0) ? MX_STATUS_NEW_MAIL : MX_STATUS_OK;
1917done:
1918 if (db)
1919 {
1920 nm_db_free(db);
1921 mutt_debug(LL_DEBUG1, "nm: count close DB\n");
1922 }
1923 url_free(&url);
1924
1925 mutt_debug(LL_DEBUG1, "nm: count done [rc=%d]\n", rc);
1926 return rc;
1927}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define _(a)
Definition message.h:28
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition string.c:808
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
void mx_alloc_memory(struct Mailbox *m, int req_size)
Create storage for the emails.
Definition mx.c:1208
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition db.c:116
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition db.c:63
void nm_db_free(notmuch_database_t *db)
Decoupled way to close a Notmuch database.
Definition db.c:262
static unsigned int count_query(notmuch_database_t *db, const char *qstr, int limit)
Count the results of a query.
Definition notmuch.c:1472
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Parsed Query String.
Definition url.h:58
char * name
Query name.
Definition url.h:59
char * value
Query value.
Definition url.h:60
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition url.h:69
struct UrlQueryList query_strings
List of query strings.
Definition url.h:76
char * path
Path.
Definition url.h:75
struct Url * url_parse(const char *src)
Fill in Url.
Definition url.c:242
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition url.c:124
+ Here is the call graph for this function: