NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
mbox.c File Reference

Mbox local mailbox type. More...

#include "config.h"
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "lib.h"
#include "progress/lib.h"
#include "copy.h"
#include "globals.h"
#include "mutt_header.h"
#include "mutt_thread.h"
#include "muttlib.h"
#include "mx.h"
#include "protos.h"
+ Include dependency graph for mbox.c:

Go to the source code of this file.

Data Structures

struct  MUpdate
 Store of new offsets, used by mutt_sync_mailbox() More...
 

Functions

static void mbox_adata_free (void **ptr)
 Free the private Account data - Implements Account::adata_free() More...
 
static struct MboxAccountDatambox_adata_new (void)
 Create a new MboxAccountData struct. More...
 
static int init_mailbox (struct Mailbox *m)
 Add Mbox data to the Mailbox. More...
 
static struct MboxAccountDatambox_adata_get (struct Mailbox *m)
 Get the private data associated with a Mailbox. More...
 
static int mbox_lock_mailbox (struct Mailbox *m, bool excl, bool retry)
 Lock a mailbox. More...
 
static void mbox_unlock_mailbox (struct Mailbox *m)
 Unlock a mailbox. More...
 
static enum MxOpenReturns mmdf_parse_mailbox (struct Mailbox *m)
 Read a mailbox in MMDF format. More...
 
static enum MxOpenReturns mbox_parse_mailbox (struct Mailbox *m)
 Read a mailbox from disk. More...
 
static int reopen_mailbox (struct Mailbox *m)
 Close and reopen a mailbox. More...
 
static bool mbox_has_new (struct Mailbox *m)
 Does the mailbox have new mail. More...
 
static int fseek_last_message (FILE *fp)
 Find the last message in the file. More...
 
static bool test_last_status_new (FILE *fp)
 Is the last message new. More...
 
bool mbox_test_new_folder (const char *path)
 Test if an mbox or mmdf mailbox has new mail. More...
 
void mbox_reset_atime (struct Mailbox *m, struct stat *st)
 Reset the access time on the mailbox file. More...
 
static bool mbox_ac_owns_path (struct Account *a, const char *path)
 Check whether an Account owns a Mailbox path - Implements MxOps::ac_owns_path() -. More...
 
static bool mbox_ac_add (struct Account *a, struct Mailbox *m)
 Add a Mailbox to an Account - Implements MxOps::ac_add() -. More...
 
static FILE * mbox_open_readwrite (struct Mailbox *m)
 Open an mbox read-write. More...
 
static FILE * mbox_open_readonly (struct Mailbox *m)
 Open an mbox read-only. More...
 
static enum MxOpenReturns mbox_mbox_open (struct Mailbox *m)
 Open a Mailbox - Implements MxOps::mbox_open() -. More...
 
static bool mbox_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static enum MxStatus mbox_mbox_check (struct Mailbox *m)
 Check for new mail - Implements MxOps::mbox_check() -. More...
 
static enum MxStatus mbox_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus mbox_mbox_close (struct Mailbox *m)
 Close a Mailbox - Implements MxOps::mbox_close() -. More...
 
static bool mbox_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -. More...
 
static bool mbox_msg_open_new (struct Mailbox *m, struct Message *msg, const struct Email *e)
 Open a new message in a Mailbox - Implements MxOps::msg_open_new() -. More...
 
static int mbox_msg_commit (struct Mailbox *m, struct Message *msg)
 Save changes to an email - Implements MxOps::msg_commit() -. More...
 
static int mbox_msg_close (struct Mailbox *m, struct Message *msg)
 Close an email - Implements MxOps::msg_close() -. More...
 
static int mbox_msg_padding_size (struct Mailbox *m)
 Bytes of padding between messages - Implements MxOps::msg_padding_size() -. More...
 
enum MailboxType mbox_path_probe (const char *path, const struct stat *st)
 Is this an mbox Mailbox? - Implements MxOps::path_probe() -. More...
 
static int mbox_path_canon (char *buf, size_t buflen)
 Canonicalise a Mailbox path - Implements MxOps::path_canon() -. More...
 
static int mbox_path_pretty (char *buf, size_t buflen, const char *folder)
 Abbreviate a Mailbox path - Implements MxOps::path_pretty() -. More...
 
static int mbox_path_parent (char *buf, size_t buflen)
 Find the parent of a Mailbox path - Implements MxOps::path_parent() -. More...
 
static int mbox_path_is_empty (const char *path)
 Is the mailbox empty - Implements MxOps::path_is_empty() -. More...
 
static int mmdf_msg_commit (struct Mailbox *m, struct Message *msg)
 Save changes to an email - Implements MxOps::msg_commit() -. More...
 
static int mmdf_msg_padding_size (struct Mailbox *m)
 Bytes of padding between messages - Implements MxOps::msg_padding_size() -. More...
 
static enum MxStatus mbox_mbox_check_stats (struct Mailbox *m, uint8_t flags)
 Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -. More...
 

Variables

const struct MxOps MxMboxOps
 Mbox Mailbox - Implements MxOps -. More...
 
const struct MxOps MxMmdfOps
 MMDF Mailbox - Implements MxOps -. More...
 

Detailed Description

Mbox local mailbox type.

Authors
  • Michael R. Elkins
  • Richard Russon
  • Pietro Cerutti

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 mbox.c.

Function Documentation

◆ mbox_adata_free()

static void mbox_adata_free ( void **  ptr)
static

Free the private Account data - Implements Account::adata_free()

Definition at line 77 of file mbox.c.

78{
79 struct MboxAccountData *m = *ptr;
80
82 FREE(ptr);
83}
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
#define FREE(x)
Definition: memory.h:43
Mbox-specific Account data -.
Definition: lib.h:49
FILE * fp
Mailbox file.
Definition: lib.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_adata_new()

static struct MboxAccountData * mbox_adata_new ( void  )
static

Create a new MboxAccountData struct.

Return values
ptrNew MboxAccountData

Definition at line 89 of file mbox.c.

90{
91 return mutt_mem_calloc(1, sizeof(struct MboxAccountData));
92}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_mailbox()

static int init_mailbox ( struct Mailbox m)
static

Add Mbox data to the Mailbox.

Parameters
mMailbox
Return values
0Success
-1Error Bad format

Definition at line 100 of file mbox.c.

101{
102 if (!m || !m->account)
103 return -1;
104 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
105 return -1;
106 if (m->account->adata)
107 return 0;
108
111 return 0;
112}
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
static struct MboxAccountData * mbox_adata_new(void)
Create a new MboxAccountData struct.
Definition: mbox.c:89
static void mbox_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free()
Definition: mbox.c:77
void(* adata_free)(void **ptr)
Free the private data attached to the Account.
Definition: account.h:52
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:127
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_adata_get()

static struct MboxAccountData * mbox_adata_get ( struct Mailbox m)
static

Get the private data associated with a Mailbox.

Parameters
mMailbox
Return values
ptrPrivate data

Definition at line 119 of file mbox.c.

120{
121 if (init_mailbox(m) == -1)
122 return NULL;
123 return m->account->adata;
124}
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition: mbox.c:100
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_lock_mailbox()

static int mbox_lock_mailbox ( struct Mailbox m,
bool  excl,
bool  retry 
)
static

Lock a mailbox.

Parameters
mMailbox to lock
exclExclusive lock?
retryShould retry if unable to lock?
Return values
0Success
-1Failure

Definition at line 134 of file mbox.c.

135{
137 if (!adata)
138 return -1;
139
140 int rc = mutt_file_lock(fileno(adata->fp), excl, retry);
141 if (rc == 0)
142 {
143 adata->locked = true;
144 }
145 else if (retry && !excl)
146 {
147 m->readonly = true;
148 return 0;
149 }
150
151 return rc;
152}
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1218
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:119
bool readonly
Don't allow changes to the mailbox.
Definition: mailbox.h:115
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_unlock_mailbox()

static void mbox_unlock_mailbox ( struct Mailbox m)
static

Unlock a mailbox.

Parameters
mMailbox to unlock

Definition at line 158 of file mbox.c.

159{
161 if (!adata)
162 return;
163
164 if (adata->locked)
165 {
166 fflush(adata->fp);
167
168 mutt_file_unlock(fileno(adata->fp));
169 adata->locked = false;
170 }
171}
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1266
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mmdf_parse_mailbox()

static enum MxOpenReturns mmdf_parse_mailbox ( struct Mailbox m)
static

Read a mailbox in MMDF format.

Parameters
mMailbox
Return values
enumMxOpenReturns

Definition at line 178 of file mbox.c.

179{
180 if (!m)
181 return MX_OPEN_ERROR;
182
184 if (!adata)
185 return MX_OPEN_ERROR;
186
187 char buf[8192] = { 0 };
188 char return_path[1024] = { 0 };
189 int count = 0;
190 int lines;
191 time_t t = 0;
192 LOFF_T loc, tmploc;
193 struct Email *e = NULL;
194 struct stat st = { 0 };
195 struct Progress *progress = NULL;
197
198 if (stat(mailbox_path(m), &st) == -1)
199 {
201 goto fail;
202 }
205 m->size = st.st_size;
206
207 buf[sizeof(buf) - 1] = '\0';
208
209 if (m->verbose)
210 {
211 char msg[PATH_MAX] = { 0 };
212 snprintf(msg, sizeof(msg), _("Reading %s..."), mailbox_path(m));
213 progress = progress_new(msg, MUTT_PROGRESS_READ, 0);
214 }
215
216 while (true)
217 {
218 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
219 break;
220
221 if (SigInt)
222 break;
223
224 if (mutt_str_equal(buf, MMDF_SEP))
225 {
226 loc = ftello(adata->fp);
227 if (loc < 0)
228 goto fail;
229
230 count++;
231 if (m->verbose)
232 progress_update(progress, count, (int) (loc / (m->size / 100 + 1)));
233
234 if (m->msg_count == m->email_max)
236 e = email_new();
237 m->emails[m->msg_count] = e;
238 e->offset = loc;
239 e->index = m->msg_count;
240
241 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
242 {
243 /* TODO: memory leak??? */
244 mutt_debug(LL_DEBUG1, "unexpected EOF\n");
245 break;
246 }
247
248 return_path[0] = '\0';
249
250 if (!is_from(buf, return_path, sizeof(return_path), &t))
251 {
252 if (!mutt_file_seek(adata->fp, loc, SEEK_SET))
253 {
254 mutt_error(_("Mailbox is corrupt"));
255 goto fail;
256 }
257 }
258 else
259 {
260 e->received = t - mutt_date_local_tz(t);
261 }
262
263 e->env = mutt_rfc822_read_header(adata->fp, e, false, false);
264
265 loc = ftello(adata->fp);
266 if (loc < 0)
267 goto fail;
268
269 if ((e->body->length > 0) && (e->lines > 0))
270 {
271 tmploc = loc + e->body->length;
272
273 if ((tmploc > 0) && (tmploc < m->size))
274 {
275 if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
276 !fgets(buf, sizeof(buf) - 1, adata->fp) || !mutt_str_equal(MMDF_SEP, buf))
277 {
278 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
279 e->body->length = -1;
280 }
281 }
282 else
283 {
284 e->body->length = -1;
285 }
286 }
287 else
288 {
289 e->body->length = -1;
290 }
291
292 if (e->body->length < 0)
293 {
294 lines = -1;
295 do
296 {
297 loc = ftello(adata->fp);
298 if (loc < 0)
299 goto fail;
300 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
301 break;
302 lines++;
303 } while (!mutt_str_equal(buf, MMDF_SEP));
304
305 e->lines = lines;
306 e->body->length = loc - e->body->offset;
307 }
308
309 if (TAILQ_EMPTY(&e->env->return_path) && return_path[0])
310 mutt_addrlist_parse(&e->env->return_path, return_path);
311
312 if (TAILQ_EMPTY(&e->env->from))
313 mutt_addrlist_copy(&e->env->from, &e->env->return_path, false);
314
315 m->msg_count++;
316 }
317 else
318 {
319 mutt_debug(LL_DEBUG1, "corrupt mailbox\n");
320 mutt_error(_("Mailbox is corrupt"));
321 goto fail;
322 }
323 }
324
325 if (SigInt)
326 {
327 SigInt = false;
328 rc = MX_OPEN_ABORT; /* action aborted */
329 goto fail;
330 }
331
332 rc = MX_OPEN_OK;
333fail:
334 progress_free(&progress);
335 return rc;
336}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:752
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:470
struct Email * email_new(void)
Create a new Email.
Definition: email.c:78
void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *st, enum MuttStatType type)
Read the stat() time into a time value.
Definition: file.c:1598
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:708
@ MUTT_STAT_ATIME
File/dir's atime - last accessed time.
Definition: file.h:63
@ MUTT_STAT_MTIME
File/dir's mtime - last modified time.
Definition: file.h:64
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition: from.c:48
SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: globals.c:59
#define mutt_error(...)
Definition: logging2.h:87
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
#define mutt_perror(...)
Definition: logging2.h:88
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:209
#define MMDF_SEP
Definition: lib.h:61
int mutt_date_local_tz(time_t t)
Calculate the local timezone in seconds east of UTC.
Definition: date.c:209
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
#define PATH_MAX
Definition: mutt.h:41
void mx_alloc_memory(struct Mailbox *m)
Create storage for the emails.
Definition: mx.c:1236
MxOpenReturns
Return values for mbox_open()
Definition: mxapi.h:97
@ MX_OPEN_ERROR
Open failed with an error.
Definition: mxapi.h:99
@ MX_OPEN_ABORT
Open was aborted.
Definition: mxapi.h:100
@ MX_OPEN_OK
Open succeeded.
Definition: mxapi.h:98
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition: parse.c:1169
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition: lib.h:49
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:89
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:73
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:121
#define TAILQ_EMPTY(head)
Definition: queue.h:721
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
The envelope/body of an email.
Definition: email.h:37
struct Envelope * env
Envelope information.
Definition: email.h:66
int lines
How many lines in the body of this message?
Definition: email.h:60
struct Body * body
List of MIME parts.
Definition: email.h:67
LOFF_T offset
Where in the stream does this message begin?
Definition: email.h:69
int index
The absolute (unsorted) message number.
Definition: email.h:109
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:59
struct AddressList return_path
Return path for the Email.
Definition: envelope.h:58
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
struct timespec mtime
Time Mailbox was last changed.
Definition: mailbox.h:104
int msg_count
Total number of messages.
Definition: mailbox.h:88
int email_max
Number of pointers in emails.
Definition: mailbox.h:97
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
off_t size
Size of the Mailbox.
Definition: mailbox.h:84
bool verbose
Display status messages?
Definition: mailbox.h:114
struct timespec atime
File's last-access time.
Definition: lib.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_parse_mailbox()

static enum MxOpenReturns mbox_parse_mailbox ( struct Mailbox m)
static

Read a mailbox from disk.

Parameters
mMailbox
Return values
enumMxOpenReturns

Note that this function is also called when new mail is appended to the currently open folder, and NOT just when the mailbox is initially read.

Note
It is assumed that the mailbox being read has been locked before this routine gets called. Strange things could happen if it's not!

Definition at line 349 of file mbox.c.

350{
351 if (!m)
352 return MX_OPEN_ERROR;
353
355 if (!adata)
356 return MX_OPEN_ERROR;
357
358 struct stat st = { 0 };
359 char buf[8192], return_path[256];
360 struct Email *e_cur = NULL;
361 time_t t = 0;
362 int count = 0, lines = 0;
363 LOFF_T loc;
364 struct Progress *progress = NULL;
366
367 /* Save information about the folder at the time we opened it. */
368 if (stat(mailbox_path(m), &st) == -1)
369 {
371 goto fail;
372 }
373
374 m->size = st.st_size;
377
378 if (!m->readonly)
379 m->readonly = access(mailbox_path(m), W_OK) ? true : false;
380
381 if (m->verbose)
382 {
383 char msg[PATH_MAX] = { 0 };
384 snprintf(msg, sizeof(msg), _("Reading %s..."), mailbox_path(m));
385 progress = progress_new(msg, MUTT_PROGRESS_READ, 0);
386 }
387
388 loc = ftello(adata->fp);
389 while ((fgets(buf, sizeof(buf), adata->fp)) && !SigInt)
390 {
391 if (is_from(buf, return_path, sizeof(return_path), &t))
392 {
393 /* Save the Content-Length of the previous message */
394 if (count > 0)
395 {
396 struct Email *e = m->emails[m->msg_count - 1];
397 if (e->body->length < 0)
398 {
399 e->body->length = loc - e->body->offset - 1;
400 if (e->body->length < 0)
401 e->body->length = 0;
402 }
403 if (e->lines == 0)
404 e->lines = lines ? lines - 1 : 0;
405 }
406
407 count++;
408
409 if (m->verbose)
410 {
411 progress_update(progress, count, (int) (ftello(adata->fp) / (m->size / 100 + 1)));
412 }
413
414 if (m->msg_count == m->email_max)
416
417 m->emails[m->msg_count] = email_new();
418 e_cur = m->emails[m->msg_count];
419 e_cur->received = t - mutt_date_local_tz(t);
420 e_cur->offset = loc;
421 e_cur->index = m->msg_count;
422
423 e_cur->env = mutt_rfc822_read_header(adata->fp, e_cur, false, false);
424
425 /* if we know how long this message is, either just skip over the body,
426 * or if we don't know how many lines there are, count them now (this will
427 * save time by not having to search for the next message marker). */
428 if (e_cur->body->length > 0)
429 {
430 LOFF_T tmploc;
431
432 loc = ftello(adata->fp);
433
434 /* The test below avoids a potential integer overflow if the
435 * content-length is huge (thus necessarily invalid). */
436 tmploc = (e_cur->body->length < m->size) ? (loc + e_cur->body->length + 1) : -1;
437
438 if ((tmploc > 0) && (tmploc < m->size))
439 {
440 /* check to see if the content-length looks valid. we expect to
441 * to see a valid message separator at this point in the stream */
442 if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
443 !fgets(buf, sizeof(buf), adata->fp) || !mutt_str_startswith(buf, "From "))
444 {
445 mutt_debug(LL_DEBUG1, "bad content-length in message %d (cl=" OFF_T_FMT ")\n",
446 e_cur->index, e_cur->body->length);
447 mutt_debug(LL_DEBUG1, " LINE: %s", buf);
448 /* nope, return the previous position */
449 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
450 e_cur->body->length = -1;
451 }
452 }
453 else if (tmploc != m->size)
454 {
455 /* content-length would put us past the end of the file, so it
456 * must be wrong */
457 e_cur->body->length = -1;
458 }
459
460 if (e_cur->body->length != -1)
461 {
462 /* good content-length. check to see if we know how many lines
463 * are in this message. */
464 if (e_cur->lines == 0)
465 {
466 int cl = e_cur->body->length;
467
468 /* count the number of lines in this message */
469 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
470 while (cl-- > 0)
471 {
472 if (fgetc(adata->fp) == '\n')
473 e_cur->lines++;
474 }
475 }
476
477 /* return to the offset of the next message separator */
478 (void) mutt_file_seek(adata->fp, tmploc, SEEK_SET);
479 }
480 }
481
482 m->msg_count++;
483
484 if (TAILQ_EMPTY(&e_cur->env->return_path) && return_path[0])
485 {
486 mutt_addrlist_parse(&e_cur->env->return_path, return_path);
487 }
488
489 if (TAILQ_EMPTY(&e_cur->env->from))
490 mutt_addrlist_copy(&e_cur->env->from, &e_cur->env->return_path, false);
491
492 lines = 0;
493 }
494 else
495 {
496 lines++;
497 }
498
499 loc = ftello(adata->fp);
500 }
501
502 /* Only set the content-length of the previous message if we have read more
503 * than one message during _this_ invocation. If this routine is called
504 * when new mail is received, we need to make sure not to clobber what
505 * previously was the last message since the headers may be sorted. */
506 if (count > 0)
507 {
508 struct Email *e = m->emails[m->msg_count - 1];
509 if (e->body->length < 0)
510 {
511 e->body->length = ftello(adata->fp) - e->body->offset - 1;
512 if (e->body->length < 0)
513 e->body->length = 0;
514 }
515
516 if (e->lines == 0)
517 e->lines = lines ? lines - 1 : 0;
518 }
519
520 if (SigInt)
521 {
522 SigInt = false;
523 rc = MX_OPEN_ABORT;
524 goto fail; /* action aborted */
525 }
526
527 rc = MX_OPEN_OK;
528fail:
529 progress_free(&progress);
530 return rc;
531}
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reopen_mailbox()

static int reopen_mailbox ( struct Mailbox m)
static

Close and reopen a mailbox.

Parameters
mMailbox
Return values
>0Success, e.g. MX_STATUS_REOPENED, MX_STATUS_NEW_MAIL
-1Error

Definition at line 539 of file mbox.c.

540{
541 if (!m)
542 return -1;
543
545 if (!adata)
546 return -1;
547
548 bool (*cmp_headers)(const struct Email *, const struct Email *) = NULL;
549 struct Email **e_old = NULL;
550 int old_msg_count;
551 bool msg_mod = false;
552 int rc = -1;
553
554 /* silent operations */
555 m->verbose = false;
556
557 /* our heuristics require the old mailbox to be unsorted */
558 const enum SortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
559 if (c_sort != SORT_ORDER)
560 {
563 cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
564 }
565
566 e_old = NULL;
567 old_msg_count = 0;
568
569 /* simulate a close */
573 FREE(&m->v2r);
574 if (m->readonly)
575 {
576 for (int i = 0; i < m->msg_count; i++)
577 email_free(&(m->emails[i])); /* nothing to do! */
578 FREE(&m->emails);
579 }
580 else
581 {
582 /* save the old headers */
583 old_msg_count = m->msg_count;
584 e_old = m->emails;
585 m->emails = NULL;
586 }
587
588 m->email_max = 0; /* force allocation of new headers */
589 m->msg_count = 0;
590 m->vcount = 0;
591 m->msg_tagged = 0;
592 m->msg_deleted = 0;
593 m->msg_new = 0;
594 m->msg_unread = 0;
595 m->msg_flagged = 0;
596 m->changed = false;
597 m->id_hash = NULL;
598 m->subj_hash = NULL;
600
601 switch (m->type)
602 {
603 case MUTT_MBOX:
604 case MUTT_MMDF:
605 cmp_headers = email_cmp_strict;
606 mutt_file_fclose(&adata->fp);
607 adata->fp = mutt_file_fopen(mailbox_path(m), "r");
608 if (!adata->fp)
609 rc = -1;
610 else if (m->type == MUTT_MBOX)
611 rc = mbox_parse_mailbox(m);
612 else
613 rc = mmdf_parse_mailbox(m);
614 break;
615
616 default:
617 rc = -1;
618 break;
619 }
620
621 if (rc == -1)
622 {
623 /* free the old headers */
624 for (int i = 0; i < old_msg_count; i++)
625 email_free(&(e_old[i]));
626 FREE(&e_old);
627
628 m->verbose = true;
629 return -1;
630 }
631
632 mutt_file_touch_atime(fileno(adata->fp));
633
634 /* now try to recover the old flags */
635
636 if (!m->readonly)
637 {
638 for (int i = 0; i < m->msg_count; i++)
639 {
640 bool found = false;
641
642 /* some messages have been deleted, and new messages have been
643 * appended at the end; the heuristic is that old messages have then
644 * "advanced" towards the beginning of the folder, so we begin the
645 * search at index "i" */
646 int j;
647 for (j = i; j < old_msg_count; j++)
648 {
649 if (!e_old[j])
650 continue;
651 if (cmp_headers(m->emails[i], e_old[j]))
652 {
653 found = true;
654 break;
655 }
656 }
657 if (!found)
658 {
659 for (j = 0; (j < i) && (j < old_msg_count); j++)
660 {
661 if (!e_old[j])
662 continue;
663 if (cmp_headers(m->emails[i], e_old[j]))
664 {
665 found = true;
666 break;
667 }
668 }
669 }
670
671 if (found)
672 {
673 m->changed = true;
674 if (e_old[j]->changed)
675 {
676 /* Only update the flags if the old header was changed;
677 * otherwise, the header may have been modified externally,
678 * and we don't want to lose _those_ changes */
679 mutt_set_flag(m, m->emails[i], MUTT_FLAG, e_old[j]->flagged, true);
680 mutt_set_flag(m, m->emails[i], MUTT_REPLIED, e_old[j]->replied, true);
681 mutt_set_flag(m, m->emails[i], MUTT_OLD, e_old[j]->old, true);
682 mutt_set_flag(m, m->emails[i], MUTT_READ, e_old[j]->read, true);
683 }
684 mutt_set_flag(m, m->emails[i], MUTT_DELETE, e_old[j]->deleted, true);
685 mutt_set_flag(m, m->emails[i], MUTT_PURGE, e_old[j]->purge, true);
686 mutt_set_flag(m, m->emails[i], MUTT_TAG, e_old[j]->tagged, true);
687
688 /* we don't need this header any more */
689 email_free(&(e_old[j]));
690 }
691 }
692
693 /* free the remaining old headers */
694 for (int j = 0; j < old_msg_count; j++)
695 {
696 if (e_old[j])
697 {
698 email_free(&(e_old[j]));
699 msg_mod = true;
700 }
701 }
702 FREE(&e_old);
703 }
704
706 m->verbose = true;
707
708 return (m->changed || msg_mod) ? MX_STATUS_REOPENED : MX_STATUS_NEW_MAIL;
709}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
bool email_cmp_strict(const struct Email *e1, const struct Email *e2)
Strictly compare message emails.
Definition: email.c:100
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
void mutt_file_touch_atime(int fd)
Set the access time to current time.
Definition: file.c:1069
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:52
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:223
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:176
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition: mailbox.h:177
static enum MxOpenReturns mbox_parse_mailbox(struct Mailbox *m)
Read a mailbox from disk.
Definition: mbox.c:349
static enum MxOpenReturns mmdf_parse_mailbox(struct Mailbox *m)
Read a mailbox in MMDF format.
Definition: mbox.c:178
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:81
@ MUTT_OLD
Old messages.
Definition: mutt.h:79
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition: mutt.h:85
@ MUTT_TAG
Tagged messages.
Definition: mutt.h:88
@ MUTT_FLAG
Flagged messages.
Definition: mutt.h:87
@ MUTT_DELETE
Messages to be deleted.
Definition: mutt.h:83
@ MUTT_REPLIED
Messages that have been replied to.
Definition: mutt.h:80
void mutt_make_label_hash(struct Mailbox *m)
Create a Hash Table to store the labels.
Definition: mutt_header.c:417
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition: mxapi.h:89
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition: mxapi.h:87
SortType
Methods for sorting.
Definition: sort2.h:38
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:44
bool changed
Email has been edited.
Definition: email.h:75
int vcount
The number of virtual messages.
Definition: mailbox.h:99
bool changed
Mailbox has been modified.
Definition: mailbox.h:110
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:98
int msg_new
Number of new messages.
Definition: mailbox.h:92
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition: mailbox.h:124
struct HashTable * id_hash
Hash Table: "message-id" -> Email.
Definition: mailbox.h:123
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:93
struct HashTable * label_hash
Hash Table: "x-labels" -> Email.
Definition: mailbox.h:125
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:90
int msg_tagged
How many messages are tagged?
Definition: mailbox.h:94
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:310
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_has_new()

static bool mbox_has_new ( struct Mailbox m)
static

Does the mailbox have new mail.

Parameters
mMailbox
Return values
trueThe mailbox has at least 1 new messages (not old)
falseotherwise

Definition at line 717 of file mbox.c.

718{
719 for (int i = 0; i < m->msg_count; i++)
720 {
721 struct Email *e = m->emails[i];
722 if (!e)
723 break;
724 if (!e->deleted && !e->read && !e->old)
725 return true;
726 }
727 return false;
728}
bool read
Email is read.
Definition: email.h:48
bool old
Email is seen, but unread.
Definition: email.h:47
bool deleted
Email is deleted.
Definition: email.h:76
+ Here is the caller graph for this function:

◆ fseek_last_message()

static int fseek_last_message ( FILE *  fp)
static

Find the last message in the file.

Parameters
fpFile to search
Return values
0Success
-1No message found

Definition at line 736 of file mbox.c.

737{
738 LOFF_T pos;
739 char buf[BUFSIZ + 7] = { 0 }; // 7 for "\n\nFrom "
740 size_t bytes_read;
741
742 if (!mutt_file_seek(fp, 0, SEEK_END))
743 {
744 return -1;
745 }
746 pos = ftello(fp);
747
748 /* Set 'bytes_read' to the size of the last, probably partial, buf;
749 * 0 < 'bytes_read' <= 'BUFSIZ'. */
750 bytes_read = pos % BUFSIZ;
751 if (bytes_read == 0)
752 bytes_read = BUFSIZ;
753 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
754 * reads will be on block boundaries, which might increase efficiency. */
755 while ((pos -= bytes_read) >= 0)
756 {
757 /* we save in the buf at the end the first 7 chars from the last read */
758 memcpy(buf + BUFSIZ, buf, 7);
759 if (!mutt_file_seek(fp, pos, SEEK_SET))
760 {
761 return -1;
762 }
763 bytes_read = fread(buf, sizeof(char), bytes_read, fp);
764 if (bytes_read == 0)
765 return -1;
766 /* 'i' is Index into 'buf' for scanning. */
767 for (int i = bytes_read; i >= 0; i--)
768 {
769 if (mutt_str_startswith(buf + i, "\n\nFrom "))
770 { /* found it - go to the beginning of the From */
771 if (!mutt_file_seek(fp, pos + i + 2, SEEK_SET))
772 {
773 return -1;
774 }
775 return 0;
776 }
777 }
778 bytes_read = BUFSIZ;
779 }
780
781 /* here we are at the beginning of the file */
782 if (mutt_str_startswith(buf, "From "))
783 {
784 if (!mutt_file_seek(fp, 0, SEEK_SET))
785 {
786 return -1;
787 }
788 return 0;
789 }
790
791 return -1;
792}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_last_status_new()

static bool test_last_status_new ( FILE *  fp)
static

Is the last message new.

Parameters
fpFile to check
Return values
trueThe last message is new

Definition at line 799 of file mbox.c.

800{
801 struct Email *e = NULL;
802 struct Envelope *tmp_envelope = NULL;
803 bool rc = false;
804
805 if (fseek_last_message(fp) == -1)
806 return false;
807
808 e = email_new();
809 tmp_envelope = mutt_rfc822_read_header(fp, e, false, false);
810 if (!e->read && !e->old)
811 rc = true;
812
813 mutt_env_free(&tmp_envelope);
814 email_free(&e);
815
816 return rc;
817}
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:97
static int fseek_last_message(FILE *fp)
Find the last message in the file.
Definition: mbox.c:736
The header of an Email.
Definition: envelope.h:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_test_new_folder()

bool mbox_test_new_folder ( const char *  path)

Test if an mbox or mmdf mailbox has new mail.

Parameters
pathPath to the mailbox
Return values
trueThe folder contains new mail

Definition at line 824 of file mbox.c.

825{
826 bool rc = false;
827
828 enum MailboxType type = mx_path_probe(path);
829
830 if ((type != MUTT_MBOX) && (type != MUTT_MMDF))
831 return false;
832
833 FILE *fp = fopen(path, "rb");
834 if (fp)
835 {
836 rc = test_last_status_new(fp);
837 mutt_file_fclose(&fp);
838 }
839
840 return rc;
841}
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
static bool test_last_status_new(FILE *fp)
Is the last message new.
Definition: mbox.c:799
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1344
+ Here is the call graph for this function:

◆ mbox_reset_atime()

void mbox_reset_atime ( struct Mailbox m,
struct stat *  st 
)

Reset the access time on the mailbox file.

Parameters
mMailbox
stTimestamp

if mailbox has at least 1 new message, sets mtime > atime of mailbox so mailbox check reports new mail

Definition at line 851 of file mbox.c.

852{
853 struct utimbuf utimebuf;
854 struct stat st2 = { 0 };
855
856 if (!st)
857 {
858 if (stat(mailbox_path(m), &st2) < 0)
859 return;
860 st = &st2;
861 }
862
863 utimebuf.actime = st->st_atime;
864 utimebuf.modtime = st->st_mtime;
865
866 /* When $mbox_check_recent is set, existing new mail is ignored, so do not
867 * reset the atime to mtime-1 to signal new mail. */
868 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
869 if (!c_mail_check_recent && (utimebuf.actime >= utimebuf.modtime) && mbox_has_new(m))
870 {
871 utimebuf.actime = utimebuf.modtime - 1;
872 }
873
874 utime(mailbox_path(m), &utimebuf);
875}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
static bool mbox_has_new(struct Mailbox *m)
Does the mailbox have new mail.
Definition: mbox.c:717
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_open_readwrite()

static FILE * mbox_open_readwrite ( struct Mailbox m)
static

Open an mbox read-write.

Parameters
mMailbox
Return values
ptrFILE handle

This function ensures that the FILE and readonly flag are changed atomically.

Definition at line 907 of file mbox.c.

908{
909 FILE *fp = fopen(mailbox_path(m), "r+");
910 if (fp)
911 m->readonly = false;
912 return fp;
913}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbox_open_readonly()

static FILE * mbox_open_readonly ( struct Mailbox m)
static

Open an mbox read-only.

Parameters
mMailbox
Return values
ptrFILE handle

This function ensures that the FILE and readonly flag are changed atomically.

Definition at line 922 of file mbox.c.

923{
924 FILE *fp = fopen(mailbox_path(m), "r");
925 if (fp)
926 m->readonly = true;
927 return fp;
928}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: