1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.smtpserver.core;
22
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.james.smtpserver.ConnectHandler;
27 import org.apache.james.smtpserver.SMTPSession;
28 import org.apache.james.util.POP3BeforeSMTPHelper;
29 import org.apache.james.util.TimeConverter;
30
31 /**
32 * This ConnectHandler can be used to activate pop-before-smtp
33 */
34 public class POP3BeforeSMTPHandler implements ConnectHandler, Configurable {
35
36 /**
37 * The time after which ipAddresses should be handled as expired
38 */
39 private long expireTime = POP3BeforeSMTPHelper.EXPIRE_TIME;
40
41 /**
42 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
43 */
44 public void configure(Configuration arg0) throws ConfigurationException {
45 Configuration config = arg0.getChild("expireTime", false);
46
47 if (config != null) {
48 try {
49 setExpireTime(config.getValue(null));
50 } catch (NumberFormatException e) {
51 throw new ConfigurationException(
52 "Please configure a valid expireTime: "
53 + e.getMessage());
54 }
55 }
56 }
57
58 /**
59 * Set the time after which an ipAddresses should be handled as expired
60 *
61 * @param rawExpireTime
62 * The time
63 */
64 public void setExpireTime(String rawExpireTime) {
65 if (rawExpireTime != null) {
66 this.expireTime = TimeConverter.getMilliSeconds(rawExpireTime);
67 }
68 }
69
70 /**
71 * @see org.apache.james.smtpserver.ConnectHandler#onConnect(SMTPSession)
72 */
73 public void onConnect(SMTPSession session) {
74
75 // some kind of random cleanup process
76 if (Math.random() > 0.99) {
77 POP3BeforeSMTPHelper.removeExpiredIP(expireTime);
78 }
79
80 // Check if the ip is allowed to relay
81 if (!session.isRelayingAllowed()
82 && POP3BeforeSMTPHelper.isAuthorized(session.getRemoteIPAddress())) {
83 session.setRelayingAllowed(true);
84 }
85 }
86
87 }