hebdobot/src/main/java/fr/imirhil/april/hebdobot/Application.java

111 lines
3.6 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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 fr.imirhil.april.hebdobot;
import java.io.File;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import fr.imirhil.april.hebdobot.irc.Bot;
import fr.imirhil.april.hebdobot.irc.ReviewListener;
import fr.imirhil.april.hebdobot.pastebin.PastebinClient;
import fr.imirhil.april.hebdobot.pastebin.Private;
import fr.imirhil.april.hebdobot.review.Review;
/**
* The Class Application.
*/
public class Application implements ReviewListener
{
private static final Logger logger = LoggerFactory.getLogger(Application.class);
private static final String FILE_SUFFIX = "file.suffix";
private final Properties properties;
private final Bot bot;
private final PastebinClient pastebinClient;
/**
* Instantiates a new application.
*
* @throws Exception
* the exception
*/
private Application() throws Exception
{
this.properties = Context.getBean("properties");
this.bot = Context.getBean(Bot.class);
this.pastebinClient = Context.getBean(PastebinClient.class);
this.bot.add(this);
}
/* (non-Javadoc)
* @see fr.imirhil.april.hebdobot.irc.ReviewListener#onEnd(fr.imirhil.april.hebdobot.review.Review)
*/
@Override
public void onEnd(final Review review)
{
final String date = ISODateTimeFormat.basicDate().print(new DateTime());
final String text = review.toString();
try
{
this.bot.sendMessage("% Compte-rendu de la revue : " + this.pastebinClient.paste(text, "Revue APRIL " + date, Private.UNLISTED));
}
catch (final Exception exception)
{
logger.error("Error during Pastebin submit", exception);
}
if (this.properties.containsKey(FILE_SUFFIX))
{
try
{
final File file = new File(date + "_" + this.properties.getProperty(FILE_SUFFIX));
FileUtils.writeStringToFile(file, text);
this.bot.sendMessage("% Compte-rendu de la revue : " + file.getName());
}
catch (final Exception exception)
{
logger.error("Error during file generation", exception);
}
}
}
/**
* The main method.
*
* @param args
* the arguments
* @throws Exception
* the exception
*/
public static void main(final String[] args) throws Exception
{
new FileSystemXmlApplicationContext(System.getProperty("spring.conf", "conf.xml")).registerShutdownHook();
new Application();
}
}