NeoMutt  2020-06-26-89-g172cd3
Teaching an old dog new tricks
DOXYGEN
lib.h File Reference

POP network mailbox. More...

#include <stdbool.h>
#include "core/lib.h"
#include "mx.h"
+ Include dependency graph for lib.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void pop_fetch_mail (void)
 Fetch messages and save them in $spoolfile. More...
 
enum MailboxType pop_path_probe (const char *path, const struct stat *st)
 Is this a POP Mailbox? - Implements MxOps::path_probe() More...
 
bool config_init_pop (struct ConfigSet *cs)
 Register pop config variables. More...
 

Variables

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

Detailed Description

POP network mailbox.

Authors
  • Vsevolod Volkov

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 lib.h.

Function Documentation

◆ pop_fetch_mail()

void pop_fetch_mail ( void  )

Fetch messages and save them in $spoolfile.

Definition at line 563 of file pop.c.

564 {
565  if (!C_PopHost)
566  {
567  mutt_error(_("POP host is not defined"));
568  return;
569  }
570 
571  char buf[1024];
572  char msgbuf[128];
573  int last = 0, msgs, bytes, rset = 0, ret;
574  struct ConnAccount cac = { { 0 } };
575 
576  char *p = mutt_mem_calloc(strlen(C_PopHost) + 7, sizeof(char));
577  char *url = p;
579  {
580  strcpy(url, "pop://");
581  p = strchr(url, '\0');
582  }
583  strcpy(p, C_PopHost);
584 
585  ret = pop_parse_path(url, &cac);
586  FREE(&url);
587  if (ret)
588  {
589  mutt_error(_("%s is an invalid POP path"), C_PopHost);
590  return;
591  }
592 
593  struct Connection *conn = mutt_conn_find(&cac);
594  if (!conn)
595  return;
596 
597  struct PopAccountData *adata = pop_adata_new();
598  adata->conn = conn;
599 
600  if (pop_open_connection(adata) < 0)
601  {
602  //XXX mutt_socket_free(adata->conn);
603  pop_adata_free((void **) &adata);
604  return;
605  }
606 
607  mutt_message(_("Checking for new messages..."));
608 
609  /* find out how many messages are in the mailbox. */
610  mutt_str_copy(buf, "STAT\r\n", sizeof(buf));
611  ret = pop_query(adata, buf, sizeof(buf));
612  if (ret == -1)
613  goto fail;
614  if (ret == -2)
615  {
616  mutt_error("%s", adata->err_msg);
617  goto finish;
618  }
619 
620  sscanf(buf, "+OK %d %d", &msgs, &bytes);
621 
622  /* only get unread messages */
623  if ((msgs > 0) && C_PopLast)
624  {
625  mutt_str_copy(buf, "LAST\r\n", sizeof(buf));
626  ret = pop_query(adata, buf, sizeof(buf));
627  if (ret == -1)
628  goto fail;
629  if (ret == 0)
630  sscanf(buf, "+OK %d", &last);
631  }
632 
633  if (msgs <= last)
634  {
635  mutt_message(_("No new mail in POP mailbox"));
636  goto finish;
637  }
638 
639  struct Mailbox *m_spool = mx_path_resolve(C_Spoolfile);
640  struct Context *ctx = mx_mbox_open(m_spool, MUTT_OPEN_NO_FLAGS);
641  if (!ctx)
642  {
643  mailbox_free(&m_spool);
644  goto finish;
645  }
646 
647  bool old_append = m_spool->append;
648  m_spool->append = true;
649 
650  enum QuadOption delanswer =
651  query_quadoption(C_PopDelete, _("Delete messages from server?"));
652 
653  snprintf(msgbuf, sizeof(msgbuf),
654  ngettext("Reading new messages (%d byte)...",
655  "Reading new messages (%d bytes)...", bytes),
656  bytes);
657  mutt_message("%s", msgbuf);
658 
659  for (int i = last + 1; i <= msgs; i++)
660  {
661  struct Message *msg = mx_msg_open_new(ctx->mailbox, NULL, MUTT_ADD_FROM);
662  if (msg)
663  {
664  snprintf(buf, sizeof(buf), "RETR %d\r\n", i);
665  ret = pop_fetch_data(adata, buf, NULL, fetch_message, msg->fp);
666  if (ret == -3)
667  rset = 1;
668 
669  if ((ret == 0) && (mx_msg_commit(ctx->mailbox, msg) != 0))
670  {
671  rset = 1;
672  ret = -3;
673  }
674 
675  mx_msg_close(ctx->mailbox, &msg);
676  }
677  else
678  {
679  ret = -3;
680  }
681 
682  if ((ret == 0) && (delanswer == MUTT_YES))
683  {
684  /* delete the message on the server */
685  snprintf(buf, sizeof(buf), "DELE %d\r\n", i);
686  ret = pop_query(adata, buf, sizeof(buf));
687  }
688 
689  if (ret == -1)
690  {
691  m_spool->append = old_append;
692  mx_mbox_close(&ctx);
693  goto fail;
694  }
695  if (ret == -2)
696  {
697  mutt_error("%s", adata->err_msg);
698  break;
699  }
700  if (ret == -3)
701  {
702  mutt_error(_("Error while writing mailbox"));
703  break;
704  }
705 
706  /* L10N: The plural is picked by the second numerical argument, i.e.
707  the %d right before 'messages', i.e. the total number of messages. */
708  mutt_message(ngettext("%s [%d of %d message read]",
709  "%s [%d of %d messages read]", msgs - last),
710  msgbuf, i - last, msgs - last);
711  }
712 
713  m_spool->append = old_append;
714  mx_mbox_close(&ctx);
715 
716  if (rset)
717  {
718  /* make sure no messages get deleted */
719  mutt_str_copy(buf, "RSET\r\n", sizeof(buf));
720  if (pop_query(adata, buf, sizeof(buf)) == -1)
721  goto fail;
722  }
723 
724 finish:
725  /* exit gracefully */
726  mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
727  if (pop_query(adata, buf, sizeof(buf)) == -1)
728  goto fail;
729  mutt_socket_close(conn);
730  FREE(&conn);
731  pop_adata_free((void **) &adata);
732  return;
733 
734 fail:
735  mutt_error(_("Server closed connection"));
736  mutt_socket_close(conn);
737  pop_adata_free((void **) &adata);
738 }
enum UrlScheme url_check_scheme(const char *str)
Check the protocol of a URL.
Definition: url.c:221
#define MUTT_OPEN_NO_FLAGS
No flags are set.
Definition: mx.h:52
The "current" mailbox.
Definition: context.h:37
#define pop_query(adata, buf, buflen)
Definition: private.h:153
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: private.h:98
int pop_parse_path(const char *path, struct ConnAccount *cac)
Parse a POP mailbox name.
Definition: lib.c:79
int mx_mbox_close(struct Context **ptr)
Save changes and close mailbox.
Definition: mx.c:604
int pop_open_connection(struct PopAccountData *adata)
Open connection and authenticate.
Definition: lib.c:318
#define mutt_message(...)
Definition: logging.h:83
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:40
An open network connection (socket)
Definition: connection.h:34
Url wasn&#39;t recognised.
Definition: url.h:34
#define _(a)
Definition: message.h:28
struct Connection * conn
Definition: private.h:80
struct Context * mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:267
char * C_PopHost
Config: (pop) Url of the POP server.
Definition: config.c:40
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1180
struct Mailbox * mailbox
Definition: context.h:50
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:60
struct Message * mx_msg_open_new(struct Mailbox *m, struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1046
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:86
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:511
A local copy of an email.
Definition: mx.h:83
A mailbox.
Definition: mailbox.h:81
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:113
static struct PopAccountData * pop_adata_new(void)
Create a new PopAccountData object.
Definition: pop.c:111
unsigned char C_PopDelete
Config: (pop) After downloading POP messages, delete them on the server.
Definition: config.c:39
Login details for a remote server.
Definition: connaccount.h:51
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:96
bool C_PopLast
Config: (pop) Use the &#39;LAST&#39; command to fetch new mail.
Definition: config.c:41
enum QuadOption query_quadoption(enum QuadOption opt, const char *prompt)
Ask the user a quad-question.
Definition: curs_lib.c:515
WHERE char * C_Spoolfile
Config: Inbox.
Definition: mutt_globals.h:111
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:721
static void pop_adata_free(void **ptr)
Free data attached to the Mailbox.
Definition: pop.c:97
#define mutt_error(...)
Definition: logging.h:84
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition: mx.c:1159
FILE * fp
pointer to the message data
Definition: mx.h:85
#define FREE(x)
Definition: memory.h:40
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1670
POP-specific Account data -.
Definition: private.h:78
#define MUTT_ADD_FROM
add a From_ line
Definition: mx.h:66
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
static int fetch_message(const char *line, void *data)
write line to file - Implements pop_fetch_t
Definition: pop.c:152
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_path_probe()

enum MailboxType pop_path_probe ( const char *  path,
const struct stat *  st 
)

Is this a POP Mailbox? - Implements MxOps::path_probe()

Definition at line 1224 of file pop.c.

1225 {
1226  if (!path)
1227  return MUTT_UNKNOWN;
1228 
1229  if (mutt_istr_startswith(path, "pop://"))
1230  return MUTT_POP;
1231 
1232  if (mutt_istr_startswith(path, "pops://"))
1233  return MUTT_POP;
1234 
1235  return MUTT_UNKNOWN;
1236 }
Mailbox wasn&#39;t recognised.
Definition: mailbox.h:47
&#39;POP3&#39; Mailbox type
Definition: mailbox.h:55
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:177
char * path
path to temp file
Definition: mx.h:86
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ config_init_pop()

bool config_init_pop ( struct ConfigSet cs)

Register pop config variables.

Definition at line 67 of file config.c.

68 {
69  return cs_register_variables(cs, PopVars, 0);
70 }
struct ConfigDef PopVars[]
Definition: config.c:49
bool cs_register_variables(const struct ConfigSet *cs, struct ConfigDef vars[], int flags)
Register a set of config items.
Definition: set.c:279
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MxPopOps

struct MxOps MxPopOps

POP Mailbox - Implements MxOps.

Definition at line 1271 of file pop.c.