/*
- * $Id$
- *
* Consolidate mail from remote POP3 accounts.
*
+ * Copyright (c) 2007-2019 by the citadel.org team
+ *
+ * This program is open source software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
+#include <sysconfig.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <libcitadel.h>
+#include <curl/curl.h>
#include "citadel.h"
#include "server.h"
#include "citserver.h"
#include "support.h"
#include "config.h"
-#include "tools.h"
-#include "room_ops.h"
#include "ctdl_module.h"
#include "clientsocket.h"
#include "msgbase.h"
#include "internet_addressing.h"
-
-struct pop3aggr {
- struct pop3aggr *next;
- char roomname[ROOMNAMELEN];
- char pop3host[128];
- char pop3user[128];
- char pop3pass[128];
+#include "database.h"
+#include "citadel_dirs.h"
+
+struct p3cq { // module-local queue of pop3 client work that needs processing
+ struct p3cq *next;
+ char *room;
+ char *host;
+ char *user;
+ char *pass;
+ int keep;
+ long interval;
};
-struct pop3aggr *palist = NULL;
+static int doing_pop3client = 0;
+struct p3cq *p3cq = NULL;
-void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
+/*
+ * Process one mailbox.
+ */
+void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval)
{
- int sock;
- char buf[SIZ];
- int msg_to_fetch = 0;
- int *msglist = NULL;
- int num_msgs = 0;
- int alloc_msgs = 0;
+ syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
+
+ char url[SIZ];
+ CURL *curl;
+ CURLcode res = CURLE_OK;
+ StrBuf *Uidls = NULL;
int i;
- char *body = NULL;
- struct CtdlMessage *msg = NULL;
- long msgnum = 0;
-
- lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
- lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
- sock = sock_connect(pop3host, "110", "tcp");
- if (sock < 0) {
- lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
+ char cmd[1024];
+
+ curl = curl_easy_init();
+ if (!curl) {
return;
}
-
- lprintf(CTDL_DEBUG, "Connected!\n");
- /* Read the server greeting */
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
+ Uidls = NewStrBuf();
+
+ curl_easy_setopt(curl, CURLOPT_USERNAME, user);
+ curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls); // Give it our StrBuf to work with
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
+
+ /* Try POP3S (SSL encrypted) first */
+ snprintf(url, sizeof url, "pop3s://%s", host);
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ res = curl_easy_perform(curl);
+ if (res == CURLE_OK) {
+ } else {
+ syslog(LOG_DEBUG, "pop3client: POP3S connection failed: %s , trying POP3 next", curl_easy_strerror(res));
+ snprintf(url, sizeof url, "pop3://%s", host); // try unencrypted next
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ FlushStrBuf(Uidls);
+ res = curl_easy_perform(curl);
+ }
- /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
- * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
- * will cause problems if the server happens to be Exchange, which is so b0rken it
- * actually barfs on LF-terminated newlines.
- */
- snprintf(buf, sizeof buf, "USER %s\r", pop3user);
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
-
- /* Password */
- snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
- lprintf(CTDL_DEBUG, "<PASS <password>\n");
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
-
- /* Get the list of messages */
- snprintf(buf, sizeof buf, "LIST\r");
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
-
- do {
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- msg_to_fetch = atoi(buf);
- if (msg_to_fetch > 0) {
- if (alloc_msgs == 0) {
- alloc_msgs = 100;
- msglist = malloc((alloc_msgs * (sizeof(int))));
+ if (res != CURLE_OK) {
+ syslog(LOG_DEBUG, "pop3client: POP3 connection failed: %s", curl_easy_strerror(res));
+ curl_easy_cleanup(curl);
+ FreeStrBuf(&Uidls);
+ return;
+ }
+
+ // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
+ // Now go through the UIDL list and look for messages.
+
+ int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
+ syslog(LOG_DEBUG, "pop3client: there are %d messages", num_msgs);
+ for (i=0; i<num_msgs; ++i) {
+ char oneuidl[1024];
+ extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
+ if (strlen(oneuidl) > 2) {
+ if (oneuidl[strlen(oneuidl)-1] == '\r') {
+ oneuidl[strlen(oneuidl)-1] = 0;
}
- else if (num_msgs >= alloc_msgs) {
- alloc_msgs = alloc_msgs * 2;
- msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
+ int this_msg = atoi(oneuidl);
+ char *c = strchr(oneuidl, ' ');
+ if (c) strcpy(oneuidl, ++c);
+
+ // Make up the Use Table record so we can check if we've already seen this message.
+ StrBuf *UT = NewStrBuf();
+ StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
+ int already_seen = CheckIfAlreadySeen(UT);
+ FreeStrBuf(&UT);
+
+ // Only fetch the message if we haven't seen it before.
+ if (already_seen == 0) {
+ StrBuf *TheMsg = NewStrBuf();
+ snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
+ res = curl_easy_perform(curl);
+ if (res == CURLE_OK) {
+ struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
+ CtdlSubmitMsg(msg, NULL, room, 0);
+ CM_Free(msg);
+ }
+ else {
+ FreeStrBuf(&TheMsg);
+ }
+
+ // Unless the configuration says to keep the message on the server, delete it.
+ if (keep == 0) {
+ snprintf(cmd, sizeof cmd, "DELE %d", this_msg);
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
+ res = curl_easy_perform(curl);
+ }
+ }
+ else {
+ syslog(LOG_DEBUG, "pop3client: %s has already been retrieved", oneuidl);
}
- if (msglist == NULL) goto bail;
- msglist[num_msgs++] = msg_to_fetch;
- }
- } while (buf[0] != '.');
-
- if (num_msgs) for (i=0; i<num_msgs; ++i) {
-
- /* Tell the server to fetch the message */
- snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
-
- /* If we get to this point, the message is on its way. Read it. */
- body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
- if (body == NULL) goto bail;
-
- lprintf(CTDL_DEBUG, "Converting message...\n");
- msg = convert_internet_message(body);
- body = NULL; /* yes, this should be dereferenced, NOT freed */
-
- /* Do Something With It (tm) */
- msgnum = CtdlSubmitMsg(msg, NULL, roomname);
- if (msgnum > 0L) {
- /* Message has been committed to the store, so delete it from the remote server */
- snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
}
- CtdlFreeMessage(msg);
}
- /* Log out */
- snprintf(buf, sizeof buf, "QUIT\r");
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- lprintf(CTDL_DEBUG, ">%s\n", buf);
-bail: sock_close(sock);
- if (msglist) free(msglist);
+ curl_easy_cleanup(curl);
+ FreeStrBuf(&Uidls);
+ return;
}
/*
* Scan a room's netconfig to determine whether it requires POP3 aggregation
*/
-void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
+void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
{
- char filename[PATH_MAX];
- char buf[1024];
- char instr[32];
- FILE *fp;
- struct pop3aggr *pptr;
-
- assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
-
- /* Only do net processing for rooms that have netconfigs */
- fp = fopen(filename, "r");
- if (fp == NULL) {
- return;
- }
-
- while (fgets(buf, sizeof buf, fp) != NULL) {
- buf[strlen(buf)-1] = 0;
-
- extract_token(instr, buf, 0, '|', sizeof instr);
- if (!strcasecmp(instr, "pop3client")) {
- pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
- if (pptr != NULL) {
- safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
- extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
- extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
- extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
- pptr->next = palist;
- palist = pptr;
- }
- }
-
+ const RoomNetCfgLine *pLine;
+ struct p3cq *pptr = NULL;
+
+ if (server_shutting_down) return;
+
+ pLine = OneRNCFG->NetConfigs[pop3client];
+
+ while (pLine != NULL)
+ {
+ pptr = malloc(sizeof(struct p3cq));
+ pptr->next = p3cq;
+ p3cq = pptr;
+ pptr->room = strdup(qrbuf->QRname);
+ pptr->host = strdup(ChrPtr(pLine->Value[0]));
+ pptr->user = strdup(ChrPtr(pLine->Value[1]));
+ pptr->pass = strdup(ChrPtr(pLine->Value[2]));
+ pptr->keep = atoi(ChrPtr(pLine->Value[3]));
+ pptr->interval = atol(ChrPtr(pLine->Value[4]));
+
+ pLine = pLine->next;
}
-
- fclose(fp);
-
}
void pop3client_scan(void) {
static time_t last_run = 0L;
- static int doing_pop3client = 0;
- struct pop3aggr *pptr;
+ time_t fastest_scan;
+ struct p3cq *pptr = NULL;
+
+ if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
+ fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
+ }
+ else {
+ fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
+ }
/*
* Run POP3 aggregation no more frequently than once every n seconds
*/
- if ( (time(NULL) - last_run) < config.c_net_freq ) {
+ if ( (time(NULL) - last_run) < fastest_scan ) {
return;
}
/*
- * This is a simple concurrency check to make sure only one pop3client run
- * is done at a time. We could do this with a mutex, but since we
+ * This is a simple concurrency check to make sure only one pop3client
+ * run is done at a time. We could do this with a mutex, but since we
* don't really require extremely fine granularity here, we'll do it
* with a static variable instead.
*/
if (doing_pop3client) return;
doing_pop3client = 1;
- lprintf(CTDL_DEBUG, "pop3client started\n");
- ForEachRoom(pop3client_scan_room, NULL);
+ syslog(LOG_DEBUG, "pop3client: scan started");
+ CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
+
+ /*
+ * We have to queue and process in separate phases, otherwise we leave a cursor open
+ */
+ syslog(LOG_DEBUG, "pop3client: processing started");
+ while (p3cq != NULL) {
+ pptr = p3cq;
+ p3cq = p3cq->next;
- while (palist != NULL) {
- pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
- pptr = palist;
- palist = palist->next;
+ pop3client_one_mailbox(pptr->room, pptr->host, pptr->user, pptr->pass, pptr->keep, pptr->interval);
+
+ free(pptr->room);
+ free(pptr->host);
+ free(pptr->user);
+ free(pptr->pass);
free(pptr);
}
- lprintf(CTDL_DEBUG, "pop3client ended\n");
+ syslog(LOG_DEBUG, "pop3client: ended");
last_run = time(NULL);
doing_pop3client = 0;
}
CTDL_MODULE_INIT(pop3client)
{
- CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
+ if (!threading)
+ {
+ CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
+ CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
+ }
- /* return our Subversion id for the Log */
- return "$Id$";
+ /* return our module id for the log */
+ return "pop3client";
}