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"
53 struct CitContext pop3_client_CC;
54 static int doing_pop3client = 0;
57 // This is how we form a USETABLE record for pop3 client
59 // StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
60 // "pop3/%s/%s:%s@%s",
61 // ChrPtr(RecvMsg->RoomName),
62 // ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
63 // RecvMsg->IO.ConnectMe->User,
64 // RecvMsg->IO.ConnectMe->Host
69 * Process one mailbox.
71 void pop3client_one_mailbox(struct ctdlroom *qrbuf, const char *host, const char *user, const char *pass, int keep, long interval)
73 syslog(LOG_DEBUG, "\033[33mpop3client: room=<%s> host=<%s> user=<%s> pass=<%s> keep=<%d> interval=<%ld>\033[0m",
74 qrbuf->QRname, host, user, pass, keep, interval
79 CURLcode res = CURLE_OK;
81 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_CUSTOMREQUEST, "UIDL");
93 /* Try POP3S (SSL encrypted) first */
94 snprintf(url, sizeof url, "pop3s://%s", host);
95 curl_easy_setopt(curl, CURLOPT_URL, url);
96 res = curl_easy_perform(curl);
97 if (res == CURLE_OK) {
99 syslog(LOG_DEBUG, "POP3S client failed: %s , trying POP3 next", curl_easy_strerror(res));
100 snprintf(url, sizeof url, "pop3://%s", host); // try unencrypted next
101 curl_easy_setopt(curl, CURLOPT_URL, url);
102 res = curl_easy_perform(curl);
105 if (res != CURLE_OK) {
106 syslog(LOG_DEBUG, "pop3 client failed: %s", curl_easy_strerror(res));
107 curl_easy_cleanup(curl);
111 // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
114 // FIXME write the rest ... all this crap was just a test to make sure libcurl is holding open a single connection.
115 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "RETR 1");
116 res = curl_easy_perform(curl);
117 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
118 res = curl_easy_perform(curl);
122 curl_easy_cleanup(curl);
128 * Scan a room's netconfig to determine whether it requires POP3 aggregation
130 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
132 const RoomNetCfgLine *pLine;
134 if (server_shutting_down) return;
136 pLine = OneRNCFG->NetConfigs[pop3client];
138 while (pLine != NULL)
140 pop3client_one_mailbox(qrbuf,
141 ChrPtr(pLine->Value[0]),
142 ChrPtr(pLine->Value[1]),
143 ChrPtr(pLine->Value[2]),
144 atoi(ChrPtr(pLine->Value[3])),
145 atol(ChrPtr(pLine->Value[4]))
153 void pop3client_scan(void) {
154 static time_t last_run = 0L;
157 become_session(&pop3_client_CC);
159 if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
160 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
163 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
167 * Run POP3 aggregation no more frequently than once every n seconds
169 if ( (time(NULL) - last_run) < fastest_scan ) {
174 * This is a simple concurrency check to make sure only one pop3client
175 * run is done at a time. We could do this with a mutex, but since we
176 * don't really require extremely fine granularity here, we'll do it
177 * with a static variable instead.
179 if (doing_pop3client) return;
180 doing_pop3client = 1;
182 syslog(LOG_DEBUG, "pop3client started");
183 CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
184 syslog(LOG_DEBUG, "pop3client ended");
185 last_run = time(NULL);
186 doing_pop3client = 0;
190 CTDL_MODULE_INIT(pop3client)
194 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
195 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
196 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
199 /* return our module id for the log */