LCOV - code coverage report
Current view: top level - server/mail - ListMailFilter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 28 28 100.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.mail;
      16             : 
      17             : import static java.util.stream.Collectors.joining;
      18             : 
      19             : import com.google.common.flogger.FluentLogger;
      20             : import com.google.gerrit.mail.MailMessage;
      21             : import com.google.gerrit.server.config.GerritServerConfig;
      22             : import com.google.inject.Inject;
      23             : import com.google.inject.Singleton;
      24             : import java.util.Arrays;
      25             : import java.util.regex.Pattern;
      26             : import org.eclipse.jgit.lib.Config;
      27             : 
      28             : @Singleton
      29             : public class ListMailFilter implements MailFilter {
      30         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      31             : 
      32         138 :   public enum ListFilterMode {
      33         138 :     OFF,
      34         138 :     ALLOW,
      35         138 :     BLOCK
      36             :   }
      37             : 
      38             :   private final ListFilterMode mode;
      39             :   private final Pattern mailPattern;
      40             : 
      41             :   @Inject
      42         138 :   ListMailFilter(@GerritServerConfig Config cfg) {
      43         138 :     mode = getListFilterMode(cfg);
      44         138 :     String[] addresses = cfg.getStringList("receiveemail", "filter", "patterns");
      45         138 :     String concat = Arrays.asList(addresses).stream().collect(joining("|"));
      46         138 :     this.mailPattern = Pattern.compile(concat);
      47         138 :   }
      48             : 
      49             :   private static final String LEGACY_ALLOW = "WHITELIST";
      50             :   private static final String LEGACY_BLOCK = "BLACKLIST";
      51             : 
      52             :   /** Legacy names are supported, but should be removed in the future. */
      53             :   private ListFilterMode getListFilterMode(Config cfg) {
      54             :     ListFilterMode mode;
      55         138 :     String modeString = cfg.getString("receiveemail", "filter", "mode");
      56         138 :     if (modeString == null) {
      57         137 :       modeString = "";
      58             :     }
      59         138 :     switch (modeString) {
      60             :       case LEGACY_ALLOW:
      61             :       case "ALLOW":
      62           1 :         mode = ListFilterMode.ALLOW;
      63           1 :         break;
      64             :       case LEGACY_BLOCK:
      65             :       case "BLOCK":
      66           1 :         mode = ListFilterMode.BLOCK;
      67           1 :         break;
      68             :       default:
      69         138 :         mode = ListFilterMode.OFF;
      70             :     }
      71         138 :     return mode;
      72             :   }
      73             : 
      74             :   @Override
      75             :   public boolean shouldProcessMessage(MailMessage message) {
      76           3 :     if (mode == ListFilterMode.OFF) {
      77           3 :       return true;
      78             :     }
      79             : 
      80           1 :     boolean match = mailPattern.matcher(message.from().email()).find();
      81           1 :     if ((mode == ListFilterMode.ALLOW && !match) || (mode == ListFilterMode.BLOCK && match)) {
      82           1 :       logger.atInfo().log("Mail message from %s rejected by list filter", message.from());
      83           1 :       return false;
      84             :     }
      85           1 :     return true;
      86             :   }
      87             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750