diff --git a/lib/README~ b/lib/README~ new file mode 100644 index 0000000..fd98efd --- /dev/null +++ b/lib/README~ @@ -0,0 +1,6 @@ + + + +Spring: +- aopalliance-1.0.jar +- spring* \ No newline at end of file diff --git a/conf.sample.xml b/resources/conf.sample.xml similarity index 100% rename from conf.sample.xml rename to resources/conf.sample.xml diff --git a/launch4j.xml b/resources/launch4j.xml similarity index 100% rename from launch4j.xml rename to resources/launch4j.xml diff --git a/build.sh b/resources/scripts/build.sh similarity index 100% rename from build.sh rename to resources/scripts/build.sh diff --git a/exe/daemon.sh b/resources/scripts/daemon.sh similarity index 100% rename from exe/daemon.sh rename to resources/scripts/daemon.sh diff --git a/exe/hebdobot.sh b/resources/scripts/hebdobot.sh similarity index 100% rename from exe/hebdobot.sh rename to resources/scripts/hebdobot.sh diff --git a/users.sample.xml b/resources/users.sample.xml similarity index 100% rename from users.sample.xml rename to resources/users.sample.xml diff --git a/src/org/april/hebdobot/Application.java b/src/org/april/hebdobot/Application.java deleted file mode 100644 index ed4d194..0000000 --- a/src/org/april/hebdobot/Application.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * 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; - -import java.io.File; -import java.util.Properties; - -import org.apache.commons.io.FileUtils; -import org.april.hebdobot.irc.Hebdobot; -import org.april.hebdobot.irc.ReviewListener; -import org.april.hebdobot.pastebin.PastebinClient; -import org.april.hebdobot.pastebin.Private; -import org.april.hebdobot.review.Review; -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; - -/** - * 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 Hebdobot 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(Hebdobot.class); - this.pastebinClient = Context.getBean(PastebinClient.class); - this.bot.add(this); - } - - /* (non-Javadoc) - * @see org.april.hebdobot.irc.ReviewListener#onEnd(org.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 - { - 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(); - } -} diff --git a/src/org/april/hebdobot/irc/Hebdobot.java b/src/org/april/hebdobot/Hebdobot.java similarity index 74% rename from src/org/april/hebdobot/irc/Hebdobot.java rename to src/org/april/hebdobot/Hebdobot.java index 2b9058e..c332254 100644 --- a/src/org/april/hebdobot/irc/Hebdobot.java +++ b/src/org/april/hebdobot/Hebdobot.java @@ -17,27 +17,33 @@ * You should have received a copy of the GNU Affero General Public License * along with Hebdobot. If not, see */ -package org.april.hebdobot.irc; +package org.april.hebdobot; import java.io.File; +import java.io.IOException; import java.util.Collection; -import java.util.LinkedList; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; -import org.april.hebdobot.Context; +import org.apache.commons.lang3.StringUtils; +import org.april.hebdobot.identica.IdenticaSettings; +import org.april.hebdobot.pastebin.PastebinClient; +import org.april.hebdobot.pastebin.PastebinSettings; import org.april.hebdobot.pastebin.Private; import org.april.hebdobot.review.CollectiveTopic; import org.april.hebdobot.review.IndividualTopic; import org.april.hebdobot.review.Message; import org.april.hebdobot.review.Review; import org.april.hebdobot.review.Topic; +import org.april.hebdobot.twitter.TwitterSettings; +import org.jibble.pircbot.IrcException; +import org.jibble.pircbot.NickAlreadyInUseException; import org.jibble.pircbot.PircBot; import org.joda.time.DateTime; import org.joda.time.format.ISODateTimeFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.social.twitter.api.impl.TwitterTemplate; /** * The Class Bot. @@ -46,11 +52,14 @@ public class Hebdobot extends PircBot { private static final Logger logger = LoggerFactory.getLogger(Hebdobot.class); - private final String host; - private final int port; - private final String channel; - private Review review = null; - private final Collection listeners; + private String host; + private int port; + private String channel; + private String reviewFileSuffix; + private Review review; + private IdenticaSettings identicaSettings; + private PastebinSettings pastebinSettings; + private TwitterSettings twitterSettings; /** * Instantiates a new bot. @@ -64,25 +73,14 @@ public class Hebdobot extends PircBot * @param channel * the channel */ - public Hebdobot(final String host, final int port, final String name, final String channel) + public Hebdobot(final String host, final int port, final String name, final String channel, final String reviewFileSuffix) { this.host = host; this.port = port; this.channel = channel; this.setName(name); - - this.listeners = new LinkedList(); - } - - /** - * Adds the. - * - * @param listener - * the listener - */ - public void add(final ReviewListener listener) - { - this.listeners.add(listener); + this.reviewFileSuffix = reviewFileSuffix; + this.review = null; } /** @@ -95,15 +93,24 @@ public class Hebdobot extends PircBot } /** - * Inits the. + * Notify. * - * @throws Exception - * the exception + * @param minutes + * the minutes */ - public void init() throws Exception + public void notify(final int minutes) { - this.connect(this.host, this.port); - this.joinChannel(this.channel); + { + String message = String.format("Revue hebdomadaire dans %dmin", minutes); + sendMessage(message); + } + + if (this.twitterSettings != null) + { + String message = String.format("Revue hebdomadaire !april dans %dmin sur irc://irc.freenode.org/april ou http://apr1.org/8l", minutes); + TwitterTemplate twitterClient = new TwitterTemplate(this.twitterSettings.getConsumerKey(), this.twitterSettings.getConsumerSecret()); + twitterClient.timelineOperations().updateStatus(message); + } } /* (non-Javadoc) @@ -167,25 +174,28 @@ public class Hebdobot extends PircBot else { { - final String date = ISODateTimeFormat.basicDate().print(new DateTime()); - final String text = this.review.toString(); + String date = ISODateTimeFormat.basicDate().print(new DateTime()); + String textReview = this.review.toString(); try { - this.bot.sendMessage( - "% Compte-rendu de la revue : " + this.pastebinClient.paste(text, "Revue APRIL " + date, Private.UNLISTED)); + PastebinClient pastebinClient = new PastebinClient(this.pastebinSettings.getApiKey()); + + String returnValue = pastebinClient.paste(textReview, "Revue APRIL " + date, Private.UNLISTED); + + sendMessage("% Compte-rendu de la revue : " + returnValue); } catch (final Exception exception) { - logger.error("Error during Pastebin submit", exception); + logger.error("Error during Pastebin submit.", exception); } - if (this.properties.containsKey(FILE_SUFFIX)) + if (this.reviewFileSuffix != null) { try { - File file = new File(date + "_" + this.properties.getProperty(FILE_SUFFIX)); + File file = new File(date + "_" + this.reviewFileSuffix); FileUtils.writeStringToFile(file, text); - this.bot.sendMessage("% Compte-rendu de la revue : " + file.getName()); + sendMessage("% Compte-rendu de la revue : " + file.getName()); } catch (final Exception exception) { @@ -199,6 +209,7 @@ public class Hebdobot extends PircBot String participants = StringUtils.join(this.review.getParticipants(), " "); sendMessage("% " + participants + ", pensez à noter votre bénévalo : http://www.april.org/my?action=benevalo"); sendMessage("% Fin de la revue hebdomadaire"); + this.review = null; } } @@ -323,6 +334,34 @@ public class Hebdobot extends PircBot } } + /** + * Inits the. + * + * the exception + * + * @throws HebdobotException + */ + public void run() throws HebdobotException + { + try + { + this.connect(this.host, this.port); + this.joinChannel(this.channel); + } + catch (NickAlreadyInUseException exception) + { + throw new HebdobotException(exception); + } + catch (IOException exception) + { + throw new HebdobotException(exception); + } + catch (IrcException exception) + { + throw new HebdobotException(exception); + } + } + /** * Send message. * @@ -334,4 +373,44 @@ public class Hebdobot extends PircBot logger.debug("Send message : {}", message); this.sendMessage(this.channel, message); } + + /** + * Sets the identica settings. + * + * @param settings + * the new identica settings + */ + public void setIdenticaSettings(final IdenticaSettings settings) + { + this.identicaSettings = new IdenticaSettings(); + this.identicaSettings.setApiKey(settings.getApiKey()); + this.identicaSettings.setApiSecret(settings.getApiSecret()); + } + + /** + * Sets the pastebin settings. + * + * @param settings + * the new pastebin settings + */ + public void setPastebinSettings(final PastebinSettings settings) + { + this.pastebinSettings = new PastebinSettings(); + this.pastebinSettings.setApiKey(settings.getApiKey()); + } + + /** + * Sets the twitter settings. + * + * @param settings + * the new twitter settings + */ + public void setTwitterSettings(final TwitterSettings settings) + { + this.twitterSettings = new TwitterSettings(); + this.twitterSettings.setAccessToken(settings.getAccessToken()); + this.twitterSettings.setAccessTokenSecret(settings.getAccessTokenSecret()); + this.twitterSettings.setConsumerKey(settings.getConsumerKey()); + this.twitterSettings.setConsumerSecret(settings.getConsumerSecret()); + } } diff --git a/src/org/april/hebdobot/HebdobotConfigFile.java b/src/org/april/hebdobot/HebdobotConfigFile.java new file mode 100644 index 0000000..bbfd32d --- /dev/null +++ b/src/org/april/hebdobot/HebdobotConfigFile.java @@ -0,0 +1,148 @@ +/** + * 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; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Properties; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class Launcher. + */ +public class HebdobotConfigFile +{ + private static final Logger logger = LoggerFactory.getLogger(HebdobotConfigFile.class); + + /** + * Instantiates a new launcher. + */ + private HebdobotConfigFile() + { + } + + /** + * Load. + * + * @param source + * the source + * @return the hebdobot config + * @throws HebdobotException + */ + public static HebdobotSettings load(final File source) throws HebdobotException + { + HebdobotSettings result; + + // + Properties config = loadProperties(source); + + // + String name = config.getProperty("hebdobot.irc.name"); + String host = config.getProperty("hebdobot.irc.host"); + Integer port = toInteger(config.getProperty("hebdobot.irc.port")); + String channel = config.getProperty("hebdobot.irc.channel"); + String reviewFileSuffix = config.getProperty("hebdobot.review.file.suffix"); + + result = new HebdobotSettings(name, host, port, channel, reviewFileSuffix); + + // + result.getPastebin().setApiKey(config.getProperty("hebdobot.pastebin.apiKey")); + + // + result.getIdentica().setApiKey(config.getProperty("hebdobot.identica.apiKey")); + result.getIdentica().setApiSecret(config.getProperty("hebdobot.identica.apiSecret")); + + // + result.getTwitter().setConsumerKey(config.getProperty("hebdobot.twitter.consumerKey")); + result.getTwitter().setConsumerKey(config.getProperty("hebdobot.twitter.consumerSecret")); + result.getTwitter().setConsumerKey(config.getProperty("hebdobot.twitter.accessToken")); + result.getTwitter().setConsumerKey(config.getProperty("hebdobot.twitter.accessTokenSecret")); + + // + return result; + } + + /** + * Load properties. + * + * @param source + * the source + * @return the properties + * @throws HebdobotException + * the hebdobot exception + */ + private static Properties loadProperties(final File source) throws HebdobotException + { + Properties result; + + System.setProperty("file.encoding", "UTF-8"); + result = new Properties(); + FileReader reader = null; + try + { + reader = new FileReader(source); + result.load(reader); + } + catch (FileNotFoundException exception) + { + throw new HebdobotException("File not found.", exception); + } + catch (IOException exception) + { + throw new HebdobotException("IO error.", exception); + } + finally + { + IOUtils.closeQuietly(reader); + } + + // + return result; + } + + /** + * To integer. + * + * @param value + * the value + * @return the integer + */ + private static Integer toInteger(final String value) + { + Integer result; + + if ((value == null) || (!NumberUtils.isDigits(value))) + { + result = null; + } + else + { + result = Integer.parseInt(value); + } + + // + return result; + } +} diff --git a/src/org/april/hebdobot/HebdobotLauncher.java b/src/org/april/hebdobot/HebdobotLauncher.java new file mode 100644 index 0000000..13fb926 --- /dev/null +++ b/src/org/april/hebdobot/HebdobotLauncher.java @@ -0,0 +1,184 @@ +/** + * 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; + +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."); + } + }); + } +} diff --git a/src/org/april/hebdobot/HebdobotSettings.java b/src/org/april/hebdobot/HebdobotSettings.java new file mode 100644 index 0000000..4bdb4f4 --- /dev/null +++ b/src/org/april/hebdobot/HebdobotSettings.java @@ -0,0 +1,139 @@ +/** + * 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; + +import org.april.hebdobot.identica.IdenticaSettings; +import org.april.hebdobot.pastebin.PastebinSettings; +import org.april.hebdobot.twitter.TwitterSettings; + +/** + * The Class IdenticaSettings. + */ +public class HebdobotSettings +{ + private String name; + private String host; + private int port; + private String channel; + private String reviewFileSuffix; + + private IdenticaSettings identica; + private PastebinSettings pastebin; + private TwitterSettings twitter; + + /** + * Instantiates a new hebdobot settings. + */ + public HebdobotSettings(final String name, final String host, final int port, final String channel, final String reviewFileSuffix) + { + this.name = name; + this.host = host; + this.port = port; + this.channel = channel; + this.reviewFileSuffix = reviewFileSuffix; + + this.identica = new IdenticaSettings(); + this.pastebin = new PastebinSettings(); + this.twitter = new TwitterSettings(); + } + + public String getChannel() + { + return this.channel; + } + + public String getHost() + { + return this.host; + } + + public IdenticaSettings getIdentica() + { + return this.identica; + } + + public String getName() + { + return this.name; + } + + public PastebinSettings getPastebin() + { + return this.pastebin; + } + + public int getPort() + { + return this.port; + } + + public String getReviewFileSuffix() + { + return this.reviewFileSuffix; + } + + public TwitterSettings getTwitter() + { + return this.twitter; + } + + public void setChannel(final String channel) + { + this.channel = channel; + } + + public void setHost(final String host) + { + this.host = host; + } + + public void setName(final String name) + { + this.name = name; + } + + public void setPort(final int port) + { + this.port = port; + } + + /** + * Checks if is valid. + * + * @param source + * the source + * @return true, if is valid + */ + public static boolean isValid(final HebdobotSettings source) + { + boolean result; + + if (source == null) + { + result = false; + } + else + { + result = true; + } + + // + return result; + } +} diff --git a/src/org/april/hebdobot/Job.java b/src/org/april/hebdobot/Job.java index f2398ff..bf20e67 100644 --- a/src/org/april/hebdobot/Job.java +++ b/src/org/april/hebdobot/Job.java @@ -21,7 +21,6 @@ package org.april.hebdobot; import javax.annotation.Resource; -import org.april.hebdobot.irc.Hebdobot; import org.springframework.social.twitter.api.impl.TwitterTemplate; /** diff --git a/src/org/april/hebdobot/irc/ReviewListener.java b/src/org/april/hebdobot/irc/ReviewListener.java deleted file mode 100644 index b688319..0000000 --- a/src/org/april/hebdobot/irc/ReviewListener.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * 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.irc; - -import org.april.hebdobot.review.Review; - -/** - * The listener interface for receiving review events. The class that is - * interested in processing a review event implements this interface, and the - * object created with that class is registered with a component using the - * component's addReviewListener method. When the review event - * occurs, that object's appropriate method is invoked. - * - * @see ReviewEvent - */ -public interface ReviewListener -{ - /** - * On end. - * - * @param review - * the review - */ - void onEnd(Review review); -} diff --git a/src/org/april/hebdobot/pastebin/PastebinClient.java b/src/org/april/hebdobot/pastebin/PastebinClient.java index 9cd2c19..a23fa86 100644 --- a/src/org/april/hebdobot/pastebin/PastebinClient.java +++ b/src/org/april/hebdobot/pastebin/PastebinClient.java @@ -19,18 +19,22 @@ */ package org.april.hebdobot.pastebin; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.LinkedList; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; +import org.april.hebdobot.HebdobotException; /** * The Class PastebinClient. @@ -48,17 +52,13 @@ public class PastebinClient private static final String API_PASTE_PRIVATE = "api_paste_private"; private static final String API_PASTE_NAME = "api_paste_name"; private static final String API_PASTE_EXPIRATION = "api_paste_expire_date"; - private static final String API_PASTE_FORMAT = "api_paste_format"; - private static final String API_PASTE_CODE = "api_paste_code"; - public static final String API_ERROR = "Bad API request,"; - private final String apiKey; + private String apiKey; private String apiUserKey; - - private HttpClient httpClient = new DefaultHttpClient(); + private HttpClient httpClient; /** * Instantiates a new pastebin client. @@ -69,6 +69,7 @@ public class PastebinClient public PastebinClient(final String apiKey) { this.apiKey = apiKey; + this.httpClient = new DefaultHttpClient(); } /** @@ -78,23 +79,42 @@ public class PastebinClient * the name * @param password * the password - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public void login(final String name, final String password) throws Exception + public void login(final String name, final String password) throws HebdobotException { - final List params = new LinkedList(); - setParameter(params, API_DEV_KEY, this.apiKey); - setParameter(params, API_USER_NAME, name); - setParameter(params, API_USER_PASSWORD, password); + try + { + final List params = new LinkedList(); + setParameter(params, API_DEV_KEY, this.apiKey); + setParameter(params, API_USER_NAME, name); + setParameter(params, API_USER_PASSWORD, password); - final HttpPost request = new HttpPost(API_LOGIN_URL); - request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); + final HttpPost request = new HttpPost(API_LOGIN_URL); + request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); - final HttpResponse response = this.httpClient.execute(request); - final String content = IOUtils.toString(response.getEntity().getContent()); - APIException.throwIfError(content); - this.apiUserKey = content; + final HttpResponse response = this.httpClient.execute(request); + final String content = IOUtils.toString(response.getEntity().getContent()); + APIException.throwIfError(content); + this.apiUserKey = content; + } + catch (UnsupportedEncodingException exception) + { + throw new HebdobotException(exception); + } + catch (ClientProtocolException exception) + { + throw new HebdobotException(exception); + } + catch (IOException exception) + { + throw new HebdobotException(exception); + } + catch (APIException exception) + { + throw new HebdobotException(exception); + } } /** @@ -103,10 +123,10 @@ public class PastebinClient * @param code * the code * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code) throws Exception + public String paste(final String code) throws HebdobotException { String result; @@ -124,10 +144,10 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Expiration expiration) throws Exception + public String paste(final String code, final Expiration expiration) throws HebdobotException { String result; @@ -145,10 +165,10 @@ public class PastebinClient * @param format * the format * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Format format) throws Exception + public String paste(final String code, final Format format) throws HebdobotException { String result; @@ -168,10 +188,10 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Format format, final Expiration expiration) throws Exception + public String paste(final String code, final Format format, final Expiration expiration) throws HebdobotException { String result; @@ -191,10 +211,10 @@ public class PastebinClient * @param privat * the privat * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Format format, final Private privat) throws Exception + public String paste(final String code, final Format format, final Private privat) throws HebdobotException { String result; @@ -216,10 +236,10 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Format format, final Private privat, final Expiration expiration) throws Exception + public String paste(final String code, final Format format, final Private privat, final Expiration expiration) throws HebdobotException { String result; @@ -237,10 +257,10 @@ public class PastebinClient * @param privat * the privat * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Private privat) throws Exception + public String paste(final String code, final Private privat) throws HebdobotException { String result; @@ -260,10 +280,10 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final Private privat, final Expiration expiration) throws Exception + public String paste(final String code, final Private privat, final Expiration expiration) throws HebdobotException { String result; @@ -281,10 +301,10 @@ public class PastebinClient * @param name * the name * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final String name) throws Exception + public String paste(final String code, final String name) throws HebdobotException { String result; @@ -304,10 +324,10 @@ public class PastebinClient * @param format * the format * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final String name, final Format format) throws Exception + public String paste(final String code, final String name, final Format format) throws HebdobotException { String result; @@ -329,10 +349,10 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final String name, final Format format, final Expiration expiration) throws Exception + public String paste(final String code, final String name, final Format format, final Expiration expiration) throws HebdobotException { String result; @@ -354,10 +374,10 @@ public class PastebinClient * @param privat * the privat * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final String name, final Format format, final Private privat) throws Exception + public String paste(final String code, final String name, final Format format, final Private privat) throws HebdobotException { String result; @@ -381,29 +401,49 @@ public class PastebinClient * @param expiration * the expiration * @return the string - * @throws Exception + * @throws HebdobotException * the exception */ - public String paste(final String code, final String name, final Format format, final Private privat, final Expiration expiration) throws Exception + public String paste(final String code, final String name, final Format format, final Private privat, final Expiration expiration) + throws HebdobotException { String result; - final List params = new LinkedList(); - setParameter(params, API_DEV_KEY, this.apiKey); - setParameter(params, API_USER_KEY, this.apiUserKey); - setParameter(params, API_OPTION, Option.PASTE.getValue()); - setParameter(params, API_PASTE_PRIVATE, privat.getValue()); - setParameter(params, API_PASTE_NAME, name); - setParameter(params, API_PASTE_EXPIRATION, expiration.getValue()); - setParameter(params, API_PASTE_FORMAT, format.getValue()); - setParameter(params, API_PASTE_CODE, code); + try + { + List params = new LinkedList(); + setParameter(params, API_DEV_KEY, this.apiKey); + setParameter(params, API_USER_KEY, this.apiUserKey); + setParameter(params, API_OPTION, Option.PASTE.getValue()); + setParameter(params, API_PASTE_PRIVATE, privat.getValue()); + setParameter(params, API_PASTE_NAME, name); + setParameter(params, API_PASTE_EXPIRATION, expiration.getValue()); + setParameter(params, API_PASTE_FORMAT, format.getValue()); + setParameter(params, API_PASTE_CODE, code); - final HttpPost request = new HttpPost(API_POST_URL); - request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); + final HttpPost request = new HttpPost(API_POST_URL); + request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); - final HttpResponse response = this.httpClient.execute(request); - result = IOUtils.toString(response.getEntity().getContent()); - APIException.throwIfError(result); + final HttpResponse response = this.httpClient.execute(request); + result = IOUtils.toString(response.getEntity().getContent()); + APIException.throwIfError(result); + } + catch (UnsupportedEncodingException exception) + { + throw new HebdobotException(exception); + } + catch (ClientProtocolException exception) + { + throw new HebdobotException(exception); + } + catch (IOException exception) + { + throw new HebdobotException(exception); + } + catch (APIException exception) + { + throw new HebdobotException(exception); + } // return result; @@ -419,10 +459,10 @@ public class PastebinClient * @param privat * the privat * @return the string - * @throws Exception - * the exception + * @throws HebdobotException + * the hebdobot exception */ - public String paste(final String code, final String name, final Private privat) throws Exception + public String paste(final String code, final String name, final Private privat) throws HebdobotException { String result; @@ -444,10 +484,9 @@ public class PastebinClient */ private static void setParameter(final List params, final String name, final String value) { - if (value == null) + if (value != null) { - return; + params.add(new BasicNameValuePair(name, value)); } - params.add(new BasicNameValuePair(name, value)); } } diff --git a/src/org/april/hebdobot/review/Review.java b/src/org/april/hebdobot/review/Review.java index 6963633..abd7ad0 100644 --- a/src/org/april/hebdobot/review/Review.java +++ b/src/org/april/hebdobot/review/Review.java @@ -25,7 +25,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.april.hebdobot.Context; import org.april.hebdobot.xml.UserAlias; import org.joda.time.DateTime; diff --git a/test/org/april/hebdobot/irc/BotTest.java b/test/org/april/hebdobot/BotTest.java similarity index 61% rename from test/org/april/hebdobot/irc/BotTest.java rename to test/org/april/hebdobot/BotTest.java index c6dae72..9aab94d 100644 --- a/test/org/april/hebdobot/irc/BotTest.java +++ b/test/org/april/hebdobot/BotTest.java @@ -17,21 +17,15 @@ * You should have received a copy of the GNU Affero General Public License * along with Hebdobot. If not, see */ -package org.april.hebdobot.irc; +package org.april.hebdobot; import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.io.FileUtils; -import org.april.hebdobot.review.Review; -import org.joda.time.DateTime; -import org.joda.time.format.ISODateTimeFormat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -42,53 +36,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/april/hebdobot/conf.xml") -public class BotTest implements ReviewListener +public class BotTest { - /** - * The Class BotMock. - */ - private static class BotMock extends Hebdobot - { - /** - * Instantiates a new bot mock. - * - * @throws Exception - * the exception - */ - public BotMock() throws Exception - { - super("", 0, "bot", "channel"); - } - - /* (non-Javadoc) - * @see org.april.hebdobot.irc.Bot#onMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) - { - super.onMessage(channel, sender, login, hostname, message); - } - } - - /* (non-Javadoc) - * @see org.april.hebdobot.irc.ReviewListener#onEnd(org.april.hebdobot.review.Review) - */ - @Override - public void onEnd(final Review review) - { - try - { - final String date = ISODateTimeFormat.basicDate().print(new DateTime()); - final String text = review.toString(); - final File file = new File("target/" + date + "_revue.txt"); - FileUtils.writeStringToFile(file, text); - } - catch (final IOException exception) - { - throw new RuntimeException(exception); - } - } - /** * Redo. * @@ -99,7 +48,7 @@ public class BotTest implements ReviewListener public void redo() throws Exception { final Hebdobot bot = new BotMock(); - bot.add(this); + bot.run(); final InputStream is = BotTest.class.getResourceAsStream("/org/april/hebdobot/review.log"); if (is == null) diff --git a/src/org/april/hebdobot/OAuthRegistration.java b/test/org/april/hebdobot/OAuthRegistration.java similarity index 100% rename from src/org/april/hebdobot/OAuthRegistration.java rename to test/org/april/hebdobot/OAuthRegistration.java