NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lib.h File Reference

POP network mailbox. More...

#include "core/lib.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.
 
enum MailboxType pop_path_probe (const char *path, const struct stat *st)
 Is this a POP Mailbox?
 

Variables

const struct MxOps MxPopOps
 POP Mailbox - Implements MxOps -.
 

Detailed Description

POP network mailbox.

Authors
  • Vsevolod Volkov
  • 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 lib.h.

Function Documentation

◆ pop_fetch_mail()

void pop_fetch_mail ( void )

Fetch messages and save them in $spool_file.

Definition at line 530 of file pop.c.

531{
532 const char *const c_pop_host = cs_subset_string(NeoMutt->sub, "pop_host");
533 if (!c_pop_host)
534 {
535 mutt_error(_("POP host is not defined"));
536 return;
537 }
538
539 char buf[1024] = { 0 };
540 char msgbuf[128] = { 0 };
541 int last = 0;
542 int msgs = 0;
543 int bytes = 0;
544 int rset = 0;
545 int rc;
546 struct ConnAccount cac = { { 0 } };
547
548 char *url = NULL;
549 if (url_check_scheme(c_pop_host) == U_UNKNOWN)
550 mutt_str_asprintf(&url, "pop://%s", c_pop_host);
551 else
552 url = mutt_str_dup(c_pop_host);
553
554 rc = pop_parse_path(url, &cac);
555 FREE(&url);
556 if (rc)
557 {
558 mutt_error(_("%s is an invalid POP path"), c_pop_host);
559 return;
560 }
561
562 struct Connection *conn = mutt_conn_find(&cac);
563 if (!conn)
564 return;
565
567 adata->conn = conn;
568
569 if (pop_open_connection(adata) < 0)
570 {
571 pop_adata_free((void **) &adata);
572 return;
573 }
574
575 mutt_message(_("Checking for new messages..."));
576
577 /* find out how many messages are in the mailbox. */
578 mutt_str_copy(buf, "STAT\r\n", sizeof(buf));
579 rc = pop_query(adata, buf, sizeof(buf));
580 if (rc == -1)
581 goto fail;
582 if (rc == -2)
583 {
584 mutt_error("%s", adata->err_msg);
585 goto finish;
586 }
587
588 sscanf(buf, "+OK %d %d", &msgs, &bytes);
589
590 /* only get unread messages */
591 const bool c_pop_last = cs_subset_bool(NeoMutt->sub, "pop_last");
592 if ((msgs > 0) && c_pop_last)
593 {
594 mutt_str_copy(buf, "LAST\r\n", sizeof(buf));
595 rc = pop_query(adata, buf, sizeof(buf));
596 if (rc == -1)
597 goto fail;
598 if (rc == 0)
599 sscanf(buf, "+OK %d", &last);
600 }
601
602 if (msgs <= last)
603 {
604 mutt_message(_("No new mail in POP mailbox"));
605 goto finish;
606 }
607
608 const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file");
609 struct Mailbox *m_spool = mx_path_resolve(c_spool_file);
610
611 if (!mx_mbox_open(m_spool, MUTT_OPEN_NONE))
612 {
613 mailbox_free(&m_spool);
614 goto finish;
615 }
616 bool old_append = m_spool->append;
617 m_spool->append = true;
618
619 enum QuadOption delanswer = query_quadoption(_("Delete messages from server?"),
620 NeoMutt->sub, "pop_delete");
621
622 snprintf(msgbuf, sizeof(msgbuf),
623 ngettext("Reading new messages (%d byte)...",
624 "Reading new messages (%d bytes)...", bytes),
625 bytes);
626 mutt_message("%s", msgbuf);
627
628 for (int i = last + 1; i <= msgs; i++)
629 {
630 struct Message *msg = mx_msg_open_new(m_spool, NULL, MUTT_ADD_FROM);
631 if (msg)
632 {
633 snprintf(buf, sizeof(buf), "RETR %d\r\n", i);
634 rc = pop_fetch_data(adata, buf, NULL, fetch_message, msg->fp);
635 if (rc == -3)
636 rset = 1;
637
638 if ((rc == 0) && (mx_msg_commit(m_spool, msg) != 0))
639 {
640 rset = 1;
641 rc = -3;
642 }
643
644 mx_msg_close(m_spool, &msg);
645 }
646 else
647 {
648 rc = -3;
649 }
650
651 if ((rc == 0) && (delanswer == MUTT_YES))
652 {
653 /* delete the message on the server */
654 snprintf(buf, sizeof(buf), "DELE %d\r\n", i);
655 rc = pop_query(adata, buf, sizeof(buf));
656 }
657
658 if (rc == -1)
659 {
660 m_spool->append = old_append;
661 mx_mbox_close(m_spool);
662 goto fail;
663 }
664 if (rc == -2)
665 {
666 mutt_error("%s", adata->err_msg);
667 break;
668 }
669 if (rc == -3)
670 {
671 mutt_error(_("Error while writing mailbox"));
672 break;
673 }
674
675 /* L10N: The plural is picked by the second numerical argument, i.e.
676 the %d right before 'messages', i.e. the total number of messages. */
677 mutt_message(ngettext("%s [%d of %d message read]",
678 "%s [%d of %d messages read]", msgs - last),
679 msgbuf, i - last, msgs - last);
680 }
681
682 m_spool->append = old_append;
683 mx_mbox_close(m_spool);
684
685 if (rset)
686 {
687 /* make sure no messages get deleted */
688 mutt_str_copy(buf, "RSET\r\n", sizeof(buf));
689 if (pop_query(adata, buf, sizeof(buf)) == -1)
690 goto fail;
691 }
692
693finish:
694 /* exit gracefully */
695 mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
696 if (pop_query(adata, buf, sizeof(buf)) == -1)
697 goto fail;
698 mutt_socket_close(conn);
699 pop_adata_free((void **) &adata);
700 return;
701
702fail:
703 mutt_error(_("Server closed connection"));
704 mutt_socket_close(conn);
705 pop_adata_free((void **) &adata);
706}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition mailbox.c:90
void pop_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free() -.
Definition adata.c:41
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
static int fetch_message(const char *line, void *data)
Parse a Message response - Implements pop_fetch_t -.
Definition pop.c:102
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition string.c:809
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:587
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition mutt_socket.c:88
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition mx.c:1185
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition mx.c:285
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition mx.c:1044
int mx_msg_commit(struct Mailbox *m, struct Message *msg)
Commit a message to a folder - Wrapper for MxOps::msg_commit()
Definition mx.c:1164
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition mx.c:1650
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition mx.c:595
@ MUTT_ADD_FROM
add a From_ line
Definition mx.h:43
@ MUTT_OPEN_NONE
No flags are set.
Definition mxapi.h:42
struct PopAccountData * pop_adata_new(void)
Create a new PopAccountData object.
Definition adata.c:63
int pop_open_connection(struct PopAccountData *adata)
Open connection and authenticate.
Definition lib.c:316
int pop_parse_path(const char *path, struct ConnAccount *cac)
Parse a POP mailbox name.
Definition lib.c:82
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
#define pop_query(adata, buf, buflen)
Definition private.h:109
QuadOption
Possible values for a quad-option.
Definition quad.h:36
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
enum QuadOption query_quadoption(const char *prompt, struct ConfigSubset *sub, const char *name)
Ask the user a quad-question.
Definition question.c:384
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition socket.c:100
void * adata
Private data (for Mailbox backends)
Definition account.h:42
Login details for a remote server.
Definition connaccount.h:59
A mailbox.
Definition mailbox.h:81
bool append
Mailbox is opened in append mode.
Definition mailbox.h:111
A local copy of an email.
Definition message.h:34
FILE * fp
pointer to the message data
Definition message.h:35
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
POP-specific Account data -.
Definition adata.h:37
char err_msg[POP_CMD_RESPONSE]
Last error message.
Definition adata.h:57
struct Connection * conn
Connection to POP server.
Definition adata.h:38
enum UrlScheme url_check_scheme(const char *str)
Check the protocol of a URL.
Definition url.c:229
@ U_UNKNOWN
Url wasn't recognised.
Definition url.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function: