]> Pileus Git - ~andy/fetchmail/blob - smtp.c
Drop back to using SockGets/SockWrite.
[~andy/fetchmail] / smtp.c
1 /*
2  * smtp.c -- code for speaking SMTP to a listener port
3  *
4  * Concept due to Harry Hochheiser.  Implementation by ESR.  Cleanup and
5  * strict RFC821 compliance by Cameron MacPherson.
6  *
7  * Copyright 1996 Eric S. Raymond
8  * All rights reserved.
9  * For license terms, see the file COPYING in this directory.
10  */
11
12 #include <stdio.h>
13 #include <config.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include "fetchmail.h"
17 #include "socket.h"
18 #include "smtp.h"
19
20 int smtp_response;      /* numeric value of SMTP response code */
21
22 int SMTP_helo(FILE *sockfp,char *host)
23 /* send a "HELO" message to the SMTP listener */
24 {
25   int ok;
26
27   SockPrintf(sockfp,"HELO %s\r\n", host);
28   if (outlevel == O_VERBOSE)
29       error(0, 0, "SMTP> HELO %s", host);
30   ok = SMTP_ok(sockfp);
31   return ok;
32 }
33
34 int SMTP_from(FILE *sockfp, char *from)
35 /* send a "MAIL FROM:" message to the SMTP listener */
36 {
37   int ok;
38
39   SockPrintf(sockfp,"MAIL FROM:<%s>\r\n", from);
40   if (outlevel == O_VERBOSE)
41       error(0, 0, "SMTP> MAIL FROM:<%s>", from);
42   ok = SMTP_ok(sockfp);
43   return ok;
44 }
45
46 int SMTP_rcpt(FILE *sockfp, char *to)
47 /* send a "RCPT TO:" message to the SMTP listener */
48 {
49   int ok;
50
51   SockPrintf(sockfp,"RCPT TO:<%s>\r\n", to);
52   if (outlevel == O_VERBOSE)
53       error(0, 0, "SMTP> RCPT TO:<%s>", to);
54   ok = SMTP_ok(sockfp);
55   return ok;
56 }
57
58 int SMTP_data(FILE *sockfp)
59 /* send a "DATA" message to the SMTP listener */
60 {
61   int ok;
62
63   SockPrintf(sockfp,"DATA\r\n");
64   if (outlevel == O_VERBOSE)
65       error(0, 0, "SMTP> DATA");
66   ok = SMTP_ok(sockfp);
67   return ok;
68 }
69
70 int SMTP_quit(FILE *sockfp)
71 /* send a "QUIT" message to the SMTP listener */
72 {
73   int ok;
74
75   SockPrintf(sockfp,"QUIT\r\n");
76   if (outlevel == O_VERBOSE)
77       error(0, 0, "SMTP> QUIT");
78   ok = SMTP_ok(sockfp);
79   return ok;
80 }
81
82 int SMTP_eom(FILE *sockfp)
83 /* send a message data terminator to the SMTP listener */
84 {
85   int ok;
86
87   SockPrintf(sockfp,".\r\n");
88   if (outlevel == O_VERBOSE)
89       error(0, 0, "SMTP>. (EOM)");
90   ok = SMTP_ok(sockfp);
91   return ok;
92 }
93
94 int SMTP_ok(FILE *sockfp)
95 /* returns status of SMTP connection */
96 {
97     char buf[SMTPBUFSIZE], *ip;
98   
99     while ((ip = SockGets(buf, sizeof(buf)-1, sockfp)))
100     {
101         int  n = strlen(ip);
102
103         if (buf[strlen(buf)-1] == '\n')
104             buf[strlen(buf)-1] = '\0';
105         if (buf[strlen(buf)-1] == '\r')
106             buf[strlen(buf)-1] = '\r';
107         if (n < 4)
108             return SM_ERROR;
109         buf[n] = '\0';
110         if (outlevel == O_VERBOSE)
111             error(0, 0, "SMTP< %s", buf);
112         smtp_response = atoi(buf);
113         if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') && buf[3] == ' ')
114             return SM_OK;
115         else if (buf[3] != '-')
116             return SM_ERROR;
117     }
118     return SM_UNRECOVERABLE;
119 }
120
121 /* smtp.c ends here */