// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.mail;

import com.google.gerrit.server.mail.receive.MailMessage;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Filters out auto-reply messages according to RFC 3834. */
@Singleton
public class AutoReplyMailFilter implements MailFilter {

  private static final Logger log = LoggerFactory.getLogger(AutoReplyMailFilter.class);

  @Override
  public boolean shouldProcessMessage(MailMessage message) {
    for (String header : message.additionalHeaders()) {
      if (header.startsWith(MailHeader.PRECEDENCE.fieldWithDelimiter())) {
        String prec = header.substring(MailHeader.PRECEDENCE.fieldWithDelimiter().length()).trim();

        if (prec.equals("list") || prec.equals("junk") || prec.equals("bulk")) {
          log.error(
              "Message %s has a Precedence header. Will ignore and delete message.", message.id());
          return false;
        }

      } else if (header.startsWith(MailHeader.AUTO_SUBMITTED.fieldWithDelimiter())) {
        String autoSubmitted =
            header.substring(MailHeader.AUTO_SUBMITTED.fieldWithDelimiter().length()).trim();

        if (!autoSubmitted.equals("no")) {
          log.error(
              "Message %s has an Auto-Submitted header. Will ignore and delete message.",
              message.id());
          return false;
        }
      }
    }

    return true;
  }
}

@@ -1440,26 +1440,26 @@
    %22Message 
-%25s
+%7B%7D
  has a Prece
@@ -1859,10 +1859,10 @@
 age 
-%25s
+%7B%7D
  has

Fix parameters in log messages