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