Removed an unused parameter from CtdlSubmitMsg(). Why was it even there?
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * Consolidate mail from remote POP3 accounts.
3  *
4  * Copyright (c) 2007-2020 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 static int doing_pop3client = 0;
63 struct p3cq *p3cq = NULL;
64
65 /*
66  * Process one mailbox.
67  */
68 void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval)
69 {
70         syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
71
72         char url[SIZ];
73         CURL *curl;
74         CURLcode res = CURLE_OK;
75         StrBuf *Uidls = NULL;
76         int i;
77         char cmd[1024];
78
79         curl = curl_easy_init();
80         if (!curl) {
81                 return;
82         }
83
84         Uidls = NewStrBuf();
85
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");
94
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) {
100         } else {
101                 syslog(LOG_DEBUG, "pop3client: POP3S connection 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);
104                 FlushStrBuf(Uidls);
105                 res = curl_easy_perform(curl);
106         }
107
108         if (res != CURLE_OK) {
109                 syslog(LOG_DEBUG, "pop3client: POP3 connection failed: %s", curl_easy_strerror(res));
110                 curl_easy_cleanup(curl);
111                 FreeStrBuf(&Uidls);
112                 return;
113         }
114
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.
117
118         int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
119         syslog(LOG_DEBUG, "pop3client: there are %d messages", num_msgs);
120         for (i=0; i<num_msgs; ++i) {
121                 char oneuidl[1024];
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;
126                         }
127                         int this_msg = atoi(oneuidl);
128                         char *c = strchr(oneuidl, ' ');
129                         if (c) strcpy(oneuidl, ++c);
130
131                         // Make up the Use Table record so we can check if we've already seen this message.
132                         StrBuf *UT = NewStrBuf();
133                         StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
134                         int already_seen = CheckIfAlreadySeen(UT);
135                         FreeStrBuf(&UT);
136
137                         // Only fetch the message if we haven't seen it before.
138                         if (already_seen == 0) {
139                                 StrBuf *TheMsg = NewStrBuf();
140                                 snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
141                                 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
142                                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
143                                 res = curl_easy_perform(curl);
144                                 if (res == CURLE_OK) {
145                                         struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
146                                         CtdlSubmitMsg(msg, NULL, room);
147                                         CM_Free(msg);
148                                 }
149                                 else {
150                                         FreeStrBuf(&TheMsg);
151                                 }
152
153                                 // Unless the configuration says to keep the message on the server, delete it.
154                                 if (keep == 0) {
155                                         snprintf(cmd, sizeof cmd, "DELE %d", this_msg);
156                                         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
157                                         res = curl_easy_perform(curl);
158                                 }
159                         }
160                         else {
161                                 syslog(LOG_DEBUG, "pop3client: %s has already been retrieved", oneuidl);
162                         }
163                 }
164         }
165
166         curl_easy_cleanup(curl);
167         FreeStrBuf(&Uidls);
168         return;
169 }
170
171
172 /*
173  * Scan a room's netconfig to determine whether it requires POP3 aggregation
174  */
175 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
176 {
177         const RoomNetCfgLine *pLine;
178         struct p3cq *pptr = NULL;
179
180         if (server_shutting_down) return;
181
182         pLine = OneRNCFG->NetConfigs[pop3client];
183
184         while (pLine != NULL)
185         {
186                 pptr = malloc(sizeof(struct p3cq));
187                 pptr->next = p3cq;
188                 p3cq = pptr;
189                 pptr->room = strdup(qrbuf->QRname);
190                 pptr->host = strdup(ChrPtr(pLine->Value[0]));
191                 pptr->user = strdup(ChrPtr(pLine->Value[1]));
192                 pptr->pass = strdup(ChrPtr(pLine->Value[2]));
193                 pptr->keep = atoi(ChrPtr(pLine->Value[3]));
194                 pptr->interval = atol(ChrPtr(pLine->Value[4]));
195         
196                 pLine = pLine->next;
197         }
198 }
199
200
201 void pop3client_scan(void) {
202         static time_t last_run = 0L;
203         time_t fastest_scan;
204         struct p3cq *pptr = NULL;
205
206         if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
207                 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
208         }
209         else {
210                 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
211         }
212
213         /*
214          * Run POP3 aggregation no more frequently than once every n seconds
215          */
216         if ( (time(NULL) - last_run) < fastest_scan ) {
217                 return;
218         }
219
220         /*
221          * This is a simple concurrency check to make sure only one pop3client
222          * run is done at a time.  We could do this with a mutex, but since we
223          * don't really require extremely fine granularity here, we'll do it
224          * with a static variable instead.
225          */
226         if (doing_pop3client) return;
227         doing_pop3client = 1;
228
229         syslog(LOG_DEBUG, "pop3client: scan started");
230         CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
231
232         /*
233          * We have to queue and process in separate phases, otherwise we leave a cursor open
234          */
235         syslog(LOG_DEBUG, "pop3client: processing started");
236         while (p3cq != NULL) {
237                 pptr = p3cq;
238                 p3cq = p3cq->next;
239
240                 pop3client_one_mailbox(pptr->room, pptr->host, pptr->user, pptr->pass, pptr->keep, pptr->interval);
241
242                 free(pptr->room);
243                 free(pptr->host);
244                 free(pptr->user);
245                 free(pptr->pass);
246                 free(pptr);
247         }
248
249         syslog(LOG_DEBUG, "pop3client: ended");
250         last_run = time(NULL);
251         doing_pop3client = 0;
252 }
253
254
255 CTDL_MODULE_INIT(pop3client)
256 {
257         if (!threading)
258         {
259                 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
260                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
261         }
262
263         /* return our module id for the log */
264         return "pop3client";
265 }