NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
pop.c File Reference

POP network mailbox. More...

#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "lib.h"
#include "bcache/lib.h"
#include "ncrypt/lib.h"
#include "progress/lib.h"
#include "question/lib.h"
#include "adata.h"
#include "edata.h"
#include "hook.h"
#include "mutt_account.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "mutt_socket.h"
#include "muttlib.h"
#include "mx.h"
#include <libintl.h>
#include "hcache/lib.h"
+ Include dependency graph for pop.c:

Go to the source code of this file.

Macros

#define HC_FNAME   "neomutt" /* filename for hcache as POP lacks paths */
 
#define HC_FEXT   "hcache" /* extension for hcache as POP lacks paths */
 

Functions

static const char * cache_id (const char *id)
 Make a message-cache-compatible id. More...
 
static int fetch_message (const char *line, void *data)
 write line to file - Implements pop_fetch_t More...
 
static int pop_read_header (struct PopAccountData *adata, struct Email *e)
 Read header. More...
 
static int fetch_uidl (const char *line, void *data)
 parse UIDL - Implements pop_fetch_t More...
 
static int msg_cache_check (const char *id, struct BodyCache *bcache, void *data)
 Check the Body Cache for an ID - Implements bcache_list_t. More...
 
static void pop_hcache_namer (const char *path, struct Buffer *dest)
 Create a header cache filename for a POP mailbox - Implements hcache_namer_t. More...
 
static struct HeaderCachepop_hcache_open (struct PopAccountData *adata, const char *path)
 Open the header cache. More...
 
static int pop_fetch_headers (struct Mailbox *m)
 Read headers. More...
 
static void pop_clear_cache (struct PopAccountData *adata)
 delete all cached messages More...
 
void pop_fetch_mail (void)
 Fetch messages and save them in $spool_file. More...
 
static bool pop_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 pop_ac_add (struct Account *a, struct Mailbox *m)
 Add a Mailbox to an Account - Implements MxOps::ac_add() -. More...
 
static enum MxOpenReturns pop_mbox_open (struct Mailbox *m)
 Open a Mailbox - Implements MxOps::mbox_open() -Fetch only headers. More...
 
static enum MxStatus pop_mbox_check (struct Mailbox *m)
 Check for new mail - Implements MxOps::mbox_check() -. More...
 
static enum MxStatus pop_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -Update POP mailbox, delete messages from server. More...
 
static enum MxStatus pop_mbox_close (struct Mailbox *m)
 Close a Mailbox - Implements MxOps::mbox_close() -. More...
 
static bool pop_msg_open (struct Mailbox *m, struct Message *msg, int msgno)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -. More...
 
static int pop_msg_close (struct Mailbox *m, struct Message *msg)
 Close an email - Implements MxOps::msg_close() -. More...
 
static int pop_msg_save_hcache (struct Mailbox *m, struct Email *e)
 Save message to the header cache - Implements MxOps::msg_save_hcache() -. More...
 
enum MailboxType pop_path_probe (const char *path, const struct stat *st)
 Is this a POP Mailbox? - Implements MxOps::path_probe() -. More...
 
static int pop_path_canon (char *buf, size_t buflen)
 Canonicalise a Mailbox path - Implements MxOps::path_canon() -. More...
 
static int pop_path_pretty (char *buf, size_t buflen, const char *folder)
 Abbreviate a Mailbox path - Implements MxOps::path_pretty() -. More...
 
static int pop_path_parent (char *buf, size_t buflen)
 Find the parent of a Mailbox path - Implements MxOps::path_parent() -. More...
 

Variables

struct MxOps MxPopOps
 POP Mailbox - Implements MxOps -. More...
 

Detailed Description

POP network mailbox.

Authors
  • Vsevolod Volkov
  • Rocco Rutte
  • Richard Russon

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

Macro Definition Documentation

◆ HC_FNAME

#define HC_FNAME   "neomutt" /* filename for hcache as POP lacks paths */

Definition at line 71 of file pop.c.

◆ HC_FEXT

#define HC_FEXT   "hcache" /* extension for hcache as POP lacks paths */

Definition at line 72 of file pop.c.

Function Documentation

◆ cache_id()

static const char* cache_id ( const char *  id)
static

Make a message-cache-compatible id.

Parameters
idPOP message id
Return values
ptrSanitised string

The POP message id may contain '/' and other awkward characters.

Note
This function returns a pointer to a static buffer.

Definition at line 83 of file pop.c.

84 {
85  static char clean[128];
86  mutt_str_copy(clean, id, sizeof(clean));
87  mutt_file_sanitize_filename(clean, true);
88  return clean;
89 }
void mutt_file_sanitize_filename(char *path, bool slash)
Replace unsafe characters in a filename.
Definition: file.c:618
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:749
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetch_message()

static int fetch_message ( const char *  line,
void *  data 
)
static

write line to file - Implements pop_fetch_t

Parameters
lineString to write
dataFILE pointer to write to
Return values
0Success
-1Failure

Definition at line 98 of file pop.c.

99 {
100  FILE *fp = data;
101 
102  fputs(line, fp);
103  if (fputc('\n', fp) == EOF)
104  return -1;
105 
106  return 0;
107 }
+ Here is the caller graph for this function:

◆ pop_read_header()

static int pop_read_header ( struct PopAccountData adata,
struct Email e 
)
static

Read header.

Parameters
adataPOP Account data
eEmail
Return values
0Success
-1Connection lost
-2Invalid command or execution error
-3Error writing to tempfile

Definition at line 118 of file pop.c.

119 {
120  FILE *fp = mutt_file_mkstemp();
121  if (!fp)
122  {
123  mutt_perror(_("Can't create temporary file"));
124  return -3;
125  }
126 
127  int index = 0;
128  size_t length = 0;
129  char buf[1024];
130 
131  struct PopEmailData *edata = pop_edata_get(e);
132 
133  snprintf(buf, sizeof(buf), "LIST %d\r\n", edata->refno);
134  int rc = pop_query(adata, buf, sizeof(buf));
135  if (rc == 0)
136  {
137  sscanf(buf, "+OK %d %zu", &index, &length);
138 
139  snprintf(buf, sizeof(buf), "TOP %d 0\r\n", edata->refno);
140  rc = pop_fetch_data(adata, buf, NULL, fetch_message, fp);
141 
142  if (adata->cmd_top == 2)
143  {
144  if (rc == 0)
145  {
146  adata->cmd_top = 1;
147 
148  mutt_debug(LL_DEBUG1, "set TOP capability\n");
149  }
150 
151  if (rc == -2)
152  {
153  adata->cmd_top = 0;
154 
155  mutt_debug(LL_DEBUG1, "unset TOP capability\n");
156  snprintf(adata->err_msg, sizeof(adata->err_msg), "%s",
157  _("Command TOP is not supported by server"));
158  }
159  }
160  }
161 
162  switch (rc)
163  {
164  case 0:
165  {
166  rewind(fp);
167  e->env = mutt_rfc822_read_header(fp, e, false, false);
168  e->body->length = length - e->body->offset + 1;
169  rewind(fp);
170  while (!feof(fp))
171  {
172  e->body->length--;
173  fgets(buf, sizeof(buf), fp);
174  }
175  break;
176  }
177  case -2:
178  {
179  mutt_error("%s", adata->err_msg);
180  break;
181  }
182  case -3:
183  {
184  mutt_error(_("Can't write header to temporary file"));
185  break;
186  }
187  }
188 
189  mutt_file_fclose(&fp);
190  return rc;
191 }
#define pop_query(adata, buf, buflen)
Definition: private.h:106
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
struct Body * body
List of MIME parts.
Definition: email.h:91
#define mutt_error(...)
Definition: logging.h:88
LOFF_T offset
offset where the actual data begins
Definition: body.h:44
#define _(a)
Definition: message.h:28
int refno
Message number on server.
Definition: edata.h:34
#define mutt_perror(...)
Definition: logging.h:89
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
struct Envelope * env
Envelope information.
Definition: email.h:90
int pop_fetch_data(struct PopAccountData *adata, const char *query, struct Progress *progress, pop_fetch_t callback, void *data)
Read Headers with callback function.
Definition: lib.c:506
LOFF_T length
length (in bytes) of attachment
Definition: body.h:45
#define mutt_file_mkstemp()
Definition: file.h:108
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
unsigned int cmd_top
optional command TOP
Definition: adata.h:46
POP-specific Email data -.
Definition: edata.h:31
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
Log at debug level 1.
Definition: logging.h:40
void * edata
Driver-specific data.
Definition: email.h:111
int index
The absolute (unsorted) message number.
Definition: email.h:86
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
parses an RFC822 header
Definition: parse.c:1124
static int fetch_message(const char *line, void *data)
write line to file - Implements pop_fetch_t
Definition: pop.c:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetch_uidl()

static int fetch_uidl ( const char *  line,
void *  data 
)
static

parse UIDL - Implements pop_fetch_t

Parameters
lineString to parse
dataMailbox
Return values
0Success
-1Failure

Definition at line 200 of file pop.c.

201 {
202  struct Mailbox *m = data;
203  struct PopAccountData *adata = pop_adata_get(m);
204  char *endp = NULL;
205 
206  errno = 0;
207  int index = strtol(line, &endp, 10);
208  if (errno)
209  return -1;
210  while (*endp == ' ')
211  endp++;
212  line = endp;
213 
214  /* uid must be at least be 1 byte */
215  if (strlen(line) == 0)
216  return -1;
217 
218  int i;
219  for (i = 0; i < m->msg_count; i++)
220  {
221  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
222  if (mutt_str_equal(line, edata->uid))
223  break;
224  }
225 
226  if (i == m->msg_count)
227  {
228  mutt_debug(LL_DEBUG1, "new header %d %s\n", index, line);
229 
230  if (i >= m->email_max)
231  mx_alloc_memory(m);
232 
233  m->msg_count++;
234  m->emails[i] = email_new();
235 
236  m->emails[i]->edata = pop_edata_new(line);
238  }
239  else if (m->emails[i]->index != index - 1)
240  adata->clear_cache = true;
241 
242  m->emails[i]->index = index - 1;
243 
244  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
245  edata->refno = index;
246 
247  return 0;
248 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
void pop_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free()
Definition: edata.c:41
int msg_count
Total number of messages.
Definition: mailbox.h:91
void mx_alloc_memory(struct Mailbox *m)
Create storage for the emails.
Definition: mx.c:1212
int refno
Message number on server.
Definition: edata.h:34
struct PopEmailData * pop_edata_new(const char *uid)
Create a new PopEmailData for an email.
Definition: edata.c:53
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:64
struct Email * email_new(void)
Create a new Email.
Definition: email.c:78
const char * uid
Definition: edata.h:33
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
A mailbox.
Definition: mailbox.h:81
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
bool clear_cache
Definition: adata.h:49
void(* edata_free)(void **ptr)
Free the private data attached to the Email.
Definition: email.h:121
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
POP-specific Email data -.
Definition: edata.h:31
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
Log at debug level 1.
Definition: logging.h:40
void * edata
Driver-specific data.
Definition: email.h:111
int index
The absolute (unsorted) message number.
Definition: email.h:86
POP-specific Account data -.
Definition: adata.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msg_cache_check()

static int msg_cache_check ( const char *  id,
struct BodyCache bcache,
void *  data 
)
static

Check the Body Cache for an ID - Implements bcache_list_t.

Definition at line 253 of file pop.c.

254 {
255  struct Mailbox *m = data;
256  if (!m)
257  return -1;
258 
259  struct PopAccountData *adata = pop_adata_get(m);
260  if (!adata)
261  return -1;
262 
263 #ifdef USE_HCACHE
264  /* keep hcache file if hcache == bcache */
265  if (strcmp(HC_FNAME "." HC_FEXT, id) == 0)
266  return 0;
267 #endif
268 
269  for (int i = 0; i < m->msg_count; i++)
270  {
271  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
272  /* if the id we get is known for a header: done (i.e. keep in cache) */
273  if (edata->uid && mutt_str_equal(edata->uid, id))
274  return 0;
275  }
276 
277  /* message not found in context -> remove it from cache
278  * return the result of bcache, so we stop upon its first error */
279  return mutt_bcache_del(bcache, cache_id(id));
280 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
int msg_count
Total number of messages.
Definition: mailbox.h:91
#define HC_FNAME
Definition: pop.c:71
static const char * cache_id(const char *id)
Make a message-cache-compatible id.
Definition: pop.c:83
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:64
const char * uid
Definition: edata.h:33
A mailbox.
Definition: mailbox.h:81
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
int mutt_bcache_del(struct BodyCache *bcache, const char *id)
Delete a file from the Body Cache.
Definition: bcache.c:264
POP-specific Email data -.
Definition: edata.h:31
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
#define HC_FEXT
Definition: pop.c:72
void * edata
Driver-specific data.
Definition: email.h:111
POP-specific Account data -.
Definition: adata.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_hcache_namer()

static void pop_hcache_namer ( const char *  path,
struct Buffer dest 
)
static

Create a header cache filename for a POP mailbox - Implements hcache_namer_t.

Definition at line 286 of file pop.c.

287 {
288  mutt_buffer_printf(dest, "%s." HC_FEXT, path);
289 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define HC_FEXT
Definition: pop.c:72
char * path
Path of Email (for local Mailboxes)
Definition: email.h:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_hcache_open()

static struct HeaderCache* pop_hcache_open ( struct PopAccountData adata,
const char *  path 
)
static

Open the header cache.

Parameters
adataPOP Account data
pathPath to the mailbox
Return values
ptrHeader cache

Definition at line 297 of file pop.c.

298 {
299  const char *const c_header_cache =
300  cs_subset_path(NeoMutt->sub, "header_cache");
301  if (!adata || !adata->conn)
302  return mutt_hcache_open(c_header_cache, path, NULL);
303 
304  struct Url url = { 0 };
305  char p[1024];
306 
307  mutt_account_tourl(&adata->conn->account, &url);
308  url.path = HC_FNAME;
309  url_tostring(&url, p, sizeof(p), U_PATH);
310  return mutt_hcache_open(c_header_cache, p, pop_hcache_namer);
311 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:40
#define HC_FNAME
Definition: pop.c:71
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:68
struct Connection * conn
Definition: adata.h:38
struct HeaderCache * mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:332
Container for Accounts, Notifications.
Definition: neomutt.h:36
void mutt_account_tourl(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:79
int url_tostring(struct Url *url, char *dest, size_t len, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:418
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
char * path
Path.
Definition: url.h:75
static void pop_hcache_namer(const char *path, struct Buffer *dest)
Create a header cache filename for a POP mailbox - Implements hcache_namer_t.
Definition: pop.c:286
char * path
Path of Email (for local Mailboxes)
Definition: email.h:92
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define U_PATH
Definition: url.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_fetch_headers()

static int pop_fetch_headers ( struct Mailbox m)
static

Read headers.

Parameters
mMailbox
Return values
0Success
-1Connection lost
-2Invalid command or execution error
-3Error writing to tempfile

Definition at line 322 of file pop.c.

323 {
324  if (!m)
325  return -1;
326 
327  struct PopAccountData *adata = pop_adata_get(m);
328  struct Progress *progress = NULL;
329 
330 #ifdef USE_HCACHE
331  struct HeaderCache *hc = pop_hcache_open(adata, mailbox_path(m));
332 #endif
333 
334  adata->check_time = mutt_date_epoch();
335  adata->clear_cache = false;
336 
337  for (int i = 0; i < m->msg_count; i++)
338  {
339  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
340  edata->refno = -1;
341  }
342 
343  const int old_count = m->msg_count;
344  int rc = pop_fetch_data(adata, "UIDL\r\n", NULL, fetch_uidl, m);
345  const int new_count = m->msg_count;
346  m->msg_count = old_count;
347 
348  if (adata->cmd_uidl == 2)
349  {
350  if (rc == 0)
351  {
352  adata->cmd_uidl = 1;
353 
354  mutt_debug(LL_DEBUG1, "set UIDL capability\n");
355  }
356 
357  if ((rc == -2) && (adata->cmd_uidl == 2))
358  {
359  adata->cmd_uidl = 0;
360 
361  mutt_debug(LL_DEBUG1, "unset UIDL capability\n");
362  snprintf(adata->err_msg, sizeof(adata->err_msg), "%s",
363  _("Command UIDL is not supported by server"));
364  }
365  }
366 
367  if (m->verbose)
368  {
369  progress = progress_new(_("Fetching message headers..."),
370  MUTT_PROGRESS_READ, new_count - old_count);
371  }
372 
373  if (rc == 0)
374  {
375  int i, deleted;
376  for (i = 0, deleted = 0; i < old_count; i++)
377  {
378  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
379  if (edata->refno == -1)
380  {
381  m->emails[i]->deleted = true;
382  deleted++;
383  }
384  }
385  if (deleted > 0)
386  {
387  mutt_error(
388  ngettext("%d message has been lost. Try reopening the mailbox.",
389  "%d messages have been lost. Try reopening the mailbox.", deleted),
390  deleted);
391  }
392 
393  bool hcached = false;
394  for (i = old_count; i < new_count; i++)
395  {
396  if (m->verbose)
397  progress_update(progress, i + 1 - old_count, -1);
398  struct PopEmailData *edata = pop_edata_get(m->emails[i]);
399 #ifdef USE_HCACHE
400  struct HCacheEntry hce = mutt_hcache_fetch(hc, edata->uid, strlen(edata->uid), 0);
401  if (hce.email)
402  {
403  /* Detach the private data */
404  m->emails[i]->edata = NULL;
405 
406  int index = m->emails[i]->index;
407  /* - POP dynamically numbers headers and relies on e->refno
408  * to map messages; so restore header and overwrite restored
409  * refno with current refno, same for index
410  * - e->data needs to a separate pointer as it's driver-specific
411  * data freed separately elsewhere
412  * (the old e->data should point inside a malloc'd block from
413  * hcache so there shouldn't be a memleak here) */
414  email_free(&m->emails[i]);
415  m->emails[i] = hce.email;
416  m->emails[i]->index = index;
417 
418  /* Reattach the private data */
419  m->emails[i]->edata = edata;
421  rc = 0;
422  hcached = true;
423  }
424  else
425 #endif
426  if ((rc = pop_read_header(adata, m->emails[i])) < 0)
427  break;
428 #ifdef USE_HCACHE
429  else
430  {
431  mutt_hcache_store(hc, edata->uid, strlen(edata->uid), m->emails[i], 0);
432  }
433 #endif
434 
435  /* faked support for flags works like this:
436  * - if 'hcached' is true, we have the message in our hcache:
437  * - if we also have a body: read
438  * - if we don't have a body: old
439  * (if $mark_old is set which is maybe wrong as
440  * $mark_old should be considered for syncing the
441  * folder and not when opening it XXX)
442  * - if 'hcached' is false, we don't have the message in our hcache:
443  * - if we also have a body: read
444  * - if we don't have a body: new */
445  const bool bcached =
446  (mutt_bcache_exists(adata->bcache, cache_id(edata->uid)) == 0);
447  m->emails[i]->old = false;
448  m->emails[i]->read = false;
449  if (hcached)
450  {
451  const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
452  if (bcached)
453  m->emails[i]->read = true;
454  else if (c_mark_old)
455  m->emails[i]->old = true;
456  }
457  else
458  {
459  if (bcached)
460  m->emails[i]->read = true;
461  }
462 
463  m->msg_count++;
464  }
465  }
466  progress_free(&progress);
467 
468 #ifdef USE_HCACHE
469  mutt_hcache_close(hc);
470 #endif
471 
472  if (rc < 0)
473  {
474  for (int i = m->msg_count; i < new_count; i++)
475  email_free(&m->emails[i]);
476  return rc;
477  }
478 
479  /* after putting the result into our structures,
480  * clean up cache, i.e. wipe messages deleted outside
481  * the availability of our cache */
482  const bool c_message_cache_clean =
483  cs_subset_bool(NeoMutt->sub, "message_cache_clean");
484  if (c_message_cache_clean)
486 
488  return new_count - old_count;
489 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
void pop_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free()
Definition: edata.c:41
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
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
Wrapper for Email retrieved from the header cache.
Definition: lib.h:95
#define mutt_error(...)
Definition: logging.h:88
struct BodyCache * bcache
body cache
Definition: adata.h:55
header cache structure
Definition: lib.h:84
#define _(a)
Definition: message.h:28
Progress tracks elements, according to $read_inc
Definition: lib.h:46
static const char * cache_id(const char *id)
Make a message-cache-compatible id.
Definition: pop.c:83
int refno
Message number on server.
Definition: edata.h:34
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:64
Container for Accounts, Notifications.
Definition: neomutt.h:36
A Progress Bar.
Definition: progress.c:47
static int msg_cache_check(const char *id, struct BodyCache *bcache, void *data)
Check the Body Cache for an ID - Implements bcache_list_t.
Definition: pop.c:253
int mutt_bcache_list(struct BodyCache *bcache, bcache_list_t want_id, void *data)
Find matching entries in the Body Cache.
Definition: bcache.c:329
static struct HeaderCache * pop_hcache_open(struct PopAccountData *adata, const char *path)
Open the header cache.
Definition: pop.c:297
bool read
Email is read.
Definition: email.h:51
bool old
Email is seen, but unread.
Definition: email.h:50
const char * uid
Definition: edata.h:33
int mutt_bcache_exists(struct BodyCache *bcache, const char *id)
Check if a file exists in the Body Cache.
Definition: bcache.c:287
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:112
int pop_fetch_data(struct PopAccountData *adata, const char *query, struct Progress *progress, pop_fetch_t callback, void *data)
Read Headers with callback function.
Definition: lib.c:506
time_t check_time
Definition: adata.h:51
static int fetch_uidl(const char *line, void *data)
parse UIDL - Implements pop_fetch_t
Definition: pop.c:200
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
bool clear_cache
Definition: adata.h:49
unsigned int cmd_uidl
optional command UIDL
Definition: adata.h:45
void(* edata_free)(void **ptr)
Free the private data attached to the Email.
Definition: email.h:121
static int pop_read_header(struct PopAccountData *adata, struct Email *e)
Read header.
Definition: pop.c:118
bool verbose
Display status messages?
Definition: mailbox.h:118
int mutt_hcache_store(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:556
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:228
POP-specific Email data -.
Definition: edata.h:31
void progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:175
void mutt_hcache_close(struct HeaderCache *hc)
Multiplexor for StoreOps::close.
Definition: hcache.c:435
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
Log at debug level 1.
Definition: logging.h:40
bool deleted
Email is deleted.
Definition: email.h:45
void * edata
Driver-specific data.
Definition: email.h:111
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:246
int index
The absolute (unsorted) message number.
Definition: email.h:86
struct HCacheEntry mutt_hcache_fetch(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition: hcache.c:461
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
struct Email * email
Retrieved email.
Definition: lib.h:99
POP-specific Account data -.
Definition: adata.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_clear_cache()

static void pop_clear_cache ( struct PopAccountData adata)
static

delete all cached messages

Parameters
adataPOP Account data

Definition at line 495 of file pop.c.

496 {
497  if (!adata->clear_cache)
498  return;
499 
500  mutt_debug(LL_DEBUG1, "delete cached messages\n");
501 
502  for (int i = 0; i < POP_CACHE_LEN; i++)
503  {
504  if (adata->cache[i].path)
505  {
506  unlink(adata->cache[i].path);
507  FREE(&adata->cache[i].path);
508  }
509  }
510 }
char * path
Definition: private.h:71
struct PopCache cache[POP_CACHE_LEN]
Definition: adata.h:57
#define POP_CACHE_LEN
Definition: private.h:39
bool clear_cache
Definition: adata.h:49
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ pop_fetch_mail()

void pop_fetch_mail ( void  )

Fetch messages and save them in $spool_file.

Definition at line 515 of file pop.c.

516 {
517  const char *const c_pop_host = cs_subset_string(NeoMutt->sub, "pop_host");
518  if (!c_pop_host)
519  {
520  mutt_error(_("POP host is not defined"));
521  return;
522  }
523 
524  char buf[1024];
525  char msgbuf[128];
526  int last = 0, msgs, bytes, rset = 0, ret;
527  struct ConnAccount cac = { { 0 } };
528 
529  char *p = mutt_mem_calloc(strlen(c_pop_host) + 7, sizeof(char));
530  char *url = p;
531  if (url_check_scheme(c_pop_host) == U_UNKNOWN)
532  {
533  strcpy(url, "pop://");
534  p = strchr(url, '\0');
535  }
536  strcpy(p, c_pop_host);
537 
538  ret = pop_parse_path(url, &cac);
539  FREE(&url);
540  if (ret)
541  {
542  mutt_error(_("%s is an invalid POP path"), c_pop_host);
543  return;
544  }
545 
546  struct Connection *conn = mutt_conn_find(&cac);
547  if (!conn)
548  return;
549 
550  struct PopAccountData *adata = pop_adata_new();
551  adata->conn = conn;
552 
553  if (pop_open_connection(adata) < 0)
554  {
555  //XXX mutt_socket_free(adata->conn);
556  pop_adata_free((void **) &adata);
557  return;
558  }
559 
560  mutt_message(_("Checking for new messages..."));
561 
562  /* find out how many messages are in the mailbox. */
563  mutt_str_copy(buf, "STAT\r\n", sizeof(buf));
564  ret = pop_query(adata, buf, sizeof(buf));
565  if (ret == -1)
566  goto fail;
567  if (ret == -2)
568  {
569  mutt_error("%s", adata->err_msg);
570  goto finish;
571  }
572 
573  sscanf(buf, "+OK %d %d", &msgs, &bytes);
574 
575  /* only get unread messages */
576  const bool c_pop_last = cs_subset_bool(NeoMutt->sub, "pop_last");
577  if ((msgs > 0) && c_pop_last)
578  {
579  mutt_str_copy(buf, "LAST\r\n", sizeof(buf));
580  ret = pop_query(adata, buf, sizeof(buf));
581  if (ret == -1)
582  goto fail;
583  if (ret == 0)
584  sscanf(buf, "+OK %d", &last);
585  }
586 
587  if (msgs <= last)
588  {
589  mutt_message(_("No new mail in POP mailbox"));
590  goto finish;
591  }
592 
593  const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file");
594  struct Mailbox *m_spool = mx_path_resolve(c_spool_file);
595 
596  if (!mx_mbox_open(m_spool, MUTT_OPEN_NO_FLAGS))
597  {
598  mailbox_free(&m_spool);
599  goto finish;
600  }
601  bool old_append = m_spool->append;
602  m_spool->append = true;
603 
604  const enum QuadOption c_pop_delete =
605  cs_subset_quad(NeoMutt->sub, "pop_delete");
606  enum QuadOption delanswer =
607  query_quadoption(c_pop_delete, _("Delete messages from server?"));
608 
609  snprintf(msgbuf, sizeof(msgbuf),
610  ngettext("Reading new messages (%d byte)...",
611  "Reading new messages (%d bytes)...", bytes),
612  bytes);
613  mutt_message("%s", msgbuf);
614 
615  for (int i = last + 1; i <= msgs; i++)
616  {
617  struct Message *msg = mx_msg_open_new(m_spool, NULL, MUTT_ADD_FROM);
618  if (msg)
619  {
620  snprintf(buf, sizeof(buf), "RETR %d\r\n", i);
621  ret = pop_fetch_data(adata, buf, NULL, fetch_message, msg->fp);
622  if (ret == -3)
623  rset = 1;
624 
625  if ((ret == 0) && (mx_msg_commit(m_spool, msg) != 0))
626  {
627  rset = 1;
628  ret = -3;
629  }
630 
631  mx_msg_close(m_spool, &msg);
632  }
633  else
634  {
635  ret = -3;
636  }
637 
638  if ((ret == 0) && (delanswer == MUTT_YES))
639  {
640  /* delete the message on the server */
641  snprintf(buf, sizeof(buf), "DELE %d\r\n", i);
642  ret = pop_query(adata, buf, sizeof(buf));
643  }
644 
645  if (ret == -1)
646  {
647  m_spool->append = old_append;
648  mx_mbox_close(m_spool);
649  goto fail;
650  }
651  if (ret == -2)
652  {
653  mutt_error("%s", adata->err_msg);
654  break;
655  }
656  if (ret == -3)
657  {
658  mutt_error(_("Error while writing mailbox"));
659  break;
660  }
661 
662  /* L10N: The plural is picked by the second numerical argument, i.e.
663  the %d right before 'messages', i.e. the total number of messages. */
664  mutt_message(ngettext("%s [%d of %d message read]",
665  "%s [%d of %d messages read]", msgs - last),
666  msgbuf, i - last, msgs - last);
667  }
668 
669  m_spool->append = old_append;
670  mx_mbox_close(m_spool);
671 
672  if (rset)
673  {
674  /* make sure no messages get deleted */
675  mutt_str_copy(buf, "RSET\r\n", sizeof(buf));
676  if (pop_query(adata, buf, sizeof(buf)) == -1)
677  goto fail;
678  }
679 
680 finish:
681  /* exit gracefully */
682  mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
683  if (pop_query(adata, buf, sizeof(buf)) == -1)
684  goto fail;
685  mutt_socket_close(conn);
686  FREE(&conn);
687  pop_adata_free((void **) &adata);
688  return;
689 
690 fail:
691  mutt_error(_("Server closed connection"));
692  mutt_socket_close(conn);
693  pop_adata_free((void **) &adata);
694 }
enum UrlScheme url_check_scheme(const char *str)
Check the protocol of a URL.
Definition: url.c:221
#define pop_query(adata, buf, buflen)
Definition: private.h:106
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:304
int pop_parse_path(const char *path, struct ConnAccount *cac)
Parse a POP mailbox name.
Definition: lib.c:83
#define mutt_error(...)
Definition: logging.h:88
int pop_open_connection(struct PopAccountData *adata)
Open connection and authenticate.
Definition: lib.c:310
Url wasn&#39;t recognised.
Definition: url.h:35
#define _(a)
Definition: message.h:28
struct Connection * conn
Definition: adata.h:38
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
QuadOption
Possible values for a quad-option.
Definition: quad.h:35
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1186
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1054
struct PopAccountData * pop_adata_new(void)
Create a new PopAccountData object.
Definition: adata.c:54
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:87
enum QuadOption query_quadoption(enum QuadOption opt, const char *prompt)
Ask the user a quad-question.
Definition: question.c:347
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:89
int pop_fetch_data(struct PopAccountData *adata, const char *query, struct Progress *progress, pop_fetch_t callback, void *data)
Read Headers with callback function.
Definition: lib.c:506
A local copy of an email.
Definition: mxapi.h:41
A mailbox.
Definition: mailbox.h:81
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:113
void pop_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free()
Definition: adata.c:40
Login details for a remote server.
Definition: connaccount.h:51
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:97
enum QuadOption cs_subset_quad(const struct ConfigSubset *sub, const char *name)
Get a quad-value config item by name.
Definition: helpers.c:218
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:749
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition: mx.c:1165
FILE * fp
pointer to the message data
Definition: mxapi.h:43
#define mutt_message(...)
Definition: logging.h:87
#define FREE(x)
Definition: memory.h:40
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1668
#define MUTT_OPEN_NO_FLAGS
No flags are set.
Definition: mxapi.h:60
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
POP-specific Account data -.
Definition: adata.h:36
#define MUTT_ADD_FROM
add a From_ line
Definition: mx.h:42
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
static int fetch_message(const char *line, void *data)
write line to file - Implements pop_fetch_t
Definition: pop.c:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function: