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;
|
chris@3
|
24 |
import java.net.Socket;
|
chris@3
|
25 |
import java.net.UnknownHostException;
|
chris@3
|
26 |
import org.sonews.config.Config;
|
chris@3
|
27 |
import org.sonews.storage.Article;
|
chris@3
|
28 |
import org.sonews.util.io.ArticleInputStream;
|
chris@3
|
29 |
|
chris@3
|
30 |
/**
|
chris@3
|
31 |
* Connects to a SMTP server and sends a given Article to it.
|
chris@3
|
32 |
* @author Christian Lins
|
cli@28
|
33 |
* @since sonews/1.0
|
chris@3
|
34 |
*/
|
cli@58
|
35 |
class SMTPTransport {
|
cli@58
|
36 |
|
cli@58
|
37 |
public static final String NEWLINE = "\r\n";
|
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@58
|
44 |
throws IOException, UnknownHostException {
|
cli@58
|
45 |
this.socket = new Socket(host, port);
|
cli@58
|
46 |
this.in = new BufferedReader(
|
cli@58
|
47 |
new InputStreamReader(socket.getInputStream()));
|
cli@37
|
48 |
this.out = new BufferedOutputStream(socket.getOutputStream());
|
chris@3
|
49 |
|
cli@58
|
50 |
// Read HELO from server
|
cli@37
|
51 |
String line = this.in.readLine();
|
cli@37
|
52 |
if (line == null || !line.startsWith("220 ")) {
|
cli@58
|
53 |
throw new IOException("Invalid HELO from server: " + line);
|
cli@37
|
54 |
}
|
cli@37
|
55 |
}
|
chris@3
|
56 |
|
cli@37
|
57 |
public void close()
|
cli@58
|
58 |
throws IOException {
|
cli@37
|
59 |
this.out.write("QUIT".getBytes("UTF-8"));
|
cli@37
|
60 |
this.out.flush();
|
cli@37
|
61 |
this.in.readLine();
|
chris@3
|
62 |
|
cli@37
|
63 |
this.socket.close();
|
cli@37
|
64 |
}
|
chris@3
|
65 |
|
cli@58
|
66 |
private void ehlo(String hostname) throws IOException {
|
cli@58
|
67 |
StringBuilder strBuf = new StringBuilder();
|
cli@58
|
68 |
strBuf.append("EHLO ");
|
cli@58
|
69 |
strBuf.append(hostname);
|
cli@58
|
70 |
strBuf.append(NEWLINE);
|
cli@58
|
71 |
|
cli@58
|
72 |
// Send EHLO to server
|
cli@58
|
73 |
this.out.write(strBuf.toString().getBytes("UTF-8"));
|
cli@58
|
74 |
this.out.flush();
|
cli@58
|
75 |
|
cli@58
|
76 |
// Read reply: "250-example.org Hello example.net"
|
cli@58
|
77 |
String line = this.in.readLine();
|
cli@58
|
78 |
if (line == null || !line.startsWith("250")) {
|
cli@58
|
79 |
throw new IOException("Unexpected reply: " + line);
|
cli@58
|
80 |
}
|
cli@58
|
81 |
|
cli@58
|
82 |
// FIXME: More 250- lines possible!
|
cli@58
|
83 |
|
cli@58
|
84 |
// Read reply: "250 AUTH CRAM-MD5 LOGIN PLAIN"
|
cli@58
|
85 |
line = this.in.readLine();
|
cli@58
|
86 |
if (line == null || !line.startsWith("250 ")) {
|
cli@58
|
87 |
throw new IOException("Unexpected reply: " + line);
|
cli@58
|
88 |
}
|
cli@58
|
89 |
String[] authMethods = line.split(" ");
|
cli@58
|
90 |
// TODO: Check for supported methods
|
cli@58
|
91 |
|
cli@58
|
92 |
// Do a PLAIN login
|
cli@58
|
93 |
strBuf = new StringBuilder();
|
cli@58
|
94 |
strBuf.append("AUTH PLAIN");
|
cli@58
|
95 |
strBuf.append(NEWLINE);
|
cli@58
|
96 |
|
cli@58
|
97 |
// Send AUTH to server
|
cli@58
|
98 |
this.out.write(strBuf.toString().getBytes("UTF-8"));
|
cli@58
|
99 |
this.out.flush();
|
cli@58
|
100 |
|
cli@58
|
101 |
// Read reply
|
cli@58
|
102 |
line = this.in.readLine();
|
cli@58
|
103 |
if (line == null || !line.startsWith("250 ")) {
|
cli@58
|
104 |
throw new IOException("Unexpected reply: " + line);
|
cli@58
|
105 |
}
|
cli@58
|
106 |
}
|
cli@58
|
107 |
|
cli@58
|
108 |
private void helo(String hostname) throws IOException {
|
cli@58
|
109 |
StringBuilder heloStr = new StringBuilder();
|
cli@58
|
110 |
heloStr.append("HELO ");
|
cli@58
|
111 |
heloStr.append(hostname);
|
cli@58
|
112 |
heloStr.append(NEWLINE);
|
cli@58
|
113 |
|
cli@58
|
114 |
// Send HELO to server
|
cli@58
|
115 |
this.out.write(heloStr.toString().getBytes("UTF-8"));
|
cli@58
|
116 |
this.out.flush();
|
cli@58
|
117 |
|
cli@58
|
118 |
// Read reply
|
cli@58
|
119 |
String line = this.in.readLine();
|
cli@58
|
120 |
if (line == null || !line.startsWith("250 ")) {
|
cli@58
|
121 |
throw new IOException("Unexpected reply: " + line);
|
cli@58
|
122 |
}
|
cli@58
|
123 |
}
|
cli@58
|
124 |
|
cli@58
|
125 |
public void login() throws IOException {
|
cli@58
|
126 |
String hostname = Config.inst().get(Config.HOSTNAME, "localhost");
|
cli@58
|
127 |
String auth = Config.inst().get(Config.MLSEND_AUTH, "none");
|
cli@58
|
128 |
if(auth.equals("none")) {
|
cli@58
|
129 |
helo(hostname);
|
cli@58
|
130 |
} else {
|
cli@58
|
131 |
ehlo(hostname);
|
cli@58
|
132 |
}
|
cli@58
|
133 |
}
|
cli@58
|
134 |
|
cli@37
|
135 |
public void send(Article article, String mailFrom, String rcptTo)
|
cli@58
|
136 |
throws IOException {
|
cli@37
|
137 |
assert (article != null);
|
cli@37
|
138 |
assert (mailFrom != null);
|
cli@37
|
139 |
assert (rcptTo != null);
|
cli@12
|
140 |
|
cli@37
|
141 |
this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
|
cli@37
|
142 |
this.out.flush();
|
cli@37
|
143 |
String line = this.in.readLine();
|
cli@37
|
144 |
if (line == null || !line.startsWith("250 ")) {
|
cli@37
|
145 |
throw new IOException("Unexpected reply: " + line);
|
cli@37
|
146 |
}
|
chris@3
|
147 |
|
cli@37
|
148 |
this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
|
cli@37
|
149 |
this.out.flush();
|
cli@37
|
150 |
line = this.in.readLine();
|
cli@37
|
151 |
if (line == null || !line.startsWith("250 ")) {
|
cli@37
|
152 |
throw new IOException("Unexpected reply: " + line);
|
cli@37
|
153 |
}
|
chris@3
|
154 |
|
cli@37
|
155 |
this.out.write("DATA".getBytes("UTF-8"));
|
cli@37
|
156 |
this.out.flush();
|
cli@37
|
157 |
line = this.in.readLine();
|
cli@37
|
158 |
if (line == null || !line.startsWith("354 ")) {
|
cli@37
|
159 |
throw new IOException("Unexpected reply: " + line);
|
cli@37
|
160 |
}
|
chris@3
|
161 |
|
cli@37
|
162 |
ArticleInputStream artStream = new ArticleInputStream(article);
|
cli@37
|
163 |
for (int b = artStream.read(); b >= 0; b = artStream.read()) {
|
cli@37
|
164 |
this.out.write(b);
|
cli@37
|
165 |
}
|
chris@3
|
166 |
|
cli@37
|
167 |
// Flush the binary stream; important because otherwise the output
|
cli@37
|
168 |
// will be mixed with the PrintWriter.
|
cli@37
|
169 |
this.out.flush();
|
cli@37
|
170 |
this.out.write("\r\n.\r\n".getBytes("UTF-8"));
|
cli@37
|
171 |
this.out.flush();
|
cli@37
|
172 |
line = this.in.readLine();
|
cli@37
|
173 |
if (line == null || !line.startsWith("250 ")) {
|
cli@37
|
174 |
throw new IOException("Unexpected reply: " + line);
|
cli@37
|
175 |
}
|
cli@37
|
176 |
}
|
chris@3
|
177 |
}
|