Cleanup.
This commit is contained in:
parent
34636ea9f9
commit
aba2633dc4
12 changed files with 145 additions and 164 deletions
|
@ -54,6 +54,14 @@ public final class Constants {
|
|||
* The debug command.
|
||||
*/
|
||||
public static final String DEBUG_CMD = "debug";
|
||||
/**
|
||||
* Default IRC Port.
|
||||
*/
|
||||
public static final int DEFAULT_PORT = 6667;
|
||||
/**
|
||||
* Default IRC Server.
|
||||
*/
|
||||
public static final String DEFAULT_SERVER = "irc.freenode.net";
|
||||
/**
|
||||
* The die command.
|
||||
*/
|
||||
|
|
|
@ -35,6 +35,7 @@ package net.thauvin.erik.mobibot;
|
|||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import net.thauvin.erik.mobibot.commands.AbstractCommand;
|
||||
import net.thauvin.erik.mobibot.commands.AddLog;
|
||||
import net.thauvin.erik.mobibot.commands.ChannelFeed;
|
||||
import net.thauvin.erik.mobibot.commands.Cycle;
|
||||
import net.thauvin.erik.mobibot.commands.Ignore;
|
||||
import net.thauvin.erik.mobibot.commands.Info;
|
||||
|
@ -122,16 +123,12 @@ public class Mobibot extends PircBot {
|
|||
ReleaseInfo.PROJECT + " v" + ReleaseInfo.VERSION
|
||||
+ " (" + Utils.green("https://www.mobitopia.org/mobibot/") + ')',
|
||||
"Written by Erik C. Thauvin (" + Utils.green("https://erik.thauvin.net/") + ')');
|
||||
// Timer
|
||||
public static final Timer timer = new Timer(true);
|
||||
// Default port
|
||||
private static final int DEFAULT_PORT = 6667;
|
||||
// Default server
|
||||
private static final String DEFAULT_SERVER = "irc.freenode.net";
|
||||
// Logger
|
||||
private static final Logger LOGGER = LogManager.getLogger(Mobibot.class);
|
||||
// Maximum number of times the bot will try to reconnect, if disconnected
|
||||
private static final int MAX_RECONNECT = 10;
|
||||
// Logger
|
||||
private static final Logger logger = LogManager.getLogger(Mobibot.class);
|
||||
// Timer
|
||||
private static final Timer TIMER = new Timer(true);
|
||||
// Ignore command
|
||||
public final Ignore ignoreCommand;
|
||||
// Automatically post links to Twitter
|
||||
|
@ -179,6 +176,7 @@ public class Mobibot extends PircBot {
|
|||
// Weblog URL
|
||||
private String weblogUrl = "";
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link Mobibot} instance.
|
||||
*
|
||||
|
@ -196,25 +194,20 @@ public class Mobibot extends PircBot {
|
|||
|
||||
setName(nickname);
|
||||
|
||||
ircServer = p.getProperty("server", DEFAULT_SERVER);
|
||||
ircPort = Utils.getIntProperty(p.getProperty("port"), DEFAULT_PORT);
|
||||
ircServer = p.getProperty("server", Constants.DEFAULT_SERVER);
|
||||
ircPort = Utils.getIntProperty(p.getProperty("port"), Constants.DEFAULT_PORT);
|
||||
ircChannel = channel;
|
||||
logsDir = logsDirPath;
|
||||
|
||||
// Set the logger level
|
||||
loggerLevel = logger.getLevel();
|
||||
loggerLevel = LOGGER.getLevel();
|
||||
|
||||
// Load the current entries and backlogs, if any
|
||||
try {
|
||||
UrlMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Last feed: {}", UrlMgr.getStartDate());
|
||||
}
|
||||
LOGGER.debug("Last feed: {}", UrlMgr.getStartDate());
|
||||
} catch (Exception e) {
|
||||
if (logger.isErrorEnabled()) {
|
||||
logger.error("An error occurred while loading the logs.", e);
|
||||
}
|
||||
LOGGER.error("An error occurred while loading the logs.", e);
|
||||
}
|
||||
|
||||
// Initialize the bot
|
||||
|
@ -273,15 +266,14 @@ public class Mobibot extends PircBot {
|
|||
addModule(new Ping());
|
||||
addModule(new RockPaperScissors());
|
||||
addModule(new StockQuote());
|
||||
|
||||
// Twitter
|
||||
twitterModule = new Twitter();
|
||||
addModule(twitterModule);
|
||||
|
||||
addModule(new War());
|
||||
addModule(new Weather2());
|
||||
addModule(new WorldTime());
|
||||
|
||||
// Twitter module
|
||||
twitterModule = new Twitter();
|
||||
addModule(twitterModule);
|
||||
|
||||
// Load the modules properties
|
||||
modules.stream().filter(AbstractModule::hasProperties).forEach(module -> {
|
||||
for (final String s : module.getPropertyKeys()) {
|
||||
|
@ -295,6 +287,11 @@ public class Mobibot extends PircBot {
|
|||
Boolean.parseBoolean(p.getProperty(Constants.TWITTER_AUTOPOST_PROP, "false"))
|
||||
&& twitterModule.isEnabled();
|
||||
|
||||
// Sort the command & module names
|
||||
Collections.sort(commandsNames);
|
||||
Collections.sort(opsCommandsNames);
|
||||
Collections.sort(modulesNames);
|
||||
|
||||
// Save the entries
|
||||
UrlMgr.saveEntries(this, true);
|
||||
}
|
||||
|
@ -418,6 +415,22 @@ public class Mobibot extends PircBot {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a command.
|
||||
*
|
||||
* @param command The command to add.
|
||||
*/
|
||||
private void addCommand(final AbstractCommand command) {
|
||||
commands.add(command);
|
||||
if (command.isVisible()) {
|
||||
if (command.isOp()) {
|
||||
opsCommandsNames.add(command.getCommand());
|
||||
} else {
|
||||
commandsNames.add(command.getCommand());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a module.
|
||||
*
|
||||
|
@ -426,6 +439,7 @@ public class Mobibot extends PircBot {
|
|||
private void addModule(final AbstractModule module) {
|
||||
modules.add(module);
|
||||
modulesNames.add(module.getClass().getSimpleName());
|
||||
commandsNames.addAll(module.getCommands());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -456,11 +470,7 @@ public class Mobibot extends PircBot {
|
|||
connect(ircServer, ircPort);
|
||||
} catch (Exception ex) {
|
||||
if (retries == MAX_RECONNECT) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Unable to reconnect to {} after {} retries.", ircServer, MAX_RECONNECT, ex);
|
||||
}
|
||||
|
||||
LOGGER.debug("Unable to reconnect to {} after {} retries.", ircServer, MAX_RECONNECT, ex);
|
||||
e.printStackTrace(System.err);
|
||||
System.exit(1);
|
||||
}
|
||||
|
@ -526,7 +536,7 @@ public class Mobibot extends PircBot {
|
|||
* @return The bot's logger.
|
||||
*/
|
||||
public final Logger getLogger() {
|
||||
return logger;
|
||||
return LOGGER;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -567,6 +577,15 @@ public class Mobibot extends PircBot {
|
|||
return buff.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bot's timer.
|
||||
*
|
||||
* @return The timer.
|
||||
*/
|
||||
public final Timer getTimer() {
|
||||
return TIMER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get today's date for the feed.
|
||||
*
|
||||
|
@ -616,30 +635,6 @@ public class Mobibot extends PircBot {
|
|||
Utils.helpIndent(Utils.helpFormat("%c " + Constants.HELP_CMD + " <command>", getNick(), isPrivate)),
|
||||
isPrivate);
|
||||
send(sender, "The commands are:", isPrivate);
|
||||
|
||||
if (commandsNames.isEmpty()) {
|
||||
// Feed command
|
||||
commandsNames.add(getChannelName());
|
||||
|
||||
// Commands
|
||||
for (final AbstractCommand command : commands) {
|
||||
if (command.isVisible()) {
|
||||
if (command.isOp()) {
|
||||
opsCommandsNames.add(command.getCommand());
|
||||
} else {
|
||||
commandsNames.add(command.getCommand());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Modules commands
|
||||
modules.stream().filter(AbstractModule::isEnabled)
|
||||
.forEach(module -> commandsNames.addAll(module.getCommands()));
|
||||
|
||||
Collections.sort(commandsNames);
|
||||
Collections.sort(opsCommandsNames);
|
||||
}
|
||||
|
||||
sendList(sender, commandsNames, 8, isPrivate, true);
|
||||
if (isOp) {
|
||||
send(sender, "The op commands are:", isPrivate);
|
||||
|
@ -749,15 +744,13 @@ public class Mobibot extends PircBot {
|
|||
@Override
|
||||
protected final void onMessage(final String channel, final String sender, final String login, final String hostname,
|
||||
final String message) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(">>> {} : {}", sender, message);
|
||||
LOGGER.debug(">>> {} : {}", sender, message);
|
||||
|
||||
if (tell.isEnabled()) {
|
||||
tell.send(sender, true);
|
||||
}
|
||||
|
||||
boolean isCommand = false;
|
||||
|
||||
if (message.matches(getNickPattern() + ":.*")) { // mobibot: <command>
|
||||
isCommand = true;
|
||||
|
||||
final String[] cmds = message.substring(message.indexOf(':') + 1).trim().split(" ", 2);
|
||||
final String cmd = lowerCase(cmds[0]);
|
||||
|
||||
|
@ -808,9 +801,7 @@ public class Mobibot extends PircBot {
|
|||
@Override
|
||||
protected final void onPrivateMessage(final String sender, final String login, final String hostname,
|
||||
final String message) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(">>> {} : {}", sender, message);
|
||||
}
|
||||
LOGGER.debug(">>> {} : {}", sender, message);
|
||||
|
||||
final String[] cmds = message.split(" ", 2);
|
||||
final String cmd = lowerCase(cmds[0]);
|
||||
|
@ -828,15 +819,15 @@ public class Mobibot extends PircBot {
|
|||
sendRawLine("QUIT : Poof!");
|
||||
System.exit(0);
|
||||
} else if (isOp && Constants.DEBUG_CMD.equals(cmd)) { // debug
|
||||
if (logger.isDebugEnabled()) {
|
||||
Configurator.setLevel(logger.getName(), loggerLevel);
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
Configurator.setLevel(LOGGER.getName(), loggerLevel);
|
||||
} else {
|
||||
Configurator.setLevel(logger.getName(), Level.DEBUG);
|
||||
Configurator.setLevel(LOGGER.getName(), Level.DEBUG);
|
||||
}
|
||||
send(sender, "Debug logging is " + (logger.isDebugEnabled() ? "enabled." : "disabled."), true);
|
||||
send(sender, "Debug logging is " + (LOGGER.isDebugEnabled() ? "enabled." : "disabled."), true);
|
||||
} else if (isOp && Constants.DIE_CMD.equals(cmd)) { // die
|
||||
send(sender + " has just signed my death sentence.");
|
||||
timer.cancel();
|
||||
TIMER.cancel();
|
||||
twitterShutdown();
|
||||
twitterNotification("killed by " + sender + " on " + ircChannel);
|
||||
sleep(3);
|
||||
|
@ -869,7 +860,7 @@ public class Mobibot extends PircBot {
|
|||
@Override
|
||||
protected final void onAction(final String sender, final String login, final String hostname, final String target,
|
||||
final String action) {
|
||||
if (target != null && target.equals(ircChannel)) {
|
||||
if (ircChannel.equals(target)) {
|
||||
Recap.storeRecap(sender, action, true);
|
||||
}
|
||||
}
|
||||
|
@ -905,16 +896,10 @@ public class Mobibot extends PircBot {
|
|||
public final void send(final String sender, final String message, final boolean isPrivate) {
|
||||
if (isNotBlank(message) && isNotBlank(sender)) {
|
||||
if (isPrivate) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Sending message to {} : {}", sender, message);
|
||||
}
|
||||
|
||||
LOGGER.debug("Sending message to {} : {}", sender, message);
|
||||
sendMessage(sender, message);
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Sending notice to {} : {}", sender, message);
|
||||
}
|
||||
|
||||
LOGGER.debug("Sending notice to {} : {}", sender, message);
|
||||
sendNotice(sender, message);
|
||||
}
|
||||
}
|
||||
|
@ -1028,7 +1013,7 @@ public class Mobibot extends PircBot {
|
|||
}
|
||||
|
||||
/**
|
||||
* Add an entry to be posted on twitter.
|
||||
* Add an entry to be posted on Twitter.
|
||||
*
|
||||
* @param index The entry index.
|
||||
*/
|
||||
|
@ -1049,11 +1034,12 @@ public class Mobibot extends PircBot {
|
|||
entry.getTitle() + ' ' + entry.getLink() + " via " + entry.getNick() + " on " + getChannel();
|
||||
new Thread(() -> {
|
||||
try {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("Posting {}{} to Twitter.", Constants.LINK_CMD, index + 1);
|
||||
}
|
||||
twitterModule.post(twitterHandle, msg, false);
|
||||
} catch (ModuleException e) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Failed to post entry on twitter.", e);
|
||||
}
|
||||
LOGGER.warn("Failed to post entry on Twitter.", e);
|
||||
}
|
||||
}).start();
|
||||
twitterEntries.remove(index);
|
||||
|
@ -1083,16 +1069,14 @@ public class Mobibot extends PircBot {
|
|||
getName() + ' ' + ReleaseInfo.VERSION + " " + msg,
|
||||
true);
|
||||
} catch (ModuleException e) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Failed to notify @{}: {}", twitterHandle, msg, e);
|
||||
}
|
||||
LOGGER.warn("Failed to notify @{}: {}", twitterHandle, msg, e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes entry from twitter auto-post.
|
||||
* Removes entry from Twitter auto-post.
|
||||
*
|
||||
* @param index The entry's index.
|
||||
*/
|
||||
|
@ -1106,6 +1090,7 @@ public class Mobibot extends PircBot {
|
|||
*/
|
||||
final void twitterShutdown() {
|
||||
if (twitterModule.isEnabled() && isNotBlank(twitterHandle)) {
|
||||
LOGGER.debug("Twitter shutdown.");
|
||||
for (final int i : twitterEntries) {
|
||||
twitterEntryPost(i);
|
||||
}
|
||||
|
|
|
@ -237,7 +237,8 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
|||
private fun twitterPost(bot: Mobibot, index: Int) {
|
||||
if (bot.isTwitterAutoPost) {
|
||||
bot.twitterAddEntry(index)
|
||||
Mobibot.timer.schedule(TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L)
|
||||
bot.logger.debug("Scheduling ${Constants.LINK_CMD}${index + 1} for posting on Twitter.")
|
||||
bot.timer.schedule(TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,10 +106,7 @@ public class Tell extends AbstractCommand {
|
|||
*/
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
final boolean clean() {
|
||||
if (bot.getLogger().isDebugEnabled()) {
|
||||
bot.getLogger().debug("Cleaning the messages.");
|
||||
}
|
||||
|
||||
bot.getLogger().debug("Cleaning the messages.");
|
||||
return TellMessagesMgr.clean(messages, maxDays);
|
||||
}
|
||||
|
||||
|
|
|
@ -91,9 +91,7 @@ final class TellMessagesMgr {
|
|||
try {
|
||||
try (final ObjectInput input = new ObjectInputStream(
|
||||
new BufferedInputStream(Files.newInputStream(Paths.get(file))))) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Loading the messages.");
|
||||
}
|
||||
logger.debug("Loading the messages.");
|
||||
|
||||
return ((List<TellMessage>) input.readObject());
|
||||
}
|
||||
|
@ -117,10 +115,7 @@ final class TellMessagesMgr {
|
|||
try {
|
||||
try (final ObjectOutput output = new ObjectOutputStream(
|
||||
new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Saving the messages.");
|
||||
}
|
||||
|
||||
logger.debug("Saving the messages.");
|
||||
output.writeObject(messages);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -102,13 +102,13 @@ public final class EntriesMgr {
|
|||
* @throws FeedException If an error occurred while reading the feed.
|
||||
*/
|
||||
public static void loadBacklogs(final String file, final Collection<String> history)
|
||||
throws IOException, FeedException {
|
||||
throws IOException, FeedException {
|
||||
history.clear();
|
||||
|
||||
final SyndFeedInput input = new SyndFeedInput();
|
||||
|
||||
try (final InputStreamReader reader =
|
||||
new InputStreamReader(Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) {
|
||||
new InputStreamReader(Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) {
|
||||
|
||||
final SyndFeed feed = input.build(reader);
|
||||
|
||||
|
@ -134,7 +134,7 @@ public final class EntriesMgr {
|
|||
*/
|
||||
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
|
||||
public static String loadEntries(final String file, final String channel, final Collection<EntryLink> entries)
|
||||
throws IOException, FeedException {
|
||||
throws IOException, FeedException {
|
||||
entries.clear();
|
||||
|
||||
final SyndFeedInput input = new SyndFeedInput();
|
||||
|
@ -142,7 +142,7 @@ public final class EntriesMgr {
|
|||
final String today;
|
||||
|
||||
try (final InputStreamReader reader = new InputStreamReader(
|
||||
Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) {
|
||||
Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) {
|
||||
final SyndFeed feed = input.build(reader);
|
||||
|
||||
today = Utils.isoLocalDate(feed.getPublishedDate());
|
||||
|
@ -157,13 +157,13 @@ public final class EntriesMgr {
|
|||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
item = items.get(i);
|
||||
author = item.getAuthor()
|
||||
.substring(item.getAuthor().lastIndexOf('(') + 1, item.getAuthor().length() - 1);
|
||||
.substring(item.getAuthor().lastIndexOf('(') + 1, item.getAuthor().length() - 1);
|
||||
entry = new EntryLink(item.getLink(),
|
||||
item.getTitle(),
|
||||
author,
|
||||
channel,
|
||||
item.getPublishedDate(),
|
||||
item.getCategories());
|
||||
item.getTitle(),
|
||||
author,
|
||||
channel,
|
||||
item.getPublishedDate(),
|
||||
item.getCategories());
|
||||
description = item.getDescription();
|
||||
comments = description.getValue().split("<br/>");
|
||||
|
||||
|
@ -191,15 +191,14 @@ public final class EntriesMgr {
|
|||
* @param history The history array.
|
||||
* @param isDayBackup Set the true if the daily backup file should also be created.
|
||||
*/
|
||||
@SuppressFBWarnings(value = {"CE_CLASS_ENVY", "CC_CYCLOMATIC_COMPLEXITY"}, justification = "Yes, it does.")
|
||||
@SuppressFBWarnings(value = { "CE_CLASS_ENVY", "CC_CYCLOMATIC_COMPLEXITY" },
|
||||
justification = "Yes, it does.")
|
||||
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
|
||||
public static void saveEntries(final Mobibot bot,
|
||||
final List<EntryLink> entries,
|
||||
final List<String> history,
|
||||
final boolean isDayBackup) {
|
||||
if (bot.getLogger().isDebugEnabled()) {
|
||||
bot.getLogger().debug("Saving the feeds...");
|
||||
}
|
||||
bot.getLogger().debug("Saving the feeds...");
|
||||
|
||||
if (StringUtils.isNotBlank(bot.getLogsDir()) && StringUtils.isNotBlank(bot.getWeblogUrl())) {
|
||||
try {
|
||||
|
@ -209,7 +208,7 @@ public final class EntriesMgr {
|
|||
SyndEntry item;
|
||||
SyndContent description;
|
||||
try (final Writer fw = new OutputStreamWriter(
|
||||
Files.newOutputStream(Paths.get(bot.getLogsDir() + CURRENT_XML)), StandardCharsets.UTF_8)) {
|
||||
Files.newOutputStream(Paths.get(bot.getLogsDir() + CURRENT_XML)), StandardCharsets.UTF_8)) {
|
||||
rss.setFeedType("rss_2.0");
|
||||
rss.setTitle(bot.getChannel() + " IRC Links");
|
||||
rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel());
|
||||
|
@ -225,14 +224,14 @@ public final class EntriesMgr {
|
|||
entry = entries.get(i);
|
||||
|
||||
buff = new StringBuilder()
|
||||
.append("Posted by <b>")
|
||||
.append(entry.getNick())
|
||||
.append("</b> on <a href=\"irc://")
|
||||
.append(bot.getIrcServer()).append('/')
|
||||
.append(entry.getChannel())
|
||||
.append("\"><b>")
|
||||
.append(entry.getChannel())
|
||||
.append("</b></a>");
|
||||
.append("Posted by <b>")
|
||||
.append(entry.getNick())
|
||||
.append("</b> on <a href=\"irc://")
|
||||
.append(bot.getIrcServer()).append('/')
|
||||
.append(entry.getChannel())
|
||||
.append("\"><b>")
|
||||
.append(entry.getChannel())
|
||||
.append("</b></a>");
|
||||
|
||||
if (entry.getCommentsCount() > 0) {
|
||||
buff.append(" <br/><br/>");
|
||||
|
@ -258,7 +257,8 @@ public final class EntriesMgr {
|
|||
item.setTitle(entry.getTitle());
|
||||
item.setPublishedDate(entry.getDate());
|
||||
item.setAuthor(
|
||||
bot.getChannel().substring(1) + '@' + bot.getIrcServer() + " (" + entry.getNick() + ')');
|
||||
bot.getChannel().substring(1) + '@' + bot.getIrcServer() + " (" + entry.getNick()
|
||||
+ ')');
|
||||
item.setCategories(entry.getTags());
|
||||
|
||||
items.add(item);
|
||||
|
@ -266,16 +266,13 @@ public final class EntriesMgr {
|
|||
|
||||
rss.setEntries(items);
|
||||
|
||||
if (bot.getLogger().isDebugEnabled()) {
|
||||
bot.getLogger().debug("Writing the entries feed.");
|
||||
}
|
||||
|
||||
bot.getLogger().debug("Writing the entries feed.");
|
||||
output.output(rss, fw);
|
||||
}
|
||||
|
||||
try (final Writer fw = new OutputStreamWriter(
|
||||
Files.newOutputStream(Paths.get(
|
||||
bot.getLogsDir() + bot.getToday() + XML_EXT)), StandardCharsets.UTF_8)) {
|
||||
Files.newOutputStream(Paths.get(
|
||||
bot.getLogsDir() + bot.getToday() + XML_EXT)), StandardCharsets.UTF_8)) {
|
||||
output.output(rss, fw);
|
||||
}
|
||||
|
||||
|
@ -290,12 +287,12 @@ public final class EntriesMgr {
|
|||
}
|
||||
|
||||
try (final Writer fw = new OutputStreamWriter(
|
||||
Files.newOutputStream(Paths.get(bot.getLogsDir() + NAV_XML)), StandardCharsets.UTF_8)) {
|
||||
Files.newOutputStream(Paths.get(bot.getLogsDir() + NAV_XML)), StandardCharsets.UTF_8)) {
|
||||
rss = new SyndFeedImpl();
|
||||
rss.setFeedType("rss_2.0");
|
||||
rss.setTitle(bot.getChannel() + " IRC Links Backlogs");
|
||||
rss.setDescription("Backlogs of Links from " + bot.getIrcServer() + " on "
|
||||
+ bot.getChannel());
|
||||
+ bot.getChannel());
|
||||
rss.setLink(bot.getBacklogsUrl());
|
||||
rss.setPublishedDate(Calendar.getInstance().getTime());
|
||||
|
||||
|
@ -317,10 +314,7 @@ public final class EntriesMgr {
|
|||
|
||||
rss.setEntries(items);
|
||||
|
||||
if (bot.getLogger().isDebugEnabled()) {
|
||||
bot.getLogger().debug("Writing the backlog feed.");
|
||||
}
|
||||
|
||||
bot.getLogger().debug("Writing the backlog feed.");
|
||||
output.output(rss, fw);
|
||||
}
|
||||
} else {
|
||||
|
@ -331,8 +325,7 @@ public final class EntriesMgr {
|
|||
bot.getLogger().warn("Unable to generate the entries feed.", e);
|
||||
}
|
||||
} else {
|
||||
bot.getLogger()
|
||||
.warn("Unable to generate the entries feed. At least one of the required property is missing.");
|
||||
bot.getLogger().warn("Unable to generate the entries feed. A required property is missing.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -180,10 +180,7 @@ public final class Lookup extends AbstractModule {
|
|||
bot.send("Unknown host.");
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
if (bot.getLogger().isDebugEnabled()) {
|
||||
bot.getLogger().debug("Unable to perform whois IP lookup: {}", args, ioe);
|
||||
}
|
||||
|
||||
bot.getLogger().debug("Unable to perform whois IP lookup: {}", args, ioe);
|
||||
bot.send("Unable to perform whois IP lookup: " + ioe.getMessage());
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -58,8 +58,8 @@ public class GoogleSearchTest extends LocalProperties {
|
|||
@SuppressWarnings("PMD.PreserveStackTrace")
|
||||
@Test
|
||||
public void testSearchGoogle() throws ModuleException {
|
||||
final String apiKey = LocalProperties.getProperty(GoogleSearch.GOOGLE_API_KEY_PROP);
|
||||
final String cseKey = LocalProperties.getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP);
|
||||
final String apiKey = getProperty(GoogleSearch.GOOGLE_API_KEY_PROP);
|
||||
final String cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP);
|
||||
try {
|
||||
List<Message> messages = GoogleSearch.searchGoogle("mobibot site:github.com", apiKey, cseKey);
|
||||
assertThat(messages).as("mobibot results not empty").isNotEmpty();
|
||||
|
|
|
@ -40,18 +40,25 @@ class RockPaperScissorsTest {
|
|||
@Test
|
||||
fun testWinLoseOrDraw() {
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "paper")).`as`("scissors vs. paper").isEqualTo("win")
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "paper")
|
||||
).`as`("scissors vs. paper").isEqualTo("win")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("paper", "rock")).`as`("paper vs. rock").isEqualTo("win")
|
||||
RockPaperScissors.winLoseOrDraw("paper", "rock")
|
||||
).`as`("paper vs. rock").isEqualTo("win")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("rock", "scissors")).`as`("rock vs. scissors").isEqualTo("win")
|
||||
RockPaperScissors.winLoseOrDraw("rock", "scissors")
|
||||
).`as`("rock vs. scissors").isEqualTo("win")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("paper", "scissors")).`as`("paper vs. scissors").isEqualTo("lose")
|
||||
RockPaperScissors.winLoseOrDraw("paper", "scissors")
|
||||
).`as`("paper vs. scissors").isEqualTo("lose")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("rock", "paper")).`as`("rock vs. paper").isEqualTo("lose")
|
||||
RockPaperScissors.winLoseOrDraw("rock", "paper")
|
||||
).`as`("rock vs. paper").isEqualTo("lose")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "rock")).`as`("scissors vs. rock").isEqualTo("lose")
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "rock")
|
||||
).`as`("scissors vs. rock").isEqualTo("lose")
|
||||
assertThat(
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "scissors")).`as`("scissors vs. scissors").isEqualTo("draw")
|
||||
RockPaperScissors.winLoseOrDraw("scissors", "scissors")
|
||||
).`as`("scissors vs. scissors").isEqualTo("draw")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ public class StockQuoteTest extends LocalProperties {
|
|||
assertThat(messages.get(0).getText()).as("same stock symbol").contains("AAPL").contains("Apple Inc.");
|
||||
|
||||
try {
|
||||
StockQuote.getQuote("012", apiKey);
|
||||
StockQuote.getQuote("blahfoo", apiKey);
|
||||
} catch (ModuleException e) {
|
||||
assertThat(e.getMessage()).as("invalid symbol").containsIgnoringCase(StockQuote.INVALID_SYMBOL);
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* @created 2019-04-19
|
||||
* @since 1.0
|
||||
*/
|
||||
public class TwitterTest {
|
||||
public class TwitterTest extends LocalProperties {
|
||||
@SuppressFBWarnings("MDM")
|
||||
private String getCi() {
|
||||
if ("true".equals(System.getenv("CIRCLECI"))) {
|
||||
|
@ -68,12 +68,12 @@ public class TwitterTest {
|
|||
public void testPostTwitter() throws ModuleException {
|
||||
final String msg = "Testing Twitter API from " + getCi();
|
||||
assertThat(Twitter.twitterPost(
|
||||
LocalProperties.getProperty(Twitter.CONSUMER_KEY_PROP),
|
||||
LocalProperties.getProperty(Twitter.CONSUMER_SECRET_PROP),
|
||||
LocalProperties.getProperty(Twitter.TOKEN_PROP),
|
||||
LocalProperties.getProperty(Twitter.TOKEN_SECRET_PROP),
|
||||
LocalProperties.getProperty(Constants.TWITTER_HANDLE_PROP),
|
||||
msg,
|
||||
true).getText()).as("twitterPost(" + msg + ')').isEqualTo(msg);
|
||||
getProperty(Twitter.CONSUMER_KEY_PROP),
|
||||
getProperty(Twitter.CONSUMER_SECRET_PROP),
|
||||
getProperty(Twitter.TOKEN_PROP),
|
||||
getProperty(Twitter.TOKEN_SECRET_PROP),
|
||||
getProperty(Constants.TWITTER_HANDLE_PROP),
|
||||
msg,
|
||||
true).getText()).as("twitterPost(" + msg + ')').isEqualTo(msg);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,21 +53,19 @@ public class Weather2Test extends LocalProperties {
|
|||
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
|
||||
@Test
|
||||
public void testWeather() throws ModuleException {
|
||||
List<Message> messages = Weather2.getWeather("98204", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
||||
List<Message> messages = Weather2.getWeather("98204", getProperty(Weather2.OWM_API_KEY_PROP));
|
||||
assertThat(messages.get(0).getText()).as("is Everett").contains("Everett").contains("US");
|
||||
assertThat(messages.get(messages.size() - 1).getText()).as("is City Search").endsWith("98204%2CUS");
|
||||
|
||||
messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
||||
messages = Weather2.getWeather("London, UK", getProperty(Weather2.OWM_API_KEY_PROP));
|
||||
assertThat(messages.get(0).getText()).as("is UK").contains("London").contains("UK");
|
||||
assertThat(messages.get(messages.size() - 1).getText()).as("is City Code").endsWith("4517009");
|
||||
|
||||
assertThatThrownBy(
|
||||
() -> Weather2.getWeather("Montpellier, FR", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP))).as(
|
||||
"Montpellier not found").hasCauseInstanceOf(APIException.class);
|
||||
assertThatThrownBy(() -> Weather2.getWeather("Montpellier, FR", getProperty(Weather2.OWM_API_KEY_PROP)))
|
||||
.as("Montpellier not found").hasCauseInstanceOf(APIException.class);
|
||||
|
||||
assertThatThrownBy(
|
||||
() -> Weather2.getWeather("test", "")).as(
|
||||
"no API key").isInstanceOf(ModuleException.class).hasNoCause();
|
||||
assertThatThrownBy(() -> Weather2.getWeather("test", ""))
|
||||
.as("no API key").isInstanceOf(ModuleException.class).hasNoCause();
|
||||
|
||||
messages = Weather2.getWeather("", "apikey");
|
||||
assertThat(messages.get(0).isError()).as("no query").isTrue();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue