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