Skip to content
This repository
Browse code

Add RCPT validator for LMTP. Fix enhanced status codes.

  • Loading branch information...
commit e366e3b61dd3f2a5ba7367d72a11107e0c72bfba 1 parent fc7398e
Rustam Aliyev authored January 20, 2012
18  modules/core/pom.xml
@@ -216,14 +216,6 @@
216 216
 			<scope>provided</scope>
217 217
 		</dependency>
218 218
 
219  
-		<!-- Logging Dependencies -->
220  
-		<dependency>
221  
-			<groupId>org.slf4j</groupId>
222  
-			<artifactId>slf4j-api</artifactId>
223  
-			<version>${bundle.slf4j.version}</version>
224  
-			<scope>provided</scope>
225  
-		</dependency>
226  
-
227 219
 		<dependency>
228 220
 			<groupId>org.apache.servicemix.bundles</groupId>
229 221
 			<artifactId>org.apache.servicemix.bundles.aopalliance</artifactId>
@@ -273,11 +265,20 @@
273 265
 			<scope>provided</scope>
274 266
 		</dependency>
275 267
 
  268
+		<!-- Logging Dependencies -->
  269
+		<dependency>
  270
+			<groupId>org.slf4j</groupId>
  271
+			<artifactId>slf4j-api</artifactId>
  272
+			<version>${bundle.slf4j.version}</version>
  273
+			<scope>provided</scope>
  274
+		</dependency>
  275
+
276 276
 		<!-- Gson -->
277 277
 		<dependency>
278 278
 			<groupId>com.google.code.gson</groupId>
279 279
 			<artifactId>gson</artifactId>
280 280
 			<version>${bundle.gson.version}</version>
  281
+			<scope>provided</scope>
281 282
 		</dependency>
282 283
 
283 284
 		<!-- Guava -->
@@ -285,6 +286,7 @@
285 286
 			<groupId>com.googlecode.guava-osgi</groupId>
286 287
 			<artifactId>guava-osgi</artifactId>
287 288
 			<version>${bundle.guava.version}</version>
  289
+			<scope>provided</scope>
288 290
 		</dependency>
289 291
 
290 292
 	</dependencies>
2  modules/lmtp/src/main/java/com/elasticinbox/lmtp/LMTPProxyServer.java
@@ -45,6 +45,7 @@
45 45
 import com.elasticinbox.lmtp.server.LMTPServerConfig;
46 46
 import com.elasticinbox.lmtp.server.api.DeliveryReturnCode;
47 47
 import com.elasticinbox.lmtp.server.api.handler.ElasticInboxDeliveryHandler;
  48
+import com.elasticinbox.lmtp.server.api.handler.ValidRcptHandler;
48 49
 import com.elasticinbox.lmtp.utils.JamesProtocolsLogger;
49 50
 
50 51
 /**
@@ -67,6 +68,7 @@ public void start() throws Exception
67 68
 
68 69
 		LMTPProtocolHandlerChain chain = new LMTPProtocolHandlerChain();
69 70
 		chain.add(0, new ElasticInboxDeliveryHandler(backend));
  71
+		chain.add(0, new ValidRcptHandler());
70 72
 		chain.wireExtensibleHandlers();
71 73
 
72 74
 		server = new NettyServer(new SMTPProtocol(chain, new LMTPServerConfig(), logger));
2  modules/lmtp/src/main/java/com/elasticinbox/lmtp/delivery/DeliveryAction.java
@@ -31,7 +31,7 @@
31 31
 /**
32 32
  * Delivery actions
33 33
  * 
34  
- * @author rustam
  34
+ * @author Rustam Aliyev
35 35
  *
36 36
  */
37 37
 public enum DeliveryAction
70  modules/lmtp/src/main/java/com/elasticinbox/lmtp/server/api/handler/ElasticInboxDeliveryHandler.java
@@ -56,7 +56,6 @@
56 56
  * Default class that extends the {@link AbstractDeliveryHandler} class.
57 57
  * Provides a default implementation for mail delivery.
58 58
  * 
59  
- * @author De Oliveira Edouard &lt;doe_wanted@yahoo.fr&gt;
60 59
  * @author Rustam Aliyev
61 60
  */
62 61
 public class ElasticInboxDeliveryHandler extends DataLineMessageHookHandler
@@ -72,20 +71,7 @@ protected Response processExtensions(SMTPSession session, MailEnvelopeImpl env)
72 71
 	{
73 72
 		// tracing
74 73
 		if (session.getLogger().isTraceEnabled()) {
75  
-			// TODO: Fix me
76  
-			Charset charset = Charset.forName("US-ASCII");
77  
-
78  
-			try {
79  
-				InputStream in = env.getMessageInputStream();
80  
-				byte[] buf = new byte[16384];
81  
-				CharsetDecoder decoder = charset.newDecoder();
82  
-				int len = 0;
83  
-				while ((len = in.read(buf)) >= 0) {
84  
-					session.getLogger().trace(decoder.decode(ByteBuffer.wrap(buf, 0, len)).toString());
85  
-				}
86  
-			} catch (IOException ioex) {
87  
-				session.getLogger().debug("Mail data logging failed", ioex);
88  
-			}
  74
+			logMessage(session, env);
89 75
 		}
90 76
 
91 77
 		Map<MailAddress, DeliveryReturnCode> replies;
@@ -107,27 +93,35 @@ protected Response processExtensions(SMTPSession session, MailEnvelopeImpl env)
107 93
 			SMTPResponse response;
108 94
 
109 95
 			switch (code) {
110  
-			case NO_SUCH_USER:
111  
-				response = new SMTPResponse(SMTPRetCode.MAILBOX_PERM_UNAVAILABLE,
112  
-						DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.ADDRESS_MAILBOX)
113  
-								+ " Unknown user: " + address.toString());
114  
-				break;
115 96
 			case OK:
116 97
 				response = new SMTPResponse(SMTPRetCode.MAIL_OK,
  98
+						DSNStatus.getStatus(DSNStatus.SUCCESS, DSNStatus.UNDEFINED_STATUS)
  99
+						+ " Ok");
  100
+				break;
  101
+			case NO_SUCH_USER:
  102
+				response = new SMTPResponse(SMTPRetCode.MAILBOX_PERM_UNAVAILABLE,
117 103
 						DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.ADDRESS_MAILBOX)
118  
-								+ " Unknown user: " + address.toString());
  104
+						+ " Unknown user " + address.toString());
119 105
 				break;
120 106
 			case OVER_QUOTA:
121  
-				response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "User over quota");
  107
+				response = new SMTPResponse(SMTPRetCode.QUOTA_EXCEEDED,
  108
+						DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.MAILBOX_FULL)
  109
+						+ " User over quota");
122 110
 				break;
123 111
 			case PERMANENT_FAILURE:
124  
-				response = new SMTPResponse(SMTPRetCode.TRANSACTION_FAILED, "Unable to deliver message");
  112
+				response = new SMTPResponse(SMTPRetCode.TRANSACTION_FAILED,
  113
+						DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SYSTEM_OTHER) 
  114
+						+ " Unable to deliver message");
125 115
 				break;
126 116
 			case TEMPORARY_FAILURE:
127  
-				response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unable to process request");
  117
+				response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR,
  118
+						DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.SYSTEM_OTHER) 
  119
+						+ " Unable to process request");
128 120
 				break;
129 121
 			default:
130  
-				response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unable to process request");
  122
+				response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR,
  123
+						DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.SYSTEM_OTHER)
  124
+						+ " Unable to process request");
131 125
 				break;
132 126
 			}
133 127
 
@@ -138,6 +132,7 @@ protected Response processExtensions(SMTPSession session, MailEnvelopeImpl env)
138 132
 			}
139 133
 
140 134
 		}
  135
+
141 136
 		return lmtpResponse;
142 137
 	}
143 138
 
@@ -156,5 +151,28 @@ protected void checkMessageHookCount(List<?> messageHandlers) throws WiringExcep
156 151
     public List<Class<?>> getMarkerInterfaces() {
157 152
         return Collections.emptyList();
158 153
     }
  154
+    
  155
+    /**
  156
+     * Log message contents
  157
+     * 
  158
+     * @param env
  159
+     */
  160
+    private void logMessage(SMTPSession session, MailEnvelopeImpl env)
  161
+    {
  162
+		// TODO: Fix me
  163
+		Charset charset = Charset.forName("US-ASCII");
  164
+
  165
+		try {
  166
+			InputStream in = env.getMessageInputStream();
  167
+			byte[] buf = new byte[16384];
  168
+			CharsetDecoder decoder = charset.newDecoder();
  169
+			int len = 0;
  170
+			while ((len = in.read(buf)) >= 0) {
  171
+				session.getLogger().trace(decoder.decode(ByteBuffer.wrap(buf, 0, len)).toString());
  172
+			}
  173
+		} catch (IOException ioex) {
  174
+			session.getLogger().debug("Mail data logging failed", ioex);
  175
+		}
  176
+    }
159 177
 
160  
-}
  178
+}
34  modules/lmtp/src/main/java/com/elasticinbox/lmtp/server/api/handler/ValidRcptHandler.java
... ...
@@ -0,0 +1,34 @@
  1
+package com.elasticinbox.lmtp.server.api.handler;
  2
+
  3
+import org.apache.james.protocols.smtp.MailAddress;
  4
+import org.apache.james.protocols.smtp.SMTPSession;
  5
+import org.apache.james.protocols.smtp.core.fastfail.AbstractValidRcptHandler;
  6
+
  7
+import com.elasticinbox.core.account.validator.IValidator;
  8
+import com.elasticinbox.core.account.validator.IValidator.AccountStatus;
  9
+import com.elasticinbox.core.account.validator.ValidatorFactory;
  10
+import com.elasticinbox.core.model.Mailbox;
  11
+
  12
+public class ValidRcptHandler extends AbstractValidRcptHandler
  13
+{
  14
+	IValidator validator = ValidatorFactory.getValidator();
  15
+
  16
+	@Override
  17
+	protected boolean isValidRecipient(SMTPSession session, MailAddress recipient)
  18
+	{
  19
+		Mailbox mailbox = new Mailbox(recipient.toString());
  20
+		AccountStatus status = validator.getAccountStatus(mailbox);
  21
+		session.getLogger().debug("Validated account (" + mailbox.getId() + 
  22
+				") status is " + status.toString());
  23
+
  24
+		return status.equals(AccountStatus.ACTIVE) ? true : false;
  25
+	}
  26
+
  27
+	@Override
  28
+	protected boolean isLocalDomain(SMTPSession session, String domain)
  29
+	{
  30
+		// ignore domain check
  31
+		return true;
  32
+	}
  33
+
  34
+}

0 notes on commit e366e3b

Please sign in to comment.
Something went wrong with that request. Please try again.