]> Pileus Git - ~andy/fetchmail/commitdiff
STEP 4: Make SMTP functions take file pointer arguments.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 31 Oct 1996 06:47:56 +0000 (06:47 -0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 31 Oct 1996 06:47:56 +0000 (06:47 -0000)
svn path=/trunk/; revision=447

driver.c
fetchmail.c
fetchmail.h
smtp.c
smtp.h

index 5402fba9548f63cf866a700b7f5a93cc9536727a..c65d330e15203f65b30be6298f36fe0e37f0acf1 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -464,26 +464,26 @@ struct idlist **xmit_names;       /* list of recipient names parsed out */
 }
 #endif /* HAVE_GETHOSTBYNAME */
 
-static int smtp_open(ctl)
+static FILE *smtp_open(ctl)
 /* try to open a socket to the appropriate SMTP server for this query */ 
 struct query *ctl;
 {
     ctl = ctl->leader; /* go to the SMTP leader for this query */
 
     /* if no socket to this host is already set up, try to open one */
-    if (ctl->smtp_socket == -1)
+    if (ctl->smtp_sockfp == (FILE *)NULL)
     {
-       if ((ctl->smtp_socket = Socket(ctl->smtphost, SMTP_PORT)) == -1)
-           return(-1);
-       else if (SMTP_ok(ctl->smtp_socket, NULL) != SM_OK
-                || SMTP_helo(ctl->smtp_socket, ctl->servername) != SM_OK)
+       if ((ctl->smtp_sockfp = fdopen(Socket(ctl->smtphost, SMTP_PORT), "r+")) == (FILE *)NULL)
+           return((FILE *)NULL);
+       else if (SMTP_ok(ctl->smtp_sockfp, NULL) != SM_OK
+                || SMTP_helo(ctl->smtp_sockfp, ctl->servername) != SM_OK)
        {
-           close(ctl->smtp_socket);
-           ctl->smtp_socket = -1;
+           fclose(ctl->smtp_sockfp);
+           ctl->smtp_sockfp = (FILE *)NULL;
        }
     }
 
-    return(ctl->smtp_socket);
+    return(ctl->smtp_sockfp);
 }
 
 static int gen_readmsg (sockfp, len, delimited, ctl)
@@ -497,6 +497,7 @@ struct query *ctl;  /* query control record */
     char *bufp, *headers, *fromhdr, *tohdr, *cchdr, *bcchdr;
     int n, oldlen, mboxfd;
     int inheaders,lines,sizeticker;
+    FILE *sinkfp;
 
     /* read the message content from the server */
     inheaders = 1;
@@ -656,29 +657,31 @@ struct query *ctl;        /* query control record */
            }
            else
            {
-               if (ctl->mda[0] == '\0' && ((mboxfd = smtp_open(ctl)) < 0))
+               if (ctl->mda[0] == '\0' && ((sinkfp = smtp_open(ctl)) < 0))
                {
                    free_uid_list(&xmit_names);
                    fprintf(stderr, "fetchmail: SMTP connect failed\n");
                    return(PS_SMTP);
                }
 
-               if (SMTP_from(mboxfd, nxtaddr(fromhdr)) != SM_OK)
+               if (SMTP_from(sinkfp, nxtaddr(fromhdr)) != SM_OK)
                {
                    fprintf(stderr, "fetchmail: SMTP listener is confused\n");
                    return(PS_SMTP);
                }
 
                for (idp = xmit_names; idp; idp = idp->next)
-                   if (SMTP_rcpt(mboxfd, idp->id) != SM_OK)
+                   if (SMTP_rcpt(sinkfp, idp->id) != SM_OK)
                    {
                        fprintf(stderr, "fetchmail: SMTP listener is upset\n");
                        return(PS_SMTP);
                    }
 
-               SMTP_data(mboxfd);
+               SMTP_data(sinkfp);
                if (outlevel == O_VERBOSE)
                    fputs("SMTP> ", stderr);
+
+               mboxfd = fileno(sinkfp);
            }
            free_uid_list(&xmit_names);
 
@@ -749,7 +752,7 @@ struct query *ctl;  /* query control record */
     else
     {
        /* write message terminator */
-       if (SMTP_eom(mboxfd) != SM_OK)
+       if (SMTP_eom(sinkfp) != SM_OK)
        {
            fputs("fetchmail: SMTP listener refused delivery\n", stderr);
            return(PS_SMTP);
index 4d55bf91d489792be4f280ef72fd8d41cf7db742..f7bce817c9a8a6402c5d16698df63f9e31e064e8 100644 (file)
@@ -528,7 +528,7 @@ int optind;
                        goto no_new_leader;
                    }
                ctl->leader = ctl;
-               ctl->smtp_socket = -1;
+               ctl->smtp_sockfp = (FILE *)NULL;
            no_new_leader:;
            }
 
@@ -582,8 +582,8 @@ void termhook(int sig)
 
     /* terminate all SMTP connections cleanly */
     for (ctl = querylist; ctl; ctl = ctl->next)
-       if (ctl->leader == ctl && ctl->smtp_socket != -1)
-           SMTP_quit(ctl->smtp_socket);
+       if (ctl->leader == ctl && ctl->smtp_sockfp != (FILE *)NULL)
+           SMTP_quit(ctl->smtp_sockfp);
 
     if (!check_only)
        write_saved_lists(querylist, idfile);
index 3cd5c10421279a343fe28c8bf7b1f89847a76183..eaf11fb0481519ed8f50357793b1d45b96a5bb47 100644 (file)
@@ -94,7 +94,7 @@ struct query
     int active;
     struct query *next;                /* next query control block in chain */
     struct query *leader;      /* pointer to this query's SMTP leader */
-    int smtp_socket;           /* socket descriptor for SMTP connection */
+    FILE *smtp_sockfp;         /* socket descriptor for SMTP connection */
     unsigned int uid;          /* UID of user to deliver to */
     char digest [DIGESTLEN];   /* md5 digest buffer */
 #ifdef HAVE_GETHOSTBYNAME
diff --git a/smtp.c b/smtp.c
index 6874705fdb7e68eafb4cf6115653226af6604345..fbfe9adb5ba869e1e03baab543a47fd37407eb9c 100644 (file)
--- a/smtp.c
+++ b/smtp.c
 #include "fetchmail.h"
 #include "smtp.h"
 
-int SMTP_helo(int socket,char *host)
+int SMTP_helo(FILE *sockfp,char *host)
 /* send a "HELO" message to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,"HELO %s\r\n", host);
+  SockPrintf(fileno(sockfp),"HELO %s\r\n", host);
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> HELO %s\n", host);
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-int SMTP_from(int socket, char *from)
+int SMTP_from(FILE *sockfp, char *from)
 /* send a "MAIL FROM:" message to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,"MAIL FROM:<%s>\r\n", from);
+  SockPrintf(fileno(sockfp),"MAIL FROM:<%s>\r\n", from);
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> MAIL FROM:<%s>\n", from);
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-int SMTP_rcpt(int socket, char *to)
+int SMTP_rcpt(FILE *sockfp, char *to)
 /* send a "RCPT TO:" message to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,"RCPT TO:<%s>\r\n", to);
+  SockPrintf(fileno(sockfp),"RCPT TO:<%s>\r\n", to);
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> RCPT TO:<%s>\n", to);
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-int SMTP_data(int socket)
+int SMTP_data(FILE *sockfp)
 /* send a "DATA" message to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,"DATA\r\n");
+  SockPrintf(fileno(sockfp),"DATA\r\n");
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> DATA\n");
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-int SMTP_quit(int socket)
+int SMTP_quit(FILE *sockfp)
 /* send a "QUIT" message to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,"QUIT\r\n");
+  SockPrintf(fileno(sockfp),"QUIT\r\n");
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> QUIT\n");
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-int SMTP_eom(int socket)
+int SMTP_eom(FILE *sockfp)
 /* send a message data terminator to the SMTP listener */
 {
   int ok;
 
-  SockPrintf(socket,".\r\n");
+  SockPrintf(fileno(sockfp),".\r\n");
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP>. (EOM)\n");
-  ok = SMTP_ok(socket,NULL);
+  ok = SMTP_ok(sockfp,NULL);
   return ok;
 }
 
-void SMTP_rset(int socket)
+void SMTP_rset(FILE *sockfp)
 /* send a "RSET" message to the SMTP listener */
 {
-  SockPrintf(socket,"RSET\r\n");
+  SockPrintf(fileno(sockfp),"RSET\r\n");
   if (outlevel == O_VERBOSE)
       fprintf(stderr, "SMTP> RSET\n");
 }
 
-static int SMTP_check(int socket,char *argbuf)
+static int SMTP_check(FILE *sockfp,char *argbuf)
 /* returns status of SMTP connection */
 {
   int  ok;  
   char buf[SMTPBUFSIZE];
   
-  if ((ok = read(socket, buf, sizeof(buf)-1)) > 0) {
+  if ((ok = read(fileno(sockfp), buf, sizeof(buf)-1)) > 0) {
     buf[ok] = '\0';
     if (outlevel == O_VERBOSE)
        fprintf(stderr, "SMTP< %s", buf);
@@ -120,7 +120,7 @@ static int SMTP_check(int socket,char *argbuf)
   return (ok);
 }
 
-int SMTP_ok(int socket,char *argbuf)
+int SMTP_ok(FILE *sockfp,char *argbuf)
 /* accepts SMTP response, returns status of SMTP connection */
 {
   int  ok;  
@@ -134,11 +134,11 @@ int SMTP_ok(int socket,char *argbuf)
 
     */
 
-  ok = SMTP_check(socket,argbuf);
+  ok = SMTP_check(sockfp,argbuf);
   if (ok == SM_ERROR) /* if we got an error, */
     {
-      SMTP_rset(socket);
-      ok = SMTP_check(socket,argbuf);  /* how does it look now ? */
+      SMTP_rset(sockfp);
+      ok = SMTP_check(sockfp,argbuf);  /* how does it look now ? */
       if (ok == SM_OK)  
        ok = SM_ERROR;                /* It's just a simple error, for*/
                                      /*         the current message  */
diff --git a/smtp.h b/smtp.h
index 939ebfdcc3ca4779766390781d71eaa978d56177..dfc96c66f6bbaa88b4add5d483bf6912cae15ef6 100644 (file)
--- a/smtp.h
+++ b/smtp.h
 #define         SM_UNRECOVERABLE   129
 
 #ifdef HAVE_PROTOTYPES
-int SMTP_helo(int socket,char *host);
-int SMTP_from(int socket,char *from);
-int SMTP_rcpt(int socket,char *to);
-int SMTP_data(int socket);
-int SMTP_eom(int socket);
-int SMTP_quit(int socket);
-int SMTP_ok(int socket,char *argbuf);
-void SMTP_rset(int socket);
+int SMTP_helo(FILE *sockfp,char *host);
+int SMTP_from(FILE *sockfp,char *from);
+int SMTP_rcpt(FILE *sockfp,char *to);
+int SMTP_data(FILE *sockfp);
+int SMTP_eom(FILE *sockfp);
+int SMTP_quit(FILE *sockfp);
+int SMTP_ok(FILE *sockfp,char *argbuf);
+void SMTP_rset(FILE *sockfp);
 #endif /* HAVE_PROTOTYPES */
 
 #endif