hebdobot/src/org/april/hebdobot/cli/HebdobotCLI.java

258 lines
10 KiB
Java

/**
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.cli;
import java.io.File;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
import org.april.hebdobot.HebdobotException;
import org.april.hebdobot.model.Hebdobot;
import org.april.hebdobot.model.UserAliases;
import org.april.hebdobot.util.BuildInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.util.strings.StringList;
/**
* The Class Launcher.
*/
public class HebdobotCLI
{
private static final Logger logger = LoggerFactory.getLogger(HebdobotCLI.class);
private static final String DEFAULT_CONFIG_FILE = "hebdobot.conf";
private static final String DEFAULT_ALIAS_FILE = "users.conf";
/**
* Instantiates a new hebdobot launcher.
*/
private HebdobotCLI()
{
}
/**
* Help.
*/
public static void help()
{
StringList message = new StringList();
message.append("Hebdobot ").appendln(new BuildInformation().version());
message.appendln("Usage:");
message.appendln(" hebdobot [ -h | -help | --help ]");
message.appendln(" hebdobot [ -c config-file ]");
message.appendln(" hebdobot [ -version ]");
System.out.println(message.toString());
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void run(final String[] args)
{
try
{
System.out.println("--------------------------------START-------------------------------------------");
//
Options options = new Options();
options.addOption("h", "help", false, "Help option");
options.addOption("version", false, "Version option");
options.addOption("c", true, "Config file");
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption("h"))
{
help();
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("hebdobot", options);
}
else if (commandLine.hasOption("version"))
{
showVersion();
}
else
{
// Find the configuration file.
File configFile;
if (commandLine.hasOption("c"))
{
System.out.println(String.format("'-c' option detected with value [%s].", commandLine.getOptionValue("c")));
// Check configuration file.
String value = commandLine.getOptionValue("c");
if (StringUtils.isBlank(value))
{
throw new HebdobotException("Missing config file value.");
}
else
{
configFile = new File(commandLine.getOptionValue("c"));
if (configFile.isDirectory())
{
configFile = new File(configFile, DEFAULT_CONFIG_FILE);
}
}
}
else
{
System.out.println("'-c' option NOT detected.");
// Try to load default configuration file in current work
// directory.
File currentDirectory = new File(System.getProperty("user.dir"));
configFile = new File(currentDirectory, DEFAULT_CONFIG_FILE);
if (!configFile.exists())
{
File configDirectory = new File(currentDirectory.getParentFile(), "conf");
File alternativeConfigFile = new File(configDirectory, DEFAULT_CONFIG_FILE);
if (alternativeConfigFile.exists())
{
configFile = alternativeConfigFile;
}
}
}
if (configFile.exists())
{
// Init log file.
File loggerConfigFile = new File(configFile.getParent(), "log4j.properties");
if (loggerConfigFile.exists())
{
PropertyConfigurator.configure(loggerConfigFile.getAbsolutePath());
logger.info("--==============================INIT==========================================--");
logger.info("Dedicated log configuration done.");
logger.info("Configuration log file was found in [{}].", loggerConfigFile.getAbsoluteFile());
}
else
{
BasicConfigurator.configure();
logger.info("--==============================INIT==========================================--");
logger.info("Basic log configuration done.");
logger.info("Configuration file was not found in [{}].", loggerConfigFile.getAbsoluteFile());
}
System.out.println(new BuildInformation().toString());
// Load configuration file.
logger.info("Configuration file loading… ({}).", configFile.getAbsolutePath());
HebdobotConfigFile config = new HebdobotConfigFile(configFile);
logger.info("Configuration file loaded.");
if (config.isValid())
{
//
File reviewDirectory;
logger.info("Review directory configuration value: [{}]", config.getReviewDirectory());
if (StringUtils.isBlank(config.getReviewDirectory()))
{
reviewDirectory = new File(configFile.getParentFile(), "reviews");
}
else if (config.getReviewDirectory().startsWith("/"))
{
reviewDirectory = new File(config.getReviewDirectory());
}
else
{
reviewDirectory = new File(configFile.getParentFile(), config.getReviewDirectory());
}
logger.info("Review directory: [{}]", reviewDirectory.getAbsolutePath());
// Load user aliases file.
File aliasFile = new File(configFile.getParentFile(), DEFAULT_ALIAS_FILE);
logger.info("Aliases file loading… ({}).", aliasFile.getAbsolutePath());
UserAliases aliases = new UserAliases(aliasFile);
logger.info("Aliases file loaded (" + aliases.size() + " aliases).");
logger.info("Alias list: " + aliases.toLineString());
Hebdobot bot = new Hebdobot(config.getIrcHost(), config.getIrcPort(), config.getIrcName(), config.getIrcChannel(),
configFile.getParentFile(), reviewDirectory, config.getReviewFileSuffix());
//
logger.info("Bot configuring…");
bot.getPastebinSettings().setApiKey(config.getPastebinApiKey());
bot.getIdenticaSettings().setApiKey(config.getIdenticaApiKey());
bot.getIdenticaSettings().setApiSecret(config.getIdenticaApiSecret());
bot.getTwitterSettings().setConsumerKey(config.getTwitterConsumerKey());
bot.getTwitterSettings().setConsumerSecret(config.getTwitterConsumerSecret());
bot.getTwitterSettings().setAccessToken(config.getTwitterAccessToken());
bot.getTwitterSettings().setAccessTokenSecret(config.getTwitterAccessTokenSecret());
bot.getAliases().putAll(aliases);
bot.getCronSettings().addAll(config.getCronSettings());
logger.info("Bot configured.");
bot.run();
}
else
{
throw new HebdobotException("Invalid config file [" + configFile.getAbsolutePath() + "]");
}
}
else
{
throw new HebdobotException("Missing config file [" + configFile.getAbsolutePath() + "]");
}
}
}
catch (ParseException exception)
{
logger.error(exception.getMessage(), exception);
System.err.print("Parse error: " + exception.getMessage());
}
catch (HebdobotException exception)
{
logger.error(exception.getMessage(), exception);
System.err.println(exception.getMessage());
help();
}
}
/**
* version.
*/
public static void showVersion()
{
StringList message = new StringList();
message.appendln(new BuildInformation().toString());
System.out.println(message.toString());
}
}