From fbe36d50b7ada558ca0f8ff0145755e94d803fe1 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 29 Mar 2020 00:44:10 -0700 Subject: [PATCH] Removed complexity (detekt) --- detekt-baseline.xml | 14 +- .../net/thauvin/erik/mobibot/ReleaseInfo.java | 6 +- .../net/thauvin/erik/mobibot/Mobibot.java | 2 +- .../erik/mobibot/commands/AbstractCommand.kt | 1 - .../thauvin/erik/mobibot/commands/Ignore.kt | 96 +++++++---- .../erik/mobibot/commands/links/Comment.kt | 74 +++++---- .../erik/mobibot/commands/links/Posting.kt | 149 +++++++++-------- .../erik/mobibot/commands/links/UrlMgr.kt | 154 +++++++++--------- .../erik/mobibot/commands/links/View.kt | 97 ++++++----- version.properties | 6 +- 10 files changed, 328 insertions(+), 271 deletions(-) diff --git a/detekt-baseline.xml b/detekt-baseline.xml index 51a20bd..4e8f359 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -2,25 +2,15 @@ - ComplexCondition:UrlMgr.kt$UrlMgr$Ignore.isNotIgnored(sender) && (cmds.size == 1 || !cmds[1].contains(bot.nick) || !cmds[1].endsWith(" (${Ignore.IGNORE_CMD}")) - ComplexMethod:Posting.kt$Posting$commandResponse - ComplexMethod:UrlMgr.kt$UrlMgr$commandResponse - ComplexMethod:View.kt$View$commandResponse - LongMethod:Posting.kt$Posting$commandResponse - LongMethod:UrlMgr.kt$UrlMgr$commandResponse - LongMethod:View.kt$View$commandResponse LongParameterList:AbstractCommand.kt$AbstractCommand$( bot: Mobibot, sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean ) - LoopWithTooManyJumpStatements:View.kt$View$while (i < max) { entry = getEntry(i) if (lcArgs.isNotEmpty()) { if (entry.link.toLowerCase().contains(lcArgs) || entry.title.toLowerCase().contains(lcArgs) || entry.nick.toLowerCase().contains(lcArgs)) { if (sent > maxEntries) { bot.send( sender, "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1} $lcArgs"), isPrivate ) break } bot.send(sender, EntriesUtils.buildLink(i, entry, true), isPrivate) sent++ } } else { if (sent > maxEntries) { bot.send( sender, "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1}"), isPrivate ) break } bot.send(sender, EntriesUtils.buildLink(i, entry, true), isPrivate) sent++ } i++ } + LongParameterList:Comment.kt$Comment$( bot: Mobibot, cmd: String, sender: String, isOp: Boolean, entry: EntryLink, index: Int, commentIndex: Int ) + LongParameterList:Comment.kt$Comment$(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int) MagicNumber:Comment.kt$Comment$3 MagicNumber:Cycle.kt$Cycle$10 MagicNumber:Recap.kt$Recap.Companion$10 MagicNumber:UrlMgr.kt$UrlMgr$1000L MagicNumber:UrlMgr.kt$UrlMgr$60L MagicNumber:View.kt$View$8 - NestedBlockDepth:Comment.kt$Comment$commandResponse - NestedBlockDepth:Ignore.kt$Ignore$commandResponse - NestedBlockDepth:Posting.kt$Posting$commandResponse NestedBlockDepth:UrlMgr.kt$UrlMgr$commandResponse - NestedBlockDepth:View.kt$View$commandResponse diff --git a/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java b/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java index 3421c5d..00d806e 100644 --- a/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java +++ b/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java @@ -14,13 +14,13 @@ import java.time.*; public final class ReleaseInfo { public static final String PROJECT = "mobibot"; public static final LocalDateTime BUILDDATE = - LocalDateTime.ofInstant(Instant.ofEpochMilli(1585416159853L), ZoneId.systemDefault()); + LocalDateTime.ofInstant(Instant.ofEpochMilli(1585467573570L), ZoneId.systemDefault()); public static final int MAJOR = 0; public static final int MINOR = 8; public static final int PATCH = 0; public static final String PRERELEASE = "alpha"; - public static final String BUILDMETA = "045"; - public static final String VERSION = "0.8.0-alpha+045"; + public static final String BUILDMETA = "105"; + public static final String VERSION = "0.8.0-alpha+105"; /** * Disables the default constructor. diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index db20fd4..0afd2b0 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -212,7 +212,7 @@ public class Mobibot extends PircBot { UrlMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel); if (logger.isDebugEnabled()) { - logger.debug("Last feed: {}", UrlMgr.getToday()); + logger.debug("Last feed: {}", UrlMgr.getStartDate()); } } catch (Exception e) { if (logger.isErrorEnabled()) { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/AbstractCommand.kt b/src/main/java/net/thauvin/erik/mobibot/commands/AbstractCommand.kt index 9d1a0cb..4eeaaa8 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/AbstractCommand.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/AbstractCommand.kt @@ -63,5 +63,4 @@ abstract class AbstractCommand { open fun matches(message: String): Boolean { return false } - } 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 db3ab27..d24ccd3 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -37,7 +37,7 @@ import net.thauvin.erik.mobibot.Utils import java.util.* class Ignore(defaultIgnore: String) : AbstractCommand() { - private val keyword = "me" + private val me = "me" init { if (defaultIgnore.isNotBlank()) { @@ -47,11 +47,20 @@ class Ignore(defaultIgnore: String) : AbstractCommand() { override val command = IGNORE_CMD override val help = listOf( + Utils.bold("To ignore a link posted to the channel:"), + Utils.helpIndent("https://www.foo.bar %s"), Utils.bold("To check your ignore status:"), Utils.helpIndent("%s: $command"), Utils.bold("To toggle your ignore status:"), - Utils.helpIndent("%s: $command $keyword") + Utils.helpIndent("%s: $command $me") ) + private val helpOp = listOf( + Utils.bold("To ignore a link posted to the channel:"), + Utils.helpIndent("https://www.foo.bar %s"), + Utils.bold("To add/remove nicks from the ignored list:"), + Utils.helpIndent("/msg %s $command |$me [ ...]") + ) + override val isOp = false override val isPublic = true override val isVisible = true @@ -74,38 +83,69 @@ class Ignore(defaultIgnore: String) : AbstractCommand() { isOp: Boolean, isPrivate: Boolean ) { - if (isOp) { + if (!isOp) { val nick = sender.toLowerCase() - val isMe = args.toLowerCase().startsWith(keyword) - if (isMe) { - if (ignored.remove(nick)) { - bot.send(sender, "You are no longer ignored.") - } else { - ignored.add(nick) - bot.send(sender, "You are now ignored.") - } + val isMe = args.toLowerCase().startsWith(me) + ignoreNick(bot, nick, isMe) + } else { + ignoreOp(bot, sender, args) + } + } + + override fun helpResponse( + bot: Mobibot, + command: String, + sender: String, + isOp: Boolean, + isPrivate: Boolean + ): Boolean { + return if (isOp) { + for (h in helpOp) { + bot.send(sender, String.format(h, bot.nick)) + } + true + } else { + super.helpResponse(bot, command, sender, isOp, isPrivate) + } + } + + private fun ignoreNick(bot: Mobibot, sender: String, isMe: Boolean) { + if (isMe) { + if (ignored.remove(sender)) { + bot.send(sender, "You are no longer ignored.") } else { - if (ignored.contains(nick)) { - bot.send(sender, "You are currently ignored.") - } else { - bot.send(sender, "You are not currently ignored.") - } + ignored.add(sender) + bot.send(sender, "You are now ignored.") } } else { - if (args.isNotEmpty()) { - val nicks = args.toLowerCase().split(" ") - for (nick in nicks) { - val ignore = if (keyword == nick) { - sender.toLowerCase() - } else { - nick - } - if (!ignored.remove(ignore)) { - ignored.add(ignore) - } + if (ignored.contains(sender)) { + bot.send(sender, "You are currently ignored.") + } else { + bot.send(sender, "You are not currently ignored.") + } + } + } + + private fun ignoreOp(bot: Mobibot, sender: String, args: String) { + if (args.isNotEmpty()) { + val nicks = args.toLowerCase().split(" ") + for (nick in nicks) { + val ignore = if (me == nick) { + nick.toLowerCase() + } else { + nick + } + if (!ignored.remove(ignore)) { + ignored.add(ignore) } } - bot.send(sender, "The following nicks are ignored: ${ignored.joinToString(", ")}") + } + + if (ignored.size > 0) { + bot.send(sender, Utils.bold("The following nicks are ignored:")) + bot.sendCommandsList(sender, ignored.toList(), false) + } else { + bot.send(sender, "No one is currently ${Utils.bold("ignored")}.") } } } 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 6f8a192..435de45 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 @@ -71,37 +71,13 @@ class Comment : AbstractCommand() { if (index < UrlMgr.entriesCount) { val entry: EntryLink = UrlMgr.getEntry(index) - val cindex = cmds[1].toInt() - 1 - if (cindex < entry.commentsCount) { - val cmd = cmds[2].trim() - - // L1.1: - if (cmd.isEmpty()) { - val comment = entry.getComment(cindex) - bot.send(bot.channel, EntriesUtils.buildComment(index, cindex, comment)) - } else if ("-" == cmd) { // L11:- - entry.deleteComment(cindex) - bot.send( - bot.channel, - "Comment ${Constants.LINK_CMD}${index + 1}.${cindex + 1} removed." - ) - UrlMgr.saveEntries(bot, false) - } else if (cmd[0] == '?') { // L1.1:? - if (isOp) { - if (cmd.length > 1) { - val comment = entry.getComment(cindex) - comment.nick = cmd.substring(1) - bot.send(bot.channel, EntriesUtils.buildComment(index, cindex, comment)) - UrlMgr.saveEntries(bot, false) - } - } else { - bot.send(sender, "Please ask a channel op to change the author of this comment for you.") - } - } else { - entry.setComment(cindex, cmd, sender) - val comment = entry.getComment(cindex) - bot.send(sender, EntriesUtils.buildComment(index, cindex, comment)) - UrlMgr.saveEntries(bot, false) + val commentIndex = cmds[1].toInt() - 1 + if (commentIndex < entry.commentsCount) { + when (val cmd = cmds[2].trim()) { + "" -> showComment(bot, entry, index, commentIndex) // L1.1: + "-" -> deleteComment(bot, entry, index, commentIndex) // L11:- + "?" -> changeAuthor(bot, cmd, sender, isOp, entry, index, commentIndex) // L1.1:? + else -> setComment(bot, cmd, sender, entry, index, commentIndex) } } } @@ -127,4 +103,40 @@ class Comment : AbstractCommand() { override fun matches(message: String): Boolean { return message.matches("^${Constants.LINK_CMD}[0-9]+\\.[0-9]+:.*".toRegex()) } + + private fun changeAuthor( + bot: Mobibot, + cmd: String, + sender: String, + isOp: Boolean, + entry: EntryLink, + index: Int, + commentIndex: Int + ) { + if (isOp && cmd.length > 1) { + val comment = entry.getComment(commentIndex) + comment.nick = cmd.substring(1) + bot.send(bot.channel, EntriesUtils.buildComment(index, commentIndex, comment)) + UrlMgr.saveEntries(bot, false) + } else { + bot.send(sender, "Please ask a channel op to change the author of this comment for you.") + } + } + + private fun deleteComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) { + entry.deleteComment(commentIndex) + bot.send(bot.channel, "Comment ${Constants.LINK_CMD}${index + 1}.${commentIndex + 1} removed.") + UrlMgr.saveEntries(bot, false) + } + + private fun setComment(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int) { + entry.setComment(commentIndex, cmd, sender) + val comment = entry.getComment(commentIndex) + bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment)) + UrlMgr.saveEntries(bot, false) + } + + private fun showComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) { + bot.send(bot.channel, EntriesUtils.buildComment(index, commentIndex, entry.getComment(commentIndex))) + } } 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 6d05a0e..7b9fed5 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 @@ -69,74 +69,17 @@ class Posting : AbstractCommand() { val index = cmds[0].toInt() - 1 if (index < UrlMgr.entriesCount) { - val cmd = cmds[1].trim() - if (cmd.isEmpty()) { - val entry: EntryLink = UrlMgr.getEntry(index) - bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) - if (entry.hasTags()) { - bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) - } - if (entry.hasComments()) { - val comments = entry.comments - for (i in comments.indices) { - bot.send(bot.channel, EntriesUtils.buildComment(index, i, comments[i])) + when (val cmd = cmds[1].trim()) { + "" -> showEntry(bot, index) + "-" -> removeEntry(bot, sender, login, isOp, index) // L1:- + else -> { + when (cmd[0]) { + '|' -> changeTitle(bot, cmd, index) // L1:| + '=' -> changeUrl(bot, cmd, login, isOp, index) // L1:=<url> + '?' -> changeAuthor(bot, cmd, sender, isOp, index) // L1:?<author> + else -> addComment(bot, cmd, sender, index) } } - } else { - // L1:- - if ("-" == cmd) { - val entry: EntryLink = UrlMgr.getEntry(index) - if (entry.login == login || isOp) { - bot.deletePin(entry) - if (bot.isTwitterAutoPost) { - bot.twitterRemoveEntry(index) - } - UrlMgr.removeEntry(index) - bot.send(bot.channel, "Entry ${Constants.LINK_CMD}${index + 1} removed.") - UrlMgr.saveEntries(bot, false) - } else { - bot.send(sender, "Please ask a channel op to remove this entry for you.") - } - } else if (cmd[0] == '|') { // L1:|<title> - if (cmd.length > 1) { - val entry: EntryLink = UrlMgr.getEntry(index) - entry.title = cmd.substring(1).trim() - bot.updatePin(entry.link, entry) - bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) - } - } else if (cmd[0] == '=') { // L1:=<url> - val entry: EntryLink = UrlMgr.getEntry(index) - if (entry.login == login || isOp) { - val link = cmd.substring(1) - if (link.matches(UrlMgr.LINK_MATCH.toRegex())) { - val oldLink = entry.link - entry.link = link - bot.updatePin(oldLink, entry) - bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) - } - } else { - bot.send(sender, "Please ask channel op to change this link for you.") - } - } else if (cmd[0] == '?') { // L1:?<author> - if (isOp) { - if (cmd.length > 1) { - val entry: EntryLink = UrlMgr.getEntry(index) - entry.nick = cmd.substring(1) - bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) - UrlMgr.saveEntries(bot, false) - } - } else { - bot.send(sender, "Please ask a channel op to change the author of this link for you.") - } - } else { - val entry: EntryLink = UrlMgr.getEntry(index) - val cindex = entry.addComment(cmd, sender) - val comment = entry.getComment(cindex) - bot.send(sender, EntriesUtils.buildComment(index, cindex, comment)) - UrlMgr.saveEntries(bot, false) - } } } } @@ -144,4 +87,78 @@ class Posting : AbstractCommand() { override fun matches(message: String): Boolean { return message.matches("${Constants.LINK_CMD}[0-9]+:.*".toRegex()) } + + private fun addComment(bot: Mobibot, cmd: String, sender: String, index: Int) { + val entry: EntryLink = UrlMgr.getEntry(index) + val commentIndex = entry.addComment(cmd, sender) + val comment = entry.getComment(commentIndex) + bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment)) + UrlMgr.saveEntries(bot, false) + } + + private fun changeTitle(bot: Mobibot, cmd: String, index: Int) { + if (cmd.length > 1) { + val entry: EntryLink = UrlMgr.getEntry(index) + entry.title = cmd.substring(1).trim() + bot.updatePin(entry.link, entry) + bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) + UrlMgr.saveEntries(bot, false) + } + } + + private fun changeUrl(bot: Mobibot, cmd: String, login: String, isOp: Boolean, index: Int) { + val entry: EntryLink = UrlMgr.getEntry(index) + if (entry.login == login || isOp) { + val link = cmd.substring(1) + if (link.matches(UrlMgr.LINK_MATCH.toRegex())) { + val oldLink = entry.link + entry.link = link + bot.updatePin(oldLink, entry) + bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) + UrlMgr.saveEntries(bot, false) + } + } + } + + private fun changeAuthor(bot: Mobibot, cmd: String, sender: String, isOp: Boolean, index: Int) { + if (isOp) { + if (cmd.length > 1) { + val entry: EntryLink = UrlMgr.getEntry(index) + entry.nick = cmd.substring(1) + bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) + UrlMgr.saveEntries(bot, false) + } + } else { + bot.send(sender, "Please ask a channel op to change the author of this link for you.") + } + } + + private fun removeEntry(bot: Mobibot, sender: String, login: String, isOp: Boolean, index: Int) { + val entry: EntryLink = UrlMgr.getEntry(index) + if (entry.login == login || isOp) { + bot.deletePin(entry) + if (bot.isTwitterAutoPost) { + bot.twitterRemoveEntry(index) + } + UrlMgr.removeEntry(index) + bot.send(bot.channel, "Entry ${Constants.LINK_CMD}${index + 1} removed.") + UrlMgr.saveEntries(bot, false) + } else { + bot.send(sender, "Please ask a channel op to remove this entry for you.") + } + } + + private fun showEntry(bot: Mobibot, index: Int) { + val entry: EntryLink = UrlMgr.getEntry(index) + bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) + if (entry.hasTags()) { + bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) + } + if (entry.hasComments()) { + val comments = entry.comments + for (i in comments.indices) { + bot.send(bot.channel, EntriesUtils.buildComment(index, i, comments[i])) + } + } + } } diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt index 02afc30..89519ef 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt @@ -32,7 +32,6 @@ package net.thauvin.erik.mobibot.commands.links -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.TwitterTimer @@ -44,7 +43,6 @@ import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntryLink import org.jsoup.Jsoup import java.io.IOException -import java.util.* class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { private val tagsKeywords = ArrayList<String>() @@ -74,7 +72,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { get() = entries.size @JvmStatic - var today: String = Utils.today() + var startDate: String = Utils.today() private set @JvmStatic @@ -109,16 +107,15 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { @JvmStatic fun startup(current: String, backlogs: String, channel: String) { - today = EntriesMgr.loadEntries(current, channel, entries) - if (Utils.today() != today) { + startDate = EntriesMgr.loadEntries(current, channel, entries) + if (Utils.today() != startDate) { this.entries.clear() - today = Utils.today() + startDate = Utils.today() } EntriesMgr.loadBacklogs(backlogs, history) } } - @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") override fun commandResponse( bot: Mobibot, sender: String, @@ -129,56 +126,22 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { ) { val cmds = args.split(" ".toRegex(), 2) - if (Ignore.isNotIgnored(sender) && (cmds.size == 1 - || !cmds[1].contains(bot.nick) - || !cmds[1].endsWith(" (${Ignore.IGNORE_CMD}"))) { + if (Ignore.isNotIgnored(sender) && (cmds.size == 1 || !cmds[1].contains(bot.nick))) { val link = cmds[0].trim() - var isBackup = false - val dupIndex: Int = findDupEntry(link) - if (dupIndex == -1) { - if (Utils.today() != today) { - isBackup = true - saveEntries(bot, true) - entries.clear() - today = Utils.today() - } + if (!isDupEntry(bot, sender, link)) { + val isBackup = saveDayBackup(bot) val tags: StringBuilder = StringBuilder(defaultTags) var title = Constants.NO_TITLE if (cmds.size == 2) { val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2) - if (data.size == 1) { - title = data[0].trim() - } else { - if (data[0].isNotBlank()) { - title = data[0].trim() - } + title = data[0].trim() + if (data.size > 1) { tags.append(' ').append(data[1].trim()) } } - if (Constants.NO_TITLE == title) { - try { - val html = Jsoup.connect(link).userAgent("Mozilla").get() - val htmlTitle = html.title() - if (htmlTitle.isNotBlank()) { - val split = htmlTitle.split("( \\| )".toRegex(), 2) - title = if (split.size == 2) { - split[0] - } else { - htmlTitle - } - } - } catch (ignore: IOException) { - // Do nothing - } - } - if (tagsKeywords.isNotEmpty()) { - for (match in tagsKeywords) { - val m = match.trim() - if (title.matches("(?i).*\\b$m\\b.*".toRegex())) { - tags.append(' ').append(m) - } - } - } + tags.append(matchTagKeywords(title)) + title = fetchTitle(link, title) + entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString())) val index: Int = entries.size - 1 val entry: EntryLink = entries[index] @@ -188,13 +151,10 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { bot.addPin(entry) // Queue link for posting to twitter - if (bot.isTwitterAutoPost) { - bot.twitterAddEntry(index) - Mobibot.timer.schedule( - TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L - ) - } + twitterPost(bot, index) + saveEntries(bot, isBackup) + if (Constants.NO_TITLE == entry.title) { bot.send(sender, Utils.bold("Please specify a title, by typing:")) bot.send( @@ -202,11 +162,6 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { Utils.helpIndent(Constants.LINK_CMD + (index + 1) + ":|This is the title") ) } - } else { - val entry: EntryLink = entries[dupIndex] - bot.send( - sender, Utils.bold("Duplicate") + " >> " + EntriesUtils.buildLink(dupIndex, entry) - ) } } } @@ -219,24 +174,69 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { isPrivate: Boolean ): Boolean = false - /** - * Returns the index of the specified duplicate entry, if any. - * - * @param link The link. - * @return The index or -1 if none. - */ - private fun findDupEntry(link: String): Int { - synchronized(entries) { - for (i in entries.indices) { - if (link == entries[i].link) { - return i - } - } - } - return -1 - } - override fun matches(message: String): Boolean { return message.matches(LINK_MATCH.toRegex()) } + + private fun fetchTitle(link: String, title: String): String { + if (Constants.NO_TITLE == title) { + try { + val html = Jsoup.connect(link).userAgent("Mozilla").get() + val htmlTitle = html.title() + val split = htmlTitle.split("( \\| )".toRegex(), 2) + return if (split.size == 2) { + split[0] + } else { + htmlTitle + } + } catch (ignore: IOException) { + // Do nothing + } + } + return title + } + + private fun isDupEntry(bot: Mobibot, sender: String, link: String): Boolean { + synchronized(entries) { + for (i in entries.indices) { + if (link == entries[i].link) { + val entry: EntryLink = entries[i] + bot.send(sender, Utils.bold("Duplicate") + " >> " + EntriesUtils.buildLink(i, entry)) + return true + } + } + } + return false + } + + private fun matchTagKeywords(title: String): String { + val matches = ArrayList<String>() + if (tagsKeywords.isNotEmpty()) { + for (match in tagsKeywords) { + val m = match.trim() + if (title.matches("(?i).*\\b$m\\b.*".toRegex())) { + matches.add(m) + } + } + } + return matches.joinToString(" ") + } + + private fun saveDayBackup(bot: Mobibot): Boolean { + if (Utils.today() != startDate) { + saveEntries(bot, true) + entries.clear() + startDate = Utils.today() + return true + } + + return false + } + + private fun twitterPost(bot: Mobibot, index: Int) { + if (bot.isTwitterAutoPost) { + bot.twitterAddEntry(index) + Mobibot.timer.schedule(TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L) + } + } } 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 628ace6..1bf601a 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 @@ -65,57 +65,56 @@ class View : AbstractCommand() { isPrivate: Boolean ) { if (entriesCount != 0) { - val max = entriesCount - var lcArgs = args.toLowerCase(Constants.LOCALE) - var i = 0 - if (lcArgs.isEmpty() && max > maxEntries) { - i = max - maxEntries - } - if (lcArgs.matches("^\\d+(| .*)".toRegex())) { - val split = lcArgs.split(" ", limit = 2) - try { - i = split[0].toInt() - if (i > 0) { - i-- - } - lcArgs = if (split.size == 2) { - split[1].trim() - } else { - "" - } - if (i > max) { - i = 0 - } - } catch (ignore: NumberFormatException) { - // Do nothing - } - } - - var entry: EntryLink - var sent = 0 - while (i < max && sent < maxEntries) { - entry = getEntry(i) - if (lcArgs.isNotEmpty()) { - if (entry.link.toLowerCase().contains(lcArgs) - || entry.title.toLowerCase().contains(lcArgs) - || entry.nick.toLowerCase().contains(lcArgs)) { - bot.send(sender, EntriesUtils.buildLink(i, entry, true)) - sent++ - } - } else { - bot.send(sender, EntriesUtils.buildLink(i, entry, true)) - sent++ - } - i++ - if (sent == maxEntries) { - bot.send( - sender, - "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1} $lcArgs") - ) - } - } + showPosts(bot, args, sender) } else { bot.send(sender, "There is currently nothing to view. Why don't you post something?") } } + + private fun showPosts(bot: Mobibot, args: String, sender: String) { + val max = entriesCount + var lcArgs = args.toLowerCase(Constants.LOCALE) + var i = 0 + if (lcArgs.isEmpty() && max > maxEntries) { + i = max - maxEntries + } + if (lcArgs.matches("^\\d+(| .*)".toRegex())) { + val split = lcArgs.split(" ", limit = 2) + try { + i = split[0].toInt() - 1 + lcArgs = if (split.size == 2) { + split[1].trim() + } else { + "" + } + if (i > max) { + i = 0 + } + } catch (ignore: NumberFormatException) { + // Do nothing + } + } + + var entry: EntryLink + var sent = 0 + while (i < max && sent < maxEntries) { + entry = getEntry(i) + if (lcArgs.isNotBlank()) { + if (entry.matches(lcArgs)) { + bot.send(sender, EntriesUtils.buildLink(i, entry, true)) + sent++ + } + } else { + bot.send(sender, EntriesUtils.buildLink(i, entry, true)) + sent++ + } + i++ + if (sent == maxEntries && i < max) { + bot.send( + sender, "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1} $lcArgs") + ) + } + } + + } } diff --git a/version.properties b/version.properties index 30605d4..3f02b34 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Sat Mar 28 10:22:37 PDT 2020 -version.buildmeta=045 +#Sun Mar 29 00:39:32 PDT 2020 +version.buildmeta=105 version.major=0 version.minor=8 version.patch=0 version.prerelease=alpha version.project=mobibot -version.semver=0.8.0-alpha+045 +version.semver=0.8.0-alpha+105