]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/pop3client/serv_pop3client.c
Use IOBuffer with its StrBuf + const char* inside instead of having two of them
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
index e6ea572c96b29c4e56a999605e301156fdac4e26..40add14844bc815ec0486c27debf7b634bf9e8ac 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * Consolidate mail from remote POP3 accounts.
  *
  * Copyright (c) 2007-2009 by the citadel.org team
@@ -82,6 +80,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        char utmsgid[SIZ];
        struct cdbdata *cdbut;
        struct UseTable ut;
+       CitContext *CCC=CC;
 
        CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
        CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
@@ -89,7 +88,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        if (CtdlThreadCheckStop())
                return;
                
-       sock = sock_connect(pop3host, "110", "tcp");
+       sock = sock_connect(pop3host, "110");
        if (sock < 0) {
                CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
                return;
@@ -99,9 +98,12 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                goto bail;
 
        CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
+       CCC->SBuf.Buf = NewStrBuf();
+       CCC->sMigrateBuf = NewStrBuf();
+       CCC->SBuf.ReadWritePointer = NULL;
 
        /* Read the server greeting */
-       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
 
@@ -115,8 +117,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
         */
        snprintf(buf, sizeof buf, "USER %s\r", pop3user);
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
-       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
+       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
 
@@ -126,8 +128,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Password */
        snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
        CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
-       if (sock_puts(sock, buf) <0) goto bail;
-       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
+       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
 
@@ -137,8 +139,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Get the list of messages */
        snprintf(buf, sizeof buf, "LIST\r");
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
-       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
+       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
 
@@ -149,7 +151,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                if (CtdlThreadCheckStop())
                        goto bail;
 
-               if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+               if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                msg_to_fetch = atoi(buf);
                if (msg_to_fetch > 0) {
@@ -171,8 +173,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                /* Find out the UIDL of the message, to determine whether we've already downloaded it */
                snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
                CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-               if (sock_puts(sock, buf) <0) goto bail;
-               if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+               if (sock_puts(&sock, buf) <0) goto bail;
+               if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                if (strncasecmp(buf, "+OK", 3)) goto bail;
                extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
@@ -197,8 +199,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                        /* Message has not been seen. Tell the server to fetch the message... */
                        snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
                        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-                       if (sock_puts(sock, buf) <0) goto bail;
-                       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+                       if (sock_puts(&sock, buf) <0) goto bail;
+                       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                        if (strncasecmp(buf, "+OK", 3)) goto bail;
        
@@ -206,7 +208,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                                goto bail;
 
                        /* If we get to this point, the message is on its way.  Read it. */
-                       body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, sock);
+                       body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, &sock);
                        if (body == NULL) goto bail;
        
                        CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
@@ -221,8 +223,8 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                                if (!keep) {
                                        snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
                                        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-                                       if (sock_puts(sock, buf) <0) goto bail;
-                                       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+                                       if (sock_puts(&sock, buf) <0) goto bail;
+                                       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                                        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
                                }
 
@@ -239,10 +241,15 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Log out */
        snprintf(buf, sizeof buf, "QUIT\r");
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
-       if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
+       if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
-bail:  sock_close(sock);
+bail:  
+       FreeStrBuf(&CCC->SBuf.Buf);
+       FreeStrBuf(&CCC->sMigrateBuf);
+
+       if (sock != -1)
+               sock_close(sock);
        if (msglist) free(msglist);
 }
 
@@ -299,7 +306,7 @@ void pop3client_scan(void) {
        static int doing_pop3client = 0;
        struct pop3aggr *pptr;
        time_t fastest_scan;
-       
+
        if (config.c_pop3_fastest < config.c_pop3_fetch)
                fastest_scan = config.c_pop3_fastest;
        else
@@ -348,5 +355,5 @@ CTDL_MODULE_INIT(pop3client)
        }
        
        /* return our Subversion id for the Log */
-        return "$Id$";
+        return "pop3client";
 }