Minor tagging and comments
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * $Id: $
3  *
4  * Aggregate remote POP3 accounts
5  *
6  * NOTE: this is disabled in Citadel 7.20 -- enable with -DPOP3_AGGREGATION at your own risk.
7  *
8  */
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 #  include <sys/time.h>
20 # else
21 #  include <time.h>
22 # endif
23 #endif
24
25 #include <ctype.h>
26 #include <string.h>
27 #include <errno.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
40 struct pop3aggr {
41         struct pop3aggr *next;
42         char roomname[ROOMNAMELEN];
43         char pop3host[128];
44         char pop3user[128];
45         char pop3pass[128];
46 };
47
48 struct pop3aggr *palist = NULL;
49
50 #ifdef POP3_AGGREGATION
51
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
66         lprintf(CTDL_DEBUG, "POP3: %s %s %s %s\n", roomname, pop3host, pop3user, pop3pass);
67         lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
68         sock = sock_connect(pop3host, "110", "tcp");
69         if (sock < 0) {
70                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
71                 return;
72         }
73         
74         lprintf(CTDL_DEBUG, "Connected!\n");
75
76         /* Read the server greeting */
77         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
78         lprintf(CTDL_DEBUG, ">%s\n", buf);
79         if (strncasecmp(buf, "+OK", 3)) goto bail;
80
81         /* Identify ourselves */
82         snprintf(buf, sizeof buf, "USER %s", pop3user);
83         lprintf(CTDL_DEBUG, "<%s\n", buf);
84         if (sock_puts(sock, buf) <0) goto bail;
85         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
86         lprintf(CTDL_DEBUG, ">%s\n", buf);
87         if (strncasecmp(buf, "+OK", 3)) goto bail;
88
89         /* Password */
90         snprintf(buf, sizeof buf, "PASS %s", pop3pass);
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         /* Get the list of messages */
98         snprintf(buf, sizeof buf, "LIST");
99         lprintf(CTDL_DEBUG, "<%s\n", buf);
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         do {
106                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
107                 lprintf(CTDL_DEBUG, ">%s\n", buf);
108                 msg_to_fetch = atoi(buf);
109                 if (msg_to_fetch > 0) {
110                         if (alloc_msgs == 0) {
111                                 alloc_msgs = 100;
112                                 msglist = malloc((alloc_msgs * (sizeof(int))));
113                         }
114                         else if (num_msgs >= alloc_msgs) {
115                                 alloc_msgs = alloc_msgs * 2;
116                                 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
117                         }
118                         if (msglist == NULL) goto bail;
119                         msglist[num_msgs++] = msg_to_fetch;
120                 }
121         } while (buf[0] != '.');
122
123         if (num_msgs) for (i=0; i<num_msgs; ++i) {
124
125                 /* Tell the server to fetch the message */
126                 snprintf(buf, sizeof buf, "RETR %d", msglist[i]);
127                 lprintf(CTDL_DEBUG, "<%s\n", buf);
128                 if (sock_puts(sock, buf) <0) goto bail;
129                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
130                 lprintf(CTDL_DEBUG, ">%s\n", buf);
131                 if (strncasecmp(buf, "+OK", 3)) goto bail;
132
133                 /* If we get to this point, the message is on its way.  Read it. */
134                 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
135                 if (body == NULL) goto bail;
136
137                 lprintf(CTDL_DEBUG, "Converting message...\n");
138                 msg = convert_internet_message(body);
139                 body = NULL;    /* yes, this should be dereferenced, NOT freed */
140
141                 /* Do Something With It (tm) */
142                 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
143                 if (msgnum > 0L) {
144                         /* Message has been committed to the store, so delete it from the remote server */
145                         snprintf(buf, sizeof buf, "DELE %d", msglist[i]);
146                         lprintf(CTDL_DEBUG, "<%s\n", buf);
147                         if (sock_puts(sock, buf) <0) goto bail;
148                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
149                         lprintf(CTDL_DEBUG, ">%s\n", buf);
150                         if (strncasecmp(buf, "+OK", 3)) goto bail;
151                 }
152                 CtdlFreeMessage(msg);
153         }
154
155         /* Log out */
156         snprintf(buf, sizeof buf, "QUIT");
157         lprintf(CTDL_DEBUG, "<%s\n", buf);
158         if (sock_puts(sock, buf) <0) goto bail;
159         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
160         lprintf(CTDL_DEBUG, ">%s\n", buf);
161 bail:   sock_close(sock);
162         if (msglist) free(msglist);
163 }
164
165
166 /*
167  * Scan a room's netconfig to determine whether it requires POP3 aggregation
168  */
169 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
170 {
171         char filename[PATH_MAX];
172         char buf[1024];
173         char instr[32];
174         FILE *fp;
175         struct pop3aggr *pptr;
176
177         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
178
179         /* Only do net processing for rooms that have netconfigs */
180         fp = fopen(filename, "r");
181         if (fp == NULL) {
182                 return;
183         }
184
185         while (fgets(buf, sizeof buf, fp) != NULL) {
186                 buf[strlen(buf)-1] = 0;
187
188                 extract_token(instr, buf, 0, '|', sizeof instr);
189                 if (!strcasecmp(instr, "pop3client")) {
190                         pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
191                         if (pptr != NULL) {
192                                 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
193                                 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
194                                 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
195                                 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
196                                 pptr->next = palist;
197                                 palist = pptr;
198                         }
199                 }
200
201         }
202
203         fclose(fp);
204
205 }
206
207
208 void pop3client_scan(void) {
209         static time_t last_run = 0L;
210         static int doing_pop3client = 0;
211         struct pop3aggr *pptr;
212
213         /*
214          * Run POP3 aggregation no more frequently than once every n seconds
215          */
216         if ( (time(NULL) - last_run) < config.c_net_freq ) {
217                 return;
218         }
219
220         /*
221          * This is a simple concurrency check to make sure only one pop3client run
222          * is done at a time.  We could do this with a mutex, but since we
223          * don't really require extremely fine granularity here, we'll do it
224          * with a static variable instead.
225          */
226         if (doing_pop3client) return;
227         doing_pop3client = 1;
228
229         lprintf(CTDL_DEBUG, "pop3client started\n");
230         ForEachRoom(pop3client_scan_room, NULL);
231
232         while (palist != NULL) {
233                 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
234                 pptr = palist;
235                 palist = palist->next;
236                 free(pptr);
237         }
238
239         lprintf(CTDL_DEBUG, "pop3client ended\n");
240         last_run = time(NULL);
241         doing_pop3client = 0;
242 }
243
244 #endif
245
246 CTDL_MODULE_INIT(pop3client)
247 {
248 #ifdef POP3_AGGREGATION
249         CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
250 #endif
251
252         /* return our Subversion id for the Log */
253         return "$Id:  $";
254 }