1 21 package org.jsmtpd.plugins.filters.SA; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.InetSocketAddress ; 28 import java.net.Socket ; 29 import java.net.SocketAddress ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.jsmtpd.core.mail.Email; 34 import org.jsmtpd.tools.ByteArrayTool; 35 36 42 public class SAChat { 43 44 private Socket sock=null; 45 private InputStream is=null; 46 private OutputStream os=null; 47 private int spamdPort; 48 private String spamdHost; 49 private Log log = LogFactory.getLog(SAChat.class); 50 private int timeout; 51 52 public SAChat(String host, int port, int timeout) { 53 this.spamdHost = host; 54 this.spamdPort = port; 55 this.timeout = timeout; 56 } 57 58 public boolean checkMail(Email in) { 59 boolean statusOK = true; 60 sock = new Socket (); 61 62 SocketAddress sockaddr = new InetSocketAddress (spamdHost, spamdPort); 63 try { 64 sock.setSoTimeout(timeout * 1000); 65 sock.connect(sockaddr); 66 is = sock.getInputStream(); 67 os = sock.getOutputStream(); 68 String s = "PROCESS SPAMC/1.0\r\n"; 70 os.write(s.getBytes()); 71 os.write(in.getDataAsByte()); 72 73 sock.shutdownOutput(); 74 75 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 77 byte[] buffer = new byte[512]; 78 int rec = 0; 79 while (true) { 80 rec = is.read(buffer); 81 if (rec <= 0) 82 break; 83 bos.write(buffer, 0, rec); 84 } 85 86 is.close(); 87 is=null; 88 os.close(); 89 os=null; 90 sock.close(); 91 sock=null; 92 93 if (bos.size() > 18) { 95 byte[] responseReaded = bos.toByteArray(); 96 log.debug("Buffer is " + bos.size()); 97 int ncopy = 17; 98 byte[] ok = new byte[ncopy]; 99 System.arraycopy(responseReaded, 0, ok, 0, ncopy); 100 String response = new String (ok); 101 log.debug("Response=" + response); 102 if (response.contains("SPAMD/1.0 0 EX_OK")) { 103 byte[] newData = new byte[responseReaded.length - 19]; 104 System.arraycopy(responseReaded, 19, newData, 0, responseReaded.length - 19); 105 106 newData = ByteArrayTool.crlfFix(newData); 108 in.setDataBuffer(newData); 109 log.debug("SAFilter Success - content of " + in.getDiskName() + " is modified"); 110 111 if (newData.length > 512) { 112 byte[] detect = new byte[512]; 113 System.arraycopy(newData, 0, detect, 0, 512); 114 String dt = new String (detect); 115 if (dt.contains("X-Spam-Flag: YES")) 116 statusOK = false; 117 } else 118 log.debug("Error, in " + in.getDiskName() + " response to short"+ in.getDiskName() + " is left unmodified"); 119 } else 120 log.debug("Error, response not understood - content of " + in.getDiskName() + " is left unmodified"); 121 } else { 122 log.debug("Error, response too short - content of " + in.getDiskName() + " is left unmodified"); 123 } 124 } catch (IOException e) { 125 log.debug( "Error, Network IO Error - content of " + in.getDiskName() + " is left unmodified"); 126 } finally { 127 try { 128 if (is!=null) 129 is.close(); 130 } catch (IOException e1) {} 131 try { 132 if (os!=null) 133 os.close(); 134 } catch (IOException e2) {} 135 try { 136 if (sock!=null) 137 sock.close(); 138 } catch (IOException e3) {} 139 } 140 141 return statusOK; 142 143 } 144 145 } | Popular Tags |