Found a MUCH better way to store the UIDL's of
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * $Id$
3  *
4  * Consolidate mail from remote POP3 accounts.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
14 # include <time.h>
15 #else
16 # if HAVE_SYS_TIME_H
17 #  include <sys/time.h>
18 # else
19 #  include <time.h>
20 # endif
21 #endif
22
23 #include <ctype.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include "citadel.h"
29 #include "server.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "tools.h"
34 #include "room_ops.h"
35 #include "ctdl_module.h"
36 #include "clientsocket.h"
37 #include "msgbase.h"
38 #include "internet_addressing.h"
39 #include "database.h"
40 #include "citadel_dirs.h"
41
42 struct pop3aggr {
43         struct pop3aggr *next;
44         char roomname[ROOMNAMELEN];
45         char pop3host[128];
46         char pop3user[128];
47         char pop3pass[128];
48 };
49
50 struct pop3aggr *palist = NULL;
51
52
53 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int delete_from_server)
54 {
55         int sock;
56         char buf[SIZ];
57         int msg_to_fetch = 0;
58         int *msglist = NULL;
59         int num_msgs = 0;
60         int alloc_msgs = 0;
61         int i;
62         char *body = NULL;
63         struct CtdlMessage *msg = NULL;
64         long msgnum = 0;
65         char this_uidl[64];
66         char utmsgid[SIZ];
67         struct cdbdata *cdbut;
68         struct UseTable ut;
69
70         lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
71         lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
72         sock = sock_connect(pop3host, "110", "tcp");
73         if (sock < 0) {
74                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
75                 return;
76         }
77         
78         lprintf(CTDL_DEBUG, "Connected!\n");
79
80         /* Read the server greeting */
81         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
82         lprintf(CTDL_DEBUG, ">%s\n", buf);
83         if (strncasecmp(buf, "+OK", 3)) goto bail;
84
85         /* Identify ourselves.  NOTE: we have to append a CR to each command.  The LF will
86          * automatically be appended by sock_puts().  Believe it or not, leaving out the CR
87          * will cause problems if the server happens to be Exchange, which is so b0rken it
88          * actually barfs on LF-terminated newlines.
89          */
90         snprintf(buf, sizeof buf, "USER %s\r", pop3user);
91         lprintf(CTDL_DEBUG, "<%s\n", buf);
92         if (sock_puts(sock, buf) <0) goto bail;
93         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
94         lprintf(CTDL_DEBUG, ">%s\n", buf);
95         if (strncasecmp(buf, "+OK", 3)) goto bail;
96
97         /* Password */
98         snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
99         lprintf(CTDL_DEBUG, "<PASS <password>\n");
100         if (sock_puts(sock, buf) <0) goto bail;
101         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
102         lprintf(CTDL_DEBUG, ">%s\n", buf);
103         if (strncasecmp(buf, "+OK", 3)) goto bail;
104
105         /* Get the list of messages */
106         snprintf(buf, sizeof buf, "LIST\r");
107         lprintf(CTDL_DEBUG, "<%s\n", buf);
108         if (sock_puts(sock, buf) <0) goto bail;
109         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
110         lprintf(CTDL_DEBUG, ">%s\n", buf);
111         if (strncasecmp(buf, "+OK", 3)) goto bail;
112
113         do {
114                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
115                 lprintf(CTDL_DEBUG, ">%s\n", buf);
116                 msg_to_fetch = atoi(buf);
117                 if (msg_to_fetch > 0) {
118                         if (alloc_msgs == 0) {
119                                 alloc_msgs = 100;
120                                 msglist = malloc((alloc_msgs * (sizeof(int))));
121                         }
122                         else if (num_msgs >= alloc_msgs) {
123                                 alloc_msgs = alloc_msgs * 2;
124                                 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
125                         }
126                         if (msglist == NULL) goto bail;
127                         msglist[num_msgs++] = msg_to_fetch;
128                 }
129         } while (buf[0] != '.');
130
131         if (num_msgs) for (i=0; i<num_msgs; ++i) {
132
133                 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
134                 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
135                 lprintf(CTDL_DEBUG, "<%s\n", buf);
136                 if (sock_puts(sock, buf) <0) goto bail;
137                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
138                 lprintf(CTDL_DEBUG, ">%s\n", buf);
139                 if (strncasecmp(buf, "+OK", 3)) goto bail;
140                 extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
141
142                 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", pop3user, this_uidl, pop3host);
143
144                 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
145                 if (cdbut != NULL) {
146                         lprintf(CTDL_DEBUG, "%s has ALREADY BEEN SEEN\n", utmsgid);
147                         /* message has already been seen */
148                         cdb_free(cdbut);
149
150                         /* rewrite the record anyway, to update the timestamp */
151                         strcpy(ut.ut_msgid, utmsgid);
152                         ut.ut_timestamp = time(NULL);
153                         cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
154                 }
155
156                 else {
157                         /* message has not been seen -- fetch it! */
158                         lprintf(CTDL_DEBUG, "%s has NOT BEEN SEEN\n", utmsgid);
159
160                         /* Tell the server to fetch the message */
161                         snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
162                         lprintf(CTDL_DEBUG, "<%s\n", buf);
163                         if (sock_puts(sock, buf) <0) goto bail;
164                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
165                         lprintf(CTDL_DEBUG, ">%s\n", buf);
166                         if (strncasecmp(buf, "+OK", 3)) goto bail;
167         
168                         /* If we get to this point, the message is on its way.  Read it. */
169                         body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
170                         if (body == NULL) goto bail;
171         
172                         lprintf(CTDL_DEBUG, "Converting message...\n");
173                         msg = convert_internet_message(body);
174                         body = NULL;    /* yes, this should be dereferenced, NOT freed */
175         
176                         /* Do Something With It (tm) */
177                         msgnum = CtdlSubmitMsg(msg, NULL, roomname);
178                         if (msgnum > 0L) {
179                                 /* Message has been committed to the store */
180         
181                                 if (delete_from_server) {
182                                         snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
183                                         lprintf(CTDL_DEBUG, "<%s\n", buf);
184                                         if (sock_puts(sock, buf) <0) goto bail;
185                                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
186                                         lprintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
187                                 }
188
189                                 /* write the uidl to the use table so we don't fetch this message again */
190                                 strcpy(ut.ut_msgid, utmsgid);
191                                 ut.ut_timestamp = time(NULL);
192                                 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
193                                                 &ut, sizeof(struct UseTable) );
194                         }
195                         CtdlFreeMessage(msg);
196
197                 }
198         }
199
200         /* Log out */
201         snprintf(buf, sizeof buf, "QUIT\r");
202         lprintf(CTDL_DEBUG, "<%s\n", buf);
203         if (sock_puts(sock, buf) <0) goto bail;
204         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
205         lprintf(CTDL_DEBUG, ">%s\n", buf);
206 bail:   sock_close(sock);
207         if (msglist) free(msglist);
208 }
209
210
211 /*
212  * Scan a room's netconfig to determine whether it requires POP3 aggregation
213  */
214 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
215 {
216         char filename[PATH_MAX];
217         char buf[1024];
218         char instr[32];
219         FILE *fp;
220         struct pop3aggr *pptr;
221
222         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
223
224         /* Only do net processing for rooms that have netconfigs */
225         fp = fopen(filename, "r");
226         if (fp == NULL) {
227                 return;
228         }
229
230         while (fgets(buf, sizeof buf, fp) != NULL) {
231                 buf[strlen(buf)-1] = 0;
232
233                 extract_token(instr, buf, 0, '|', sizeof instr);
234                 if (!strcasecmp(instr, "pop3client")) {
235                         pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
236                         if (pptr != NULL) {
237                                 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
238                                 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
239                                 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
240                                 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
241                                 pptr->next = palist;
242                                 palist = pptr;
243                         }
244                 }
245
246         }
247
248         fclose(fp);
249
250 }
251
252
253 void pop3client_scan(void) {
254         static time_t last_run = 0L;
255         static int doing_pop3client = 0;
256         struct pop3aggr *pptr;
257
258         /*
259          * Run POP3 aggregation no more frequently than once every n seconds
260          */
261         if ( (time(NULL) - last_run) < config.c_net_freq ) {
262                 return;
263         }
264
265         /*
266          * This is a simple concurrency check to make sure only one pop3client run
267          * is done at a time.  We could do this with a mutex, but since we
268          * don't really require extremely fine granularity here, we'll do it
269          * with a static variable instead.
270          */
271         if (doing_pop3client) return;
272         doing_pop3client = 1;
273
274         lprintf(CTDL_DEBUG, "pop3client started\n");
275         ForEachRoom(pop3client_scan_room, NULL);
276
277         while (palist != NULL) {
278                 /* FIXME set delete_from_server to 1 if the user wants to */
279                 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass, 0);
280                 pptr = palist;
281                 palist = palist->next;
282                 free(pptr);
283         }
284
285         lprintf(CTDL_DEBUG, "pop3client ended\n");
286         last_run = time(NULL);
287         doing_pop3client = 0;
288 }
289
290
291 CTDL_MODULE_INIT(pop3client)
292 {
293         CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
294
295         /* return our Subversion id for the Log */
296         return "$Id$";
297 }