NeoMutt  2020-06-26-250-g349c94
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 <sys/time.h>
32 #include <time.h>
33 #include <utime.h>
34 #include "mutt/lib.h"
35 #include "core/lib.h"
36 #include "gui/lib.h"
37 #include "mutt_mailbox.h"
38 #include "mutt_globals.h"
39 #include "mutt_menu.h"
40 #include "muttlib.h"
41 #include "mx.h"
42 #include "protos.h"
43 #include "mbox/lib.h"
44 
45 static time_t MailboxTime = 0;
46 static time_t MailboxStatsTime = 0;
47 static short MailboxCount = 0;
48 static short MailboxNotify = 0;
49 
50 /* These Config Variables are only used in mutt_mailbox.c */
51 short C_MailCheck;
54 
62 static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check,
63  struct stat *ctx_sb, bool check_stats)
64 {
65  struct stat sb = { 0 };
66 
67 #ifdef USE_SIDEBAR
68  short orig_new = m_check->has_new;
69  int orig_count = m_check->msg_count;
70  int orig_unread = m_check->msg_unread;
71  int orig_flagged = m_check->msg_flagged;
72 #endif
73 
74  enum MailboxType mb_type = mx_path_probe(mailbox_path(m_check));
75 
76  if ((m_cur == m_check) && C_MailCheckRecent)
77  m_check->has_new = false;
78 
79  switch (mb_type)
80  {
81  case MUTT_POP:
82  case MUTT_NNTP:
83  case MUTT_NOTMUCH:
84  case MUTT_IMAP:
85  m_check->type = mb_type;
86  break;
87  default:
88  if ((stat(mailbox_path(m_check), &sb) != 0) ||
89  ((m_check->type == MUTT_UNKNOWN) && S_ISREG(sb.st_mode) && (sb.st_size == 0)) ||
90  ((m_check->type == MUTT_UNKNOWN) &&
91  ((m_check->type = mx_path_probe(mailbox_path(m_check))) <= 0)))
92  {
93  /* if the mailbox still doesn't exist, set the newly created flag to be
94  * ready for when it does. */
95  m_check->newly_created = true;
96  m_check->type = MUTT_UNKNOWN;
97  m_check->size = 0;
98  return;
99  }
100  break; // kept for consistency.
101  }
102 
103  /* check to see if the folder is the currently selected folder before polling */
104  if (!m_cur || mutt_buffer_is_empty(&m_cur->pathbuf) ||
105  (((m_check->type == MUTT_IMAP) || (m_check->type == MUTT_NNTP) ||
106  (m_check->type == MUTT_NOTMUCH) || (m_check->type == MUTT_POP)) ?
107  !mutt_str_equal(mailbox_path(m_check), mailbox_path(m_cur)) :
108  ((sb.st_dev != ctx_sb->st_dev) || (sb.st_ino != ctx_sb->st_ino))))
109  {
110  switch (m_check->type)
111  {
112  case MUTT_IMAP:
113  case MUTT_MBOX:
114  case MUTT_MMDF:
115  case MUTT_MAILDIR:
116  case MUTT_MH:
117  case MUTT_NOTMUCH:
118  if ((mx_mbox_check_stats(m_check, check_stats) > 0) && m_check->has_new)
119  MailboxCount++;
120  break;
121  default:; /* do nothing */
122  }
123  }
124  else if (C_CheckMboxSize && m_cur && mutt_buffer_is_empty(&m_cur->pathbuf))
125  m_check->size = (off_t) sb.st_size; /* update the size of current folder */
126 
127 #ifdef USE_SIDEBAR
128  if ((orig_new != m_check->has_new) || (orig_count != m_check->msg_count) ||
129  (orig_unread != m_check->msg_unread) || (orig_flagged != m_check->msg_flagged))
130  {
132  }
133 #endif
134 
135  if (!m_check->has_new)
136  m_check->notified = false;
137  else if (!m_check->notified)
138  MailboxNotify++;
139 }
140 
153 int mutt_mailbox_check(struct Mailbox *m_cur, int force)
154 {
155  struct stat contex_sb;
156  time_t t;
157  bool check_stats = false;
158  contex_sb.st_dev = 0;
159  contex_sb.st_ino = 0;
160 
161 #ifdef USE_IMAP
162  /* update postponed count as well, on force */
163  if (force & MUTT_MAILBOX_CHECK_FORCE)
165 #endif
166 
167  /* fastest return if there are no mailboxes */
169  return 0;
170 
171  t = mutt_date_epoch();
172  if (!force && (t - MailboxTime < C_MailCheck))
173  return MailboxCount;
174 
175  if ((force & MUTT_MAILBOX_CHECK_FORCE_STATS) ||
177  {
178  check_stats = true;
179  MailboxStatsTime = t;
180  }
181 
182  MailboxTime = t;
183  MailboxCount = 0;
184  MailboxNotify = 0;
185 
186  /* check device ID and serial number instead of comparing paths */
187  if (!m_cur || (m_cur->type == MUTT_IMAP) || (m_cur->type == MUTT_POP)
188 #ifdef USE_NNTP
189  || (m_cur->type == MUTT_NNTP)
190 #endif
191  || stat(mailbox_path(m_cur), &contex_sb) != 0)
192  {
193  contex_sb.st_dev = 0;
194  contex_sb.st_ino = 0;
195  }
196 
197  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
199  struct MailboxNode *np = NULL;
200  STAILQ_FOREACH(np, &ml, entries)
201  {
202  if (np->mailbox->flags & MB_HIDDEN)
203  continue;
204 
205  mailbox_check(m_cur, np->mailbox, &contex_sb,
206  check_stats || (!np->mailbox->first_check_stats_done && C_MailCheckStats));
207  np->mailbox->first_check_stats_done = true;
208  }
210 
211  return MailboxCount;
212 }
213 
219 bool mutt_mailbox_notify(struct Mailbox *m_cur)
220 {
221  if ((mutt_mailbox_check(m_cur, 0) > 0) && MailboxNotify)
222  {
223  return mutt_mailbox_list();
224  }
225  return false;
226 }
227 
233 {
234  char mailboxlist[512];
235  size_t pos = 0;
236  int first = 1;
237 
238  int have_unnotified = MailboxNotify;
239 
240  struct Buffer *path = mutt_buffer_pool_get();
241 
242  mailboxlist[0] = '\0';
243  pos += strlen(strncat(mailboxlist, _("New mail in "), sizeof(mailboxlist) - 1 - pos));
244  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
246  struct MailboxNode *np = NULL;
247  STAILQ_FOREACH(np, &ml, entries)
248  {
249  /* Is there new mail in this mailbox? */
250  if (!np->mailbox->has_new || (have_unnotified && np->mailbox->notified))
251  continue;
252 
255 
256  if (!first && (MessageWindow->state.cols >= 7) &&
257  ((pos + mutt_buffer_len(path)) >= ((size_t) MessageWindow->state.cols - 7)))
258  {
259  break;
260  }
261 
262  if (!first)
263  pos += strlen(strncat(mailboxlist + pos, ", ", sizeof(mailboxlist) - 1 - pos));
264 
265  /* Prepend an asterisk to mailboxes not already notified */
266  if (!np->mailbox->notified)
267  {
268  /* pos += strlen (strncat(mailboxlist + pos, "*", sizeof(mailboxlist)-1-pos)); */
269  np->mailbox->notified = true;
270  MailboxNotify--;
271  }
272  pos += strlen(strncat(mailboxlist + pos, mutt_b2s(path), sizeof(mailboxlist) - 1 - pos));
273  first = 0;
274  }
276 
277  if (!first && np)
278  {
279  strncat(mailboxlist + pos, ", ...", sizeof(mailboxlist) - 1 - pos);
280  }
281 
283 
284  if (!first)
285  {
286  mutt_message("%s", mailboxlist);
287  return true;
288  }
289 
290  /* there were no mailboxes needing to be notified, so clean up since
291  * MailboxNotify has somehow gotten out of sync */
292  MailboxNotify = 0;
293  return false;
294 }
295 
301 {
302  if (!m)
303  return;
304 
305  m->notified = true;
306 #ifdef HAVE_CLOCK_GETTIME
307  clock_gettime(CLOCK_REALTIME, &m->last_visited);
308 #else
310  m->last_visited.tv_nsec = 0;
311 #endif
312 }
313 
323 struct Mailbox *mutt_mailbox_next(struct Mailbox *m_cur, struct Buffer *s)
324 {
326 
327  if (mutt_mailbox_check(m_cur, 0) > 0)
328  {
329  bool found = false;
330  for (int pass = 0; pass < 2; pass++)
331  {
332  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
334  struct MailboxNode *np = NULL;
335  STAILQ_FOREACH(np, &ml, entries)
336  {
337  if (np->mailbox->type == MUTT_NOTMUCH) /* only match real mailboxes */
338  continue;
340  if ((found || (pass > 0)) && np->mailbox->has_new)
341  {
344  struct Mailbox *m_result = np->mailbox;
346  return m_result;
347  }
349  found = true;
350  }
352  }
353 
354  mutt_mailbox_check(m_cur, MUTT_MAILBOX_CHECK_FORCE); /* mailbox was wrong - resync things */
355  }
356 
357  mutt_buffer_reset(s); // no folders with new mail
358  return NULL;
359 }
360 
369 void mutt_mailbox_cleanup(const char *path, struct stat *st)
370 {
371 #ifdef HAVE_UTIMENSAT
372  struct timespec ts[2];
373 #else
374  struct utimbuf ut;
375 #endif
376 
377  if (C_CheckMboxSize)
378  {
379  struct Mailbox *m = mailbox_find(path);
380  if (m && !m->has_new)
381  mailbox_update(m);
382  }
383  else
384  {
385  /* fix up the times so mailbox won't get confused */
386  if (st->st_mtime > st->st_atime)
387  {
388 #ifdef HAVE_UTIMENSAT
389  ts[0].tv_sec = 0;
390  ts[0].tv_nsec = UTIME_OMIT;
391  ts[1].tv_sec = 0;
392  ts[1].tv_nsec = UTIME_NOW;
393  utimensat(AT_FDCWD, buf, ts, 0);
394 #else
395  ut.actime = st->st_atime;
396  ut.modtime = mutt_date_epoch();
397  utime(path, &ut);
398 #endif
399  }
400  else
401  {
402 #ifdef HAVE_UTIMENSAT
403  ts[0].tv_sec = 0;
404  ts[0].tv_nsec = UTIME_NOW;
405  ts[1].tv_sec = 0;
406  ts[1].tv_nsec = UTIME_NOW;
407  utimensat(AT_FDCWD, buf, ts, 0);
408 #else
409  utime(path, NULL);
410 #endif
411  }
412  }
413 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:876
Convenience wrapper for the gui headers.
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:416
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:201
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
bool mutt_mailbox_list(void)
List the mailboxes with new mail.
Definition: mutt_mailbox.c:232
int msg_count
Total number of messages.
Definition: mailbox.h:91
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
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
if(!test_colorize_)
Definition: acutest.h:499
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
&#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:369
#define mutt_message(...)
Definition: logging.h:83
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
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:137
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:91
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:160
String manipulation buffer.
Definition: buffer.h:33
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:46
#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:323
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:62
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:598
#define REDRAW_SIDEBAR
Redraw the sidebar.
Definition: mutt_menu.h:49
time_t tv_sec
Definition: file.h:48
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:219
static short MailboxCount
how many boxes with new mail
Definition: mutt_mailbox.c:47
long tv_nsec
Definition: file.h:49
&#39;Maildir&#39; Mailbox type
Definition: mailbox.h:51
int flags
e.g. MB_NORMAL
Definition: mailbox.h:134
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:57
#define mutt_b2s(buf)
Definition: buffer.h:41
Prototypes for many functions.
static short MailboxNotify
of unnotified new boxes
Definition: mutt_mailbox.c:48
short C_MailCheckStatsInterval
Config: How often to check for new mail.
Definition: mutt_mailbox.c:53
#define MB_HIDDEN
Definition: mailbox.h:38
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:120
&#39;POP3&#39; Mailbox type
Definition: mailbox.h:55
struct MuttWindow * MessageWindow
Message Window, ":set", etc.
Definition: mutt_window.c:47
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:153
&#39;mmdf&#39; Mailbox type
Definition: mailbox.h:49
static time_t MailboxTime
last time we started checking for mail
Definition: mutt_mailbox.c:45
GUI present the user with a selectable list.
&#39;MH&#39; Mailbox type
Definition: mailbox.h:50
bool C_CheckMboxSize
Config: (mbox,mmdf) Use mailbox size as an indicator of new mail.
Definition: config.c:35
short C_MailCheck
Config: Number of seconds before NeoMutt checks for new mail.
Definition: mutt_mailbox.c:51
WHERE bool C_MailCheckRecent
Config: Notify the user about new mail since the last time the mailbox was opened.
Definition: mutt_globals.h:152
void mailbox_update(struct Mailbox *m)
Get the mailbox&#39;s current size.
Definition: mailbox.c:156
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
void mutt_update_num_postponed(void)
Force the update of the number of postponed messages.
Definition: postpone.c:202
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:16
&#39;Notmuch&#39; (virtual) Mailbox type
Definition: mailbox.h:54
&#39;mbox&#39; Mailbox type
Definition: mailbox.h:48
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1354
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:323
MailboxType
Supported mailbox formats.
Definition: mailbox.h:43
int mx_mbox_check_stats(struct Mailbox *m, int flags)
Check the statistics for a mailbox - Wrapper for MxOps::mbox_check_stats()
Definition: mx.c:1818
bool notified
User has been notified.
Definition: mailbox.h:104
Time value with nanosecond precision.
Definition: file.h:46
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
List of Mailboxes.
Definition: mailbox.h:150
Hundreds of global variables to back the user variables.
void mutt_mailbox_set_notified(struct Mailbox *m)
Note when the user was last notified of new mail.
Definition: mutt_mailbox.c:300
bool C_MailCheckStats
Config: Periodically check for new mail.
Definition: mutt_mailbox.c:52
#define TAILQ_EMPTY(head)
Definition: queue.h:714
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
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:152
#define MUTT_MAILBOX_CHECK_FORCE_STATS
Definition: mutt_mailbox.h:17