Here it is, the new thread interface.
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
index 82937f891bc9a7a2f88d1e10a5f7c6878b299aff..b3227707b9495b9566ee720ae58f0c4471df68fb 100644 (file)
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "tools.h"
 #include "room_ops.h"
 #include "ctdl_module.h"
 #include "clientsocket.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
+#include "database.h"
+#include "citadel_dirs.h"
 
 struct pop3aggr {
        struct pop3aggr *next;
@@ -41,11 +45,13 @@ struct pop3aggr {
        char pop3host[128];
        char pop3user[128];
        char pop3pass[128];
+       int keep;
 };
 
 struct pop3aggr *palist = NULL;
 
-void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
+
+void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int keep)
 {
        int sock;
        char buf[SIZ];
@@ -58,6 +64,9 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        struct CtdlMessage *msg = NULL;
        long msgnum = 0;
        char this_uidl[64];
+       char utmsgid[SIZ];
+       struct cdbdata *cdbut;
+       struct UseTable ut;
 
        lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
        lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
@@ -129,36 +138,59 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
                lprintf(CTDL_DEBUG, ">%s\n", buf);
                if (strncasecmp(buf, "+OK", 3)) goto bail;
-               extract_token(this_uidl, buf, 3, ' ', sizeof this_uidl);
-
-               /* Tell the server to fetch the message */
-               snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
-               lprintf(CTDL_DEBUG, "<%s\n", buf);
-               if (sock_puts(sock, buf) <0) goto bail;
-               if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
-               lprintf(CTDL_DEBUG, ">%s\n", buf);
-               if (strncasecmp(buf, "+OK", 3)) goto bail;
+               extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
 
-               /* If we get to this point, the message is on its way.  Read it. */
-               body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
-               if (body == NULL) goto bail;
+               snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
 
-               lprintf(CTDL_DEBUG, "Converting message...\n");
-               msg = convert_internet_message(body);
-               body = NULL;    /* yes, this should be dereferenced, NOT freed */
+               cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
+               if (cdbut != NULL) {
+                       /* message has already been seen */
+                       lprintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
+                       cdb_free(cdbut);
 
-               /* Do Something With It (tm) */
-               msgnum = CtdlSubmitMsg(msg, NULL, roomname);
-               if (msgnum > 0L) {
-                       /* Message has been committed to the store, so delete it from the remote server */
-                       snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
+                       /* rewrite the record anyway, to update the timestamp */
+                       strcpy(ut.ut_msgid, utmsgid);
+                       ut.ut_timestamp = time(NULL);
+                       cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
+               }
+               else {
+                       /* Message has not been seen. Tell the server to fetch the message... */
+                       snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
                        lprintf(CTDL_DEBUG, "<%s\n", buf);
                        if (sock_puts(sock, buf) <0) goto bail;
                        if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
                        lprintf(CTDL_DEBUG, ">%s\n", buf);
                        if (strncasecmp(buf, "+OK", 3)) goto bail;
+       
+                       /* If we get to this point, the message is on its way.  Read it. */
+                       body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
+                       if (body == NULL) goto bail;
+       
+                       lprintf(CTDL_DEBUG, "Converting message...\n");
+                       msg = convert_internet_message(body);
+                       body = NULL;    /* yes, this should be dereferenced, NOT freed */
+       
+                       /* Do Something With It (tm) */
+                       msgnum = CtdlSubmitMsg(msg, NULL, roomname);
+                       if (msgnum > 0L) {
+                               /* Message has been committed to the store */
+       
+                               if (!keep) {
+                                       snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
+                                       lprintf(CTDL_DEBUG, "<%s\n", buf);
+                                       if (sock_puts(sock, buf) <0) goto bail;
+                                       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+                                       lprintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
+                               }
+
+                               /* write the uidl to the use table so we don't fetch this message again */
+                               strcpy(ut.ut_msgid, utmsgid);
+                               ut.ut_timestamp = time(NULL);
+                               cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
+                                               &ut, sizeof(struct UseTable) );
+                       }
+                       CtdlFreeMessage(msg);
                }
-               CtdlFreeMessage(msg);
        }
 
        /* Log out */
@@ -202,6 +234,7 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                                extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
                                extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
                                extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
+                               pptr->keep = extract_int(buf, 4);
                                pptr->next = palist;
                                palist = pptr;
                        }
@@ -239,7 +272,8 @@ void pop3client_scan(void) {
        ForEachRoom(pop3client_scan_room, NULL);
 
        while (palist != NULL) {
-               pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
+               pop3_do_fetching(palist->roomname, palist->pop3host,
+                               palist->pop3user, palist->pop3pass, palist->keep);
                pptr = palist;
                palist = palist->next;
                free(pptr);
@@ -253,8 +287,11 @@ void pop3client_scan(void) {
 
 CTDL_MODULE_INIT(pop3client)
 {
-       CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
-
+       if (!threading)
+       {
+               CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
+       }
+       
        /* return our Subversion id for the Log */
         return "$Id$";
 }