]> Pileus Git - ~andy/fetchmail/blob - smtp.c
Eliminate SockWrite and SockPrintf.
[~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 <sys/types.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include "socket.h"
18 #include "fetchmail.h"
19 #include "smtp.h"
20
21 int smtp_response;      /* numeric value of SMTP response code */
22
23 int SMTP_helo(FILE *sockfp,char *host)
24 /* send a "HELO" message to the SMTP listener */
25 {
26   int ok;
27
28   fprintf(sockfp,"HELO %s\r\n", host);
29   if (outlevel == O_VERBOSE)
30       error(0, 0, "SMTP> HELO %s", host);
31   ok = SMTP_ok(sockfp);
32   return ok;
33 }
34
35 int SMTP_from(FILE *sockfp, char *from)
36 /* send a "MAIL FROM:" message to the SMTP listener */
37 {
38   int ok;
39
40   fprintf(sockfp,"MAIL FROM:<%s>\r\n", from);
41   if (outlevel == O_VERBOSE)
42       error(0, 0, "SMTP> MAIL FROM:<%s>", from);
43   ok = SMTP_ok(sockfp);
44   return ok;
45 }
46
47 int SMTP_rcpt(FILE *sockfp, char *to)
48 /* send a "RCPT TO:" message to the SMTP listener */
49 {
50   int ok;
51
52   fprintf(sockfp,"RCPT TO:<%s>\r\n", to);
53   if (outlevel == O_VERBOSE)
54       error(0, 0, "SMTP> RCPT TO:<%s>", to);
55   ok = SMTP_ok(sockfp);
56   return ok;
57 }
58
59 int SMTP_data(FILE *sockfp)
60 /* send a "DATA" message to the SMTP listener */
61 {
62   int ok;
63
64   fprintf(sockfp,"DATA\r\n");
65   if (outlevel == O_VERBOSE)
66       error(0, 0, "SMTP> DATA");
67   ok = SMTP_ok(sockfp);
68   return ok;
69 }
70
71 int SMTP_quit(FILE *sockfp)
72 /* send a "QUIT" message to the SMTP listener */
73 {
74   int ok;
75
76   fprintf(sockfp,"QUIT\r\n");
77   if (outlevel == O_VERBOSE)
78       error(0, 0, "SMTP> QUIT");
79   ok = SMTP_ok(sockfp);
80   return ok;
81 }
82
83 int SMTP_eom(FILE *sockfp)
84 /* send a message data terminator to the SMTP listener */
85 {
86   int ok;
87
88   fprintf(sockfp,".\r\n");
89   if (outlevel == O_VERBOSE)
90       error(0, 0, "SMTP>. (EOM)");
91   ok = SMTP_ok(sockfp);
92   return ok;
93 }
94
95 int SMTP_ok(FILE *sockfp)
96 /* returns status of SMTP connection */
97 {
98     int  n;
99     char buf[SMTPBUFSIZE];
100   
101     while ((n = SockGets(buf, sizeof(buf)-1, sockfp)) > 0)
102     {
103         if (n < 4)
104             return SM_ERROR;
105         buf[n] = '\0';
106         if (outlevel == O_VERBOSE)
107             error(0, 0, "SMTP< %s", buf);
108         smtp_response = atoi(buf);
109         if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') && buf[3] == ' ')
110             return SM_OK;
111         else if (buf[3] != '-')
112             return SM_ERROR;
113     }
114     return SM_UNRECOVERABLE;
115 }
116
117 /* smtp.c ends here */