You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/08/25 04:22:14 UTC

[GitHub] [james-project] chibenwa commented on a diff in pull request #1154: JAMES-3775 Pass SMTP transport information to Rspamd

chibenwa commented on code in PR #1154:
URL: https://github.com/apache/james-project/pull/1154#discussion_r954487276


##########
third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RspamdHttpClient.java:
##########
@@ -69,6 +75,50 @@ public Mono<AnalysisResult> checkV2(InputStream mimeMessage) {
             .subscribeOn(ReactorUtils.BLOCKING_CALL_WRAPPER);
     }
 
+    public Mono<AnalysisResult> checkV2(Mail mail) throws MessagingException {
+        return httpClient
+            .headers(headers -> transportInformationToHeaders(mail, headers))
+            .post()
+            .uri(CHECK_V2_ENDPOINT)
+            .send(ReactorUtils.toChunks(new MimeMessageInputStream(mail.getMessage()), BUFFER_SIZE)
+                .map(Unpooled::wrappedBuffer))
+            .responseSingle(this::checkMailHttpResponseHandler)
+            .subscribeOn(ReactorUtils.BLOCKING_CALL_WRAPPER);
+    }
+
+    // CF https://rspamd.com/doc/architecture/protocol.html#http-headers
+    // Adding SMTP transport information improves Rspamd accuracy
+    private void transportInformationToHeaders(Mail mail, io.netty.handler.codec.http.HttpHeaders headers) {
+        // IP: Defines IP from which this message is received.
+        Optional.ofNullable(mail.getRemoteAddr()).ifPresent(ip -> headers.add("IP", ip));
+
+        // HELO: Defines SMTP helo
+        mail.getAttribute(Mail.SMTP_HELO)
+            .map(attr -> attr.getValue().value())
+            .filter(String.class::isInstance)
+            .map(String.class::cast)
+            .ifPresent(helo -> headers.add("HELO", helo));
+
+        // From: Defines SMTP mail from command data
+        mail.getMaybeSender().asOptional().ifPresent(from -> headers.add("From", from.asString()));
+
+        // Rcpt: Defines SMTP recipient (there may be several Rcpt headers)
+        Optional.ofNullable(mail.getRecipients()).orElse(ImmutableList.of())
+            .forEach(rcpt -> headers.add("Rcpt", rcpt.asString()));
+
+        // User: Defines username for authenticated SMTP client.
+        mail.getAttribute(Mail.SMTP_AUTH_USER)
+            .map(attr -> attr.getValue().value())
+            .filter(String.class::isInstance)
+            .map(String.class::cast)
+            .ifPresent(helo -> headers.add("User", helo));
+        mail.getAttribute(AttributeName.of("org.apache.james.jmap.send.MailMetaData.username"))
+            .map(attr -> attr.getValue().value())
+            .filter(String.class::isInstance)
+            .map(String.class::cast)
+            .ifPresent(helo -> headers.add("User", helo));

Review Comment:
   One for SMTP one for JMAP



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org