NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
lib.h File Reference
#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 $spool_file. More...
 
enum MailboxType pop_path_probe (const char *path, const struct stat *st)
 Is this a POP Mailbox? - Implements MxOps::path_probe() 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 $spool_file.

Definition at line 509 of file pop.c.

510 {
511  if (!C_PopHost)
512  {
513  mutt_error(_("POP host is not defined"));
514  return;
515  }
516 
517  char buf[1024];
518  char msgbuf[128];
519  int last = 0, msgs, bytes, rset = 0, ret;
520  struct ConnAccount cac = { { 0 } };
521 
522  char *p = mutt_mem_calloc(strlen(C_PopHost) + 7, sizeof(char));
523  char *url = p;
525  {
526  strcpy(url, "pop://");
527  p = strchr(url, '\0');
528  }
529  strcpy(p, C_PopHost);
530 
531  ret = pop_parse_path(url, &cac);
532  FREE(&url);
533  if (ret)
534  {
535  mutt_error(_("%s is an invalid POP path"), C_PopHost);
536  return;
537  }
538 
539  struct Connection *conn = mutt_conn_find(&cac);
540  if (!conn)
541  return;
542 
543  struct PopAccountData *adata = pop_adata_new();
544  adata->conn = conn;
545 
546  if (pop_open_connection(adata) < 0)
547  {
548  //XXX mutt_socket_free(adata->conn);
549  pop_adata_free((void **) &adata);
550  return;
551  }
552 
553  mutt_message(_("Checking for new messages..."));
554 
555  /* find out how many messages are in the mailbox. */
556  mutt_str_copy(buf, "STAT\r\n", sizeof(buf));
557  ret = pop_query(adata, buf, sizeof(buf));
558  if (ret == -1)
559  goto fail;
560  if (ret == -2)
561  {
562  mutt_error("%s", adata->err_msg);
563  goto finish;
564  }
565 
566  sscanf(buf, "+OK %d %d", &msgs, &bytes);
567 
568  /* only get unread messages */
569  if ((msgs > 0) && C_PopLast)
570  {
571  mutt_str_copy(buf, "LAST\r\n", sizeof(buf));
572  ret = pop_query(adata, buf, sizeof(buf));
573  if (ret == -1)
574  goto fail;
575  if (ret == 0)
576  sscanf(buf, "+OK %d", &last);
577  }
578 
579  if (msgs <= last)
580  {
581  mutt_message(_("No new mail in POP mailbox"));
582  goto finish;
583  }
584 
585  struct Mailbox *m_spool = mx_path_resolve(C_SpoolFile);
586  struct Context *ctx = mx_mbox_open(m_spool, MUTT_OPEN_NO_FLAGS);
587  if (!ctx)
588  {
589  mailbox_free(&m_spool);
590  goto finish;
591  }
592 
593  bool old_append = m_spool->append;
594  m_spool->append = true;
595 
596  enum QuadOption delanswer =
597  query_quadoption(C_PopDelete, _("Delete messages from server?"));
598 
599  snprintf(msgbuf, sizeof(msgbuf),
600  ngettext("Reading new messages (%d byte)...",
601  "Reading new messages (%d bytes)...", bytes),
602  bytes);
603  mutt_message("%s", msgbuf);
604 
605  for (int i = last + 1; i <= msgs; i++)
606  {
607  struct Message *msg = mx_msg_open_new(ctx->mailbox, NULL, MUTT_ADD_FROM);
608  if (msg)
609  {
610  snprintf(buf, sizeof(buf), "RETR %d\r\n", i);
611  ret = pop_fetch_data(adata, buf, NULL, fetch_message, msg->fp);
612  if (ret == -3)
613  rset = 1;
614 
615  if ((ret == 0) && (mx_msg_commit(ctx->mailbox, msg) != 0))
616  {
617  rset = 1;
618  ret = -3;
619  }
620 
621  mx_msg_close(ctx->mailbox, &msg);
622  }
623  else
624  {
625  ret = -3;
626  }
627 
628  if ((ret == 0) && (delanswer == MUTT_YES))
629  {
630  /* delete the message on the server */
631  snprintf(buf, sizeof(buf), "DELE %d\r\n", i);
632  ret = pop_query(adata, buf, sizeof(buf));
633  }
634 
635  if (ret == -1)
636  {
637  m_spool->append = old_append;
638  mx_mbox_close(&ctx);
639  goto fail;
640  }
641  if (ret == -2)
642  {
643  mutt_error("%s", adata->err_msg);
644  break;
645  }
646  if (ret == -3)
647  {
648  mutt_error(_("Error while writing mailbox"));
649  break;
650  }
651 
652  /* L10N: The plural is picked by the second numerical argument, i.e.
653  the %d right before 'messages', i.e. the total number of messages. */
654  mutt_message(ngettext("%s [%d of %d message read]",
655  "%s [%d of %d messages read]", msgs - last),
656  msgbuf, i - last, msgs - last);
657  }
658 
659  m_spool->append = old_append;
660  mx_mbox_close(&ctx);
661 
662  if (rset)
663  {
664  /* make sure no messages get deleted */
665  mutt_str_copy(buf, "RSET\r\n", sizeof(buf));
666  if (pop_query(adata, buf, sizeof(buf)) == -1)
667  goto fail;
668  }
669 
670 finish:
671  /* exit gracefully */
672  mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
673  if (pop_query(adata, buf, sizeof(buf)) == -1)
674  goto fail;
675  mutt_socket_close(conn);
676  FREE(&conn);
677  pop_adata_free((void **) &adata);
678  return;
679 
680 fail:
681  mutt_error(_("Server closed connection"));
682  mutt_socket_close(conn);
683  pop_adata_free((void **) &adata);
684 }
+ 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 1149 of file pop.c.

1150 {
1151  if (mutt_istr_startswith(path, "pop://"))
1152  return MUTT_POP;
1153 
1154  if (mutt_istr_startswith(path, "pops://"))
1155  return MUTT_POP;
1156 
1157  return MUTT_UNKNOWN;
1158 }
+ 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 1190 of file pop.c.

ConnAccount
Login details for a remote server.
Definition: connaccount.h:51
Connection
An open network connection (socket)
Definition: connection.h:34
QuadOption
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
_
#define _(a)
Definition: message.h:28
Mailbox
A mailbox.
Definition: mailbox.h:81
Mailbox::append
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:113
C_PopLast
bool C_PopLast
Config: (pop) Use the 'LAST' command to fetch new mail.
Definition: config.c:42
pop_parse_path
int pop_parse_path(const char *path, struct ConnAccount *cac)
Parse a POP mailbox name.
Definition: lib.c:81
url_check_scheme
enum UrlScheme url_check_scheme(const char *str)
Check the protocol of a URL.
Definition: url.c:221
PopAccountData::err_msg
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:54
U_UNKNOWN
@ U_UNKNOWN
Url wasn't recognised.
Definition: url.h:34
MUTT_POP
@ MUTT_POP
'POP3' Mailbox type
Definition: mailbox.h:55
fetch_message
static int fetch_message(const char *line, void *data)
write line to file - Implements pop_fetch_t
Definition: pop.c:98
mutt_socket_close
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:96
MUTT_YES
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:40
Context
The "current" mailbox.
Definition: context.h:38
FREE
#define FREE(x)
Definition: memory.h:40
pop_open_connection
int pop_open_connection(struct PopAccountData *adata)
Open connection and authenticate.
Definition: lib.c:308
mailbox_free
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:71
mx_path_resolve
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1681
query_quadoption
enum QuadOption query_quadoption(enum QuadOption opt, const char *prompt)
Ask the user a quad-question.
Definition: curs_lib.c:518
pop_adata_new
struct PopAccountData * pop_adata_new(void)
Create a new PopAccountData object.
Definition: adata.c:54
mx_msg_commit
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition: mx.c:1183
Message::path
char * path
path to temp file
Definition: mx.h:97
mx_mbox_close
enum MxStatus mx_mbox_close(struct Context **ptr)
Save changes and close mailbox.
Definition: mx.c:632
PopAccountData::conn
struct Connection * conn
Definition: adata.h:36
pop_adata_free
void pop_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free()
Definition: adata.c:40
MUTT_UNKNOWN
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition: mailbox.h:47
Context::mailbox
struct Mailbox * mailbox
Definition: context.h:50
MUTT_OPEN_NO_FLAGS
#define MUTT_OPEN_NO_FLAGS
No flags are set.
Definition: mx.h:51
MUTT_ADD_FROM
#define MUTT_ADD_FROM
add a From_ line
Definition: mx.h:65
Message::fp
FILE * fp
pointer to the message data
Definition: mx.h:96
C_PopDelete
unsigned char C_PopDelete
Config: (pop) After downloading POP messages, delete them on the server.
Definition: config.c:40
C_PopHost
char * C_PopHost
Config: (pop) Url of the POP server.
Definition: config.c:41
C_SpoolFile
WHERE char * C_SpoolFile
Config: Inbox.
Definition: mutt_globals.h:108
mutt_istr_startswith
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:172
mx_mbox_open
struct Context * mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:309
pop_query
#define pop_query(adata, buf, buflen)
Definition: private.h:119
Account::adata
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
pop_fetch_data
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:501
mx_msg_close
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1204
mutt_message
#define mutt_message(...)
Definition: logging.h:83
mx_msg_open_new
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1072
mutt_conn_find
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:86
PopAccountData
POP-specific Account data -.
Definition: adata.h:34
Message
A local copy of an email.
Definition: mx.h:94
mutt_error
#define mutt_error(...)
Definition: logging.h:84
mutt_str_copy
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:716