import org.ibex.crypto.*;
import org.ibex.js.*;
import org.ibex.util.*;
+import org.ibex.mail.protocol.*;
import java.util.*;
import java.net.*;
import java.io.*;
}
public static class StoredMessage extends Message {
- public StoredMessage(/*ReadStream rs*/BufferedReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
+ public StoredMessage(/*ReadStream rs*/SMTP.LineReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
super(rs, dotTerminatedLikeSMTP); uid = -1; }
public final int uid;
public boolean deleted = false;
}
// FIXME: support dotTerminatedLikeSMTP
- public Message(/*ReadStream rs*/BufferedReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
+ public Message(/*ReadStream rs*/SMTP.LineReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
String key = null;
StringBuffer all = new StringBuffer();
String lastKey = null;
public class SMTP extends MessageProtocol {
+ public static void main(String[] s) throws IOException {
+ SMTP smtp = new SMTP();
+ ServerSocket ss = new ServerSocket(1025);
+ Socket sock = ss.accept();
+ smtp.handle(sock);
+ }
+
public SMTP() {/* setProtocolName("SMTP"); */}
+ public void handle(Socket s) throws IOException { new Listener(s).handleRequest(); }
//public ServerRequest createRequest(Connection conn) { return new Listener((TcpConnection)conn); }
public static class Outgoing {
}
}
+ 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') return ret.toString();
+ ret.append((char)c);
+ }
+ }
+ }
+
private class Listener extends Incoming /*implements ServerRequest*/ {
//TcpConnection conn;
Socket conn;
public boolean handleRequest() throws IOException {
//ReadStream rs = conn.getReadStream();
//WriteStream ws = conn.getWriteStream();
- BufferedReader rs = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ LineReader rs = new LineReader(conn.getInputStream());
PrintWriter ws = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
// FIXME
//ws.setNewLineString("\r\n");
ws.println("220 " + /*conn.getVirtualHost()*/ "megacz.com" + " ESMTP " + this.getClass().getName());
+ ws.flush();
Message.Address from = null;
Vector to = new Vector();
// (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();
from = null;
to = new Vector();
ws.println("250-" + /*conn.getVirtualHost()*/("megacz.com"));
ws.println("250-SIZE");
ws.println("250 PIPELINING");
+ ws.flush();
from = null;
to = new Vector();
from = null;
to = new Vector();
ws.println("250 reset ok");
+ ws.flush();
} else if (command.toUpperCase().startsWith("MAIL FROM:")) {
command = command.substring(10).trim();
if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
from = new Message.Address(command);
+ ws.println("250 " + from + " is syntactically correct");
+ ws.flush();
} else if (command.toUpperCase().startsWith("RCPT TO:")) {
if (from == null) {
ws.println("503 MAIL FROM must precede RCPT TO");
+ ws.flush();
continue;
}
command = command.substring(10).trim();
if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
- to.addElement(new Message.Address(command));
+ Message.Address addr = new Message.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"); continue; }
- if (to == null) { ws.println("503 RCPT TO command must precede DATA"); continue; }
+ 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; }
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;
accept(m);
} finally {
//ws.println("251 user not local; will forward");
- if (good) ws.println("250 OK message accepted for delivery");
+ if (good) { ws.println("250 OK message accepted for delivery"); ws.flush(); }
else { /* FIXME */ }
}
} 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");
- } else if (command.toUpperCase().startsWith("NOOP")) { ws.println("250 OK");
+ } 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();
}
}