~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/NU-Smtp/.svn/text-base/SmtpStream.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************************
3
 
 |
4
 
 | Copyright (c) 2007 Novell, Inc.
5
 
 | All Rights Reserved.
6
 
 |
7
 
 | This program is free software; you can redistribute it and/or
8
 
 | modify it under the terms of version 2 of the GNU General Public License as
9
 
 | published by the Free Software Foundation.
10
 
 |
11
 
 | This program is distributed in the hope that it will be useful,
12
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 | GNU General Public License for more details.
15
 
 |
16
 
 | You should have received a copy of the GNU General Public License
17
 
 | along with this program; if not, contact Novell, Inc.
18
 
 |
19
 
 | To contact Novell about this file by physical or electronic mail,
20
 
 | you may find current contact information at www.novell.com 
21
 
 |
22
 
 | Author: Per Arneng <pt99par@student.bth.se>
23
 
 |***************************************************************************/
24
 
 
25
 
using System;
26
 
using System.IO;
27
 
using System.Collections;
28
 
using System.Text;
29
 
using System.Security.Cryptography;
30
 
 
31
 
namespace Simias.Mail {
32
 
 
33
 
 
34
 
    internal class SmtpStream
35
 
    {
36
 
 
37
 
        protected Stream stream;
38
 
        protected Encoding encoding;
39
 
        protected SmtpResponse lastResponse;
40
 
        protected string command = "";
41
 
 
42
 
        public SmtpStream( Stream stream )
43
 
        {
44
 
            this.stream = stream;
45
 
            encoding = new ASCIIEncoding();
46
 
        }
47
 
 
48
 
        public Stream Stream {
49
 
            get { return stream;}
50
 
        }
51
 
 
52
 
        public SmtpResponse LastResponse {
53
 
            get { return lastResponse;}
54
 
        }
55
 
 
56
 
        public void WriteRset()
57
 
        {
58
 
            command = "RSET";
59
 
            WriteLine( command );
60
 
            ReadResponse();
61
 
            CheckForStatusCode( 250 );
62
 
 
63
 
        }
64
 
 
65
 
        public void WriteHelo( string hostName )
66
 
        {
67
 
            command = "HELO " + hostName;
68
 
            WriteLine( command );
69
 
            ReadResponse();
70
 
            CheckForStatusCode( 250 );
71
 
 
72
 
        }
73
 
 
74
 
        public void WriteMailFrom( string from )
75
 
        {
76
 
            command = "MAIL FROM: <" + from + ">";
77
 
            WriteLine( command );
78
 
            ReadResponse();
79
 
            CheckForStatusCode( 250 );
80
 
 
81
 
        }
82
 
 
83
 
        public void WriteRcptTo( string to )
84
 
        {
85
 
            command = "RCPT TO: <" + to + ">";  
86
 
            WriteLine( command );
87
 
            ReadResponse();
88
 
            CheckForStatusCode( 250 );
89
 
 
90
 
        }
91
 
 
92
 
 
93
 
        public void WriteData()
94
 
        {
95
 
            command = "DATA";
96
 
            WriteLine( command );
97
 
            ReadResponse();
98
 
            CheckForStatusCode( 354 );
99
 
 
100
 
        }
101
 
 
102
 
        public void WriteQuit()
103
 
        {
104
 
            command = "QUIT";
105
 
            WriteLine( command );
106
 
            ReadResponse();
107
 
            CheckForStatusCode( 221 );
108
 
 
109
 
        }
110
 
 
111
 
        public void WriteBoundary( string boundary )
112
 
        {
113
 
 
114
 
            WriteLine( "\r\n--{0}" , boundary );
115
 
 
116
 
        }
117
 
 
118
 
        public void WriteFinalBoundary( string boundary )
119
 
        {
120
 
 
121
 
            WriteLine( "\r\n--{0}--" , boundary );
122
 
 
123
 
        }
124
 
 
125
 
    // single dot by itself
126
 
        public void WriteDataEndTag()
127
 
        {
128
 
            command = "\r\n.";
129
 
            WriteLine( command );
130
 
            ReadResponse();
131
 
            CheckForStatusCode( 250 );
132
 
 
133
 
        }
134
 
 
135
 
 
136
 
        public void WriteHeader( MailHeader header )
137
 
        {
138
 
        // write the headers
139
 
            foreach( string key in header.Data.AllKeys )
140
 
            WriteLine( "{0}: {1}" , key , header.Data[ key ] );
141
 
 
142
 
        // write the header end tag
143
 
            WriteLine( "" );
144
 
        }
145
 
 
146
 
        public void CheckForStatusCode( int statusCode )
147
 
        {
148
 
 
149
 
            if( LastResponse.StatusCode != statusCode )
150
 
            {
151
 
 
152
 
                string msg = "" + 
153
 
                        "Server reponse: '" + lastResponse.RawResponse + "';" +
154
 
                        "Status code: '" +  lastResponse.StatusCode + "';" + 
155
 
                        "Expected status code: '" + statusCode + "';" + 
156
 
                        "Last command: '" + command + "'";
157
 
 
158
 
                throw new SmtpException( msg ); 
159
 
 
160
 
            }
161
 
        }
162
 
 
163
 
 
164
 
    // write buffer's bytes to the stream
165
 
        public void WriteBytes( byte[] buffer )
166
 
        {
167
 
            stream.Write( buffer , 0 , buffer.Length );
168
 
        }
169
 
 
170
 
    // writes a formatted line to the server
171
 
        public void WriteLine( string format ,  params object[] args )
172
 
        {
173
 
            WriteLine( String.Format( format , args ) );
174
 
        }
175
 
 
176
 
    // writes a line to the server
177
 
        public void WriteLine( string line )
178
 
        {
179
 
            byte[] buffer = encoding.GetBytes( line + "\r\n" );
180
 
 
181
 
            stream.Write( buffer , 0 , buffer.Length );
182
 
 
183
 
#if DEBUG 
184
 
            DebugPrint( line );
185
 
#endif
186
 
        }
187
 
 
188
 
    // read a line from the server
189
 
        public void ReadResponse( )
190
 
        {
191
 
            string line = null;
192
 
 
193
 
            byte[] buffer = new byte[ 4096 ];
194
 
 
195
 
            int readLength = stream.Read( buffer , 0 , buffer.Length );
196
 
 
197
 
            if( readLength > 0 )
198
 
            {
199
 
 
200
 
                line = encoding.GetString( buffer , 0 , readLength );
201
 
 
202
 
                line = line.TrimEnd( new Char[]{ '\r' , '\n' , ' '} );
203
 
 
204
 
            }
205
 
 
206
 
        // parse the line to the lastResponse object
207
 
            lastResponse = SmtpResponse.Parse( line );
208
 
 
209
 
#if DEBUG
210
 
            DebugPrint( line );
211
 
#endif
212
 
        }
213
 
 
214
 
                ISimiasLog      logger = SimiasLogManager.GetLogger(typeof(SmtpStream));
215
 
    /// debug printing 
216
 
        private void DebugPrint( string line )
217
 
        {
218
 
            logger.Debug(line);
219
 
        }
220
 
 
221
 
    }
222
 
 
223
 
 
224
 
}