From 8bc04b09403458e62465b7f3597fff8d22d2c86a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 30 Apr 2020 14:51:02 -0700 Subject: [PATCH] Renamed UrlMgr to LinksMgr. --- detekt-baseline.xml | 2 +- .../net/thauvin/erik/mobibot/Mobibot.java | 45 ++++++------------- .../thauvin/erik/mobibot/commands/AddLog.kt | 4 +- .../thauvin/erik/mobibot/commands/Ignore.kt | 4 +- .../net/thauvin/erik/mobibot/commands/Info.kt | 4 +- .../erik/mobibot/commands/links/Comment.kt | 10 ++--- .../commands/links/{UrlMgr.kt => LinksMgr.kt} | 2 +- .../erik/mobibot/commands/links/Posting.kt | 28 ++++++------ .../erik/mobibot/commands/links/Tags.kt | 6 +-- .../erik/mobibot/commands/links/View.kt | 4 +- .../erik/mobibot/entries/EntryLink.java | 4 +- .../thauvin/erik/mobibot/modules/Twitter.java | 6 +-- 12 files changed, 50 insertions(+), 69 deletions(-) rename src/main/java/net/thauvin/erik/mobibot/commands/links/{UrlMgr.kt => LinksMgr.kt} (99%) diff --git a/detekt-baseline.xml b/detekt-baseline.xml index 19a2a8f..d61a8d8 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -14,6 +14,6 @@ MagicNumber:View.kt$View$8 NestedBlockDepth:Addons.kt$Addons$add NestedBlockDepth:Comment.kt$Comment$commandResponse - NestedBlockDepth:UrlMgr.kt$UrlMgr$commandResponse + NestedBlockDepth:LinksMgr.kt$LinksMgr$commandResponse diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index 5911a83..16d09b9 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -48,9 +48,9 @@ import net.thauvin.erik.mobibot.commands.Say; import net.thauvin.erik.mobibot.commands.Users; import net.thauvin.erik.mobibot.commands.Versions; import net.thauvin.erik.mobibot.commands.links.Comment; +import net.thauvin.erik.mobibot.commands.links.LinksMgr; import net.thauvin.erik.mobibot.commands.links.Posting; import net.thauvin.erik.mobibot.commands.links.Tags; -import net.thauvin.erik.mobibot.commands.links.UrlMgr; import net.thauvin.erik.mobibot.commands.links.View; import net.thauvin.erik.mobibot.commands.tell.Tell; import net.thauvin.erik.mobibot.entries.EntriesMgr; @@ -96,6 +96,7 @@ import java.nio.file.Paths; import java.util.List; import java.util.Properties; import java.util.Timer; +import java.util.regex.Pattern; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.lowerCase; @@ -184,8 +185,8 @@ public class Mobibot extends PircBot { // Load the current entries and backlogs, if any try { - UrlMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel); - LOGGER.debug("Last feed: {}", UrlMgr.getStartDate()); + LinksMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel); + LOGGER.debug("Last feed: {}", LinksMgr.getStartDate()); } catch (Exception e) { LOGGER.error("An error occurred while loading the logs.", e); } @@ -228,7 +229,7 @@ public class Mobibot extends PircBot { addons.add(new Comment(this), p); addons.add(new Posting(this), p); addons.add(new Tags(this), p); - addons.add(new UrlMgr(this), p); + addons.add(new LinksMgr(this), p); addons.add(new View(this), p); // Load the modules @@ -253,7 +254,7 @@ public class Mobibot extends PircBot { addons.sort(); // Save the entries - UrlMgr.saveEntries(this, true); + LinksMgr.saveEntries(this, true); } /** @@ -286,20 +287,20 @@ public class Mobibot extends PircBot { // Parse the command line final CommandLineParser parser = new DefaultParser(); - CommandLine line = null; + CommandLine commandLine = null; try { - line = parser.parse(options, args); + commandLine = parser.parse(options, args); } catch (ParseException e) { System.err.println("CLI Parsing failed. Reason: " + e.getMessage()); e.printStackTrace(System.err); System.exit(1); } - if (line.hasOption(Constants.HELP_ARG.charAt(0))) { + if (commandLine.hasOption(Constants.HELP_ARG.charAt(0))) { // Output the usage new HelpFormatter().printHelp(Mobibot.class.getName(), options); - } else if (line.hasOption(Constants.VERSION_ARG.charAt(0))) { + } else if (commandLine.hasOption(Constants.VERSION_ARG.charAt(0))) { for (final String s : INFO) { System.out.println(s); } @@ -307,7 +308,7 @@ public class Mobibot extends PircBot { final Properties p = new Properties(); try (final InputStream fis = Files.newInputStream( - Paths.get(line.getOptionValue(Constants.PROPS_ARG.charAt(0), "./mobibot.properties")))) { + Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG.charAt(0), "./mobibot.properties")))) { // Load the properties files p.load(fis); } catch (FileNotFoundException e) { @@ -325,7 +326,7 @@ public class Mobibot extends PircBot { final String logsDir = Utils.ensureDir(p.getProperty("logs", "."), false); // Redirect the stdout and stderr - if (!line.hasOption(Constants.DEBUG_ARG.charAt(0))) { + if (!commandLine.hasOption(Constants.DEBUG_ARG.charAt(0))) { try { final PrintStream stdout = new PrintStream( new FileOutputStream(logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true)); @@ -493,26 +494,6 @@ public class Mobibot extends PircBot { return addons.getModulesNames(); } - /** - * Returns the bot's nickname regexp pattern. - * - * @return The nickname regexp pattern. - */ - private String getNickPattern() { - final StringBuilder buff = new StringBuilder(0); - - for (final char c : getNick().toCharArray()) { - if (Character.isLetter(c)) { - buff.append('[').append(lowerCase(String.valueOf(c))).append(StringUtils.upperCase(String.valueOf(c))) - .append(']'); - } else { - buff.append(c); - } - } - - return buff.toString(); - } - /** * Returns the Tell command. * @@ -700,7 +681,7 @@ public class Mobibot extends PircBot { tell.send(sender, true); - if (message.matches(getNickPattern() + ":.*")) { // mobibot: + if (message.matches("(?i)" + Pattern.quote(getNick()) + ":.*")) { // mobibot: final String[] cmds = message.substring(message.indexOf(':') + 1).trim().split(" ", 2); final String cmd = lowerCase(cmds[0]); diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/AddLog.kt b/src/main/java/net/thauvin/erik/mobibot/commands/AddLog.kt index addface..f7639a5 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/AddLog.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/AddLog.kt @@ -33,8 +33,8 @@ package net.thauvin.erik.mobibot.commands import net.thauvin.erik.mobibot.Mobibot -import net.thauvin.erik.mobibot.commands.links.UrlMgr.Companion.addHistory -import net.thauvin.erik.mobibot.commands.links.UrlMgr.Companion.getHistory +import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.addHistory +import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.getHistory import net.thauvin.erik.mobibot.entries.EntriesMgr import java.io.File diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt b/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt index 2999272..de8d58b 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.commands import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Utils -import net.thauvin.erik.mobibot.commands.links.UrlMgr +import net.thauvin.erik.mobibot.commands.links.LinksMgr import java.util.* class Ignore(bot: Mobibot) : AbstractCommand(bot) { @@ -150,7 +150,7 @@ class Ignore(bot: Mobibot) : AbstractCommand(bot) { override fun setProperty(key: String, value: String) { super.setProperty(key, value) if (IGNORE_PROP == key) { - ignored.addAll(value.split(UrlMgr.LINK_MATCH.toRegex())) + ignored.addAll(value.split(LinksMgr.LINK_MATCH.toRegex())) } } diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Info.kt b/src/main/java/net/thauvin/erik/mobibot/commands/Info.kt index fc6e546..1698028 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Info.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Info.kt @@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.commands import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Utils -import net.thauvin.erik.mobibot.commands.links.UrlMgr +import net.thauvin.erik.mobibot.commands.links.LinksMgr import java.lang.management.ManagementFactory class Info(bot: Mobibot) : AbstractCommand(bot) { @@ -63,7 +63,7 @@ class Info(bot: Mobibot) : AbstractCommand(bot) { with(info) { append(Utils.uptime(ManagementFactory.getRuntimeMXBean().uptime)) append(" [Entries: ") - append(UrlMgr.entriesCount) + append(LinksMgr.entriesCount) if (isOp) { if (bot.tell.isEnabled()) { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/Comment.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/Comment.kt index 2ecba68..63f5ffa 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/Comment.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/Comment.kt @@ -68,8 +68,8 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) { val cmds = args.substring(1).split("[.:]".toRegex(), 3) val index = cmds[0].toInt() - 1 - if (index < UrlMgr.entriesCount) { - val entry: EntryLink = UrlMgr.getEntry(index) + if (index < LinksMgr.entriesCount) { + val entry: EntryLink = LinksMgr.getEntry(index) val commentIndex = cmds[1].toInt() - 1 if (commentIndex < entry.commentsCount) { when (val cmd = cmds[2].trim()) { @@ -124,7 +124,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) { val comment = entry.getComment(commentIndex) comment.nick = cmd.substring(1) bot.send(EntriesUtils.buildComment(index, commentIndex, comment)) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } else { bot.send(sender, "Please ask a channel op to change the author of this comment for you.", false) } @@ -141,7 +141,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) { if (isOp || sender == entry.getComment(commentIndex).nick) { entry.deleteComment(commentIndex) bot.send("Comment ${Constants.LINK_CMD}${index + 1}.${commentIndex + 1} removed.") - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } else { bot.send(sender, "Please ask a channel op to delete this comment for you.", false) } @@ -151,7 +151,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) { entry.setComment(commentIndex, cmd, sender) val comment = entry.getComment(commentIndex) bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } private fun showComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt similarity index 99% rename from src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt rename to src/main/java/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt index 5a914a4..f4213d4 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt @@ -43,7 +43,7 @@ import net.thauvin.erik.mobibot.entries.EntryLink import org.jsoup.Jsoup import java.io.IOException -class UrlMgr(bot: Mobibot) : AbstractCommand(bot) { +class LinksMgr(bot: Mobibot) : AbstractCommand(bot) { private val keywords: MutableList = ArrayList() private val defaultTags: MutableList = ArrayList() diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/Posting.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/Posting.kt index 42e7e4d..26a74ce 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/Posting.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/Posting.kt @@ -67,7 +67,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) { val cmds = args.substring(1).split(":", limit = 2) val index = cmds[0].toInt() - 1 - if (index < UrlMgr.entriesCount) { + if (index < LinksMgr.entriesCount) { when (val cmd = cmds[1].trim()) { "" -> showEntry(index) "-" -> removeEntry(sender, login, isOp, index) // L1:- @@ -88,33 +88,33 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) { } private fun addComment(cmd: String, sender: String, index: Int) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) val commentIndex = entry.addComment(cmd, sender) val comment = entry.getComment(commentIndex) bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } private fun changeTitle(cmd: String, index: Int) { if (cmd.length > 1) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) entry.title = cmd.substring(1).trim() bot.updatePin(entry.link, entry) bot.send(EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } } private fun changeUrl(cmd: String, login: String, isOp: Boolean, index: Int) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) if (entry.login == login || isOp) { val link = cmd.substring(1) - if (link.matches(UrlMgr.LINK_MATCH.toRegex())) { + if (link.matches(LinksMgr.LINK_MATCH.toRegex())) { val oldLink = entry.link entry.link = link bot.updatePin(oldLink, entry) bot.send(EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } } } @@ -122,10 +122,10 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) { private fun changeAuthor(cmd: String, sender: String, isOp: Boolean, index: Int) { if (isOp) { if (cmd.length > 1) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) entry.nick = cmd.substring(1) bot.send(EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } } else { bot.send(sender, "Please ask a channel op to change the author of this link for you.", false) @@ -133,19 +133,19 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) { } private fun removeEntry(sender: String, login: String, isOp: Boolean, index: Int) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) if (entry.login == login || isOp) { bot.deletePin(index, entry) - UrlMgr.removeEntry(index) + LinksMgr.removeEntry(index) bot.send("Entry ${Constants.LINK_CMD}${index + 1} removed.") - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } else { bot.send(sender, "Please ask a channel op to remove this entry for you.", false) } } private fun showEntry(index: Int) { - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) bot.send(EntriesUtils.buildLink(index, entry)) if (entry.hasTags()) { bot.send(EntriesUtils.buildTags(index, entry)) diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/Tags.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/Tags.kt index 511ca46..a313b01 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/Tags.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/Tags.kt @@ -63,15 +63,15 @@ class Tags(bot: Mobibot) : AbstractCommand(bot) { val cmds = args.substring(1).split("T:", limit = 2) val index = cmds[0].toInt() - 1 - if (index < UrlMgr.entriesCount) { + if (index < LinksMgr.entriesCount) { val cmd = cmds[1].trim() - val entry: EntryLink = UrlMgr.getEntry(index) + val entry: EntryLink = LinksMgr.getEntry(index) if (cmd.isNotEmpty()) { if (entry.login == login || isOp) { entry.setTags(cmd) bot.updatePin(entry.link, entry) bot.send(EntriesUtils.buildTags(index, entry)) - UrlMgr.saveEntries(bot, false) + LinksMgr.saveEntries(bot, false) } else { bot.send(sender, "Please ask a channel op to change the tags for you.",isPrivate) } diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/View.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/View.kt index 329b190..98dd423 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/View.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/View.kt @@ -35,8 +35,8 @@ package net.thauvin.erik.mobibot.commands.links import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.commands.AbstractCommand -import net.thauvin.erik.mobibot.commands.links.UrlMgr.Companion.entriesCount -import net.thauvin.erik.mobibot.commands.links.UrlMgr.Companion.getEntry +import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entriesCount +import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.getEntry import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntryLink diff --git a/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java b/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java index aaa09cf..ba9a3f5 100644 --- a/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java +++ b/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java @@ -35,7 +35,7 @@ package net.thauvin.erik.mobibot.entries; import com.rometools.rome.feed.synd.SyndCategory; import com.rometools.rome.feed.synd.SyndCategoryImpl; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import net.thauvin.erik.mobibot.commands.links.UrlMgr; +import net.thauvin.erik.mobibot.commands.links.LinksMgr; import org.apache.commons.lang3.StringUtils; import java.io.Serializable; @@ -347,7 +347,7 @@ public class EntryLink implements Serializable { * @param tags The space-delimited tags. */ public final void setTags(final String tags) { - setTags(Arrays.asList(tags.split(UrlMgr.TAG_MATCH))); + setTags(Arrays.asList(tags.split(LinksMgr.TAG_MATCH))); } /** 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 5f47ef9..7264694 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java @@ -38,7 +38,7 @@ import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.ReleaseInfo; import net.thauvin.erik.mobibot.TwitterTimer; import net.thauvin.erik.mobibot.Utils; -import net.thauvin.erik.mobibot.commands.links.UrlMgr; +import net.thauvin.erik.mobibot.commands.links.LinksMgr; import net.thauvin.erik.mobibot.entries.EntryLink; import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.NoticeMessage; @@ -221,8 +221,8 @@ public final class Twitter extends ThreadedModule { */ @SuppressFBWarnings("SUI_CONTAINS_BEFORE_REMOVE") public final void postEntry(final int index) { - if (isAutoPost() && hasEntry(index) && UrlMgr.getEntriesCount() >= index) { - final EntryLink entry = UrlMgr.getEntry(index); + if (isAutoPost() && hasEntry(index) && LinksMgr.getEntriesCount() >= index) { + final EntryLink entry = LinksMgr.getEntry(index); final String msg = entry.getTitle() + ' ' + entry.getLink() + " via " + entry.getNick() + " on " + bot.getChannel(); new Thread(() -> {