2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2021 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
20 #include <sysconfig.h>
25 #include <sys/types.h>
27 #include <libcitadel.h>
28 #include <curl/curl.h>
31 #include "citserver.h"
34 #include "ctdl_module.h"
35 #include "clientsocket.h"
37 #include "internet_addressing.h"
39 #include "citadel_dirs.h"
41 struct p3cq { // module-local queue of pop3 client work that needs processing
51 static int doing_pop3client = 0;
52 struct p3cq *p3cq = NULL;
55 * Process one mailbox.
57 void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval) {
58 syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
62 CURLcode res = CURLE_OK;
67 curl = curl_easy_init();
74 curl_easy_setopt(curl, CURLOPT_USERNAME, user);
75 curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
76 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
77 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
78 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
79 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
80 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls); // Give it our StrBuf to work with
81 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
83 /* Try POP3S (SSL encrypted) first */
84 snprintf(url, sizeof url, "pop3s://%s", host);
85 curl_easy_setopt(curl, CURLOPT_URL, url);
86 res = curl_easy_perform(curl);
87 if (res == CURLE_OK) {
90 syslog(LOG_DEBUG, "pop3client: POP3S connection failed: %s , trying POP3 next", curl_easy_strerror(res));
91 snprintf(url, sizeof url, "pop3://%s", host); // try unencrypted next
92 curl_easy_setopt(curl, CURLOPT_URL, url);
94 res = curl_easy_perform(curl);
97 if (res != CURLE_OK) {
98 syslog(LOG_DEBUG, "pop3client: POP3 connection failed: %s", curl_easy_strerror(res));
99 curl_easy_cleanup(curl);
104 // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
105 // Now go through the UIDL list and look for messages.
107 int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
108 syslog(LOG_DEBUG, "pop3client: there are %d messages", num_msgs);
109 for (i=0; i<num_msgs; ++i) {
111 extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
112 if (strlen(oneuidl) > 2) {
113 if (oneuidl[strlen(oneuidl)-1] == '\r') {
114 oneuidl[strlen(oneuidl)-1] = 0;
116 int this_msg = atoi(oneuidl);
117 char *c = strchr(oneuidl, ' ');
118 if (c) strcpy(oneuidl, ++c);
120 // Make up the Use Table record so we can check if we've already seen this message.
121 StrBuf *UT = NewStrBuf();
122 StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
123 int already_seen = CheckIfAlreadySeen(UT);
126 // Only fetch the message if we haven't seen it before.
127 if (already_seen == 0) {
128 StrBuf *TheMsg = NewStrBuf();
129 snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
130 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
131 curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
132 res = curl_easy_perform(curl);
133 if (res == CURLE_OK) {
134 struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
135 CtdlSubmitMsg(msg, NULL, room);
142 // Unless the configuration says to keep the message on the server, delete it.
144 snprintf(cmd, sizeof cmd, "DELE %d", this_msg);
145 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
146 res = curl_easy_perform(curl);
150 syslog(LOG_DEBUG, "pop3client: %s has already been retrieved", oneuidl);
155 curl_easy_cleanup(curl);
161 // Scan a room's netconfig looking for RSS feed parsing requests
163 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
165 char *serialized_config = NULL;
168 char cfgelement[SIZ];
171 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
172 if (!serialized_config) {
176 num_configs = num_tokens(serialized_config, '\n');
177 for (i=0; i<num_configs; ++i) {
178 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
179 if (!strncasecmp(cfgline, HKEY("pop3client|"))) {
180 struct p3cq *pptr = malloc(sizeof(struct p3cq));
183 p3cq->room = strdup(qrbuf->QRname);
184 extract_token(cfgelement, cfgline, 1, '|', sizeof cfgelement);
185 p3cq->host = strdup(cfgelement);
186 extract_token(cfgelement, cfgline, 2, '|', sizeof cfgelement);
187 p3cq->user = strdup(cfgelement);
188 extract_token(cfgelement, cfgline, 3, '|', sizeof cfgelement);
189 p3cq->pass = strdup(cfgelement);
190 p3cq->keep = extract_int(cfgline, 4);
191 p3cq->interval = extract_long(cfgline, 5);
195 free(serialized_config);
199 void pop3client_scan(void) {
200 static time_t last_run = 0L;
202 struct p3cq *pptr = NULL;
204 if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
205 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
208 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
212 * Run POP3 aggregation no more frequently than once every n seconds
214 if ( (time(NULL) - last_run) < fastest_scan ) {
219 * This is a simple concurrency check to make sure only one pop3client
220 * run is done at a time. We could do this with a mutex, but since we
221 * don't really require extremely fine granularity here, we'll do it
222 * with a static variable instead.
224 if (doing_pop3client) return;
225 doing_pop3client = 1;
227 syslog(LOG_DEBUG, "pop3client: scan started");
228 CtdlForEachRoom(pop3client_scan_room, NULL);
231 * We have to queue and process in separate phases, otherwise we leave a cursor open
233 syslog(LOG_DEBUG, "pop3client: processing started");
234 while (p3cq != NULL) {
238 pop3client_one_mailbox(pptr->room, pptr->host, pptr->user, pptr->pass, pptr->keep, pptr->interval);
247 syslog(LOG_DEBUG, "pop3client: ended");
248 last_run = time(NULL);
249 doing_pop3client = 0;
253 CTDL_MODULE_INIT(pop3client)
257 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
260 /* return our module id for the log */