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

          Line data    Source code
       1             : // Copyright (C) 2016 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.receive;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.common.primitives.Ints;
      19             : import com.google.gerrit.mail.MailMessage;
      20             : import com.google.gerrit.mail.MailParsingException;
      21             : import com.google.gerrit.mail.RawMailParser;
      22             : import com.google.gerrit.server.git.WorkQueue;
      23             : import com.google.gerrit.server.mail.EmailSettings;
      24             : import com.google.gerrit.server.mail.Encryption;
      25             : import com.google.inject.Inject;
      26             : import com.google.inject.Singleton;
      27             : import java.io.BufferedReader;
      28             : import java.io.IOException;
      29             : import java.util.ArrayList;
      30             : import java.util.List;
      31             : import org.apache.commons.net.pop3.POP3Client;
      32             : import org.apache.commons.net.pop3.POP3MessageInfo;
      33             : import org.apache.commons.net.pop3.POP3SClient;
      34             : 
      35             : /** An implementation of {@link MailReceiver} for POP3. */
      36             : @Singleton
      37             : public class Pop3MailReceiver extends MailReceiver {
      38           2 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      39             : 
      40             :   @Inject
      41             :   Pop3MailReceiver(EmailSettings mailSettings, MailProcessor mailProcessor, WorkQueue workQueue) {
      42           2 :     super(mailSettings, mailProcessor, workQueue);
      43           2 :   }
      44             : 
      45             :   /**
      46             :    * Opens a connection to the mail server, removes emails where deletion is pending, reads new
      47             :    * email and closes the connection.
      48             :    *
      49             :    * @param async determines if processing messages should happen asynchronously
      50             :    * @throws MailTransferException in case of a known transport failure
      51             :    * @throws IOException in case of a low-level transport failure
      52             :    */
      53             :   @Override
      54             :   public synchronized void handleEmails(boolean async) throws MailTransferException, IOException {
      55             :     POP3Client pop3;
      56           2 :     if (mailSettings.encryption != Encryption.NONE) {
      57           0 :       pop3 = new POP3SClient(mailSettings.encryption.name(), true);
      58             :     } else {
      59           2 :       pop3 = new POP3Client();
      60             :     }
      61           2 :     if (mailSettings.port > 0) {
      62           1 :       pop3.setDefaultPort(mailSettings.port);
      63             :     }
      64           1 :     pop3.connect(mailSettings.host);
      65             :     try {
      66           1 :       if (!pop3.login(mailSettings.username, mailSettings.password)) {
      67           0 :         throw new MailTransferException(
      68             :             "Could not login to POP3 email server. Check username and password");
      69             :       }
      70             :       try {
      71           1 :         POP3MessageInfo[] messages = pop3.listMessages();
      72           1 :         if (messages == null) {
      73           0 :           throw new MailTransferException("Could not retrieve message list via POP3");
      74             :         }
      75           1 :         logger.atInfo().log("Received %d messages via POP3", messages.length);
      76             :         // Fetch messages
      77           1 :         List<MailMessage> mailMessages = new ArrayList<>();
      78           1 :         for (POP3MessageInfo msginfo : messages) {
      79           1 :           if (msginfo == null) {
      80             :             // Message was deleted
      81           0 :             continue;
      82             :           }
      83           1 :           try (BufferedReader reader = (BufferedReader) pop3.retrieveMessage(msginfo.number)) {
      84           1 :             if (reader == null) {
      85           0 :               throw new MailTransferException(
      86             :                   "Could not retrieve POP3 message header for message " + msginfo.identifier);
      87             :             }
      88           1 :             int[] message = fetchMessage(reader);
      89           1 :             MailMessage mailMessage = RawMailParser.parse(message);
      90             :             // Delete messages where deletion is pending. This requires
      91             :             // knowing the integer message ID of the email. We therefore parse
      92             :             // the message first and extract the Message-ID specified in RFC
      93             :             // 822 and delete the message if deletion is pending.
      94           1 :             if (pendingDeletion.contains(mailMessage.id())) {
      95           1 :               if (pop3.deleteMessage(msginfo.number)) {
      96           1 :                 pendingDeletion.remove(mailMessage.id());
      97             :               } else {
      98           0 :                 logger.atSevere().log("Could not delete message %d", msginfo.number);
      99             :               }
     100             :             } else {
     101             :               // Process message further
     102           1 :               mailMessages.add(mailMessage);
     103             :             }
     104           0 :           } catch (MailParsingException e) {
     105           0 :             logger.atSevere().log("Could not parse message %d", msginfo.number);
     106           1 :           }
     107             :         }
     108           1 :         dispatchMailProcessor(mailMessages, async);
     109             :       } finally {
     110           1 :         pop3.logout();
     111             :       }
     112             :     } finally {
     113           1 :       pop3.disconnect();
     114             :     }
     115           1 :   }
     116             : 
     117             :   public final int[] fetchMessage(BufferedReader reader) throws IOException {
     118           1 :     List<Integer> character = new ArrayList<>();
     119             :     int ch;
     120           1 :     while ((ch = reader.read()) != -1) {
     121           1 :       character.add(ch);
     122             :     }
     123           1 :     return Ints.toArray(character);
     124             :   }
     125             : }

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