src/org/sonews/mlgw/SMTPTransport.java
author cli
Mon, 12 Sep 2011 20:20:00 +0200
changeset 52 d97b223eab4e
parent 37 74139325d305
child 58 b2df305a13ce
permissions -rwxr-xr-x
Change binary package name sonews-server to server
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.mlgw;
chris@3
    20
chris@3
    21
import java.io.BufferedOutputStream;
chris@3
    22
import java.io.BufferedReader;
chris@3
    23
import java.io.IOException;
chris@3
    24
import java.io.InputStreamReader;
chris@3
    25
import java.net.Socket;
chris@3
    26
import java.net.UnknownHostException;
chris@3
    27
import org.sonews.config.Config;
chris@3
    28
import org.sonews.storage.Article;
chris@3
    29
import org.sonews.util.io.ArticleInputStream;
chris@3
    30
chris@3
    31
/**
chris@3
    32
 * Connects to a SMTP server and sends a given Article to it.
chris@3
    33
 * @author Christian Lins
cli@28
    34
 * @since sonews/1.0
chris@3
    35
 */
chris@3
    36
class SMTPTransport
chris@3
    37
{
chris@3
    38
cli@37
    39
	protected BufferedReader in;
cli@37
    40
	protected BufferedOutputStream out;
cli@37
    41
	protected Socket socket;
chris@3
    42
cli@37
    43
	public SMTPTransport(String host, int port)
cli@37
    44
		throws IOException, UnknownHostException
cli@37
    45
	{
cli@37
    46
		socket = new Socket(host, port);
cli@37
    47
		this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
cli@37
    48
		this.out = new BufferedOutputStream(socket.getOutputStream());
chris@3
    49
cli@37
    50
		// Read helo from server
cli@37
    51
		String line = this.in.readLine();
cli@37
    52
		if (line == null || !line.startsWith("220 ")) {
cli@37
    53
			throw new IOException("Invalid helo from server: " + line);
cli@37
    54
		}
chris@3
    55
cli@37
    56
		// Send HELO to server
cli@37
    57
		this.out.write(
cli@37
    58
			("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
cli@37
    59
		this.out.flush();
cli@37
    60
		line = this.in.readLine();
cli@37
    61
		if (line == null || !line.startsWith("250 ")) {
cli@37
    62
			throw new IOException("Unexpected reply: " + line);
cli@37
    63
		}
cli@37
    64
	}
chris@3
    65
cli@37
    66
	public SMTPTransport(String host)
cli@37
    67
		throws IOException
cli@37
    68
	{
cli@37
    69
		this(host, 25);
cli@37
    70
	}
chris@3
    71
cli@37
    72
	public void close()
cli@37
    73
		throws IOException
cli@37
    74
	{
cli@37
    75
		this.out.write("QUIT".getBytes("UTF-8"));
cli@37
    76
		this.out.flush();
cli@37
    77
		this.in.readLine();
chris@3
    78
cli@37
    79
		this.socket.close();
cli@37
    80
	}
chris@3
    81
cli@37
    82
	public void send(Article article, String mailFrom, String rcptTo)
cli@37
    83
		throws IOException
cli@37
    84
	{
cli@37
    85
		assert (article != null);
cli@37
    86
		assert (mailFrom != null);
cli@37
    87
		assert (rcptTo != null);
cli@12
    88
cli@37
    89
		this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
cli@37
    90
		this.out.flush();
cli@37
    91
		String line = this.in.readLine();
cli@37
    92
		if (line == null || !line.startsWith("250 ")) {
cli@37
    93
			throw new IOException("Unexpected reply: " + line);
cli@37
    94
		}
chris@3
    95
cli@37
    96
		this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
cli@37
    97
		this.out.flush();
cli@37
    98
		line = this.in.readLine();
cli@37
    99
		if (line == null || !line.startsWith("250 ")) {
cli@37
   100
			throw new IOException("Unexpected reply: " + line);
cli@37
   101
		}
chris@3
   102
cli@37
   103
		this.out.write("DATA".getBytes("UTF-8"));
cli@37
   104
		this.out.flush();
cli@37
   105
		line = this.in.readLine();
cli@37
   106
		if (line == null || !line.startsWith("354 ")) {
cli@37
   107
			throw new IOException("Unexpected reply: " + line);
cli@37
   108
		}
chris@3
   109
cli@37
   110
		ArticleInputStream artStream = new ArticleInputStream(article);
cli@37
   111
		for (int b = artStream.read(); b >= 0; b = artStream.read()) {
cli@37
   112
			this.out.write(b);
cli@37
   113
		}
chris@3
   114
cli@37
   115
		// Flush the binary stream; important because otherwise the output
cli@37
   116
		// will be mixed with the PrintWriter.
cli@37
   117
		this.out.flush();
cli@37
   118
		this.out.write("\r\n.\r\n".getBytes("UTF-8"));
cli@37
   119
		this.out.flush();
cli@37
   120
		line = this.in.readLine();
cli@37
   121
		if (line == null || !line.startsWith("250 ")) {
cli@37
   122
			throw new IOException("Unexpected reply: " + line);
cli@37
   123
		}
cli@37
   124
	}
chris@3
   125
}