4 * Aggregate remote POP3 accounts
6 * NOTE: this is disabled in Citadel 7.20 -- enable with -DPOP3_AGGREGATION at your own risk.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
30 #include "citserver.h"
35 #include "ctdl_module.h"
36 #include "clientsocket.h"
38 #include "internet_addressing.h"
41 struct pop3aggr *next;
42 char roomname[ROOMNAMELEN];
48 struct pop3aggr *palist = NULL;
50 #ifdef POP3_AGGREGATION
53 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
63 struct CtdlMessage *msg = NULL;
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");
70 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
74 lprintf(CTDL_DEBUG, "Connected!\n");
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;
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;
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;
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;
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) {
112 msglist = malloc((alloc_msgs * (sizeof(int))));
114 else if (num_msgs >= alloc_msgs) {
115 alloc_msgs = alloc_msgs * 2;
116 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
118 if (msglist == NULL) goto bail;
119 msglist[num_msgs++] = msg_to_fetch;
121 } while (buf[0] != '.');
123 if (num_msgs) for (i=0; i<num_msgs; ++i) {
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;
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;
137 lprintf(CTDL_DEBUG, "Converting message...\n");
138 msg = convert_internet_message(body);
139 body = NULL; /* yes, this should be dereferenced, NOT freed */
141 /* Do Something With It (tm) */
142 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
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;
152 CtdlFreeMessage(msg);
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);
167 * Scan a room's netconfig to determine whether it requires POP3 aggregation
169 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
171 char filename[PATH_MAX];
175 struct pop3aggr *pptr;
177 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
179 /* Only do net processing for rooms that have netconfigs */
180 fp = fopen(filename, "r");
185 while (fgets(buf, sizeof buf, fp) != NULL) {
186 buf[strlen(buf)-1] = 0;
188 extract_token(instr, buf, 0, '|', sizeof instr);
189 if (!strcasecmp(instr, "pop3client")) {
190 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
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);
208 void pop3client_scan(void) {
209 static time_t last_run = 0L;
210 static int doing_pop3client = 0;
211 struct pop3aggr *pptr;
214 * Run POP3 aggregation no more frequently than once every n seconds
216 if ( (time(NULL) - last_run) < config.c_net_freq ) {
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.
226 if (doing_pop3client) return;
227 doing_pop3client = 1;
229 lprintf(CTDL_DEBUG, "pop3client started\n");
230 ForEachRoom(pop3client_scan_room, NULL);
232 while (palist != NULL) {
233 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
235 palist = palist->next;
239 lprintf(CTDL_DEBUG, "pop3client ended\n");
240 last_run = time(NULL);
241 doing_pop3client = 0;
246 CTDL_MODULE_INIT(pop3client)
248 #ifdef POP3_AGGREGATION
249 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
252 /* return our Subversion id for the Log */