Skeletonize pop3client for easy_curl rewrite
[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 "citadel.h"
40 #include "server.h"
41 #include "citserver.h"
42 #include "support.h"
43 #include "config.h"
44 #include "ctdl_module.h"
45 #include "clientsocket.h"
46 #include "msgbase.h"
47 #include "internet_addressing.h"
48 #include "database.h"
49 #include "citadel_dirs.h"
50
51
52 struct CitContext pop3_client_CC;
53 static int doing_pop3client = 0;
54
55
56 //      This is how we form a USETABLE record for pop3 client
57 //
58 //      StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
59 //                   "pop3/%s/%s:%s@%s",
60 //                   ChrPtr(RecvMsg->RoomName),
61 //                   ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
62 //                   RecvMsg->IO.ConnectMe->User,
63 //                   RecvMsg->IO.ConnectMe->Host
64 //      );
65
66
67 /*
68  * Process one mailbox.
69  */
70 void pop3client_one_mailbox(struct ctdlroom *qrbuf, const char *host, const char *user, const char *pass, int keep, long interval)
71 {
72                 syslog(LOG_DEBUG, "\033[33mpop3client: room=<%s> host=<%s> user=<%s> pass=<%s> keep=<%d> interval=<%ld>\033[0m",
73                         qrbuf->QRname, host, user, pass, keep, interval
74                 );
75 }
76
77
78 /*
79  * Scan a room's netconfig to determine whether it requires POP3 aggregation
80  */
81 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
82 {
83         const RoomNetCfgLine *pLine;
84
85         if (server_shutting_down) return;
86
87         pLine = OneRNCFG->NetConfigs[pop3client];
88
89         while (pLine != NULL)
90         {
91                 pop3client_one_mailbox(qrbuf, 
92                         ChrPtr(pLine->Value[0]),
93                         ChrPtr(pLine->Value[1]),
94                         ChrPtr(pLine->Value[2]),
95                         atoi(ChrPtr(pLine->Value[3])),
96                         atol(ChrPtr(pLine->Value[4]))
97                 );
98                 pLine = pLine->next;
99
100         }
101 }
102
103
104 void pop3client_scan(void) {
105         static time_t last_run = 0L;
106         time_t fastest_scan;
107
108         become_session(&pop3_client_CC);
109
110         if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
111                 fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
112         }
113         else {
114                 fastest_scan = CtdlGetConfigLong("c_pop3_fetch");
115         }
116
117         /*
118          * Run POP3 aggregation no more frequently than once every n seconds
119          */
120         if ( (time(NULL) - last_run) < fastest_scan ) {
121                 return;
122         }
123
124         /*
125          * This is a simple concurrency check to make sure only one pop3client
126          * run is done at a time.  We could do this with a mutex, but since we
127          * don't really require extremely fine granularity here, we'll do it
128          * with a static variable instead.
129          */
130         if (doing_pop3client) return;
131         doing_pop3client = 1;
132
133         syslog(LOG_DEBUG, "pop3client started");
134         CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
135         syslog(LOG_DEBUG, "pop3client ended");
136         last_run = time(NULL);
137         doing_pop3client = 0;
138 }
139
140
141 CTDL_MODULE_INIT(pop3client)
142 {
143         if (!threading)
144         {
145                 CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
146                 CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
147                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
148         }
149
150         /* return our module id for the log */
151         return "pop3client";
152 }