/** * Copyright (C) 2017-2021 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 java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.april.hebdobot.HebdobotException; import org.april.hebdobot.cron.CronSettings; import org.april.hebdobot.cron.CronValue; import org.april.hebdobot.util.HebdobotUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringSet; /** * The Class Launcher. */ public class HebdobotConfigFile extends Properties { private static final Logger logger = LoggerFactory.getLogger(HebdobotConfigFile.class); private static final long serialVersionUID = 5954809202666104536L; private static final String TIME_PATTERN = "^\\d\\dh\\d\\d$"; /** * Instantiates a new launcher. * * @param source * the source * @throws HebdobotException * the hebdobot exception */ public HebdobotConfigFile(final File source) throws HebdobotException { super(); try { Properties config = HebdobotUtils.loadProperties(source); for (String key : config.stringPropertyNames()) { put(key, config.getProperty(key)); } } catch (FileNotFoundException exception) { throw new HebdobotException("File not found.", exception); } catch (IOException exception) { throw new HebdobotException("IO error.", exception); } } /** * Gets the cron settings. * * @return the cron settings */ public CronSettings getCronSettings() { CronSettings result; result = new CronSettings(); for (String notifyName : getNotifyNames()) { String notifyDescription = getNotifyCron(notifyName); String notifyCron = getNotifyCron(notifyName); String notifyIrcMessage = getNotifyIrcMessage(notifyName); String notifyTwitterMessage = getNotifyTwitterMessage(notifyName); String notifyImageFileName = getNotifyImage(notifyName); File notifyImageFile; if (notifyImageFileName == null) { notifyImageFile = null; } else { notifyImageFile = new File(notifyImageFileName); } CronValue cronValue = new CronValue(notifyName, notifyDescription, notifyCron, notifyIrcMessage, notifyTwitterMessage, notifyImageFile); result.add(cronValue); } logger.debug("Cron settings count=" + result.size()); // return result; } /** * Gets the identica api key. * * @return the identica api key */ public String getIdenticaApiKey() { String result; result = getProperty("identica.apiKey"); // return result; } /** * Gets the identica api secret. * * @return the identica api secret */ public String getIdenticaApiSecret() { String result; result = getProperty("identica.apiSecret"); // return result; } /** * Gets the irc channel. * * @return the irc channel */ public String getIrcChannel() { String result; result = getProperty("irc.channel"); // return result; } /** * Gets the irc host. * * @return the irc host */ public String getIrcHost() { String result; result = getProperty("irc.host"); // return result; } /** * Gets the irc identify login. * * @return the irc identify login */ public String getIrcIdentifyNick() { String result; result = getProperty("irc.identify.nick"); // return result; } /** * Gets the identify password. * * @return the identify password */ public String getIrcIdentifyPassword() { String result; result = getProperty("irc.identify.password"); // return result; } /** * Gets the name. * * @return the name */ public String getIrcName() { String result; result = getProperty("irc.name"); // return result; } /** * Gets the irc port. * * @return the irc port */ public Integer getIrcPort() { Integer result; result = HebdobotUtils.toInteger(getProperty("irc.port")); // return result; } /** * Gets the notify cron. * * @param notifyName * the notify name * @return the notify cron */ public String getNotifyCron(final String notifyName) { String result; result = getProperty("notify." + notifyName + ".cron"); // return result; } /** * Gets the notify image file. * * @param notifyName * the notify name * @return the notify image file */ public String getNotifyImage(final String notifyName) { String result; result = getProperty("notify." + notifyName + ".image"); // return result; } /** * Gets the notify irc message. * * @param notifyName * the notify name * @return the notify irc message */ public String getNotifyIrcMessage(final String notifyName) { String result; result = getProperty("notify." + notifyName + ".irc"); // return result; } /** * Gets the notification names. * * @return the notification names */ public StringSet getNotifyNames() { StringSet result; result = new StringSet(); for (Object key : this.keySet()) { String propertyName = (String) key; if (propertyName.startsWith("notify.")) { String notifyName = propertyName.split("\\.")[1]; result.add(notifyName); } } // return result; } /** * Gets the notify twitter message. * * @param notifyName * the notify name * @return the notify twitter message */ public String getNotifyTwitterMessage(final String notifyName) { String result; result = getProperty("notify." + notifyName + ".twitter"); // return result; } /** * Gets the private bin url. * * @return the private bin url */ public URL getPrivatebinUrl() { URL result; try { String value = getProperty("privatebin.url"); result = new URL(value); } catch (MalformedURLException exception) { result = null; } // return result; } /** * Gets the review directory. * * @return the review directory */ public String getReviewDirectory() { String result; result = getProperty("review.directory"); // return result; } /** * Gets the review file suffix. * * @return the review file suffix */ public String getReviewFileSuffix() { String result; result = getProperty("review.file.suffix"); // return result; } /** * Gets the review wait time. * * @return the review wait time * @throws HebdobotException * the Hebdobot exception */ public LocalTime getReviewWaitTime() throws HebdobotException { LocalTime result; String value = getProperty("review.waittime"); if ((!StringUtils.isBlank(value)) && (value.matches(TIME_PATTERN))) { result = LocalTime.parse(value, DateTimeFormatter.ofPattern("HH'h'mm")); } else { result = null; } // return result; } /** * Gets the twitter access token. * * @return the twitter access token */ public String getTwitterAccessToken() { String result; result = getProperty("twitter.accessToken"); // return result; } /** * Gets the twitter access token secret. * * @return the twitter access token secret */ public String getTwitterAccessTokenSecret() { String result; result = getProperty("twitter.accessTokenSecret"); // return result; } /** * Gets the twitter consumer key. * * @return the twitter consumer key */ public String getTwitterConsumerKey() { String result; result = getProperty("twitter.consumerKey"); // return result; } /** * Gets the twitter consumer secret. * * @return the twitter consumer secret */ public String getTwitterConsumerSecret() { String result; result = getProperty("twitter.consumerSecret"); // return result; } /** * Checks if is valid. * * @return true, if is valid */ public boolean isValid() { boolean result; if ((getProperty("review.waittime") != null) && (!getProperty("review.waittime").matches(TIME_PATTERN))) { result = false; } else if ((getProperty("irc.identify.nick") != null) && (getProperty("irc.identify.password") == null)) { result = false; } else if ((getProperty("irc.identify.nick") == null) && (getProperty("irc.identify.password") != null)) { result = false; } else { result = true; } // return result; } }