diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index f0b705b..6e3271a 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -51,7 +51,7 @@ import java.nio.charset.StandardCharsets; * @created Feb 7, 2004 * @since 1.0 */ -public final class GoogleSearch extends AbstractModule { +public final class GoogleSearch extends ThreadedModule { /** * The google command. */ @@ -74,18 +74,6 @@ public final class GoogleSearch extends AbstractModule { properties.put(GOOGLE_CSE_KEY_PROP, ""); } - /** - * {@inheritDoc} - */ - @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); - } - } - /** * {@inheritDoc} */ @@ -98,20 +86,12 @@ public final class GoogleSearch extends AbstractModule { bot.send(sender, "The Google searching facility is disabled."); } } - - /** - * {@inheritDoc} - */ - @Override - public boolean isEnabled() { - return isValidProperties(); - } - + /** * Searches Google. */ @SuppressFBWarnings(value = {"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"}) - private void run(final Mobibot bot, final String sender, final String query) { + void run(final Mobibot bot, final String sender, final String query) { try { final String q = URLEncoder.encode(query, "UTF-8"); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java b/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java new file mode 100644 index 0000000..85b3e3d --- /dev/null +++ b/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java @@ -0,0 +1,68 @@ +/* + * ThreadedModule.java + * + * Copyright (c) 2004-2019, 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; + +/** + * The ThreadedModule class. + * + * @author Erik C. Thauvin + * @created 2019-04-03 + * @since 1.0 + */ +public abstract class ThreadedModule extends AbstractModule { + /** + * {@inheritDoc} + */ + @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); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isEnabled() { + return isValidProperties(); + } + + /** + * Run the thread. + */ + abstract void run(Mobibot bot, String sender, String args); +} diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java index db7c0bb..42c70bc 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java @@ -43,7 +43,7 @@ import twitter4j.conf.ConfigurationBuilder; * @created Sept 10, 2008 * @since 1.0 */ -public final class Twitter extends AbstractModule { +public final class Twitter extends ThreadedModule { /** * The twitter command. */ @@ -66,18 +66,6 @@ public final class Twitter extends AbstractModule { properties.put(TOKEN_SECRET_PROP, ""); } - /** - * {@inheritDoc} - */ - @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); - } - } - /** * {@inheritDoc} */ @@ -91,18 +79,11 @@ public final class Twitter extends AbstractModule { } } - /** - * {@inheritDoc} - */ - @Override - public boolean isEnabled() { - return isValidProperties(); - } - /** * Posts to twitter. */ - private void run(final Mobibot bot, final String sender, final String message) { + @Override + void run(final Mobibot bot, final String sender, final String message) { try { final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true)