Merge pull request #4297 from kumpera/fix-profiler-osx-linking
[mono.git] / mcs / class / System.Web / System.Web.Mail / SmtpStream.cs
1 //
2 // System.Web.Mail.SmtpStream.cs
3 //
4 // Author(s):
5 //   Per Arneng <pt99par@student.bth.se>
6 //
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.IO;
31 using System.Collections;
32 using System.Text;
33 using System.Diagnostics;
34
35 namespace System.Web.Mail {
36
37     internal class SmtpStream {
38         
39         protected Stream stream;
40         protected Encoding encoding;
41         protected SmtpResponse lastResponse;
42         protected string command = "";
43
44         public SmtpStream( Stream stream ) {
45             this.stream = stream;
46             encoding = new ASCIIEncoding();
47         }
48         
49         public Stream Stream {
50             get { return stream; }
51         }
52         
53         public SmtpResponse LastResponse {
54             get { return lastResponse; }
55         }
56         
57         public void WriteRset() {
58             command = "RSET";
59             WriteLine( command );
60             ReadResponse();
61             CheckForStatusCode( 250 );
62         
63         }
64         
65         public void WriteAuthLogin()
66         {
67                 command = "AUTH LOGIN";
68                 WriteLine( command );
69                 ReadResponse();         
70         }
71
72         public bool WriteStartTLS( ) 
73         { 
74                 command = "STARTTLS";
75                 WriteLine( command );
76                 ReadResponse();
77                 return LastResponse.StatusCode == 220;
78
79         }
80
81         public void WriteEhlo( string hostName ) 
82         { 
83                 command = "EHLO " + hostName;
84                 WriteLine( command );
85                 ReadResponse();
86                 CheckForStatusCode( 250 );
87         
88         }
89         
90         public void WriteHelo( string hostName ) { 
91             command = "HELO " + hostName;
92             WriteLine( command );
93             ReadResponse();
94             CheckForStatusCode( 250 );
95             
96         }
97         
98         public void WriteMailFrom( string from ) {
99             command = "MAIL FROM: <" + from + ">";
100             WriteLine( command );
101             ReadResponse();
102             CheckForStatusCode( 250 );
103             
104         }
105         
106         public void WriteRcptTo( string to ) {
107             command = "RCPT TO: <" + to + ">";  
108             WriteLine( command );
109             ReadResponse();
110             CheckForStatusCode( 250 );
111                     
112         }
113         
114
115         public void WriteData() {
116             command = "DATA";
117             WriteLine( command );
118             ReadResponse();
119             CheckForStatusCode( 354 );
120         
121         }
122         
123         public void WriteQuit() {
124             command = "QUIT";
125             WriteLine( command );
126             ReadResponse();
127             CheckForStatusCode( 221 );
128         
129         }
130                 
131         public void WriteBoundary( string boundary ) {
132         
133             WriteLine( "\r\n--{0}" , boundary );
134         
135         }
136         
137         public void WriteFinalBoundary( string boundary ) {
138         
139             WriteLine( "\r\n--{0}--" , boundary );
140         
141         }
142         
143         // single dot by itself
144         public void WriteDataEndTag() {
145             command = "\r\n.";
146             WriteLine( command );
147             ReadResponse();
148             CheckForStatusCode( 250 );
149         
150         }
151         
152         
153         public void WriteHeader( MailHeader header ) {
154             // write the headers
155             foreach( string key in header.Data.AllKeys )
156                 WriteLine( "{0}: {1}" , key , header.Data[ key ] );
157             
158             // write the header end tag
159             WriteLine( "" );
160         }
161         
162         public void CheckForStatusCode( int statusCode ) {
163             
164             if( LastResponse.StatusCode != statusCode ) {
165                 
166                 string msg = "" + 
167                     "Server reponse: '" + lastResponse.RawResponse + "';" +
168                     "Status code: '" +  lastResponse.StatusCode + "';" + 
169                     "Expected status code: '" + statusCode + "';" + 
170                     "Last command: '" + command + "'";
171                 
172                 throw new SmtpException( msg ); 
173                                         
174             }
175         }
176         
177         
178         // write buffer's bytes to the stream
179         public void WriteBytes( byte[] buffer ) {
180             stream.Write( buffer , 0 , buffer.Length );
181         }
182         
183         // writes a formatted line to the server
184         public void WriteLine( string format ,  params object[] args ) {
185             WriteLine( String.Format( format , args ) );
186         }
187         
188         // writes a line to the server
189         public void WriteLine( string line ) {
190             byte[] buffer = encoding.GetBytes( line + "\r\n" );
191             
192             stream.Write( buffer , 0 , buffer.Length );
193
194                 Debug.WriteLine ("smtp: {0}", line);
195         }
196         
197         // read a line from the server
198         public void ReadResponse( ) {
199                 
200                 byte[] buffer = new byte [512];
201                 int position = 0;
202                 bool lastLine = false;
203
204                 do {
205                         int readLength = stream.Read (buffer , position , buffer.Length - position);
206                         if (readLength > 0) { 
207                                 int available = position + readLength - 1;
208                                 if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
209                                         for (int index = available - 3; ; index--) {
210                                                 if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
211                                                         lastLine = buffer [index + 4] == ' ';
212                                                         break;
213                                                 }
214                                         }
215
216                                 // move position
217                                 position += readLength;
218
219                                 // check if buffer is full
220                                 if (position == buffer.Length) {
221                                         byte [] newBuffer = new byte [buffer.Length * 2];
222                                         Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
223                                         buffer = newBuffer;
224                                 }
225                         }
226                 } while(!lastLine);
227
228                 string line = encoding.GetString (buffer , 0 , position - 1);
229                         
230             // parse the line to the lastResponse object
231             lastResponse = SmtpResponse.Parse (line);
232
233                 Debug.WriteLine ("smtp: {0}", line);
234         }
235
236     }
237
238
239 }