KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > filters > SA > SAChat


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.filters.SA;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.SocketAddress JavaDoc;
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 /**
37  * Spamassassin plugin. <br>
38  * Connects to a spamd, passes it the message, then read the response and replace the content with it.
39  * @author Jean-Francois POUX
40  *
41  */

42 public class SAChat {
43
44     private Socket JavaDoc sock=null;
45     private InputStream JavaDoc is=null;
46     private OutputStream JavaDoc os=null;
47     private int spamdPort;
48     private String JavaDoc spamdHost;
49     private Log log = LogFactory.getLog(SAChat.class);
50     private int timeout;
51
52     public SAChat(String JavaDoc 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 JavaDoc();
61         
62         SocketAddress JavaDoc sockaddr = new InetSocketAddress JavaDoc(spamdHost, spamdPort);
63         try {
64             sock.setSoTimeout(timeout * 1000);
65             sock.connect(sockaddr);
66             is = sock.getInputStream();
67             os = sock.getOutputStream();
68             //Send the command + message
69
String JavaDoc s = "PROCESS SPAMC/1.0\r\n";
70             os.write(s.getBytes());
71             os.write(in.getDataAsByte());
72
73             sock.shutdownOutput();
74
75             //Read response
76
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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             //Parse response
94
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 JavaDoc response = new String JavaDoc(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                     // spamassassin sends messages with mixed CRLF and LF, so we have to clean up...
107
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 JavaDoc dt = new String JavaDoc(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 JavaDoc 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 JavaDoc e1) {}
131             try {
132                 if (os!=null)
133                 os.close();
134             } catch (IOException JavaDoc e2) {}
135             try {
136                 if (sock!=null)
137                 sock.close();
138             } catch (IOException JavaDoc e3) {}
139         }
140
141         return statusOK;
142
143     }
144
145 }
Popular Tags