src/org/sonews/mlgw/SMTPTransport.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 08 Oct 2011 23:32:18 +0200
changeset 63 d883d4ab7b9d
parent 61 da0c30d28e22
permissions -rwxr-xr-x
Drupal: kostry tříd
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
package org.sonews.mlgw;
chris@3
    19
chris@3
    20
import java.io.BufferedOutputStream;
chris@3
    21
import java.io.BufferedReader;
chris@3
    22
import java.io.IOException;
chris@3
    23
import java.io.InputStreamReader;
cli@61
    24
import java.io.UnsupportedEncodingException;
chris@3
    25
import java.net.Socket;
chris@3
    26
import java.net.UnknownHostException;
cli@59
    27
import java.util.ArrayList;
cli@59
    28
import java.util.List;
cli@61
    29
import org.apache.commons.codec.binary.Base64;
chris@3
    30
import org.sonews.config.Config;
chris@3
    31
import org.sonews.storage.Article;
chris@3
    32
import org.sonews.util.io.ArticleInputStream;
chris@3
    33
chris@3
    34
/**
chris@3
    35
 * Connects to a SMTP server and sends a given Article to it.
chris@3
    36
 * @author Christian Lins
cli@28
    37
 * @since sonews/1.0
chris@3
    38
 */
cli@58
    39
class SMTPTransport {
cli@58
    40
cli@58
    41
	public static final String NEWLINE = "\r\n";
chris@3
    42
cli@37
    43
	protected BufferedReader in;
cli@37
    44
	protected BufferedOutputStream out;
cli@37
    45
	protected Socket socket;
chris@3
    46
cli@37
    47
	public SMTPTransport(String host, int port)
cli@58
    48
			throws IOException, UnknownHostException {
cli@58
    49
		this.socket = new Socket(host, port);
cli@58
    50
		this.in = new BufferedReader(
cli@58
    51
				new InputStreamReader(socket.getInputStream()));
cli@37
    52
		this.out = new BufferedOutputStream(socket.getOutputStream());
chris@3
    53
cli@58
    54
		// Read HELO from server
cli@37
    55
		String line = this.in.readLine();
cli@37
    56
		if (line == null || !line.startsWith("220 ")) {
cli@58
    57
			throw new IOException("Invalid HELO from server: " + line);
cli@37
    58
		}
cli@37
    59
	}
chris@3
    60
cli@37
    61
	public void close()
cli@58
    62
			throws IOException {
cli@37
    63
		this.out.write("QUIT".getBytes("UTF-8"));
cli@37
    64
		this.out.flush();
cli@37
    65
		this.in.readLine();
chris@3
    66
cli@37
    67
		this.socket.close();
cli@37
    68
	}
chris@3
    69
cli@61
    70
	private byte[] createCredentials() throws UnsupportedEncodingException {
cli@61
    71
		String user = Config.inst().get(Config.MLSEND_USER, "");
cli@61
    72
		String pass = Config.inst().get(Config.MLSEND_PASSWORD, "");
cli@61
    73
		StringBuilder credBuf = new StringBuilder();
cli@61
    74
		credBuf.append(user);
cli@61
    75
		credBuf.append("\u0000");
cli@61
    76
		credBuf.append(pass);
cli@61
    77
		return Base64.encodeBase64(credBuf.toString().getBytes("UTF-8"));
cli@61
    78
	}
cli@61
    79
cli@58
    80
	private void ehlo(String hostname) throws IOException {
cli@58
    81
		StringBuilder strBuf = new StringBuilder();
cli@58
    82
		strBuf.append("EHLO ");
cli@58
    83
		strBuf.append(hostname);
cli@58
    84
		strBuf.append(NEWLINE);
cli@58
    85
cli@58
    86
		// Send EHLO to server
cli@58
    87
		this.out.write(strBuf.toString().getBytes("UTF-8"));
cli@58
    88
		this.out.flush();
cli@58
    89
cli@59
    90
		List<String> ehloReplies = readReply("250");
cli@58
    91
cli@58
    92
		// TODO: Check for supported methods
cli@58
    93
cli@58
    94
		// Do a PLAIN login
cli@58
    95
		strBuf = new StringBuilder();
cli@58
    96
		strBuf.append("AUTH PLAIN");
cli@58
    97
		strBuf.append(NEWLINE);
cli@58
    98
cli@58
    99
		// Send AUTH to server
cli@58
   100
		this.out.write(strBuf.toString().getBytes("UTF-8"));
cli@58
   101
		this.out.flush();
cli@58
   102
cli@59
   103
		readReply("334");
cli@59
   104
cli@59
   105
		// Send PLAIN credentials to server
cli@61
   106
		this.out.write(createCredentials());
cli@61
   107
		this.out.flush();
cli@59
   108
cli@61
   109
		// Read reply of successful login
cli@61
   110
		readReply("235");
cli@58
   111
	}
cli@58
   112
cli@58
   113
	private void helo(String hostname) throws IOException {
cli@58
   114
		StringBuilder heloStr = new StringBuilder();
cli@58
   115
		heloStr.append("HELO ");
cli@58
   116
		heloStr.append(hostname);
cli@58
   117
		heloStr.append(NEWLINE);
cli@58
   118
cli@58
   119
		// Send HELO to server
cli@58
   120
		this.out.write(heloStr.toString().getBytes("UTF-8"));
cli@58
   121
		this.out.flush();
cli@58
   122
cli@58
   123
		// Read reply
cli@59
   124
		readReply("250");
cli@58
   125
	}
cli@58
   126
cli@58
   127
	public void login() throws IOException {
cli@58
   128
		String hostname = Config.inst().get(Config.HOSTNAME, "localhost");
cli@58
   129
		String auth = Config.inst().get(Config.MLSEND_AUTH, "none");
cli@58
   130
		if(auth.equals("none")) {
cli@58
   131
			helo(hostname);
cli@58
   132
		} else {
cli@58
   133
			ehlo(hostname);
cli@58
   134
		}
cli@58
   135
	}
cli@58
   136
cli@59
   137
	/**
cli@59
   138
	 * Read one or more exspected reply lines.
cli@59
   139
	 * @param expectedReply
cli@59
   140
	 * @return
cli@59
   141
	 * @throws IOException If the reply of the server does not fit the exspected
cli@59
   142
	 * reply code.
cli@59
   143
	 */
cli@59
   144
	private List<String> readReply(String expectedReply) throws IOException {
cli@59
   145
		List<String> replyStrings = new ArrayList<String>();
cli@59
   146
cli@59
   147
		for(;;) {
cli@59
   148
			String line = this.in.readLine();
cli@59
   149
			if (line == null || !line.startsWith(expectedReply)) {
cli@59
   150
				throw new IOException("Unexpected reply: " + line);
cli@59
   151
			}
cli@59
   152
cli@59
   153
			replyStrings.add(line);
cli@59
   154
cli@59
   155
			if(line.charAt(3) == ' ') { // Last reply line
cli@59
   156
				break;
cli@59
   157
			}
cli@59
   158
		}
cli@59
   159
cli@59
   160
		return replyStrings;
cli@59
   161
	}
cli@59
   162
cli@37
   163
	public void send(Article article, String mailFrom, String rcptTo)
cli@58
   164
			throws IOException {
cli@37
   165
		assert (article != null);
cli@37
   166
		assert (mailFrom != null);
cli@37
   167
		assert (rcptTo != null);
cli@12
   168
cli@37
   169
		this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
cli@37
   170
		this.out.flush();
cli@37
   171
		String line = this.in.readLine();
cli@37
   172
		if (line == null || !line.startsWith("250 ")) {
cli@37
   173
			throw new IOException("Unexpected reply: " + line);
cli@37
   174
		}
chris@3
   175
cli@37
   176
		this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
cli@37
   177
		this.out.flush();
cli@37
   178
		line = this.in.readLine();
cli@37
   179
		if (line == null || !line.startsWith("250 ")) {
cli@37
   180
			throw new IOException("Unexpected reply: " + line);
cli@37
   181
		}
chris@3
   182
cli@37
   183
		this.out.write("DATA".getBytes("UTF-8"));
cli@37
   184
		this.out.flush();
cli@37
   185
		line = this.in.readLine();
cli@37
   186
		if (line == null || !line.startsWith("354 ")) {
cli@37
   187
			throw new IOException("Unexpected reply: " + line);
cli@37
   188
		}
chris@3
   189
cli@37
   190
		ArticleInputStream artStream = new ArticleInputStream(article);
cli@37
   191
		for (int b = artStream.read(); b >= 0; b = artStream.read()) {
cli@37
   192
			this.out.write(b);
cli@37
   193
		}
chris@3
   194
cli@37
   195
		// Flush the binary stream; important because otherwise the output
cli@37
   196
		// will be mixed with the PrintWriter.
cli@37
   197
		this.out.flush();
cli@37
   198
		this.out.write("\r\n.\r\n".getBytes("UTF-8"));
cli@37
   199
		this.out.flush();
cli@37
   200
		line = this.in.readLine();
cli@37
   201
		if (line == null || !line.startsWith("250 ")) {
cli@37
   202
			throw new IOException("Unexpected reply: " + line);
cli@37
   203
		}
cli@37
   204
	}
chris@3
   205
}