chris@3: /* chris@3: * SONEWS News Server chris@3: * see AUTHORS for the list of contributors chris@3: * chris@3: * This program is free software: you can redistribute it and/or modify chris@3: * it under the terms of the GNU General Public License as published by chris@3: * the Free Software Foundation, either version 3 of the License, or chris@3: * (at your option) any later version. chris@3: * chris@3: * This program is distributed in the hope that it will be useful, chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@3: * GNU General Public License for more details. chris@3: * chris@3: * You should have received a copy of the GNU General Public License chris@3: * along with this program. If not, see . chris@3: */ chris@3: chris@3: package org.sonews.mlgw; chris@3: chris@3: import java.io.BufferedOutputStream; chris@3: import java.io.BufferedReader; chris@3: import java.io.FileOutputStream; chris@3: import java.io.IOException; chris@3: import java.io.InputStreamReader; chris@3: import java.io.PrintWriter; chris@3: import java.net.Socket; chris@3: import java.net.UnknownHostException; chris@3: import org.sonews.config.Config; chris@3: import org.sonews.storage.Article; chris@3: import org.sonews.util.io.ArticleInputStream; chris@3: chris@3: /** chris@3: * Connects to a SMTP server and sends a given Article to it. chris@3: * @author Christian Lins chris@3: */ chris@3: class SMTPTransport chris@3: { chris@3: chris@3: protected BufferedReader in; chris@3: protected PrintWriter out; chris@3: protected Socket socket; chris@3: chris@3: public SMTPTransport(String host, int port) chris@3: throws IOException, UnknownHostException chris@3: { chris@3: socket = new Socket(host, port); chris@3: this.in = new BufferedReader(new InputStreamReader(socket.getInputStream())); chris@3: this.out = new PrintWriter(socket.getOutputStream()); chris@3: chris@3: // Read helo from server chris@3: String line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("220 ")) chris@3: { chris@3: throw new IOException("Invalid helo from server: " + line); chris@3: } chris@3: chris@3: // Send HELO to server chris@3: this.out.println("HELO " + Config.inst().get(Config.HOSTNAME, "localhost")); chris@3: this.out.flush(); chris@3: line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("250 ")) chris@3: { chris@3: throw new IOException("Unexpected reply: " + line); chris@3: } chris@3: } chris@3: chris@3: public SMTPTransport(String host) chris@3: throws IOException chris@3: { chris@3: this(host, 25); chris@3: } chris@3: chris@3: public void close() chris@3: throws IOException chris@3: { chris@3: this.out.println("QUIT"); chris@3: this.out.flush(); chris@3: this.in.readLine(); chris@3: chris@3: this.socket.close(); chris@3: } chris@3: chris@3: public void send(Article article, String mailFrom, String rcptTo) chris@3: throws IOException chris@3: { cli@12: assert(article != null); cli@12: assert(mailFrom != null); cli@12: assert(rcptTo != null); cli@12: chris@3: this.out.println("MAIL FROM: " + mailFrom); chris@3: this.out.flush(); chris@3: String line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("250 ")) chris@3: { chris@3: throw new IOException("Unexpected reply: " + line); chris@3: } chris@3: chris@3: this.out.println("RCPT TO: " + rcptTo); chris@3: this.out.flush(); chris@3: line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("250 ")) chris@3: { chris@3: throw new IOException("Unexpected reply: " + line); chris@3: } chris@3: chris@3: this.out.println("DATA"); chris@3: this.out.flush(); chris@3: line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("354 ")) chris@3: { chris@3: throw new IOException("Unexpected reply: " + line); chris@3: } chris@3: chris@3: ArticleInputStream artStream = new ArticleInputStream(article); chris@3: BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream()); chris@3: FileOutputStream fileStream = new FileOutputStream("smtp.dump"); chris@3: for(int b = artStream.read(); b >= 0; b = artStream.read()) chris@3: { chris@3: outStream.write(b); chris@3: fileStream.write(b); chris@3: } chris@3: chris@3: // Flush the binary stream; important because otherwise the output chris@3: // will be mixed with the PrintWriter. chris@3: outStream.flush(); chris@3: fileStream.flush(); chris@3: fileStream.close(); chris@3: this.out.print("\r\n.\r\n"); chris@3: this.out.flush(); chris@3: line = this.in.readLine(); chris@3: if(line == null || !line.startsWith("250 ")) chris@3: { chris@3: throw new IOException("Unexpected reply: " + line); chris@3: } chris@3: } chris@3: chris@3: }