summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/mail/receive/ImapMailReceiver.java
blob: 6bb6211bb4a6ab7bc4dd63e053206cbd9ade45fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2016 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.receive;

import com.google.gerrit.server.git.WorkQueue;
import com.google.gerrit.server.mail.EmailSettings;
import com.google.gerrit.server.mail.Encryption;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.imap.IMAPClient;
import org.apache.commons.net.imap.IMAPSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class ImapMailReceiver extends MailReceiver {
  private static final Logger log = LoggerFactory.getLogger(ImapMailReceiver.class);
  private static final String INBOX_FOLDER = "INBOX";

  @Inject
  ImapMailReceiver(EmailSettings mailSettings, MailProcessor mailProcessor, WorkQueue workQueue) {
    super(mailSettings, mailProcessor, workQueue);
  }

  /**
   * Opens a connection to the mail server, removes emails where deletion is pending, reads new
   * email and closes the connection.
   *
   * @param async determines if processing messages should happen asynchronously
   * @throws MailTransferException in case of a known transport failure
   * @throws IOException in case of a low-level transport failure
   */
  @Override
  public synchronized void handleEmails(boolean async) throws MailTransferException, IOException {
    IMAPClient imap;
    if (mailSettings.encryption != Encryption.NONE) {
      imap = new IMAPSClient(mailSettings.encryption.name(), true);
    } else {
      imap = new IMAPClient();
    }
    if (mailSettings.port > 0) {
      imap.setDefaultPort(mailSettings.port);
    }
    // Set a 30s timeout for each operation
    imap.setDefaultTimeout(30 * 1000);
    imap.connect(mailSettings.host);
    try {
      if (!imap.login(mailSettings.username, mailSettings.password)) {
        throw new MailTransferException("Could not login to IMAP server");
      }
      try {
        if (!imap.select(INBOX_FOLDER)) {
          throw new MailTransferException("Could not select IMAP folder " + INBOX_FOLDER);
        }
        // Fetch just the internal dates first to know how many messages we
        // should fetch.
        if (!imap.fetch("1:*", "(INTERNALDATE)")) {
          // false indicates that there are no messages to fetch
          log.info("Fetched 0 messages via IMAP");
          return;
        }
        // Format of reply is one line per email and one line to indicate
        // that the fetch was successful.
        // Example:
        // * 1 FETCH (INTERNALDATE "Mon, 24 Oct 2016 16:53:22 +0200 (CEST)")
        // * 2 FETCH (INTERNALDATE "Mon, 24 Oct 2016 16:53:22 +0200 (CEST)")
        // AAAC OK FETCH completed.
        int numMessages = imap.getReplyStrings().length - 1;
        log.info("Fetched " + numMessages + " messages via IMAP");
        // Fetch the full version of all emails
        List<MailMessage> mailMessages = new ArrayList<>(numMessages);
        for (int i = 1; i <= numMessages; i++) {
          if (imap.fetch(i + ":" + i, "(BODY.PEEK[])")) {
            // Obtain full reply
            String[] rawMessage = imap.getReplyStrings();
            if (rawMessage.length < 2) {
              continue;
            }
            // First and last line are IMAP status codes. We have already
            // checked, that the fetch returned true (OK), so we safely ignore
            // those two lines.
            StringBuilder b = new StringBuilder(2 * (rawMessage.length - 2));
            for (int j = 1; j < rawMessage.length - 1; j++) {
              if (j > 1) {
                b.append("\n");
              }
              b.append(rawMessage[j]);
            }
            try {
              MailMessage mailMessage = RawMailParser.parse(b.toString());
              if (pendingDeletion.contains(mailMessage.id())) {
                // Mark message as deleted
                if (imap.store(i + ":" + i, "+FLAGS", "(\\Deleted)")) {
                  pendingDeletion.remove(mailMessage.id());
                } else {
                  log.error("Could not mark mail message as deleted: " + mailMessage.id());
                }
              } else {
                mailMessages.add(mailMessage);
              }
            } catch (MailParsingException e) {
              log.error("Exception while parsing email after IMAP fetch", e);
            }
          } else {
            log.error("IMAP fetch failed. Will retry in next fetch cycle.");
          }
        }
        // Permanently delete emails marked for deletion
        if (!imap.expunge()) {
          log.error("Could not expunge IMAP emails");
        }
        dispatchMailProcessor(mailMessages, async);
      } finally {
        imap.logout();
      }
    } finally {
      imap.disconnect();
    }
  }
}