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