NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
mutt_mailbox.c
Go to the documentation of this file.
1 
28 #include "config.h"
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <time.h>
32 #include <utime.h>
33 #include "mutt/lib.h"
34 #include "config/lib.h"
35 #include "core/lib.h"
36 #include "gui/lib.h"
37 #include "mutt_mailbox.h"
38 #include "muttlib.h"
39 #include "mx.h"
40 #include "protos.h"
41 
42 static time_t MailboxTime = 0;
43 static time_t MailboxStatsTime = 0;
44 static short MailboxCount = 0;
45 static short MailboxNotify = 0;
46 
54 static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check,
55  struct stat *ctx_sb, bool check_stats)
56 {
57  struct stat sb = { 0 };
58 
59  enum MailboxType mb_type = mx_path_probe(mailbox_path(m_check));
60 
61  const bool c_mail_check_recent =
62  cs_subset_bool(NeoMutt->sub, "mail_check_recent");
63  if ((m_cur == m_check) && c_mail_check_recent)
64  m_check->has_new = false;
65 
66  switch (mb_type)
67  {
68  case MUTT_POP:
69  case MUTT_NNTP:
70  case MUTT_NOTMUCH:
71  case MUTT_IMAP:
72  m_check->type = mb_type;
73  break;
74  default:
75  if ((stat(mailbox_path(m_check), &sb) != 0) ||
76  ((m_check->type == MUTT_UNKNOWN) && S_ISREG(sb.st_mode) && (sb.st_size == 0)) ||
77  ((m_check->type == MUTT_UNKNOWN) &&
78  ((m_check->type = mx_path_probe(mailbox_path(m_check))) <= 0)))
79  {
80  /* if the mailbox still doesn't exist, set the newly created flag to be
81  * ready for when it does. */
82  m_check->newly_created = true;
83  m_check->type = MUTT_UNKNOWN;
84  m_check->size = 0;
85  return;
86  }
87  break; // kept for consistency.
88  }
89 
90  const bool c_check_mbox_size =
91  cs_subset_bool(NeoMutt->sub, "check_mbox_size");
92  /* check to see if the folder is the currently selected folder before polling */
93  if (!m_cur || mutt_buffer_is_empty(&m_cur->pathbuf) ||
94  (((m_check->type == MUTT_IMAP) || (m_check->type == MUTT_NNTP) ||
95  (m_check->type == MUTT_NOTMUCH) || (m_check->type == MUTT_POP)) ?
96  !mutt_str_equal(mailbox_path(m_check), mailbox_path(m_cur)) :
97  ((sb.st_dev != ctx_sb->st_dev) || (sb.st_ino != ctx_sb->st_ino))))
98  {
99  switch (m_check->type)
100  {
101  case MUTT_IMAP:
102  case MUTT_MBOX:
103  case MUTT_MMDF:
104  case MUTT_MAILDIR:
105  case MUTT_MH:
106  case MUTT_NOTMUCH:
107  if ((mx_mbox_check_stats(m_check, check_stats) != MX_STATUS_ERROR) &&
108  m_check->has_new)
109  {
110  MailboxCount++;
111  }
112  break;
113  default:; /* do nothing */
114  }
115  }
116  else if (c_check_mbox_size && m_cur && mutt_buffer_is_empty(&m_cur->pathbuf))
117  m_check->size = (off_t) sb.st_size; /* update the size of current folder */
118 
119  if (!m_check->has_new)
120  m_check->notified = false;
121  else if (!m_check->notified)
122  MailboxNotify++;
123 }
124 
137 int mutt_mailbox_check(struct Mailbox *m_cur, int force)
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 }
203 
209 bool mutt_mailbox_notify(struct Mailbox *m_cur)
210 {
211  if ((mutt_mailbox_check(m_cur, 0) > 0) && MailboxNotify)
212  {
213  return mutt_mailbox_list();
214  }
215  return false;
216 }
217 
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 }
286 
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 }
304 
314 struct Mailbox *mutt_mailbox_next(struct Mailbox *m_cur, struct Buffer *s)
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 }
351 
360 void mutt_mailbox_cleanup(const char *path, struct stat *st)
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 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
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 mutt_mailbox_list(void)
List the mailboxes with new mail.
Definition: mutt_mailbox.c:222
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
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
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
&#39;NNTP&#39; (Usenet) Mailbox type
Definition: mailbox.h:52
void mutt_mailbox_cleanup(const char *path, struct stat *st)
Restore the timestamp of a mailbox.
Definition: mutt_mailbox.c:360
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
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:125
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
enum MxStatus mx_mbox_check_stats(struct Mailbox *m, uint8_t flags)
Check the statistics for a mailbox - Wrapper for MxOps::mbox_check_stats()
Definition: mx.c:1789
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
#define _(a)
Definition: message.h:28
Mailbox wasn&#39;t recognised.
Definition: mailbox.h:47
struct Mailbox * mutt_mailbox_next(struct Mailbox *m_cur, struct Buffer *s)
incoming folders completion routine
Definition: mutt_mailbox.c:314
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
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:603
Convenience wrapper for the config headers.
time_t tv_sec
Definition: file.h:50
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
Some miscellaneous functions.
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
API for mailboxes.
Convenience wrapper for the core headers.
bool mutt_mailbox_notify(struct Mailbox *m_cur)
Notify the user if there&#39;s new mail.
Definition: mutt_mailbox.c:209
static short MailboxCount
how many boxes with new mail
Definition: mutt_mailbox.c:44
long tv_nsec
Definition: file.h:51
&#39;Maildir&#39; Mailbox type
Definition: mailbox.h:51
Prototypes for many functions.
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
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:356
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
&#39;mmdf&#39; Mailbox type
Definition: mailbox.h:49
static time_t MailboxTime
last time we started checking for mail
Definition: mutt_mailbox.c:42
&#39;MH&#39; Mailbox type
Definition: mailbox.h:50
void mailbox_update(struct Mailbox *m)
Get the mailbox&#39;s current size.
Definition: mailbox.c:190
#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
bool newly_created
Mbox or mmdf just popped into existence.
Definition: mailbox.h:106
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
&#39;mbox&#39; Mailbox type
Definition: mailbox.h:48
uint8_t flags
e.g. MB_NORMAL
Definition: mailbox.h:134
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1317
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
MailboxType
Supported mailbox formats.
Definition: mailbox.h:43
bool notified
User has been notified.
Definition: mailbox.h:104
#define mutt_message(...)
Definition: logging.h:87
Time value with nanosecond precision.
Definition: file.h:48
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
List of Mailboxes.
Definition: mailbox.h:156
void mutt_mailbox_set_notified(struct Mailbox *m)
Note when the user was last notified of new mail.
Definition: mutt_mailbox.c:291
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define TAILQ_EMPTY(head)
Definition: queue.h:721
struct Buffer pathbuf
Definition: mailbox.h:83
Convenience wrapper for the library headers.
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
&#39;IMAP&#39; Mailbox type
Definition: mailbox.h:53
Mailbox helper functions.
An error occurred.
Definition: mxapi.h:77
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:158
#define MUTT_MAILBOX_CHECK_FORCE_STATS
Definition: mutt_mailbox.h:33