ab2c4a6cdc20c2e37d8c0c4193bbfea3ab8745ba
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * Consolidate mail from remote POP3 accounts.
3  *
4  * Copyright (c) 2007-2017 by the citadel.org team
5  *
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.
10  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sysconfig.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <ctype.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <libcitadel.h>
39 #include <curl/curl.h>
40 #include "citadel.h"
41 #include "server.h"
42 #include "citserver.h"
43 #include "support.h"
44 #include "config.h"
45 #include "ctdl_module.h"
46 #include "clientsocket.h"
47 #include "msgbase.h"
48 #include "internet_addressing.h"
49 #include "database.h"
50 #include "citadel_dirs.h"
51
52 struct p3cq {                           // module-local queue of pop3 client work that needs processing
53         struct p3cq *next;
54         char *room;
55         char *host;
56         char *user;
57         char *pass;
58         int keep;
59         long interval;
60 };
61
62 struct CitContext pop3_client_CC;
63 static int doing_pop3client = 0;
64 struct p3cq *p3cq = NULL;
65
66 /*
67  * Process one mailbox.
68  */
69 void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval)
70 {
71         syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
72
73         char url[SIZ];
74         CURL *curl;
75         CURLcode res = CURLE_OK;
76         StrBuf *Uidls = NULL;
77         int i;
78         char cmd[1024];
79
80         curl = curl_easy_init();
81         if (!curl) {
82                 return;
83         }
84
85         Uidls = NewStrBuf();
86
87         curl_easy_setopt(curl, CURLOPT_USERNAME, user);
88         curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
89         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
90         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
91         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
92         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
93         curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls);                       // Give it our StrBuf to work with
94         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
95
96         /* Try POP3S (SSL encrypted) first */
97         snprintf(url, sizeof url, "pop3s://%s", host);
98         curl_easy_setopt(curl, CURLOPT_URL, url);
99         res = curl_easy_perform(curl);
100         if (res == CURLE_OK) {
101         } else {
102                 syslog(LOG_DEBUG, "pop3client: POP3S connection failed: %s , trying POP3 next", curl_easy_strerror(res));
103                 snprintf(url, sizeof url, "pop3://%s", host);                   // try unencrypted next
104                 curl_easy_setopt(curl, CURLOPT_URL, url);
105                 FlushStrBuf(Uidls);
106                 res = curl_easy_perform(curl);
107         }
108
109         if (res != CURLE_OK) {
110                 syslog(LOG_DEBUG, "pop3client: POP3 connection failed: %s", curl_easy_strerror(res));
111                 curl_easy_cleanup(curl);
112                 FreeStrBuf(&Uidls);
113                 return;
114         }
115
116         // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
117         // Now go through the UIDL list and look for messages.
118
119         int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
120         syslog(LOG_DEBUG, "pop3client: there are %d messages", num_msgs);
121         for (i=0; i<num_msgs; ++i) {
122                 char oneuidl[1024];
123                 extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
124                 if (strlen(oneuidl) > 2) {
125                         if (oneuidl[strlen(oneuidl)-1] == '\r') {
126                                 oneuidl[strlen(oneuidl)-1] = 0;
127                         }
128                         int this_msg = atoi(oneuidl);
129                         char *c = strchr(oneuidl, ' ');
130                         if (c) strcpy(oneuidl, ++c);
131
132                         // Make up the Use Table record so we can check if we've already seen this message.
133                         StrBuf *UT = NewStrBuf();
134                         StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
135                         int already_seen = CheckIfAlreadySeen(UT);
136                         FreeStrBuf(&UT);
137
138                         // Only fetch the message if we haven't seen it before.
139                         if (already_seen == 0) {
140                                 StrBuf *TheMsg = NewStrBuf();
141                                 snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
142                                 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
143                                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
144                                 res = curl_easy_perform(curl);
145                                 if (res == CURLE_OK) {
146                                         struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
147                                         CtdlSubmitMsg(msg, NULL, room, 0);
148                                         CM_Free(msg);
149                                 }
150                                 else {
151                                         FreeStrBuf(&TheMsg);
152                                 }
153
154                                 // Unless the configuration says to keep the message on the server, delete it.
155                                 if (keep == 0) {
156                                         snprintf(cmd, sizeof cmd, "DELE %d", this_msg);
157                                         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
158                                         res = curl_easy_perform(curl);
159                                 }
160                         }
161                         else {
162                                 syslog(LOG_DEBUG, "pop3client: %s has already been retrieved", oneuidl);
163                         }
164                 }
165         }
166
167         curl_easy_cleanup(curl);
168         FreeStrBuf(&Uidls);
169         return;
170 }
171
172
173 /*
174  * Scan a room's netconfig to determine whether it requires POP3 aggregation
175  */
176 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
177 {
178         const RoomNetCfgLine *pLine;
179         struct p3cq *pptr = NULL;
180
181         if (server_shutting_down) return;
182
183         pLine = OneRNCFG->NetConfigs[pop3client];
184
185         while (pLine != NULL)
186         {
187                 pptr = malloc(sizeof(struct p3cq));
188                 pptr->next = p3cq;
189                 p3cq = pptr;
190                 pptr->room = strdup(qrbuf->QRname);
191                 pptr->host = strdup(ChrPtr(pLine->Value[0]));
192                 pptr->user = strdup(ChrPtr(pLine->Value[1]));
193                 pptr->pass = strdup(ChrPtr(pLine->Value[2]));
194                 pptr->keep = atoi(ChrPtr(pLine->Value[3]));
195                 pptr->interval = atol(ChrPtr(pLine->Value[4]));
196         
197                 pLine = pLine->next;
198         }
199 }
200
201
202 void pop3client_scan(void) {
203         static time_t last_run = 0L;
204         time_t fastest_scan;
205         struct p3cq *pptr = NULL;
206
207         become_session(&pop3_client_CC);
208
209         if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
210                 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
211         }
212         else {
213                 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
214         }
215
216         /*
217          * Run POP3 aggregation no more frequently than once every n seconds
218          */
219         if ( (time(NULL) - last_run) < fastest_scan ) {
220                 return;
221         }
222
223         /*
224          * This is a simple concurrency check to make sure only one pop3client
225          * run is done at a time.  We could do this with a mutex, but since we
226          * don't really require extremely fine granularity here, we'll do it
227          * with a static variable instead.
228          */
229         if (doing_pop3client) return;
230         doing_pop3client = 1;
231
232         syslog(LOG_DEBUG, "pop3client: scan started");
233         CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
234
235         /*
236          * We have to queue and process in separate phases, otherwise we leave a cursor open
237          */
238         syslog(LOG_DEBUG, "pop3client: processing started");
239         while (p3cq != NULL) {
240                 pptr = p3cq;
241                 p3cq = p3cq->next;
242
243                 pop3client_one_mailbox(pptr->room, pptr->host, pptr->user, pptr->pass, pptr->keep, pptr->interval);
244
245                 free(pptr->room);
246                 free(pptr->host);
247                 free(pptr->user);
248                 free(pptr->pass);
249                 free(pptr);
250         }
251
252         syslog(LOG_DEBUG, "pop3client: ended");
253         last_run = time(NULL);
254         doing_pop3client = 0;
255 }
256
257
258 CTDL_MODULE_INIT(pop3client)
259 {
260         if (!threading)
261         {
262                 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
263                 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
264                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
265         }
266
267         /* return our module id for the log */
268         return "pop3client";
269 }