185 lines
6.1 KiB
Java
185 lines
6.1 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;
|
|
|
|
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.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import fr.devinsy.util.strings.StringList;
|
|
|
|
/**
|
|
* The Class Launcher.
|
|
*/
|
|
public class HebdobotLauncher
|
|
{
|
|
private static final Logger logger = LoggerFactory.getLogger(HebdobotLauncher.class);
|
|
|
|
public static final String HEBDOBOT_VERSION = "v2.0";
|
|
private static final String DEFAULT_CONFIG_FILE = "hebdobot.conf";
|
|
|
|
/**
|
|
* Instantiates a new hebdobot launcher.
|
|
*/
|
|
private HebdobotLauncher()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 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 ]");
|
|
message.appendln(" hebdobot [ -c configuration-file ]");
|
|
|
|
System.out.println(message.toString());
|
|
}
|
|
|
|
/**
|
|
* The main method.
|
|
*
|
|
* @param args
|
|
* the arguments
|
|
*/
|
|
public static void main(final String[] args)
|
|
{
|
|
try
|
|
{
|
|
setDefaultException();
|
|
|
|
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"))
|
|
{
|
|
logger.debug("Help requires.");
|
|
help();
|
|
|
|
HelpFormatter formatter = new HelpFormatter();
|
|
formatter.printHelp("hebdobot", options);
|
|
}
|
|
else
|
|
{
|
|
// Find the configuration file.
|
|
File configFile;
|
|
if (commandLine.hasOption("c"))
|
|
{
|
|
logger.debug("'-c' option detected with value [{}].", 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"));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logger.debug("'-c' option NOT detected.");
|
|
|
|
// Try to load default configuration file in current work
|
|
// directory.
|
|
configFile = new File(DEFAULT_CONFIG_FILE);
|
|
}
|
|
|
|
// Load configuration file.
|
|
HebdobotSettings settings = HebdobotConfigFile.load(configFile);
|
|
|
|
if (HebdobotSettings.isValid(settings))
|
|
{
|
|
Hebdobot bot = new Hebdobot(settings.getHost(), settings.getPort(), settings.getName(), settings.getChannel(),
|
|
settings.getReviewFileSuffix());
|
|
bot.setIdenticaSettings(settings.getIdentica());
|
|
bot.setPastebinSettings(settings.getPastebin());
|
|
bot.setTwitterSettings(settings.getTwitter());
|
|
bot.run();
|
|
}
|
|
else
|
|
{
|
|
throw new HebdobotException("Invalid config file [" + configFile.getPath() + "]");
|
|
}
|
|
}
|
|
}
|
|
catch (ParseException exception)
|
|
{
|
|
System.out.print("Parse error: ");
|
|
System.out.println(exception.getMessage());
|
|
}
|
|
catch (HebdobotException exception)
|
|
{
|
|
System.err.println("HebdobotException = " + exception.getMessage());
|
|
logger.error(exception.getMessage(), exception);
|
|
help();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the default exception.
|
|
*/
|
|
public static void setDefaultException()
|
|
{
|
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
|
|
{
|
|
/* (non-Javadoc)
|
|
* @see java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.lang.Thread, java.lang.Throwable)
|
|
*/
|
|
@Override
|
|
public void uncaughtException(final Thread thread, final Throwable exception)
|
|
{
|
|
String message;
|
|
if (exception instanceof OutOfMemoryError)
|
|
{
|
|
message = "Java ran out of memory!\n\n";
|
|
}
|
|
else
|
|
{
|
|
message = String.format("An error occured: %1s(%2s)", exception.getClass(), exception.getMessage());
|
|
}
|
|
|
|
HebdobotLauncher.logger.error("uncaughtException ", exception);
|
|
HebdobotLauncher.logger.error(message);
|
|
HebdobotLauncher.logger.info("Unexpected error.");
|
|
}
|
|
});
|
|
}
|
|
}
|