KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jsmtpd.core.common.PluginInitException;
32 import org.jsmtpd.core.common.PluginStore;
33 import org.jsmtpd.core.common.acl.IACL;
34 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
35 import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
36 import org.jsmtpd.core.common.filter.IFilter;
37 import org.jsmtpd.core.mail.Email;
38 import org.jsmtpd.core.mail.EmailAddress;
39 import org.jsmtpd.core.mail.Rcpt;
40
41 /**
42  * @author Jean-Francois POUX
43  */

44 public class RcptRewrite implements IFilter {
45
46     private Map JavaDoc<EmailAddress,EmailAddress> rewriteMap = new HashMap JavaDoc<EmailAddress,EmailAddress>();
47     private Map JavaDoc<EmailAddress,EmailAddress> catchAllMap = new HashMap JavaDoc<EmailAddress,EmailAddress>();
48     private IACL acl = null;
49
50     private Log log = LogFactory.getLog(RcptRewrite.class);
51
52     public boolean doFilter(Email input) throws FilterTreeFailureException, FilterTreeSuccesException {
53         List JavaDoc<Rcpt> rcpts = input.getRcpt();
54         Vector JavaDoc<Rcpt> pendingRemove = new Vector JavaDoc<Rcpt>();
55
56         for (Iterator JavaDoc iter = rcpts.iterator(); iter.hasNext();) {
57             Rcpt element = (Rcpt) iter.next();
58             EmailAddress rcpt = element.getEmailAddress();
59
60             // Check if rcpt is in rw map, then apply rw
61
if (rewriteMap.containsKey(rcpt)) {
62                 EmailAddress newRcpt = (EmailAddress) rewriteMap.get(rcpt);
63                 if (newRcpt != null) {
64                     log.info("rcpt " + rcpt.toString() + " is now " + newRcpt + " in mail " + input.getDiskName());
65                     element.setEmailAddress(newRcpt);
66                 } else {
67                     log.info("Removed rcpt " + rcpt.toString() + " in mail " + input.getDiskName());
68                     pendingRemove.add(element);
69                 }
70
71             } else //might be a catchall
72
{
73                 // Apply catch all if the catchall is defined AND if the user is not excplicitly defined
74
if (catchAllMap.keySet().contains(rcpt) && !acl.isValidAddressStandardUser(rcpt)) {
75                     EmailAddress newRcpt = (EmailAddress) catchAllMap.get(rcpt);
76                     if (newRcpt != null) {
77                         log.info("rcpt " + rcpt.toString() + " is now at catch all " + newRcpt + " in mail " + input.getDiskName());
78                         element.setEmailAddress(newRcpt);
79                     } else {
80                         log.info("Removed rcpt by catchall " + rcpt.toString() + " in mail " + input.getDiskName());
81                         pendingRemove.add(element);
82                     }
83                 }//else leave alone
84
}
85         }//end for loop over rcpt
86

87         // Apply pendings
88
for (Iterator JavaDoc iter = pendingRemove.iterator(); iter.hasNext();) {
89             Rcpt element = (Rcpt) iter.next();
90             rcpts.remove(element);
91         }
92
93         return true;
94     }
95
96     public String JavaDoc getPluginName() {
97         return "Recipient Rewriter for Jsmtpd";
98     }
99
100     public void initPlugin() throws PluginInitException {
101         acl = PluginStore.getInstance().getAcl();
102         if (acl == null)
103             throw new PluginInitException();
104     }
105
106     public void shutdownPlugin() {
107
108     }
109
110     public void setRewrite(String JavaDoc in) throws Exception JavaDoc {
111         if (!in.contains(","))
112             throw new Exception JavaDoc("Property set must be user@domain,user2@domain2");
113         String JavaDoc[] args = in.split(",");
114
115         EmailAddress from = EmailAddress.parseAddress(args[0]);
116         EmailAddress to;
117         if (args[1].equals("remove"))
118             to = null;
119         else
120             to = EmailAddress.parseAddress(args[1]);
121
122         if (rewriteMap.containsKey(from))
123             throw new Exception JavaDoc("Alias already defined");
124
125         rewriteMap.put(from, to);
126     }
127
128     public void setCatchAll(String JavaDoc in) throws Exception JavaDoc {
129         if (!in.contains(","))
130             throw new Exception JavaDoc("Property set must be *@domain,user2@domain2");
131         String JavaDoc[] args = in.split(",");
132
133         if (!args[0].contains("*"))
134             throw new Exception JavaDoc("Property set must be *@domain,user2@domain2");
135         if (args[1].contains("*"))
136             throw new Exception JavaDoc("Property set must be *@domain,user2@domain2");
137
138         EmailAddress to;
139         EmailAddress from = EmailAddress.parseAddress(args[0]);
140
141         if (args[1].equals("remove"))
142             to = null;
143         else
144             to = EmailAddress.parseAddress(args[1]);
145
146         if (catchAllMap.containsKey(from))
147             throw new Exception JavaDoc("Catch All already defined");
148
149         catchAllMap.put(from, to);
150     }
151 }
Popular Tags