NeoMutt  2024-10-02-37-gfa9146
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? - Implements MxOps::path_probe() -.
 

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 513 of file pop.c.

514{
515 const char *const c_pop_host = cs_subset_string(NeoMutt->sub, "pop_host");
516 if (!c_pop_host)
517 {
518 mutt_error(_("POP host is not defined"));
519 return;
520 }
521
522 char buf[1024] = { 0 };
523 char msgbuf[128] = { 0 };
524 int last = 0, msgs = 0, bytes = 0, rset = 0, rc;
525 struct ConnAccount cac = { { 0 } };
526
527 char *p = mutt_mem_calloc(strlen(c_pop_host) + 7, sizeof(char));
528 char *url = p;
529 if (url_check_scheme(c_pop_host) == U_UNKNOWN)
530 {
531 strcpy(url, "pop://");
532 p = strchr(url, '\0');
533 }
534 strcpy(p, c_pop_host);
535
536 rc = pop_parse_path(url, &cac);
537 FREE(&url);
538 if (rc)
539 {
540 mutt_error(_("%s is an invalid POP path"), c_pop_host);
541 return;
542 }
543
544 struct Connection *conn = mutt_conn_find(&cac);
545 if (!conn)
546 return;
547
549 adata->conn = conn;
550
551 if (pop_open_connection(adata) < 0)
552 {
553 pop_adata_free((void **) &adata);
554 return;
555 }
556
557 mutt_message(_("Checking for new messages..."));
558
559 /* find out how many messages are in the mailbox. */
560 mutt_str_copy(buf, "STAT\r\n", sizeof(buf));
561 rc = pop_query(adata, buf, sizeof(buf));
562 if (rc == -1)
563 goto fail;
564 if (rc == -2)
565 {
566 mutt_error("%s", adata->err_msg);
567 goto finish;
568 }
569
570 sscanf(buf, "+OK %d %d", &msgs, &bytes);
571
572 /* only get unread messages */
573 const bool c_pop_last = cs_subset_bool(NeoMutt->sub, "pop_last");
574 if ((msgs > 0) && c_pop_last)
575 {
576 mutt_str_copy(buf, "LAST\r\n", sizeof(buf));
577 rc = pop_query(adata, buf, sizeof(buf));
578 if (rc == -1)
579 goto fail;
580 if (rc == 0)
581 sscanf(buf, "+OK %d", &last);
582 }
583
584 if (msgs <= last)
585 {
586 mutt_message(_("No new mail in POP mailbox"));
587 goto finish;
588 }
589
590 const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file");
591 struct Mailbox *m_spool = mx_path_resolve(c_spool_file);
592
593 if (!mx_mbox_open(m_spool, MUTT_OPEN_NO_FLAGS))
594 {
595 mailbox_free(&m_spool);
596 goto finish;
597 }
598 bool old_append = m_spool->append;
599 m_spool->append = true;
600
601 enum QuadOption delanswer = query_quadoption(_("Delete messages from server?"),
602 NeoMutt->sub, "pop_delete");
603
604 snprintf(msgbuf, sizeof(msgbuf),
605 ngettext("Reading new messages (%d byte)...",
606 "Reading new messages (%d bytes)...", bytes),
607 bytes);
608 mutt_message("%s", msgbuf);
609
610 for (int i = last + 1; i <= msgs; i++)
611 {
612 struct Message *msg = mx_msg_open_new(m_spool, NULL, MUTT_ADD_FROM);
613 if (msg)
614 {
615 snprintf(buf, sizeof(buf), "RETR %d\r\n", i);
616 rc = pop_fetch_data(adata, buf, NULL, fetch_message, msg->fp);
617 if (rc == -3)
618 rset = 1;
619
620 if ((rc == 0) && (mx_msg_commit(m_spool, msg) != 0))
621 {
622 rset = 1;
623 rc = -3;
624 }
625
626 mx_msg_close(m_spool, &msg);
627 }
628 else
629 {
630 rc = -3;
631 }
632
633 if ((rc == 0) && (delanswer == MUTT_YES))
634 {
635 /* delete the message on the server */
636 snprintf(buf, sizeof(buf), "DELE %d\r\n", i);
637 rc = pop_query(adata, buf, sizeof(buf));
638 }
639
640 if (rc == -1)
641 {
642 m_spool->append = old_append;
643 mx_mbox_close(m_spool);
644 goto fail;
645 }
646 if (rc == -2)
647 {
648 mutt_error("%s", adata->err_msg);
649 break;
650 }
651 if (rc == -3)
652 {
653 mutt_error(_("Error while writing mailbox"));
654 break;
655 }
656
657 /* L10N: The plural is picked by the second numerical argument, i.e.
658 the %d right before 'messages', i.e. the total number of messages. */
659 mutt_message(ngettext("%s [%d of %d message read]",
660 "%s [%d of %d messages read]", msgs - last),
661 msgbuf, i - last, msgs - last);
662 }
663
664 m_spool->append = old_append;
665 mx_mbox_close(m_spool);
666
667 if (rset)
668 {
669 /* make sure no messages get deleted */
670 mutt_str_copy(buf, "RSET\r\n", sizeof(buf));
671 if (pop_query(adata, buf, sizeof(buf)) == -1)
672 goto fail;
673 }
674
675finish:
676 /* exit gracefully */
677 mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
678 if (pop_query(adata, buf, sizeof(buf)) == -1)
679 goto fail;
680 mutt_socket_close(conn);
681 pop_adata_free((void **) &adata);
682 return;
683
684fail:
685 mutt_error(_("Server closed connection"));
686 mutt_socket_close(conn);
687 pop_adata_free((void **) &adata);
688}
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:89
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:92
#define mutt_message(...)
Definition: logging2.h:91
static int fetch_message(const char *line, void *data)
Parse a Message response - Implements pop_fetch_t -.
Definition: pop.c:99
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
#define _(a)
Definition: message.h:28
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:581
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:90
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition: mx.c:1180
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:288
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1040
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
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1636
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:598
#define MUTT_ADD_FROM
add a From_ line
Definition: mx.h:39
#define MUTT_OPEN_NO_FLAGS
No flags are set.
Definition: mxapi.h:40
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:317
int pop_parse_path(const char *path, struct ConnAccount *cac)
Parse a POP mailbox name.
Definition: lib.c:83
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:111
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:379
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:53
A mailbox.
Definition: mailbox.h:79
bool append
Mailbox is opened in append mode.
Definition: mailbox.h:109
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:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
POP-specific Account data -.
Definition: adata.h:37
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
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:226
@ 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: