/** * Copyright (C) 2011-2013 Nicolas Vinot * Copyright (C) 2017 Christian Pierre MOMON * * 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 */ 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.april.hebdobot.HebdobotException; import org.april.hebdobot.model.Hebdobot; import org.april.hebdobot.model.UserAliases; 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"; 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 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 run(final String[] args) { try { // 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. File currentDirectory = new File(System.getProperty("user.dir")); configFile = new File(currentDirectory, DEFAULT_CONFIG_FILE); } if (configFile.exists()) { // Load configuration file. logger.info("Config file loading… ({}).", configFile.getAbsolutePath()); HebdobotConfigFile config = new HebdobotConfigFile(configFile); logger.info("Config file loaded."); // 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 liste:\n" + aliases.toString()); if (config.isValid()) { logger.info("Bot configuring…"); Hebdobot bot = new Hebdobot(config.getIrcHost(), config.getIrcPort(), config.getIrcName(), config.getIrcChannel(), configFile.getParentFile(), config.getReviewFileSuffix()); 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(); } } }