2 * Aggregate remote POP3 accounts
10 # include <sys/time.h>
14 # include <sys/time.h>
25 #include "citserver.h"
30 #include "ctdl_module.h"
31 #include "clientsocket.h"
33 #include "internet_addressing.h"
36 struct pop3aggr *next;
37 char roomname[ROOMNAMELEN];
43 struct pop3aggr *palist = NULL;
45 #ifdef POP3_AGGREGATION
48 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
58 struct CtdlMessage *msg = NULL;
61 lprintf(CTDL_DEBUG, "POP3: %s %s %s %s\n", roomname, pop3host, pop3user, pop3pass);
62 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
63 sock = sock_connect(pop3host, "110", "tcp");
65 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
69 lprintf(CTDL_DEBUG, "Connected!\n");
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;
76 /* Identify ourselves */
77 snprintf(buf, sizeof buf, "USER %s", pop3user);
78 lprintf(CTDL_DEBUG, "<%s\n", buf);
79 if (sock_puts(sock, buf) <0) goto bail;
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;
85 snprintf(buf, sizeof buf, "PASS %s", pop3pass);
86 lprintf(CTDL_DEBUG, "<%s\n", buf);
87 if (sock_puts(sock, buf) <0) goto bail;
88 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
89 lprintf(CTDL_DEBUG, ">%s\n", buf);
90 if (strncasecmp(buf, "+OK", 3)) goto bail;
92 /* Get the list of messages */
93 snprintf(buf, sizeof buf, "LIST");
94 lprintf(CTDL_DEBUG, "<%s\n", buf);
95 if (sock_puts(sock, buf) <0) goto bail;
96 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
97 lprintf(CTDL_DEBUG, ">%s\n", buf);
98 if (strncasecmp(buf, "+OK", 3)) goto bail;
101 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
102 lprintf(CTDL_DEBUG, ">%s\n", buf);
103 msg_to_fetch = atoi(buf);
104 if (msg_to_fetch > 0) {
105 if (alloc_msgs == 0) {
107 msglist = malloc((alloc_msgs * (sizeof(int))));
109 else if (num_msgs >= alloc_msgs) {
110 alloc_msgs = alloc_msgs * 2;
111 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
113 if (msglist == NULL) goto bail;
114 msglist[num_msgs++] = msg_to_fetch;
116 } while (buf[0] != '.');
118 if (num_msgs) for (i=0; i<num_msgs; ++i) {
120 /* Tell the server to fetch the message */
121 snprintf(buf, sizeof buf, "RETR %d", msglist[i]);
122 lprintf(CTDL_DEBUG, "<%s\n", buf);
123 if (sock_puts(sock, buf) <0) goto bail;
124 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
125 lprintf(CTDL_DEBUG, ">%s\n", buf);
126 if (strncasecmp(buf, "+OK", 3)) goto bail;
128 /* If we get to this point, the message is on its way. Read it. */
129 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
130 if (body == NULL) goto bail;
132 lprintf(CTDL_DEBUG, "Converting message...\n");
133 msg = convert_internet_message(body);
134 body = NULL; /* yes, this should be dereferenced, NOT freed */
136 /* Do Something With It (tm) */
137 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
139 /* Message has been committed to the store, so delete it from the remote server */
140 snprintf(buf, sizeof buf, "DELE %d", msglist[i]);
141 lprintf(CTDL_DEBUG, "<%s\n", buf);
142 if (sock_puts(sock, buf) <0) goto bail;
143 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
144 lprintf(CTDL_DEBUG, ">%s\n", buf);
145 if (strncasecmp(buf, "+OK", 3)) goto bail;
147 CtdlFreeMessage(msg);
151 snprintf(buf, sizeof buf, "QUIT");
152 lprintf(CTDL_DEBUG, "<%s\n", buf);
153 if (sock_puts(sock, buf) <0) goto bail;
154 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
155 lprintf(CTDL_DEBUG, ">%s\n", buf);
156 bail: sock_close(sock);
157 if (msglist) free(msglist);
162 * Scan a room's netconfig to determine whether it requires POP3 aggregation
164 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
166 char filename[PATH_MAX];
170 struct pop3aggr *pptr;
172 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
174 /* Only do net processing for rooms that have netconfigs */
175 fp = fopen(filename, "r");
180 while (fgets(buf, sizeof buf, fp) != NULL) {
181 buf[strlen(buf)-1] = 0;
183 extract_token(instr, buf, 0, '|', sizeof instr);
184 if (!strcasecmp(instr, "pop3client")) {
185 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
187 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
188 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
189 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
190 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
203 void pop3client_scan(void) {
204 static time_t last_run = 0L;
205 static int doing_pop3client = 0;
206 struct pop3aggr *pptr;
209 * Run POP3 aggregation no more frequently than once every n seconds
211 if ( (time(NULL) - last_run) < config.c_net_freq ) {
216 * This is a simple concurrency check to make sure only one pop3client run
217 * is done at a time. We could do this with a mutex, but since we
218 * don't really require extremely fine granularity here, we'll do it
219 * with a static variable instead.
221 if (doing_pop3client) return;
222 doing_pop3client = 1;
224 lprintf(CTDL_DEBUG, "pop3client started\n");
225 ForEachRoom(pop3client_scan_room, NULL);
227 while (palist != NULL) {
228 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
230 palist = palist->next;
234 lprintf(CTDL_DEBUG, "pop3client ended\n");
235 last_run = time(NULL);
236 doing_pop3client = 0;
241 CTDL_MODULE_INIT(pop3client)
243 #ifdef POP3_AGGREGATION
244 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
247 /* return our Subversion id for the Log */