import java.net.*;
import java.io.*;
import java.util.*;
+import java.text.*;
public class SMTP extends MessageProtocol {
- public static void main(String[] s) throws IOException {
+ public static String convdir = null;
+ public static void main(String[] s) throws Exception {
+ String logto = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
+ logto += File.separatorChar + "log";
+ Log.file(logto);
+ convdir = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
+ convdir += File.separatorChar + "conversation";
+ new File(convdir).mkdirs();
SMTP smtp = new SMTP();
- ServerSocket ss = new ServerSocket(1025);
- Socket sock = ss.accept();
- smtp.handle(sock);
+ int port = Integer.parseInt(System.getProperty("ibex.mail.port", "25"));
+ Log.info(SMTP.class, "binding to port " + port + "...");
+ ServerSocket ss = new ServerSocket(port);
+ Log.info(SMTP.class, "listening for connections...");
+ while(true) {
+ final Socket sock = ss.accept();
+ new Thread() { public void run() { smtp.handle(sock); } }.start();
+ }
}
- public SMTP() {/* setProtocolName("SMTP"); */}
- public void handle(Socket s) throws IOException { new Listener(s).handleRequest(); }
+ //public SMTP() { setProtocolName("SMTP"); }
//public ServerRequest createRequest(Connection conn) { return new Listener((TcpConnection)conn); }
+ public void handle(Socket s) { new Listener(s, "megacz.com").handleRequest(); }
+
public static class Outgoing {
// recommended retry interval is 30 minutes
// give up after 4-5 days
}
}
- public static class LineReader extends InputStreamReader {
- public LineReader(InputStream r) { super(r); }
- public String readLine() throws IOException {
- StringBuffer ret = new StringBuffer();
- while(true) {
- int c = read();
- if (c == -1) throw new EOFException();
- if (c == '\n') return ret.toString();
- if (c == '\r') continue; //return ret.toString();
- ret.append((char)c);
- }
- }
- }
+ private static String lastTime = null;
+ private static int lastCounter = 0;
private class Listener extends Incoming /*implements ServerRequest*/ {
- //TcpConnection conn;
Socket conn;
- //public Listener(TcpConnection conn) { this.conn = conn; conn.getSocket().setSoTimeout(5 * 60 * 1000); }
- public Listener(Socket conn) throws IOException { this.conn = conn; conn.setSoTimeout(5 * 60 * 1000); }
+ String vhost;
public void init() { }
+ public Listener(Socket conn, String vhost) { this.vhost = vhost; this.conn = conn; }
- public boolean handleRequest() throws IOException {
- //ReadStream rs = conn.getReadStream();
- //WriteStream ws = conn.getWriteStream();
- LineReader rs = new LineReader(conn.getInputStream());
- PrintWriter ws = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
+ //TcpConnection conn;
+ //public Listener(TcpConnection conn) { this.conn = conn; conn.getSocket().setSoTimeout(5 * 60 * 1000); }
+ public boolean handleRequest() {
+ try {
+ conn.setSoTimeout(5 * 60 * 1000);
+ StringBuffer logMessage = new StringBuffer();
+ String time = new SimpleDateFormat("yy.MMM.dd-hh:mm:ss").format(new Date());
+ synchronized (SMTP.class) {
+ if (lastTime != null && lastTime.equals(time)) {
+ time += "." + (++lastCounter);
+ } else {
+ lastTime = time;
+ }
+ }
+ String conversationId = time;
+ Log.setThreadAnnotation("[conversation/" + conversationId + "] ");
+ InetSocketAddress remote = (InetSocketAddress)conn.getRemoteSocketAddress();
+ Log.info(this, "connection from " + remote.getHostName() + ":" + remote.getPort() +
+ " (" + remote.getAddress() + ")");
+ PrintWriter logf =
+ new PrintWriter(new OutputStreamWriter(new FileOutputStream(convdir + File.separatorChar + conversationId)));
+ try {
+ return handleRequest(new LoggedLineReader(new InputStreamReader(conn.getInputStream()), logf),
+ new LoggedPrintWriter(new OutputStreamWriter(conn.getOutputStream()), logf));
+ } catch(Throwable t) {
+ Log.warn(this, t);
+ } finally {
+ logf.close();
+ Log.setThreadAnnotation("");
+ }
+ } catch (Exception e) {
+ Log.error(this, e);
+ }
+ return false;
+ }
- // FIXME
- //ws.setNewLineString("\r\n");
+ private class LoggedLineReader extends LineReader {
+ PrintWriter log;
+ public LoggedLineReader(Reader r, PrintWriter log) { super(r); this.log = log; }
+ public String readLine() throws IOException {
+ String s = super.readLine();
+ if (s != null) { log.println("C: " + s); log.flush(); }
+ return s;
+ }
+ }
- ws.println("220 " + /*conn.getVirtualHost()*/ "megacz.com" + " ESMTP " + this.getClass().getName());
- ws.flush();
+ private class LoggedPrintWriter extends PrintWriter {
+ PrintWriter log;
+ public LoggedPrintWriter(Writer w, PrintWriter log) { super(w); this.log = log; }
+ public void println(String s) {
+ log.println("S: " + s);
+ super.println(s);
+ flush();
+ }
+ }
- Message.Address from = null;
+ public boolean handleRequest(LineReader rs, PrintWriter ws) throws IOException, MailException {
+ //ReadStream rs = conn.getReadStream();
+ //WriteStream ws = conn.getWriteStream();
+ //ws.setNewLineString("\r\n");
+ ws.println("220 " + vhost + " ESMTP " + this.getClass().getName());
+ Address from = null;
Vector to = new Vector();
- // 551 = no, i won't forward that
- // 452 = mailbox full
- // see 4.4 for trace info
while(true) {
String command = rs.readLine();
- // FIXME: validate the HELO domain argument
- // (double check against other end of connection? must not reject though)
- if (command.toUpperCase().startsWith("HELO")) {
- ws.println("250 HELO " + /*conn.getVirtualHost()*/("megacz.com"));
- ws.flush();
+ String c = command.toUpperCase();
+ if (c.startsWith("HELO")) {
+ ws.println("250 HELO " + vhost);
from = null;
to = new Vector();
- } else if (command.toUpperCase().startsWith("EHLO")) {
- ws.println("250-" + /*conn.getVirtualHost()*/("megacz.com"));
+ } else if (c.startsWith("EHLO")) {
+ ws.println("250-" + vhost);
ws.println("250-SIZE");
ws.println("250 PIPELINING");
- ws.flush();
from = null;
to = new Vector();
- } else if (command.toUpperCase().startsWith("RSET")) {
+ } else if (c.startsWith("RSET")) {
from = null;
to = new Vector();
ws.println("250 reset ok");
- ws.flush();
- } else if (command.toUpperCase().startsWith("MAIL FROM:")) {
+ } else if (c.startsWith("MAIL FROM:")) {
command = command.substring(10).trim();
- if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
- from = new Message.Address(command);
+ from = new Address(command);
ws.println("250 " + from + " is syntactically correct");
- ws.flush();
- } else if (command.toUpperCase().startsWith("RCPT TO:")) {
+ } else if (c.startsWith("RCPT TO:")) {
if (from == null) {
ws.println("503 MAIL FROM must precede RCPT TO");
- ws.flush();
continue;
}
+ // FIXME: 551 = no, i won't forward that
command = command.substring(9).trim();
if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
- Message.Address addr = new Message.Address(command);
+ Address addr = new Address(command);
to.addElement(addr);
ws.println("250 " + addr + " is syntactically correct");
- ws.flush();
- } else if (command.toUpperCase().startsWith("DATA")) {
- if (from == null) { ws.println("503 MAIL FROM command must precede DATA"); ws.flush(); continue; }
- if (to == null) { ws.println("503 RCPT TO command must precede DATA"); ws.flush(); continue; }
+ } else if (c.startsWith("DATA")) {
+ if (from == null) { ws.println("503 MAIL FROM command must precede DATA"); continue; }
+ if (to == null) { ws.println("503 RCPT TO command must precede DATA"); continue; }
ws.println("354 Enter message, ending with \".\" on a line by itself");
- ws.flush();
StringBuffer data = new StringBuffer();
// move this into the Message class
boolean good = false;
try {
- good = true;
- Message m = new Message(rs, true);
- accept(m);
- } finally {
- //ws.println("251 user not local; will forward");
- if (good) { ws.println("250 OK message accepted for delivery"); ws.flush(); }
- else { /* FIXME */ }
+ accept(new Message(new DotTerminatedLineReader(rs)));
+ } catch (MailException.Malformed mfe) {
+ ws.println("501 " + mfe.toString()); break;
+ } catch (MailException.MailboxFull mbf) {
+ ws.println("452 " + mbf);
+ } catch (IOException ioe) {
+ ws.println("554 " + ioe.toString()); break;
}
+ ws.println("250 message accepted"); break;
- } else if (command.toUpperCase().startsWith("HELP")) {
- ws.println("214 sorry, you are beyond help. please see a trained professional.");
- ws.flush();
-
- } else if (command.toUpperCase().startsWith("VRFY")) { // FIXME, see code 252
- } else if (command.toUpperCase().startsWith("EXPN")) { ws.println("550 EXPN not available"); ws.flush();
- } else if (command.toUpperCase().startsWith("NOOP")) { ws.println("250 OK"); ws.flush();
- } else if (command.toUpperCase().startsWith("QUIT")) {
- ws.println("221 " + /*conn.getVirtualHost()*/("megacz.com") + " closing connection");
- ws.flush();
- break;
-
- } else {
- ws.println("500 unrecognized command");
- ws.flush();
+ } else if (c.startsWith("HELP")) { ws.println("214 you are beyond help. see a trained professional.");
+ } else if (c.startsWith("VRFY")) { ws.println("252 We don't VRFY; proceed anyway");
+ } else if (c.startsWith("EXPN")) { ws.println("550 EXPN not available");
+ } else if (c.startsWith("NOOP")) { ws.println("250 OK");
+ } else if (c.startsWith("QUIT")) { ws.println("221 " + vhost + " closing connection"); break;
+ } else { ws.println("500 unrecognized command");
}
}
- return false; // FIXME: what does this mean?
+ return false; // always tell resin to close the connection
+ }
+ }
+
+ private static class DotTerminatedLineReader extends LineReader {
+ private final LineReader r;
+ private boolean done = false;
+ public DotTerminatedLineReader(LineReader r) { super(null); this.r = r; }
+ public String readLine() throws IOException {
+ if (done) return null;
+ String s = r.readLine();
+ if (s.equals(".")) { done = true; return null; }
+ if (s.startsWith(".")) return s.substring(1);
+ return s;
}
}
}