package SmtpServer; import java.io.InputStream; import java.io.IOException; public class Request { public static final int SEND = 0; //SEND public static final int SEND_OR_MAIL = 1; //SOML public static final int SEND_AND_MAIL = 2; //SAML public static final int RESET = 3; //REST public static final int VERIFY = 4; //VRFY public static final int EXPAND = 5; //EXPN public static final int HELP = 6; //HELP public static final int NOOP = 7; //NOOP public static final int QUIT = 8; //QUIT public static final int TURN = 9; //TURN public static final int HELO = 10; //HELO public static final int MAIL = 11; //MAIL public static final int RECIPIENT = 12; //RCPT public static final int DATA = 13; //DATA public static final int UNKNOWN_CMD = 99; //unknow cmd public static final int ERROR = -1; private InputStream input; private String request; private String cmd; private int requestType; private String domain; private String reversePath; private String forwardPath; private String string; private String user; public String getRequest() { return request; } public String getRequestCmd() { return cmd; } public int getRequestType() { return requestType; } public String Domain() { return domain; } public String getReversePath() { return reversePath; } public String getForwardPath() { return forwardPath; } public String getString() { return string; } public String getUser() { return user; } public void clear() { domain = ""; reversePath = ""; forwardPath = ""; string = ""; user = ""; } public Request() { requestType = ERROR; } public Request(InputStream input) { this.input = input; } private void parse() { int index = request.indexOf(' '); if (index else cmd = request.substring(0,index).toUpperCase(); if (cmd.equals("SEND")) { requestType = SEND; int bracketL = request.indexOf(' int bracketR = request.indexOf('>'); reversePath = request.substring(bracketL+1,bracketR); return; } if (cmd.equals("SOML")) { requestType = SEND_OR_MAIL; int bracketL = request.indexOf(' int bracketR = request.indexOf('>'); reversePath = request.substring(bracketL+1,bracketR); return; } if (cmd.equals("SAML")) { requestType = SEND_AND_MAIL ; int bracketL = request.indexOf(' int bracketR = request.indexOf('>'); reversePath = request.substring(bracketL+1,bracketR); return; } if (cmd.equals("REST")) { requestType = RESET; return; } if (cmd.equals("VRFY")) { requestType = VERIFY; string = request.substring(index+1,request.length()); return; } if (cmd.equals("EXPN")) { requestType = EXPAND; string = request.substring(index+1,request.length()); return; } if (cmd.equals("HELP")) { requestType = HELP; string = request.substring(index+1,request.length()); return; } if (cmd.equals("NOOP")) { requestType = NOOP; return; } if (cmd.equals("QUIT")) { requestType = QUIT; return; } if (cmd.equals("TURN")) { requestType = TURN; return; } if (cmd.equals("HELO")) { requestType = HELO; domain = request.substring(index+1,request.length()); return; } if (cmd.equals("MAIL")) { requestType = MAIL; int bracketL = request.indexOf(' int bracketR = request.indexOf('>'); reversePath = request.substring(bracketL+1,bracketR); return; } if (cmd.equals("RCPT")) { requestType = RECIPIENT; int bracketL = request.indexOf(' int bracketR = request.indexOf('>'); forwardPath = request.substring(bracketL+1,bracketR); int index2 = forwardPath.indexOf('@'); if (index2 >=0) user = forwardPath.substring(0,index2); else user = ""; return; } if (cmd.equals("DATA")) { requestType = DATA; return; } requestType = UNKNOWN_CMD; } public String getMsg() { StringBuffer requestBuf = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j requestBuf.append((char) buffer[j]); } System.out.print(requestBuf.toString()); request = requestBuf.toString().trim(); parse(); return request; } }