pop3 retrieval working again
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
index 2b713830d6f08ac177aa88c39e4d77554c10101b..a859fb51e7f0f04612acf999b0716416bd7d2516 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Consolidate mail from remote POP3 accounts.
  *
- * Copyright (c) 2007-2017 by the citadel.org team
+ * Copyright (c) 2007-2021 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
 #include <unistd.h>
 #include <stdio.h>
 #include <sysconfig.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
@@ -59,15 +48,13 @@ struct p3cq {                               // module-local queue of pop3 client work that needs processing
        long interval;
 };
 
-struct CitContext pop3_client_CC;
 static int doing_pop3client = 0;
 struct p3cq *p3cq = NULL;
 
 /*
  * Process one mailbox.
  */
-void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval)
-{
+void pop3client_one_mailbox(char *room, const char *host, const char *user, const char *pass, int keep, long interval) {
        syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>", room, host, user, keep, interval);
 
        char url[SIZ];
@@ -75,6 +62,7 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
        CURLcode res = CURLE_OK;
        StrBuf *Uidls = NULL;
        int i;
+       char cmd[1024];
 
        curl = curl_easy_init();
        if (!curl) {
@@ -97,8 +85,9 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
        curl_easy_setopt(curl, CURLOPT_URL, url);
        res = curl_easy_perform(curl);
        if (res == CURLE_OK) {
-       } else {
-               syslog(LOG_DEBUG, "POP3S client failed: %s , trying POP3 next", curl_easy_strerror(res));
+       }
+       else {
+               syslog(LOG_DEBUG, "pop3client: POP3S connection failed: %s , trying POP3 next", curl_easy_strerror(res));
                snprintf(url, sizeof url, "pop3://%s", host);                   // try unencrypted next
                curl_easy_setopt(curl, CURLOPT_URL, url);
                FlushStrBuf(Uidls);
@@ -106,7 +95,7 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
        }
 
        if (res != CURLE_OK) {
-               syslog(LOG_DEBUG, "pop3 client failed: %s", curl_easy_strerror(res));
+               syslog(LOG_DEBUG, "pop3client: POP3 connection failed: %s", curl_easy_strerror(res));
                curl_easy_cleanup(curl);
                FreeStrBuf(&Uidls);
                return;
@@ -116,7 +105,7 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
        // Now go through the UIDL list and look for messages.
 
        int num_msgs = num_tokens(ChrPtr(Uidls), '\n');
-       syslog(LOG_DEBUG, "There are %d messages.", num_msgs);
+       syslog(LOG_DEBUG, "pop3client: there are %d messages", num_msgs);
        for (i=0; i<num_msgs; ++i) {
                char oneuidl[1024];
                extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
@@ -128,15 +117,38 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
                        char *c = strchr(oneuidl, ' ');
                        if (c) strcpy(oneuidl, ++c);
 
-                       syslog(LOG_DEBUG, "<\033[34m%d\033[0m> <\033[34m%s\033[0m>", this_msg, oneuidl);
-
                        // Make up the Use Table record so we can check if we've already seen this message.
                        StrBuf *UT = NewStrBuf();
                        StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
-                       time_t already_seen = CheckIfAlreadySeen(UT, time(NULL), 31536000, eUpdate);
+                       int already_seen = CheckIfAlreadySeen(UT);
                        FreeStrBuf(&UT);
-                       syslog(LOG_DEBUG, "%s seen?  %ld", oneuidl, already_seen);
 
+                       // Only fetch the message if we haven't seen it before.
+                       if (already_seen == 0) {
+                               StrBuf *TheMsg = NewStrBuf();
+                               snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
+                               curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
+                               curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
+                               res = curl_easy_perform(curl);
+                               if (res == CURLE_OK) {
+                                       struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
+                                       CtdlSubmitMsg(msg, NULL, room);
+                                       CM_Free(msg);
+                               }
+                               else {
+                                       FreeStrBuf(&TheMsg);
+                               }
+
+                               // Unless the configuration says to keep the message on the server, delete it.
+                               if (keep == 0) {
+                                       snprintf(cmd, sizeof cmd, "DELE %d", this_msg);
+                                       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
+                                       res = curl_easy_perform(curl);
+                               }
+                       }
+                       else {
+                               syslog(LOG_DEBUG, "pop3client: %s has already been retrieved", oneuidl);
+                       }
                }
        }
 
@@ -146,32 +158,41 @@ void pop3client_one_mailbox(char *room, const char *host, const char *user, cons
 }
 
 
-/*
- * Scan a room's netconfig to determine whether it requires POP3 aggregation
- */
-void pop3client_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
+// Scan a room's netconfig looking for RSS feed parsing requests
+//
+void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
 {
-       const RoomNetCfgLine *pLine;
-       struct p3cq *pptr = NULL;
-
-       if (server_shutting_down) return;
-
-       pLine = OneRNCFG->NetConfigs[pop3client];
+       char *serialized_config = NULL;
+       int num_configs = 0;
+       char cfgline[SIZ];
+       char cfgelement[SIZ];
+       int i = 0;
+
+        serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
+        if (!serialized_config) {
+               return;
+       }
 
-       while (pLine != NULL)
-       {
-               pptr = malloc(sizeof(struct p3cq));
-               pptr->next = p3cq;
-               p3cq = pptr;
-               pptr->room = strdup(qrbuf->QRname);
-               pptr->host = strdup(ChrPtr(pLine->Value[0]));
-               pptr->user = strdup(ChrPtr(pLine->Value[1]));
-               pptr->pass = strdup(ChrPtr(pLine->Value[2]));
-               pptr->keep = atoi(ChrPtr(pLine->Value[3]));
-               pptr->interval = atol(ChrPtr(pLine->Value[4]));
-       
-               pLine = pLine->next;
+       num_configs = num_tokens(serialized_config, '\n');
+       for (i=0; i<num_configs; ++i) {
+               extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
+               if (!strncasecmp(cfgline, HKEY("pop3client|"))) {
+                       struct p3cq *pptr = malloc(sizeof(struct p3cq));
+                       pptr->next = p3cq;
+                       p3cq = pptr;
+                       p3cq->room = strdup(qrbuf->QRname);
+                       extract_token(cfgelement, cfgline, 1, '|', sizeof cfgelement);
+                       p3cq->host = strdup(cfgelement);
+                       extract_token(cfgelement, cfgline, 2, '|', sizeof cfgelement);
+                       p3cq->user = strdup(cfgelement);
+                       extract_token(cfgelement, cfgline, 3, '|', sizeof cfgelement);
+                       p3cq->pass = strdup(cfgelement);
+                       p3cq->keep = extract_int(cfgline, 4);
+                       p3cq->interval = extract_long(cfgline, 5);
+               }
        }
+
+       free(serialized_config);
 }
 
 
@@ -180,8 +201,6 @@ void pop3client_scan(void) {
        time_t fastest_scan;
        struct p3cq *pptr = NULL;
 
-       become_session(&pop3_client_CC);
-
        if (CtdlGetConfigLong("c_pop3_fastest") < CtdlGetConfigLong("c_pop3_fetch")) {
                fastest_scan = CtdlGetConfigLong("c_pop3_fastest");
        }
@@ -205,13 +224,13 @@ void pop3client_scan(void) {
        if (doing_pop3client) return;
        doing_pop3client = 1;
 
-       syslog(LOG_DEBUG, "pop3client scan started");
-       CtdlForEachNetCfgRoom(pop3client_scan_room, NULL);
+       syslog(LOG_DEBUG, "pop3client: scan started");
+       CtdlForEachRoom(pop3client_scan_room, NULL);
 
        /*
         * We have to queue and process in separate phases, otherwise we leave a cursor open
         */
-       syslog(LOG_DEBUG, "pop3client processing started");
+       syslog(LOG_DEBUG, "pop3client: processing started");
        while (p3cq != NULL) {
                pptr = p3cq;
                p3cq = p3cq->next;
@@ -225,7 +244,7 @@ void pop3client_scan(void) {
                free(pptr);
        }
 
-       syslog(LOG_DEBUG, "pop3client ended");
+       syslog(LOG_DEBUG, "pop3client: ended");
        last_run = time(NULL);
        doing_pop3client = 0;
 }
@@ -235,8 +254,6 @@ CTDL_MODULE_INIT(pop3client)
 {
        if (!threading)
        {
-               CtdlFillSystemContext(&pop3_client_CC, "POP3aggr");
-               CtdlREGISTERRoomCfgType(pop3client, ParseGeneric, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
                CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER, PRIO_AGGR + 50);
        }