parse uidl list
authorArt Cancro <ajc@citadel.org>
Fri, 10 Mar 2017 04:00:11 +0000 (23:00 -0500)
committerArt Cancro <ajc@citadel.org>
Fri, 10 Mar 2017 04:00:11 +0000 (23:00 -0500)
citadel/modules/pop3client/serv_pop3client.c
citadel/modules/rssclient/serv_rssclient.c

index df8a4addce035f4a1b2463eb367e497d4faf1094..1af84971a5eaefc91a1cb79429e249bf71c14af3 100644 (file)
@@ -70,24 +70,30 @@ static int doing_pop3client = 0;
  */
 void pop3client_one_mailbox(struct ctdlroom *qrbuf, const char *host, const char *user, const char *pass, int keep, long interval)
 {
-       syslog(LOG_DEBUG, "\033[33mpop3client: room=<%s> host=<%s> user=<%s> pass=<%s> keep=<%d> interval=<%ld>\033[0m",
-               qrbuf->QRname, host, user, pass, keep, interval
+       syslog(LOG_DEBUG, "pop3client: room=<%s> host=<%s> user=<%s> keep=<%d> interval=<%ld>",
+               qrbuf->QRname, host, user, keep, interval
        );
 
        char url[SIZ];
        CURL *curl;
        CURLcode res = CURLE_OK;
+       StrBuf *Uidls = NULL;
+       int i;
 
        curl = curl_easy_init();
        if (!curl) {
                return;
        }
 
+       Uidls = NewStrBuf();
+
        curl_easy_setopt(curl, CURLOPT_USERNAME, user);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, Uidls);                       // Give it our StrBuf to work with
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
 
        /* Try POP3S (SSL encrypted) first */
@@ -99,27 +105,40 @@ void pop3client_one_mailbox(struct ctdlroom *qrbuf, const char *host, const char
                syslog(LOG_DEBUG, "POP3S client 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);
                res = curl_easy_perform(curl);
        }
 
        if (res != CURLE_OK) {
                syslog(LOG_DEBUG, "pop3 client failed: %s", curl_easy_strerror(res));
                curl_easy_cleanup(curl);
+               FreeStrBuf(&Uidls);
                return;
        }
 
        // If we got this far, a connection was established, we know whether it's pop3s or pop3, and UIDL is supported.
-
-
-       // FIXME write the rest ... all this crap was just a test to make sure libcurl is holding open a single connection.
-       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "RETR 1");
-       res = curl_easy_perform(curl);
-       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
-       res = curl_easy_perform(curl);
-
-
+       // 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);
+       for (i=0; i<num_msgs; ++i) {
+               char oneuidl[1024];
+               extract_token(oneuidl, ChrPtr(Uidls), i, '\n', sizeof oneuidl);
+               if (strlen(oneuidl) > 2) {
+                       if (oneuidl[strlen(oneuidl)-1] == '\r') {
+                               oneuidl[strlen(oneuidl)-1] = 0;
+                       }
+                       int this_msg = atoi(oneuidl);
+                       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);
+
+               }
+       }
 
        curl_easy_cleanup(curl);
+       FreeStrBuf(&Uidls);
        return;
 }
 
index 6c9d364bbe83d90eab250483d5741f657566a914..79b1fc3054ba20f1e4f61db19fb0b9aee8611fcb 100644 (file)
@@ -332,17 +332,6 @@ void rssclient_push_todo(char *rssurl, char *roomname)
 }
 
 
-// Callback function for curl
-//
-size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
-{
-       StrBuf *Downloaded = (StrBuf *)userp;
-       size_t bytes = size * nmemb;
-       StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
-       return(bytes);
-}
-
-
 // pull one feed (possibly multiple rooms)
 //
 void rss_pull_one_feed(struct rssurl *url)
@@ -362,7 +351,7 @@ void rss_pull_one_feed(struct rssurl *url)
 
        curl_easy_setopt(curl, CURLOPT_URL, url->url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);                     // Follow redirects
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data);      // What to do with downloaded data
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded);                  // Give it our StrBuf to work with
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);                           // Time out after 20 seconds
        res = curl_easy_perform(curl);                                          // Perform the request