Line data Source code
1 : // Copyright (C) 2009 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.pgm.init; 16 : 17 : import static com.google.gerrit.pgm.init.api.InitUtil.isLocal; 18 : import static com.google.gerrit.pgm.init.api.InitUtil.username; 19 : 20 : import com.google.gerrit.pgm.init.api.ConsoleUI; 21 : import com.google.gerrit.pgm.init.api.InitStep; 22 : import com.google.gerrit.pgm.init.api.Section; 23 : import com.google.gerrit.server.config.SitePaths; 24 : import com.google.gerrit.server.mail.Encryption; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : import java.nio.file.Files; 28 : 29 : /** Initialize the {@code sendemail} configuration section. */ 30 : @Singleton 31 : class InitSendEmail implements InitStep { 32 : private final ConsoleUI ui; 33 : private final Section sendemail; 34 : private final SitePaths site; 35 : 36 : @Inject 37 15 : InitSendEmail(ConsoleUI ui, SitePaths site, Section.Factory sections) { 38 15 : this.ui = ui; 39 15 : this.sendemail = sections.get("sendemail", null); 40 15 : this.site = site; 41 15 : } 42 : 43 : @Override 44 : public void run() { 45 15 : ui.header("Email Delivery"); 46 : 47 15 : final String hostname = sendemail.string("SMTP server hostname", "smtpServer", "localhost"); 48 : 49 15 : sendemail.string("SMTP server port", "smtpServerPort", "(default)", true); 50 : 51 15 : final Encryption enc = 52 15 : sendemail.select("SMTP encryption", "smtpEncryption", Encryption.NONE, true); 53 : 54 15 : String username = null; 55 15 : if (Files.exists(site.gerrit_config)) { 56 15 : username = sendemail.get("smtpUser"); 57 0 : } else if ((enc != null && enc != Encryption.NONE) || !isLocal(hostname)) { 58 0 : username = username(); 59 : } 60 15 : sendemail.string("SMTP username", "smtpUser", username); 61 15 : sendemail.password("smtpUser", "smtpPass"); 62 15 : } 63 : }