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