2017-12-24 11:21:44 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-01-07 22:28:57 +01:00
|
|
|
import org.apache.log4j.BasicConfigurator;
|
|
|
|
import org.apache.log4j.PropertyConfigurator;
|
2017-12-24 11:21:44 +01:00
|
|
|
import org.april.hebdobot.HebdobotException;
|
|
|
|
import org.april.hebdobot.model.Hebdobot;
|
2017-12-25 00:29:57 +01:00
|
|
|
import org.april.hebdobot.model.UserAliases;
|
2017-12-24 11:21:44 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
public static final String HEBDOBOT_VERSION = "v2.0";
|
|
|
|
private static final String DEFAULT_CONFIG_FILE = "hebdobot.conf";
|
2017-12-25 00:29:57 +01:00
|
|
|
private static final String DEFAULT_ALIAS_FILE = "users.conf";
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new hebdobot launcher.
|
|
|
|
*/
|
|
|
|
private HebdobotCLI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Help.
|
|
|
|
*/
|
|
|
|
public static void help()
|
|
|
|
{
|
|
|
|
StringList message = new StringList();
|
|
|
|
|
|
|
|
message.append("Hebdobot version ").appendln(HEBDOBOT_VERSION);
|
|
|
|
message.appendln("Usage:");
|
|
|
|
message.appendln(" hebdobot [ -h | -help | --help ]");
|
2018-01-07 03:10:35 +01:00
|
|
|
message.appendln(" hebdobot [ -c config-file ]");
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
System.out.println(message.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main method.
|
|
|
|
*
|
|
|
|
* @param args
|
|
|
|
* the arguments
|
|
|
|
*/
|
|
|
|
public static void run(final String[] args)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-01-07 22:28:57 +01:00
|
|
|
System.out.println("--------------------------------START-------------------------------------------");
|
|
|
|
|
2017-12-24 11:21:44 +01:00
|
|
|
//
|
|
|
|
Options options = new Options();
|
|
|
|
options.addOption("h", "help", false, "Help 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
|
|
|
|
{
|
|
|
|
// Find the configuration file.
|
|
|
|
File configFile;
|
|
|
|
if (commandLine.hasOption("c"))
|
|
|
|
{
|
2018-01-07 22:28:57 +01:00
|
|
|
System.out.println(String.format("'-c' option detected with value [%s].", commandLine.getOptionValue("c")));
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
// 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"));
|
2018-01-10 01:03:13 +01:00
|
|
|
|
|
|
|
if (configFile.isDirectory())
|
|
|
|
{
|
|
|
|
configFile = new File(configFile, DEFAULT_CONFIG_FILE);
|
|
|
|
}
|
2017-12-24 11:21:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-07 22:28:57 +01:00
|
|
|
System.out.println("'-c' option NOT detected.");
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
// 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);
|
2018-01-10 01:03:13 +01:00
|
|
|
|
|
|
|
if (!configFile.exists())
|
|
|
|
{
|
|
|
|
File configDirectory = new File(currentDirectory.getParentFile(), "conf");
|
|
|
|
File alternativeConfigFile = new File(configDirectory, DEFAULT_CONFIG_FILE);
|
|
|
|
|
|
|
|
if (alternativeConfigFile.exists())
|
|
|
|
{
|
|
|
|
configFile = alternativeConfigFile;
|
|
|
|
}
|
|
|
|
}
|
2017-12-24 11:21:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (configFile.exists())
|
|
|
|
{
|
2018-01-07 22:28:57 +01:00
|
|
|
// Init log file.
|
|
|
|
File loggerConfigFile = new File(configFile.getParent(), "log4j.properties");
|
2018-01-04 08:41:56 +01:00
|
|
|
if (loggerConfigFile.exists())
|
|
|
|
{
|
|
|
|
PropertyConfigurator.configure(loggerConfigFile.getAbsolutePath());
|
2018-01-07 22:28:57 +01:00
|
|
|
logger.info("--==============================INIT==========================================--");
|
2018-01-04 08:41:56 +01:00
|
|
|
logger.info("Dedicated log configuration done.");
|
2018-01-07 22:28:57 +01:00
|
|
|
logger.info("Configuration log file was found in [{}].", loggerConfigFile.getAbsoluteFile());
|
2018-01-04 08:41:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BasicConfigurator.configure();
|
2018-01-07 22:28:57 +01:00
|
|
|
logger.info("--==============================INIT==========================================--");
|
2018-01-04 08:41:56 +01:00
|
|
|
logger.info("Basic log configuration done.");
|
|
|
|
logger.info("Configuration file was not found in [{}].", loggerConfigFile.getAbsoluteFile());
|
2018-01-07 22:28:57 +01:00
|
|
|
}
|
2017-12-25 00:29:57 +01:00
|
|
|
|
2017-12-24 11:21:44 +01:00
|
|
|
// Load configuration file.
|
2018-01-07 22:28:57 +01:00
|
|
|
logger.info("Configuration file loading… ({}).", configFile.getAbsolutePath());
|
2017-12-24 11:21:44 +01:00
|
|
|
HebdobotConfigFile config = new HebdobotConfigFile(configFile);
|
2018-01-07 22:28:57 +01:00
|
|
|
logger.info("Configuration file loaded.");
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
if (config.isValid())
|
|
|
|
{
|
2018-01-07 22:28:57 +01:00
|
|
|
//
|
2018-01-07 16:32:50 +01:00
|
|
|
File reviewDirectory;
|
|
|
|
if (config.getReviewDirectory() == null)
|
|
|
|
{
|
|
|
|
reviewDirectory = new File(configFile.getParentFile(), "reviews");
|
|
|
|
}
|
|
|
|
else if (config.getReviewDirectory().startsWith("/"))
|
|
|
|
{
|
|
|
|
reviewDirectory = new File(config.getReviewDirectory());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reviewDirectory = new File(configFile.getParentFile(), config.getReviewDirectory());
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:28:57 +01:00
|
|
|
// 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).");
|
2018-01-07 22:45:50 +01:00
|
|
|
logger.info("Alias list: " + aliases.toLineString());
|
2018-01-07 22:28:57 +01:00
|
|
|
|
2017-12-24 11:21:44 +01:00
|
|
|
Hebdobot bot = new Hebdobot(config.getIrcHost(), config.getIrcPort(), config.getIrcName(), config.getIrcChannel(),
|
2018-01-07 16:32:50 +01:00
|
|
|
configFile.getParentFile(), reviewDirectory, config.getReviewFileSuffix());
|
2017-12-24 11:21:44 +01:00
|
|
|
|
2018-01-07 22:28:57 +01:00
|
|
|
//
|
|
|
|
logger.info("Bot configuring…");
|
2017-12-24 11:21:44 +01:00
|
|
|
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());
|
2017-12-31 03:48:51 +01:00
|
|
|
bot.getTwitterSettings().setAccessToken(config.getTwitterAccessToken());
|
|
|
|
bot.getTwitterSettings().setAccessTokenSecret(config.getTwitterAccessTokenSecret());
|
2017-12-25 00:29:57 +01:00
|
|
|
bot.getAliases().putAll(aliases);
|
2017-12-28 04:21:54 +01:00
|
|
|
bot.getCronSettings().addAll(config.getCronSettings());
|
|
|
|
|
2017-12-25 00:29:57 +01:00
|
|
|
logger.info("Bot configured.");
|
2017-12-24 11:21:44 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|