📄 pop3protocol.java
字号:
/** * <p>Title: StandBayeMail </p> * <p>Description: A bayesian spam filter</p> * <p>Copyright: Copyright (c) 2004 by Luca M. Viola</p> * <p>Company: 3AM.it</p> * @author Luca M. Viola <luca@3am.it> * @version 1.0 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package pop3proxy;import java.net.*;import StandBayeMail.*;import java.io.File;class Pop3Protocol implements Runnable{ protected String user; protected SocketWrapper client = null; protected SocketWrapper server = null; protected ServerMapper userContainer = null; protected Pop3StateMachine sm = new Pop3StateMachine(); Logger trace; StandBayeMail sd; public Pop3Protocol(Socket socket, ServerMapper userContainer, Logger trace,StandBayeMail sd) { try { client = new LoggedSocketWrapper(socket, trace, Logger.CLIENT); this.userContainer = userContainer; this.trace = trace; this.sd=sd; new Thread(this).start(); } catch (Exception e) { } } public void close() { if (client != null) { client.close(); client = null; } if (server != null) { server.close(); server = null; } } public void run() { try { client.write("+OK POP3 bayesian proxy server ready", true); String line; while (client != null) { if ( (line = client.read()) == null) break; execute(line); } } catch (Exception e) { e.printStackTrace(); } close(); trace.log(Logger.PROXY,"CONNECTION CLOSED (" + (new java.util.Date()) +")"); } protected void execute(String line) throws Exception { String command = line; String arguments = new String(); int space = line.indexOf(' '); if (space != -1) { command = line.substring(0, space); command = command.toUpperCase(); arguments = line.substring(space + 1); } if (command.compareTo("USER") == 0) { execute_USER(arguments); } else if (command.compareTo("PASS") == 0) { execute_PASS(arguments); } else if (command.compareTo("DELE") == 0) { execute_DELE(arguments); } else if (command.compareTo("LIST") == 0) { execute_LIST(arguments); } else if (command.compareTo("QUIT") == 0) { execute_QUIT(); } else if (command.compareTo("RETR") == 0) { execute_RETR(arguments); } else if (command.compareTo("STAT") == 0) { execute_STAT(); } else if (command.compareTo("NOOP") == 0) { execute_NOOP(); } else if (command.compareTo("UIDL") == 0) { execute_UIDL(arguments); } else if (command.compareTo("RSET") == 0) { execute_RSET(); } else if (command.compareTo("TOP") == 0) { execute_TOP(arguments); } else if (command.compareTo("APOP") == 0) { client.write("-ERR Not implemented"); } else { client.write("-ERR unknown command"); } if (client != null) { client.flush(); } } void execute_USER(String name) throws Exception { sm.checkState(sm.STATE_AUTH); String serverName = userContainer.getServer(name); if (serverName == null) { client.write("-ERR invalid username", true); close(); } else { try { server = new LoggedSocketWrapper(serverName, trace, Logger.SERVER); String welcome = server.read(); if (!welcome.startsWith("+OK")) { throw new Exception(welcome); } client.write(server.writeread("USER " + name)); } catch (Exception e) { client.write("-ERR invalid username (2)", true); close(); } } } void execute_PASS(String password) throws Exception { sm.checkState(sm.STATE_AUTH); String response = server.writeread("PASS " + password); if (response.startsWith("+OK")) { sm.setState(sm.STATE_ACTION); } client.write(response); } void execute_NOOP() throws Exception { sm.checkState(sm.STATE_ACTION); client.write(server.writeread("NOOP")); } void execute_QUIT() throws Exception { if (sm.getState() == sm.STATE_ACTION) { sm.setState(sm.STATE_QUIT); } client.write(server.writeread("QUIT")); close(); } void execute_STAT() throws Exception { sm.checkState(sm.STATE_ACTION); client.write(server.writeread("STAT")); } void execute_DELE(String arg) throws Exception { sm.checkState(sm.STATE_ACTION); client.write(server.writeread("DELE " + arg)); } // Insert a new header at the end of the header list private StringBuffer insHeader( String header,StringBuffer mail ) { int last=0,pos=0; String _mail=mail.toString();/* while( pos!=-1 ) { pos = _mail.indexOf(": ",pos+1); if( pos!=-1 ) last=pos; } */ int eol=_mail.indexOf("\r\n\r\n",last); if( eol!=-1 ) mail.replace(eol,eol+4, "\r\n" + header + "\r\n\r\n"); return mail; } void execute_RETR(String message_id) throws Exception { sm.checkState(sm.STATE_ACTION); String response = server.writeread("RETR " + message_id); String backup = response; if (response.startsWith("+OK")) { StringBuffer body = new StringBuffer(""); while (true) { response = server.read(); if (response.compareTo(".") == 0) { break; } body.append(response); body.append("\r\n"); } String _body = body.toString(); Statistics st = sd.calculateSpamicity(Pop3Proxy.getCurrentWDir()+File.separator+"goodlist.sps", Pop3Proxy.getCurrentWDir()+File.separator+"spamlist.sps", _body); if (st.spamicity >= BayesianFilter.getSpamicityThreshold()) { String subjHdr = "Subject: "; int subjpos = _body.indexOf(subjHdr); if( subjpos==-1 ) { body=insHeader(subjHdr,body ); _body=body.toString(); subjpos = _body.indexOf(subjHdr); } if( subjpos!=-1 ) body.replace(subjpos, subjpos + subjHdr.length(), subjHdr + "[SPAM] "); String spamhdr = "X-StandBayeMail-Spamicity: " + st.spamicity; body=insHeader(spamhdr,body); _body = body.toString(); response = "+OK " + _body.length() + " bytes"; client.write(response); } else client.write(backup); client.write(_body + "."); client.flush(); java.text.DecimalFormat df=new java.text.DecimalFormat("0.################################"); String d=df.format(st.spamicity); trace.log(Logger.PROXY,"Spamicity: "+d); for( int k=0; k<st.extrema.length; k++ ) trace.log(Logger.PROXY,st.extrema[k].key+"="+df.format(st.extrema[k].prob)); return; } client.write(backup,true); } void execute_LIST(String msg) throws Exception { sm.checkState(sm.STATE_ACTION); if (msg.length() > 0) { client.write(server.writeread("LIST " + msg)); } else { String response = server.writeread("LIST"); client.write(response); if (response.startsWith("+OK")) { while (true) { response = server.read(); client.write(response); if (response.compareTo(".") == 0) break; } } } } void execute_UIDL(String arg) throws Exception { sm.checkState(sm.STATE_ACTION); if (arg.length() > 0) { client.write(server.writeread("UIDL " + arg)); } else { String request = server.writeread("UIDL"); client.write(request); if (request.startsWith("+OK")) { while (true) { request = server.read(); client.write(request); if (request.compareTo(".") == 0) break; } } } } void execute_RSET() throws Exception { sm.checkState(sm.STATE_ACTION); client.write(server.writeread("RSET")); } void execute_TOP(String arg) throws Exception { sm.checkState(sm.STATE_ACTION); client.write(server.writeread("TOP " + arg)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -