2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2017 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>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
36 #include <sys/types.h>
38 #include <libcitadel.h>
39 #include <curl/curl.h>
42 #include "citserver.h"
45 #include "ctdl_module.h"
46 #include "clientsocket.h"
48 #include "internet_addressing.h"
50 #include "citadel_dirs.h"
52 struct p3cq { // module-local queue of pop3 client work that needs processing
62 struct CitContext pop3_client_CC;
63 static int doing_pop3client = 0;
64 struct p3cq *p3cq = NULL;
67 * Process one mailbox.
69 void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval)
71 syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
75 CURLcode res = CURLE_OK;
79 curl = curl_easy_init();
86 curl_easy_setopt(curl, CURLOPT_USERNAME, user);
87 curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
88 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
89 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
90 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
91 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
92 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls); // Give it our StrBuf to work with
93 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
95 /* Try POP3S (SSL encrypted) first */
96 snprintf(url, sizeof url, "pop3s://%s", host);
97 curl_easy_setopt(curl, CURLOPT_URL, url);
98 res = curl_easy_perform(curl);
99 if (res == CURLE_OK) {
101 syslog(LOG_DEBUG, "POP3S client failed: %s , trying POP3 next", curl_easy_strerror(res));
102 snprintf(url, sizeof url, "pop3://%s", host); // try unencrypted next
103 curl_easy_setopt(curl, CURLOPT_URL, url);
105 res = curl_easy_perform(curl);
108 if (res != CURLE_OK) {
109 syslog(LOG_DEBUG, "pop3 client failed: %s", curl_easy_strerror(res));
110 curl_easy_cleanup(curl);
115 // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
116 // Now go through the UIDL list and look for messages.
118 int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
119 syslog(LOG_DEBUG, "There are %d messages.", num_msgs);
120 for (i=0; i<num_msgs; ++i) {
122 extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
123 if (strlen(oneuidl) > 2) {
124 if (oneuidl[strlen(oneuidl)-1] == '\r') {
125 oneuidl[strlen(oneuidl)-1] = 0;
127 int this_msg = atoi(oneuidl);
128 char *c = strchr(oneuidl, ' ');
129 if (c) strcpy(oneuidl, ++c);
131 syslog(LOG_DEBUG, "<\033[34m%d\033[0m> <\033[34m%s\033[0m>", this_msg, oneuidl);
133 // Make up the Use Table record so we can check if we've already seen this message.
134 StrBuf *UT = NewStrBuf();
135 StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
136 time_t already_seen = CheckIfAlreadySeen(UT, time(NULL), 0, eUpdate);
137 syslog(LOG_DEBUG, "%s seen? \033[32m%ld\033[0m", ChrPtr(UT), already_seen);
143 curl_easy_cleanup(curl);
150 * Scan a room's netconfig to determine whether it requires POP3 aggregation
152 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
154 const RoomNetCfgLine *pLine;
155 struct p3cq *pptr = NULL;
157 if (server_shutting_down) return;
159 pLine = OneRNCFG->NetConfigs[pop3client];
161 while (pLine != NULL)
163 pptr = malloc(sizeof(struct p3cq));
166 pptr->room = strdup(qrbuf->QRname);
167 pptr->host = strdup(ChrPtr(pLine->Value[0]));
168 pptr->user = strdup(ChrPtr(pLine->Value[1]));
169 pptr->pass = strdup(ChrPtr(pLine->Value[2]));
170 pptr->keep = atoi(ChrPtr(pLine->Value[3]));
171 pptr->interval = atol(ChrPtr(pLine->Value[4]));
178 void pop3client_scan(void) {
179 static time_t last_run = 0L;
181 struct p3cq *pptr = NULL;
183 become_session(&pop3_client_CC);
185 if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
186 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
189 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
193 * Run POP3 aggregation no more frequently than once every n seconds
195 if ( (time(NULL) - last_run) < fastest_scan ) {
200 * This is a simple concurrency check to make sure only one pop3client
201 * run is done at a time. We could do this with a mutex, but since we
202 * don't really require extremely fine granularity here, we'll do it
203 * with a static variable instead.
205 if (doing_pop3client) return;
206 doing_pop3client = 1;
208 syslog(LOG_DEBUG, "pop3client scan started");
209 CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
212 * We have to queue and process in separate phases, otherwise we leave a cursor open
214 syslog(LOG_DEBUG, "pop3client processing started");
215 while (p3cq != NULL) {
219 pop3client_one_mailbox(pptr->room, pptr->host, pptr->user, pptr->pass, pptr->keep, pptr->interval);
228 syslog(LOG_DEBUG, "pop3client ended");
229 last_run = time(NULL);
230 doing_pop3client = 0;
234 CTDL_MODULE_INIT(pop3client)
238 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
239 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
240 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
243 /* return our module id for the log */