src/org/sonews/mlgw/SMTPTransport.java
changeset 58 b2df305a13ce
parent 37 74139325d305
child 59 68a6825a4f4d
     1.1 --- a/src/org/sonews/mlgw/SMTPTransport.java	Tue Sep 13 20:14:44 2011 +0200
     1.2 +++ b/src/org/sonews/mlgw/SMTPTransport.java	Tue Sep 13 23:34:16 2011 +0200
     1.3 @@ -15,7 +15,6 @@
     1.4   *   You should have received a copy of the GNU General Public License
     1.5   *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.6   */
     1.7 -
     1.8  package org.sonews.mlgw;
     1.9  
    1.10  import java.io.BufferedOutputStream;
    1.11 @@ -33,45 +32,30 @@
    1.12   * @author Christian Lins
    1.13   * @since sonews/1.0
    1.14   */
    1.15 -class SMTPTransport
    1.16 -{
    1.17 +class SMTPTransport {
    1.18 +
    1.19 +	public static final String NEWLINE = "\r\n";
    1.20  
    1.21  	protected BufferedReader in;
    1.22  	protected BufferedOutputStream out;
    1.23  	protected Socket socket;
    1.24  
    1.25  	public SMTPTransport(String host, int port)
    1.26 -		throws IOException, UnknownHostException
    1.27 -	{
    1.28 -		socket = new Socket(host, port);
    1.29 -		this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.30 +			throws IOException, UnknownHostException {
    1.31 +		this.socket = new Socket(host, port);
    1.32 +		this.in = new BufferedReader(
    1.33 +				new InputStreamReader(socket.getInputStream()));
    1.34  		this.out = new BufferedOutputStream(socket.getOutputStream());
    1.35  
    1.36 -		// Read helo from server
    1.37 +		// Read HELO from server
    1.38  		String line = this.in.readLine();
    1.39  		if (line == null || !line.startsWith("220 ")) {
    1.40 -			throw new IOException("Invalid helo from server: " + line);
    1.41 -		}
    1.42 -
    1.43 -		// Send HELO to server
    1.44 -		this.out.write(
    1.45 -			("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
    1.46 -		this.out.flush();
    1.47 -		line = this.in.readLine();
    1.48 -		if (line == null || !line.startsWith("250 ")) {
    1.49 -			throw new IOException("Unexpected reply: " + line);
    1.50 +			throw new IOException("Invalid HELO from server: " + line);
    1.51  		}
    1.52  	}
    1.53  
    1.54 -	public SMTPTransport(String host)
    1.55 -		throws IOException
    1.56 -	{
    1.57 -		this(host, 25);
    1.58 -	}
    1.59 -
    1.60  	public void close()
    1.61 -		throws IOException
    1.62 -	{
    1.63 +			throws IOException {
    1.64  		this.out.write("QUIT".getBytes("UTF-8"));
    1.65  		this.out.flush();
    1.66  		this.in.readLine();
    1.67 @@ -79,9 +63,77 @@
    1.68  		this.socket.close();
    1.69  	}
    1.70  
    1.71 +	private void ehlo(String hostname) throws IOException {
    1.72 +		StringBuilder strBuf = new StringBuilder();
    1.73 +		strBuf.append("EHLO ");
    1.74 +		strBuf.append(hostname);
    1.75 +		strBuf.append(NEWLINE);
    1.76 +
    1.77 +		// Send EHLO to server
    1.78 +		this.out.write(strBuf.toString().getBytes("UTF-8"));
    1.79 +		this.out.flush();
    1.80 +
    1.81 +		// Read reply: "250-example.org Hello example.net"
    1.82 +		String line = this.in.readLine();
    1.83 +		if (line == null || !line.startsWith("250")) {
    1.84 +			throw new IOException("Unexpected reply: " + line);
    1.85 +		}
    1.86 +
    1.87 +		// FIXME: More 250- lines possible!
    1.88 +
    1.89 +		// Read reply: "250 AUTH CRAM-MD5 LOGIN PLAIN"
    1.90 +		line = this.in.readLine();
    1.91 +		if (line == null || !line.startsWith("250 ")) {
    1.92 +			throw new IOException("Unexpected reply: " + line);
    1.93 +		}
    1.94 +		String[] authMethods = line.split(" ");
    1.95 +		// TODO: Check for supported methods
    1.96 +
    1.97 +		// Do a PLAIN login
    1.98 +		strBuf = new StringBuilder();
    1.99 +		strBuf.append("AUTH PLAIN");
   1.100 +		strBuf.append(NEWLINE);
   1.101 +
   1.102 +		// Send AUTH to server
   1.103 +		this.out.write(strBuf.toString().getBytes("UTF-8"));
   1.104 +		this.out.flush();
   1.105 +
   1.106 +		// Read reply
   1.107 +		line = this.in.readLine();
   1.108 +		if (line == null || !line.startsWith("250 ")) {
   1.109 +			throw new IOException("Unexpected reply: " + line);
   1.110 +		}
   1.111 +	}
   1.112 +
   1.113 +	private void helo(String hostname) throws IOException {
   1.114 +		StringBuilder heloStr = new StringBuilder();
   1.115 +		heloStr.append("HELO ");
   1.116 +		heloStr.append(hostname);
   1.117 +		heloStr.append(NEWLINE);
   1.118 +
   1.119 +		// Send HELO to server
   1.120 +		this.out.write(heloStr.toString().getBytes("UTF-8"));
   1.121 +		this.out.flush();
   1.122 +
   1.123 +		// Read reply
   1.124 +		String line = this.in.readLine();
   1.125 +		if (line == null || !line.startsWith("250 ")) {
   1.126 +			throw new IOException("Unexpected reply: " + line);
   1.127 +		}
   1.128 +	}
   1.129 +
   1.130 +	public void login() throws IOException {
   1.131 +		String hostname = Config.inst().get(Config.HOSTNAME, "localhost");
   1.132 +		String auth = Config.inst().get(Config.MLSEND_AUTH, "none");
   1.133 +		if(auth.equals("none")) {
   1.134 +			helo(hostname);
   1.135 +		} else {
   1.136 +			ehlo(hostname);
   1.137 +		}
   1.138 +	}
   1.139 +
   1.140  	public void send(Article article, String mailFrom, String rcptTo)
   1.141 -		throws IOException
   1.142 -	{
   1.143 +			throws IOException {
   1.144  		assert (article != null);
   1.145  		assert (mailFrom != null);
   1.146  		assert (rcptTo != null);