parse uidl list
[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
53 struct CitContext pop3_client_CC;
54 static int doing_pop3client = 0;
55
56
57 //      This is how we form a USETABLE record for pop3 client
58 //
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
65 //      );
66
67
68 /*
69  * Process one mailbox.
70  */
71 void pop3client_one_mailbox(struct ctdlroom *qrbuf, const char *host, const char *user, const char *pass, int keep, long interval)
72 {
73         syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>",
74                 qrbuf->QRname, host, user, keep, interval
75         );
76
77         char url[SIZ];
78         CURL *curl;
79         CURLcode res = CURLE_OK;
80         StrBuf *Uidls = NULL;
81         int i;
82
83         curl = curl_easy_init();
84         if (!curl) {
85                 return;
86         }
87
88         Uidls = NewStrBuf();
89
90         curl_easy_setopt(curl, CURLOPT_USERNAME, user);
91         curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
92         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
93         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
94         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
95         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
96         curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls);                       // Give it our StrBuf to work with
97         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
98
99         /* Try POP3S (SSL encrypted) first */
100         snprintf(url, sizeof url, "pop3s://%s", host);
101         curl_easy_setopt(curl, CURLOPT_URL, url);
102         res = curl_easy_perform(curl);
103         if (res == CURLE_OK) {
104         } else {
105                 syslog(LOG_DEBUG, "POP3S client failed: %s , trying POP3 next", curl_easy_strerror(res));
106                 snprintf(url, sizeof url, "pop3://%s", host);                   // try unencrypted next
107                 curl_easy_setopt(curl, CURLOPT_URL, url);
108                 FlushStrBuf(Uidls);
109                 res = curl_easy_perform(curl);
110         }
111
112         if (res != CURLE_OK) {
113                 syslog(LOG_DEBUG, "pop3 client failed: %s", curl_easy_strerror(res));
114                 curl_easy_cleanup(curl);
115                 FreeStrBuf(&Uidls);
116                 return;
117         }
118
119         // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
120         // Now go through the UIDL list and look for messages.
121
122         int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
123         syslog(LOG_DEBUG, "There are %d messages.", num_msgs);
124         for (i=0; i<num_msgs; ++i) {
125                 char oneuidl[1024];
126                 extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
127                 if (strlen(oneuidl) > 2) {
128                         if (oneuidl[strlen(oneuidl)-1] == '\r') {
129                                 oneuidl[strlen(oneuidl)-1] = 0;
130                         }
131                         int this_msg = atoi(oneuidl);
132                         char *c = strchr(oneuidl, ' ');
133                         if (c) strcpy(oneuidl, ++c);
134
135                         syslog(LOG_DEBUG, "<\033[34m%d\033[0m> <\033[34m%s\033[0m>", this_msg, oneuidl);
136
137                 }
138         }
139
140         curl_easy_cleanup(curl);
141         FreeStrBuf(&Uidls);
142         return;
143 }
144
145
146 /*
147  * Scan a room's netconfig to determine whether it requires POP3 aggregation
148  */
149 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
150 {
151         const RoomNetCfgLine *pLine;
152
153         if (server_shutting_down) return;
154
155         pLine = OneRNCFG->NetConfigs[pop3client];
156
157         while (pLine != NULL)
158         {
159                 pop3client_one_mailbox(qrbuf, 
160                         ChrPtr(pLine->Value[0]),
161                         ChrPtr(pLine->Value[1]),
162                         ChrPtr(pLine->Value[2]),
163                         atoi(ChrPtr(pLine->Value[3])),
164                         atol(ChrPtr(pLine->Value[4]))
165                 );
166                 pLine = pLine->next;
167
168         }
169 }
170
171
172 void pop3client_scan(void) {
173         static time_t last_run = 0L;
174         time_t fastest_scan;
175
176         become_session(&pop3_client_CC);
177
178         if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
179                 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
180         }
181         else {
182                 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
183         }
184
185         /*
186          * Run POP3 aggregation no more frequently than once every n seconds
187          */
188         if ( (time(NULL) - last_run) < fastest_scan ) {
189                 return;
190         }
191
192         /*
193          * This is a simple concurrency check to make sure only one pop3client
194          * run is done at a time.  We could do this with a mutex, but since we
195          * don't really require extremely fine granularity here, we'll do it
196          * with a static variable instead.
197          */
198         if (doing_pop3client) return;
199         doing_pop3client = 1;
200
201         syslog(LOG_DEBUG, "pop3client started");
202         CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
203         syslog(LOG_DEBUG, "pop3client ended");
204         last_run = time(NULL);
205         doing_pop3client = 0;
206 }
207
208
209 CTDL_MODULE_INIT(pop3client)
210 {
211         if (!threading)
212         {
213                 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
214                 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
215                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
216         }
217
218         /* return our module id for the log */
219         return "pop3client";
220 }