Moved most commands to modules.
This commit is contained in:
parent
b120e1da87
commit
015a368bd1
24 changed files with 1197 additions and 1436 deletions
|
@ -13,11 +13,11 @@ import java.util.Date;
|
|||
* Annotation Processor</a>
|
||||
*/
|
||||
public final class ReleaseInfo {
|
||||
private final static String buildmeta = "008";
|
||||
private final static Date date = new Date(1467076393978L);
|
||||
private final static String buildmeta = "001";
|
||||
private final static Date date = new Date(1467451308717L);
|
||||
private final static int major = 0;
|
||||
private final static int minor = 6;
|
||||
private final static int patch = 1;
|
||||
private final static int patch = 5;
|
||||
private final static String prerelease = "beta";
|
||||
private final static String project = "mobibot";
|
||||
|
||||
|
|
|
@ -45,21 +45,6 @@ final class Commands
|
|||
*/
|
||||
public static final String ADDLOG_CMD = "addlog";
|
||||
|
||||
/**
|
||||
* The math command.
|
||||
*/
|
||||
public static final String CALC_CMD = "calc";
|
||||
|
||||
/**
|
||||
* The currency command.
|
||||
*/
|
||||
public static final String CURRENCY_CMD = "currency";
|
||||
|
||||
/**
|
||||
* The rates keyword.
|
||||
*/
|
||||
public static final String CURRENCY_RATES_KEYWORD = "rates";
|
||||
|
||||
/**
|
||||
* The cycle command.
|
||||
*/
|
||||
|
@ -75,21 +60,11 @@ final class Commands
|
|||
*/
|
||||
public static final String DEBUG_CMD = "debug";
|
||||
|
||||
/**
|
||||
* The dices command.
|
||||
*/
|
||||
public static final String DICE_CMD = "dice";
|
||||
|
||||
/**
|
||||
* The die command.
|
||||
*/
|
||||
public static final String DIE_CMD = "die";
|
||||
|
||||
/**
|
||||
* The Google command.
|
||||
*/
|
||||
public static final String GOOGLE_CMD = "google";
|
||||
|
||||
/**
|
||||
* Help command line argument.
|
||||
*/
|
||||
|
@ -125,21 +100,11 @@ final class Commands
|
|||
*/
|
||||
public static final String INFO_CMD = "info";
|
||||
|
||||
/**
|
||||
* The joke command.
|
||||
*/
|
||||
public static final String JOKE_CMD = "joke";
|
||||
|
||||
/**
|
||||
* The link command.
|
||||
*/
|
||||
public static final String LINK_CMD = "L";
|
||||
|
||||
/**
|
||||
* The lookup command.
|
||||
*/
|
||||
public static final String LOOKUP_CMD = "lookup";
|
||||
|
||||
/**
|
||||
* The me command.
|
||||
*/
|
||||
|
@ -155,16 +120,6 @@ final class Commands
|
|||
*/
|
||||
public static final String NICK_CMD = "nick";
|
||||
|
||||
/**
|
||||
* The ping command.
|
||||
*/
|
||||
public static final String PING_CMD = "ping";
|
||||
|
||||
/**
|
||||
* The pong command.
|
||||
*/
|
||||
public static final String PONG_CMD = "pong";
|
||||
|
||||
/**
|
||||
* Properties command line argument.
|
||||
*/
|
||||
|
@ -180,11 +135,6 @@ final class Commands
|
|||
*/
|
||||
public static final String SAY_CMD = "say";
|
||||
|
||||
/**
|
||||
* The stock command.
|
||||
*/
|
||||
public static final String STOCK_CMD = "stock";
|
||||
|
||||
/**
|
||||
* The {@link #TELL_CMD} all command.
|
||||
*/
|
||||
|
@ -200,16 +150,6 @@ final class Commands
|
|||
*/
|
||||
public static final String TELL_DEL_CMD = "del";
|
||||
|
||||
/**
|
||||
* The time command.
|
||||
*/
|
||||
public static final String TIME_CMD = "time";
|
||||
|
||||
/**
|
||||
* The Twitter command.
|
||||
*/
|
||||
public static final String TWITTER_CMD = "twitter";
|
||||
|
||||
/**
|
||||
* The users command.
|
||||
*/
|
||||
|
@ -230,16 +170,6 @@ final class Commands
|
|||
*/
|
||||
public static final String VIEW_CMD = "view";
|
||||
|
||||
/**
|
||||
* The war command.
|
||||
*/
|
||||
public static final String WAR_CMD = "war";
|
||||
|
||||
/**
|
||||
* The weather command.
|
||||
*/
|
||||
public static final String WEATHER_CMD = "weather";
|
||||
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -34,10 +34,6 @@ package net.thauvin.erik.mobibot;
|
|||
import org.jibble.pircbot.Colors;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
|
@ -48,7 +44,7 @@ import java.util.Calendar;
|
|||
* @created 2014-04-26
|
||||
* @since 1.0
|
||||
*/
|
||||
final class Utils
|
||||
final public class Utils
|
||||
{
|
||||
/**
|
||||
* The timestamp simple date format.
|
||||
|
@ -58,12 +54,12 @@ final class Utils
|
|||
/**
|
||||
* The ISO (YYYY-MM-DD) simple date format.
|
||||
*/
|
||||
static final SimpleDateFormat ISO_SDF = new SimpleDateFormat("yyyy-MM-dd");
|
||||
public static final SimpleDateFormat ISO_SDF = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
/**
|
||||
* The UTC (yyyy-MM-dd HH:mm) simple date format.
|
||||
*/
|
||||
static final SimpleDateFormat UTC_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
public static final SimpleDateFormat UTC_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
|
@ -191,71 +187,6 @@ final class Utils
|
|||
return (Commands.LINK_CMD + (entryIndex + 1) + "T: " + entry.getDeliciousTags().replaceAll(",", ", "));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a file.
|
||||
*
|
||||
* @param in The source file.
|
||||
* @param out The destination file.
|
||||
*
|
||||
* @throws java.io.IOException If the file could not be copied.
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public static void copyFile(final File in, final File out)
|
||||
throws IOException
|
||||
{
|
||||
FileChannel inChannel = null;
|
||||
FileChannel outChannel = null;
|
||||
FileInputStream input = null;
|
||||
FileOutputStream output = null;
|
||||
|
||||
try
|
||||
{
|
||||
input = new FileInputStream(in);
|
||||
output = new FileOutputStream(out);
|
||||
|
||||
inChannel = input.getChannel();
|
||||
outChannel = output.getChannel();
|
||||
|
||||
inChannel.transferTo(0L, inChannel.size(), outChannel);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (inChannel != null)
|
||||
{
|
||||
inChannel.close();
|
||||
}
|
||||
|
||||
if (input != null)
|
||||
{
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ignore)
|
||||
{
|
||||
; // Do nothing
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (outChannel != null)
|
||||
{
|
||||
outChannel.close();
|
||||
}
|
||||
|
||||
if (output != null)
|
||||
{
|
||||
output.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ignore)
|
||||
{
|
||||
; // Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the given location (File/URL) has a trailing slash (<code>/</code>) to indicate a directory.
|
||||
*
|
||||
|
@ -264,7 +195,7 @@ final class Utils
|
|||
*
|
||||
* @return The location ending with a slash.
|
||||
*/
|
||||
public static String ensureDir(final String location, final boolean isUrl)
|
||||
static String ensureDir(final String location, final boolean isUrl)
|
||||
{
|
||||
if (isUrl)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Module.java
|
||||
*
|
||||
* Copyright (c) 2004-2016, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* The <code>Module</code> class.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2016-07-01
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class AbstractModule
|
||||
{
|
||||
|
||||
final List<String> commands = new ArrayList<>();
|
||||
|
||||
final Map<String, String> properties = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Responds to a command.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender.
|
||||
* @param args The command arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
public abstract void commandResponse(final Mobibot bot, final String sender, final String args,
|
||||
final boolean isPrivate);
|
||||
|
||||
/**
|
||||
* Returns the module's commands, if any.
|
||||
*
|
||||
* @return The commands.
|
||||
*/
|
||||
public List<String> getCommands()
|
||||
{
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the module's property keys.
|
||||
*
|
||||
* @return The keys.
|
||||
*/
|
||||
public Set<String> getPropertyKeys()
|
||||
{
|
||||
return properties.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the module has properties.
|
||||
*
|
||||
* @return <code>true</code> or <code>false</code> .
|
||||
*/
|
||||
public boolean hasProperties()
|
||||
{
|
||||
return !properties.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Responds with the module's help.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender.
|
||||
* @param args The help arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
public abstract void helpResponse(final Mobibot bot, final String sender, final String args,
|
||||
final boolean isPrivate);
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the module is enabled.
|
||||
*
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
*/
|
||||
public boolean isEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <codde>true</codde> if the module responds to private messages.
|
||||
*
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
*/
|
||||
public boolean isPrivateMsgEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a property key and value.
|
||||
*
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
*/
|
||||
public void setProperty(final String key, final String value)
|
||||
{
|
||||
if (Utils.isValidString(key))
|
||||
{
|
||||
properties.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
98
src/main/java/net/thauvin/erik/mobibot/modules/Calc.java
Normal file
98
src/main/java/net/thauvin/erik/mobibot/modules/Calc.java
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Calc.java
|
||||
*
|
||||
* Copyright (c) 2004-2016, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.objecthunter.exp4j.Expression;
|
||||
import net.objecthunter.exp4j.ExpressionBuilder;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* The Calc module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2016-07-01
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Calc extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The Calc command.
|
||||
*/
|
||||
private static final String CALC_CMD = "calc";
|
||||
|
||||
/**
|
||||
* The default constructor.
|
||||
*/
|
||||
public Calc()
|
||||
{
|
||||
commands.add(CALC_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (Utils.isValidString(args))
|
||||
{
|
||||
final DecimalFormat decimalFormat = new DecimalFormat("#.##");
|
||||
|
||||
try
|
||||
{
|
||||
final Expression calc = new ExpressionBuilder(args).build();
|
||||
bot.send(bot.getChannel(), args.replaceAll(" ", "") + " = " + decimalFormat.format(calc.evaluate()));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (bot.getLogger().isDebugEnabled())
|
||||
{
|
||||
bot.getLogger().debug("Unable to calculate: " + args, e);
|
||||
}
|
||||
|
||||
bot.send(bot.getChannel(), "No idea. This is the kind of math I don't get.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To solve a mathematical calculation:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + CALC_CMD + " <calculation>"));
|
||||
}
|
||||
}
|
|
@ -29,8 +29,10 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.jdom2.Document;
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.JDOMException;
|
||||
|
@ -46,14 +48,24 @@ import java.util.Map;
|
|||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#CURRENCY_CMD} command.
|
||||
* The CurrentConverter module.
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @created Feb 11, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
class CurrencyConverter implements Runnable
|
||||
final public class CurrencyConverter extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The currency command.
|
||||
*/
|
||||
private static final String CURRENCY_CMD = "currency";
|
||||
|
||||
/**
|
||||
* The rates keyword.
|
||||
*/
|
||||
private static final String CURRENCY_RATES_KEYWORD = "rates";
|
||||
|
||||
/**
|
||||
* The exchange rates.
|
||||
*/
|
||||
|
@ -64,40 +76,54 @@ class CurrencyConverter implements Runnable
|
|||
*/
|
||||
private static final String EXCHANGE_TABLE_URL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
|
||||
|
||||
/**
|
||||
* The bot.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
|
||||
/**
|
||||
* The last exchange rates table publication date.
|
||||
*/
|
||||
private String pubDate = "";
|
||||
|
||||
/**
|
||||
* The actual currency query.
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private String sender;
|
||||
|
||||
/**
|
||||
* Creates a new {@link CurrencyConverter} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
*/
|
||||
public CurrencyConverter(final Mobibot bot)
|
||||
public CurrencyConverter()
|
||||
{
|
||||
this.bot = bot;
|
||||
commands.add(CURRENCY_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
if (!pubDate.equals(Utils.today()))
|
||||
{
|
||||
EXCHANGE_RATES.clear();
|
||||
}
|
||||
}
|
||||
|
||||
new Thread(() -> {
|
||||
run(bot, sender, args);
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To convert from one currency to another:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + CURRENCY_CMD + " [100 USD to EUR]"));
|
||||
|
||||
if (args.endsWith(CURRENCY_CMD))
|
||||
{
|
||||
bot.send(sender, "For a listing of currency rates:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + CURRENCY_CMD) + ' ' + CURRENCY_RATES_KEYWORD);
|
||||
bot.send(sender, "For a listing of supported currencies:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + CURRENCY_CMD));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the specified currencies.
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender, final String query)
|
||||
{
|
||||
if (Utils.isValidString(sender))
|
||||
{
|
||||
|
@ -183,7 +209,7 @@ class CurrencyConverter implements Runnable
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (query.equals(Commands.CURRENCY_RATES_KEYWORD))
|
||||
else if (query.equals(CURRENCY_RATES_KEYWORD))
|
||||
{
|
||||
bot.send(sender, "Last Update: " + pubDate);
|
||||
|
||||
|
@ -204,27 +230,9 @@ class CurrencyConverter implements Runnable
|
|||
}
|
||||
else
|
||||
{
|
||||
bot.helpResponse(sender, Commands.CURRENCY_CMD + ' ' + query);
|
||||
helpResponse(bot, sender, CURRENCY_CMD + ' ' + query, true);
|
||||
bot.send(sender, "The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the query.
|
||||
*
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param query The currency query.
|
||||
*/
|
||||
|
||||
public synchronized void setQuery(final String sender, final String query)
|
||||
{
|
||||
this.query = query;
|
||||
this.sender = sender;
|
||||
|
||||
if (!pubDate.equals(Utils.today()))
|
||||
{
|
||||
EXCHANGE_RATES.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,37 +29,45 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#DICE_CMD} command.
|
||||
* The Dice module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2014-04-28
|
||||
* @since 1.0
|
||||
*/
|
||||
final class Dice
|
||||
final public class Dice extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
* @throws UnsupportedOperationException If the constructor is called.
|
||||
* The dice command.
|
||||
*/
|
||||
private Dice()
|
||||
throws UnsupportedOperationException
|
||||
private final String DICE_CMD = "dice";
|
||||
|
||||
/**
|
||||
* The default constructor.
|
||||
*/
|
||||
public Dice()
|
||||
{
|
||||
throw new UnsupportedOperationException("Illegal constructor call.");
|
||||
commands.add(DICE_CMD);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rolls the dice.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender's nickname.
|
||||
* @param sender The sender.
|
||||
* @param args The command arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
public static void roll(final Mobibot bot, final String sender)
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
final Random r = new Random();
|
||||
|
||||
|
@ -91,4 +99,17 @@ final class Dice
|
|||
bot.action("tied.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To roll the dice:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + DICE_CMD));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -29,8 +29,10 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
@ -41,67 +43,78 @@ import java.net.URLConnection;
|
|||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#GOOGLE_CMD} command.
|
||||
* The GoogleSearch module.
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @created Feb 7, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
class GoogleSearch implements Runnable
|
||||
final public class GoogleSearch extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The Google API Key property.
|
||||
*/
|
||||
private static final String GOOGLE_API_KEY_PROP = "google-api-key";
|
||||
|
||||
/**
|
||||
* The google command.
|
||||
*/
|
||||
private static final String GOOGLE_CMD = "google";
|
||||
|
||||
/**
|
||||
* The Google Custom Search Engine ID property.
|
||||
*/
|
||||
private static final String GOOGLE_CSE_KEY_PROP = "google-cse-cx";
|
||||
|
||||
/**
|
||||
* The tab indent (4 spaces).
|
||||
*/
|
||||
private static final String TAB_INDENT = " ";
|
||||
|
||||
/**
|
||||
* The bot.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
|
||||
/**
|
||||
* The Google Custom Search Engine ID.
|
||||
*/
|
||||
private final String cseCx;
|
||||
|
||||
/**
|
||||
* The search query.
|
||||
*/
|
||||
private final String query;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private final String sender;
|
||||
|
||||
/**
|
||||
* Creates a new {@link GoogleSearch} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param cseCx The Google Custom Search Engine ID.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param query The Google query
|
||||
*/
|
||||
public GoogleSearch(final Mobibot bot, final String cseCx, final String sender, final String query)
|
||||
public GoogleSearch()
|
||||
{
|
||||
this.bot = bot;
|
||||
this.cseCx = cseCx;
|
||||
this.sender = sender;
|
||||
this.query = query;
|
||||
commands.add(GOOGLE_CMD);
|
||||
properties.put(GOOGLE_API_KEY_PROP, "");
|
||||
properties.put(GOOGLE_CSE_KEY_PROP, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (isEnabled() && args.length() > 0)
|
||||
{
|
||||
if (args.length() > 0)
|
||||
{
|
||||
new Thread(() -> {
|
||||
run(bot, sender, args);
|
||||
}).start();
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches Google.
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender, final String query)
|
||||
{
|
||||
try
|
||||
{
|
||||
final String query = URLEncoder.encode(this.query, "UTF-8");
|
||||
final String q = URLEncoder.encode(query, "UTF-8");
|
||||
|
||||
final URL url =
|
||||
new URL("https://www.googleapis.com/customsearch/v1?key=" + bot.getGoogleApiKey() + "&cx=" + cseCx
|
||||
+ "&q=" + query + "&filter=1&num=5&alt=json");
|
||||
new URL("https://www.googleapis.com/customsearch/v1?key=" + properties.get(GOOGLE_API_KEY_PROP)
|
||||
+ "&cx=" + properties.get(GOOGLE_CSE_KEY_PROP) + "&q=" + q + "&filter=1&num=5&alt=json");
|
||||
final URLConnection conn = url.openConnection();
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
@ -131,4 +144,25 @@ class GoogleSearch implements Runnable
|
|||
bot.send(sender, "An error has occurred: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (isEnabled())
|
||||
{
|
||||
bot.send(sender, "To search Google:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.send(sender, "The Google searching facility is disabled.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled()
|
||||
{
|
||||
return Utils.isValidString(properties.get(GOOGLE_API_KEY_PROP)) && Utils
|
||||
.isValidString(properties.get(GOOGLE_CSE_KEY_PROP));
|
||||
}
|
||||
}
|
|
@ -29,8 +29,9 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import org.jibble.pircbot.Colors;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
@ -40,47 +41,53 @@ import java.net.URL;
|
|||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#JOKE_CMD} command.
|
||||
* The Joke module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2014-04-20
|
||||
* @since 1.0
|
||||
*/
|
||||
class Joke implements Runnable
|
||||
final public class Joke extends AbstractModule
|
||||
{
|
||||
|
||||
/**
|
||||
* The joke command.
|
||||
*/
|
||||
private static final String JOKE_CMD = "joke";
|
||||
|
||||
/**
|
||||
* The ICNDB URL.
|
||||
*/
|
||||
private static final String JOKE_URL =
|
||||
"http://api.icndb.com/jokes/random?escape=javascript&exclude=[explicit]&limitTo=[nerdy]";
|
||||
|
||||
/**
|
||||
* The bot's instance.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private final String sender;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Joke} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
*/
|
||||
public Joke(final Mobibot bot, final String sender)
|
||||
public Joke()
|
||||
{
|
||||
this.bot = bot;
|
||||
this.sender = sender;
|
||||
commands.add(JOKE_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
new Thread(() -> {
|
||||
run(bot, sender);
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To retrieve a random joke:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + JOKE_CMD));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender)
|
||||
{
|
||||
try
|
||||
{
|
|
@ -29,8 +29,9 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import org.apache.commons.net.whois.WhoisClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -38,14 +39,18 @@ import java.net.InetAddress;
|
|||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#LOOKUP_CMD} command.
|
||||
* The Lookup module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2014-04-26
|
||||
* @since 1.0
|
||||
*/
|
||||
final class Lookup
|
||||
final public class Lookup extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* THe lookup command.
|
||||
*/
|
||||
private static final String LOOKUP_CMD = "lookup";
|
||||
|
||||
/**
|
||||
* The whois host.
|
||||
|
@ -53,14 +58,78 @@ final class Lookup
|
|||
private static final String WHOIS_HOST = "whois.arin.net";
|
||||
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
* @throws UnsupportedOperationException If the constructor is called.
|
||||
* The default constructor
|
||||
*/
|
||||
private Lookup()
|
||||
throws UnsupportedOperationException
|
||||
public Lookup()
|
||||
{
|
||||
throw new UnsupportedOperationException("Illegal constructor call.");
|
||||
commands.add(LOOKUP_CMD);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a command.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender.
|
||||
* @param args The command arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (args.matches("(\\S.)+(\\S)+"))
|
||||
{
|
||||
try
|
||||
{
|
||||
bot.send(bot.getChannel(), Lookup.lookup(args));
|
||||
}
|
||||
catch (UnknownHostException ignore)
|
||||
{
|
||||
if (args.matches(
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"))
|
||||
{
|
||||
try
|
||||
{
|
||||
final String[] lines = Lookup.whois(args);
|
||||
|
||||
if ((lines != null) && (lines.length > 0))
|
||||
{
|
||||
String line;
|
||||
|
||||
for (final String rawLine : lines)
|
||||
{
|
||||
line = rawLine.trim();
|
||||
|
||||
if ((line.length() > 0) && (line.charAt(0) != '#'))
|
||||
{
|
||||
bot.send(bot.getChannel(), line);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.send(bot.getChannel(), "Unknown host.");
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
if (bot.getLogger().isDebugEnabled())
|
||||
{
|
||||
bot.getLogger().debug("Unable to perform whois IP lookup: " + args, ioe);
|
||||
}
|
||||
|
||||
bot.send(bot.getChannel(), "Unable to perform whois IP lookup: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.send(bot.getChannel(), "Unknown host.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,7 +141,7 @@ final class Lookup
|
|||
*
|
||||
* @throws java.net.UnknownHostException If the host is unknown.
|
||||
*/
|
||||
public static String lookup(final String query)
|
||||
private static String lookup(final String query)
|
||||
throws UnknownHostException
|
||||
{
|
||||
final StringBuilder buffer = new StringBuilder("");
|
||||
|
@ -116,7 +185,7 @@ final class Lookup
|
|||
*
|
||||
* @throws java.io.IOException If a connection error occurs.
|
||||
*/
|
||||
public static String[] whois(final String query)
|
||||
private static String[] whois(final String query)
|
||||
throws IOException
|
||||
{
|
||||
return whois(query, WHOIS_HOST);
|
||||
|
@ -155,4 +224,11 @@ final class Lookup
|
|||
|
||||
return lines;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To perform a DNS lookup query:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + LOOKUP_CMD + " <ip address or hostname>"));
|
||||
}
|
||||
}
|
67
src/main/java/net/thauvin/erik/mobibot/modules/Ping.java
Normal file
67
src/main/java/net/thauvin/erik/mobibot/modules/Ping.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Ping.java
|
||||
*
|
||||
* Copyright (c) 2016 Erik C. Thauvin (http://erik.thauvin.net/)
|
||||
* All rights reserved.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* The <code>Ping</code> class.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2016-07-02
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Ping extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The ping responses.
|
||||
*/
|
||||
private static final List<String> PINGS = Arrays.asList("is barely alive.",
|
||||
"is trying to stay awake.",
|
||||
"has gone fishing.",
|
||||
"is somewhere over the rainbow.",
|
||||
"has fallen and can't get up.",
|
||||
"is running. You better go chase it.",
|
||||
"has just spontaneously combusted.",
|
||||
"is talking to itself... don't interrupt. That's rude.",
|
||||
"is bartending at an AA meeting.",
|
||||
"is hibernating.",
|
||||
"is saving energy: apathetic mode activated.",
|
||||
"is busy. Go away!");
|
||||
|
||||
/**
|
||||
* The ping command.
|
||||
*/
|
||||
private static final String PING_CMD = "ping";
|
||||
|
||||
/**
|
||||
* The default constructor.
|
||||
*/
|
||||
public Ping()
|
||||
{
|
||||
commands.add(PING_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
final Random r = new Random();
|
||||
|
||||
bot.action(PINGS.get(r.nextInt(PINGS.size())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To ping the bot:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + PING_CMD));
|
||||
}
|
||||
}
|
|
@ -29,61 +29,68 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import com.Ostermiller.util.CSVParser;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#STOCK_CMD} command.
|
||||
* The StockQuote module.
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @created Feb 7, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
class StockQuote implements Runnable
|
||||
final public class StockQuote extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The quote command.
|
||||
*/
|
||||
private static final String STOCK_CMD = "stock";
|
||||
|
||||
/**
|
||||
* The Yahoo! stock quote URL.
|
||||
*/
|
||||
private static final String YAHOO_URL = "http://finance.yahoo.com/d/quotes.csv?&f=snl1d1t1c1oghv&e=.csv&s=";
|
||||
|
||||
/**
|
||||
* The bot.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private final String sender;
|
||||
|
||||
/**
|
||||
* The stock symbol.
|
||||
*/
|
||||
private final String symbol;
|
||||
|
||||
/**
|
||||
* Creates a new {@link StockQuote} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param symbol The stock symbol.
|
||||
*/
|
||||
public StockQuote(final Mobibot bot, final String sender, final String symbol)
|
||||
public StockQuote()
|
||||
{
|
||||
this.bot = bot;
|
||||
this.sender = sender;
|
||||
this.symbol = symbol;
|
||||
commands.add(STOCK_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (args.length() > 0)
|
||||
{
|
||||
new Thread(() -> {
|
||||
run(bot, sender, args);
|
||||
}).start();
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To retrieve a stock quote:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + STOCK_CMD + " <symbol[.country code]>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the specified stock quote from Yahoo!
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender, final String symbol)
|
||||
{
|
||||
try
|
||||
{
|
|
@ -29,89 +29,97 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#TWITTER_CMD} command.
|
||||
* The Twitter module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created Sept 10, 2008
|
||||
* @since 1.0
|
||||
*/
|
||||
class Twitter implements Runnable
|
||||
final public class Twitter extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The Twitter access token.
|
||||
*/
|
||||
private final String accessToken;
|
||||
private final static String CONSUMER_KEY_PROP = "twitter-consumerKey";
|
||||
|
||||
private final static String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
|
||||
|
||||
private final static String TOKEN_PROP = "twitter-token";
|
||||
|
||||
private final static String TOKEN_SECRET_PROP = "twitter-tokenSecret";
|
||||
|
||||
/**
|
||||
* The Twitter access token secret.
|
||||
* The twitter command.
|
||||
*/
|
||||
private final String accessTokenSecret;
|
||||
|
||||
/**
|
||||
* The bot.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
|
||||
/**
|
||||
* The Twitter consumer key.
|
||||
*/
|
||||
private final String consumerKey;
|
||||
|
||||
/**
|
||||
* The Twitter consumer secret.
|
||||
*/
|
||||
private final String consumerSecret;
|
||||
|
||||
/**
|
||||
* The Twitter message.
|
||||
*/
|
||||
private final String message;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private final String sender;
|
||||
private final static String TWITTER_CMD = "twitter";
|
||||
|
||||
/**
|
||||
* Creates a new {@link Twitter} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param consumerKey The Twitter consumer key.
|
||||
* @param consumerSecret The Twitter consumer secret.
|
||||
* @param accessToken The Twitter access token.
|
||||
* @param accessTokenSecret The Twitter access token secret.
|
||||
* @param message The Twitter message.
|
||||
*/
|
||||
public Twitter(final Mobibot bot, final String sender, final String consumerKey, final String consumerSecret,
|
||||
final String accessToken, final String accessTokenSecret, final String message)
|
||||
public Twitter()
|
||||
{
|
||||
this.bot = bot;
|
||||
this.consumerKey = consumerKey;
|
||||
this.consumerSecret = consumerSecret;
|
||||
this.accessToken = accessToken;
|
||||
this.accessTokenSecret = accessTokenSecret;
|
||||
this.message = message;
|
||||
this.sender = sender;
|
||||
commands.add(TWITTER_CMD);
|
||||
properties.put(CONSUMER_SECRET_PROP, "");
|
||||
properties.put(CONSUMER_KEY_PROP, "");
|
||||
properties.put(TOKEN_PROP, "");
|
||||
properties.put(TOKEN_SECRET_PROP, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled()
|
||||
{
|
||||
return Utils.isValidString(properties.get(CONSUMER_KEY_PROP)) && Utils
|
||||
.isValidString(properties.get(CONSUMER_SECRET_PROP)) && Utils.isValidString(properties.get(TOKEN_PROP))
|
||||
&& Utils.isValidString(properties.get(TOKEN_SECRET_PROP));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (isEnabled() && args.length() > 0)
|
||||
{
|
||||
new Thread(() -> {
|
||||
run(bot, sender, args);
|
||||
}).start();
|
||||
}
|
||||
else
|
||||
{
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
if (isEnabled())
|
||||
{
|
||||
bot.send(sender, "To post to Twitter:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + TWITTER_CMD + " <message>"));
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.send(sender, "The Twitter posting facility is disabled.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts to twitter.
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender, final String message)
|
||||
{
|
||||
try
|
||||
{
|
||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
|
||||
.setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);
|
||||
cb.setDebugEnabled(true).setOAuthConsumerKey(properties.get(CONSUMER_KEY_PROP))
|
||||
.setOAuthConsumerSecret(properties.get(CONSUMER_SECRET_PROP))
|
||||
.setOAuthAccessToken(properties.get(TOKEN_PROP))
|
||||
.setOAuthAccessTokenSecret(properties.get(TOKEN_SECRET_PROP));
|
||||
final TwitterFactory tf = new TwitterFactory(cb.build());
|
||||
final twitter4j.Twitter twitter = tf.getInstance();
|
||||
|
|
@ -29,48 +29,56 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#WAR_CMD} command.
|
||||
* The War module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2014-04-28
|
||||
* @since 1.0
|
||||
*/
|
||||
final class War
|
||||
final public class War extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The deck of card for the {@link net.thauvin.erik.mobibot.Commands#WAR_CMD war} command.
|
||||
* The war command.
|
||||
*/
|
||||
private static final String WAR_CMD = "war";
|
||||
|
||||
/**
|
||||
* The deck of card.
|
||||
*/
|
||||
private static final String[] WAR_DECK =
|
||||
new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
|
||||
|
||||
/**
|
||||
* The suits for the deck of card for the {@link Commands#WAR_CMD war} command.
|
||||
* The suits for the deck of card.
|
||||
*/
|
||||
private static final String[] WAR_SUITS = new String[]{"Hearts", "Spades", "Diamonds", "Clubs"};
|
||||
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
* @throws UnsupportedOperationException If the constructor is called.
|
||||
* The default constructor.
|
||||
*/
|
||||
private War()
|
||||
throws UnsupportedOperationException
|
||||
public War()
|
||||
{
|
||||
throw new UnsupportedOperationException("Illegal constructor call.");
|
||||
commands.add(WAR_CMD);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays war.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender's nickname.
|
||||
* @param sender The sender.
|
||||
* @param args The command arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
public static void play(final Mobibot bot, final String sender)
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
final Random r = new Random();
|
||||
|
||||
|
@ -102,9 +110,12 @@ final class War
|
|||
{
|
||||
bot.action("wins.");
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.action("tied.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To play war:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WAR_CMD));
|
||||
}
|
||||
}
|
|
@ -29,74 +29,75 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.sf.jweather.metar.Metar;
|
||||
import net.sf.jweather.metar.SkyCondition;
|
||||
import net.sf.jweather.metar.WeatherCondition;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Processes the {@link Commands#LOOKUP_CMD} command.
|
||||
* The Weather module
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @created Feb 7, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
class Weather implements Runnable
|
||||
final public class Weather extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The URL where the stations are listed.
|
||||
*/
|
||||
public static final String STATIONS_URL = "http://www.rap.ucar.edu/weather/surface/stations.txt";
|
||||
|
||||
/**
|
||||
* The decimal number format.
|
||||
*/
|
||||
private static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("0.##");
|
||||
|
||||
/**
|
||||
* The bot.
|
||||
* The URL where the stations are listed.
|
||||
*/
|
||||
private final Mobibot bot;
|
||||
private static final String STATIONS_URL = "http://www.rap.ucar.edu/weather/surface/stations.txt";
|
||||
|
||||
/**
|
||||
* The private message flag.
|
||||
* THe weather command.
|
||||
*/
|
||||
private final boolean isPrivate;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
*/
|
||||
private final String sender;
|
||||
|
||||
/**
|
||||
* The station ID.
|
||||
*/
|
||||
private final String station;
|
||||
private static final String WEATHER_CMD = "weather";
|
||||
|
||||
/**
|
||||
* Creates a new {@link Weather} instance.
|
||||
*
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param station The station ID.
|
||||
* @param isPrivate Set to true is the response should be send as a private message.
|
||||
*/
|
||||
public Weather(final Mobibot bot, final String sender, final String station, final boolean isPrivate)
|
||||
public Weather()
|
||||
{
|
||||
this.bot = bot;
|
||||
this.sender = sender;
|
||||
this.station = station.toUpperCase();
|
||||
this.isPrivate = isPrivate;
|
||||
commands.add(WEATHER_CMD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
new Thread(() -> {
|
||||
run(bot, sender, args.toUpperCase(), isPrivate);
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To display weather information:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " <station id>"));
|
||||
bot.send(sender, "For a listing of the ICAO station IDs, please visit: " + STATIONS_URL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrivateMsgEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the weather data from a specific station ID.
|
||||
*/
|
||||
public final void run()
|
||||
private void run(final Mobibot bot, final String sender, final String station, final boolean isPrivate)
|
||||
{
|
||||
if (station.length() == 4)
|
||||
{
|
||||
|
@ -177,6 +178,6 @@ class Weather implements Runnable
|
|||
}
|
||||
}
|
||||
|
||||
bot.helpResponse(sender, Commands.WEATHER_CMD);
|
||||
helpResponse(bot, sender, station, isPrivate);
|
||||
}
|
||||
}
|
|
@ -29,7 +29,10 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
@ -38,13 +41,13 @@ import java.util.TimeZone;
|
|||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* The {@link Commands#TIME_CMD} command.
|
||||
* The WorldTime module.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2014-04-27
|
||||
* @since 1.0
|
||||
*/
|
||||
class WorldTime
|
||||
final public class WorldTime extends AbstractModule
|
||||
{
|
||||
/**
|
||||
* The beats (Internet Time) keyword.
|
||||
|
@ -52,12 +55,17 @@ class WorldTime
|
|||
private static final String BEATS_KEYWORD = ".beats";
|
||||
|
||||
/**
|
||||
* The countries supported by the {@link net.thauvin.erik.mobibot.Commands#TIME_CMD time} command.
|
||||
* The supported countries.
|
||||
*/
|
||||
private static final Map<String, String> COUNTRIES_MAP = new TreeMap<>();
|
||||
|
||||
/**
|
||||
* The date/time format for the {@link net.thauvin.erik.mobibot.Commands#TIME_CMD time} command.
|
||||
* The time command.
|
||||
*/
|
||||
private static final String TIME_CMD = "time";
|
||||
|
||||
/**
|
||||
* The date/time format.
|
||||
*/
|
||||
private static final SimpleDateFormat TIME_SDF =
|
||||
new SimpleDateFormat("'The time is 'HH:mm' on 'EEEE, d MMMM yyyy' in '");
|
||||
|
@ -67,6 +75,8 @@ class WorldTime
|
|||
*/
|
||||
public WorldTime()
|
||||
{
|
||||
commands.add(TIME_CMD);
|
||||
|
||||
// Initialize the countries map
|
||||
COUNTRIES_MAP.put("AU", "Australia/Sydney");
|
||||
COUNTRIES_MAP.put("BE", "Europe/Brussels");
|
||||
|
@ -140,12 +150,13 @@ class WorldTime
|
|||
/**
|
||||
* Responds with the current time in the specified timezone/country.
|
||||
*
|
||||
* @param bot The bot instance.
|
||||
* @param sender The nick of the person who sent the message.
|
||||
* @param args The time command arguments.
|
||||
* @param isPrivate Set to true is the response should be send as a private message.
|
||||
* @param bot The bot's instance.
|
||||
* @param sender The sender.
|
||||
* @param args The command arguments.
|
||||
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
|
||||
*/
|
||||
public final void timeResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
boolean isInvalidTz = false;
|
||||
final String tz = (COUNTRIES_MAP.get((args.substring(args.indexOf(' ') + 1).trim().toUpperCase())));
|
||||
|
@ -167,7 +178,7 @@ class WorldTime
|
|||
else
|
||||
{
|
||||
isInvalidTz = true;
|
||||
response = "The supported time zones/countries are: " + COUNTRIES_MAP.keySet().toString();
|
||||
response = "The supported time zones are: " + COUNTRIES_MAP.keySet().toString();
|
||||
}
|
||||
|
||||
if (isPrivate)
|
||||
|
@ -186,4 +197,20 @@ class WorldTime
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate)
|
||||
{
|
||||
bot.send(sender, "To display a country's current date/time:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + TIME_CMD) + " [<country code>]");
|
||||
|
||||
bot.send(sender, "For a listing of the supported countries:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + TIME_CMD));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrivateMsgEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue