KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > filters > builtin > SimpleBlackListManager


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.builtin;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26 import javax.mail.MessagingException JavaDoc;
27 import javax.mail.internet.MimeMessage JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jsmtpd.config.ModuleNotFoundException;
32 import org.jsmtpd.core.common.IGenericPlugin;
33 import org.jsmtpd.core.common.PluginInitException;
34 import org.jsmtpd.core.common.PluginStore;
35 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
36 import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
37 import org.jsmtpd.core.common.filter.IFilter;
38 import org.jsmtpd.core.mail.Email;
39 import org.jsmtpd.core.mail.EmailAddress;
40 import org.jsmtpd.core.mail.InvalidAddress;
41 import org.jsmtpd.core.mail.Rcpt;
42 import org.jsmtpd.core.send.QueueService;
43 import org.jsmtpd.plugins.inputIPFilters.BlackList;
44
45 /**
46  * @author Jean-Francois POUX
47  */

48 public class SimpleBlackListManager implements IFilter {
49
50     String JavaDoc blackListPluginName = "";
51     BlackList controlled = null;
52
53     String JavaDoc allowedSource = "";
54     String JavaDoc managerMail = "";
55     EmailAddress manMail = null;
56     QueueService dsvc;
57
58     private Log log = LogFactory.getLog(SimpleBlackListManager.class);
59
60     public boolean doFilter(Email input) throws FilterTreeFailureException, FilterTreeSuccesException {
61         // if mail is not from relayed domains, return true (continue process)
62
// if mail is not for jsmtpd-blacklistmanager return true
63
// If mail is not from admin ip adresses, return true
64
// else : query the plugin manager for the plugin name
65
// Then analyse the mail for command an execute it over the ip
66
// send confirm message
67
// return false (break of chain, don't deliver the mail)
68

69         if (!input.getReceivedFrom().equals(allowedSource))
70             return true; // Skip
71

72         if (!(input.getRcpt().size() != 1))
73             return true; // Skip
74

75         Rcpt tmp = (Rcpt) input.getRcpt().get(0);
76         if (!tmp.getEmailAddress().equals(manMail))
77             return true;
78
79         try {
80             MimeMessage JavaDoc msg = new MimeMessage JavaDoc(null, new ByteArrayInputStream JavaDoc(input.getDataAsByte()));
81             String JavaDoc command = msg.getSubject();
82
83             if ((command == null) || (command.length() < 7))
84                 return true;
85
86             if (command.equals("add")) {
87                 String JavaDoc ip = command.substring(4);
88                 //controlled.addPermanentBL(ip, new Date(0));
89
log.info("IP address " + ip + " added to BlackList");
90
91                 LinkedList JavaDoc<String JavaDoc> mesgs = new LinkedList JavaDoc<String JavaDoc>();
92                 mesgs.add("Sucessfully added IP " + ip + " to blacklist manager");
93                 mesgs.add("");
94                 mesgs.add("This host can't connect now to Jsmtpd");
95                 mesgs.add("");
96
97                 Email e = Email.createInternalMail(input.getFrom(), "BlackList admin - added address", mesgs, null);
98                 dsvc.queueMail(e);
99             }
100
101             if (command.equals("remove")) {
102                 String JavaDoc ip = command.substring(4);
103                 //controlled.removePermanentBL(ip);
104
log.info("IP address " + ip + " removed from BlackList");
105
106             }
107
108             return false; // Break of chain.
109

110         } catch (MessagingException JavaDoc e) {
111             log.error("Can't parse message " + input.getDiskName() + " ", e);
112             return false;
113         }
114
115         // transform to MimeMsg
116
// Parse command
117
//Create new Email with mmsg
118
// Apply & confirm
119

120     }
121
122     public String JavaDoc getPluginName() {
123         return "Email controlled BlackList manager";
124     }
125
126     public void initPlugin() throws PluginInitException {
127         PluginStore pluginStore = PluginStore.getInstance();
128         IGenericPlugin query;
129         try {
130             query = pluginStore.getPluginByLogicalName(blackListPluginName);
131         } catch (ModuleNotFoundException e) {
132             throw new PluginInitException(e);
133         }
134
135         if (!(query instanceof BlackList))
136             throw new PluginInitException("The plugin name " + blackListPluginName + " is not controlable");
137
138         dsvc = QueueService.getInstance();
139         controlled = (BlackList) query;
140
141         try {
142             manMail = EmailAddress.parseAddress(managerMail);
143         } catch (InvalidAddress e1) {
144             throw new PluginInitException(e1);
145         }
146
147     }
148
149     public void shutdownPlugin() {
150
151     }
152
153     public String JavaDoc getBlackListPluginName() {
154         return blackListPluginName;
155     }
156
157     public void setBlackListPluginName(String JavaDoc blackListPluginName) {
158         this.blackListPluginName = blackListPluginName;
159     }
160
161     public String JavaDoc getAllowedSource() {
162         return allowedSource;
163     }
164
165     public void setAllowedSource(String JavaDoc allowedSource) {
166         this.allowedSource = allowedSource;
167     }
168
169     public String JavaDoc getManagerMail() {
170         return managerMail;
171     }
172
173     public void setManagerMail(String JavaDoc managerMail) {
174         this.managerMail = managerMail;
175     }
176 }
Popular Tags