Removed complexity (detekt)

This commit is contained in:
Erik C. Thauvin 2020-03-29 00:44:10 -07:00
parent 02fae4d8e4
commit fbe36d50b7
10 changed files with 328 additions and 271 deletions

View file

@ -2,25 +2,15 @@
<SmellBaseline> <SmellBaseline>
<Blacklist></Blacklist> <Blacklist></Blacklist>
<Whitelist> <Whitelist>
<ID>ComplexCondition:UrlMgr.kt$UrlMgr$Ignore.isNotIgnored(sender) &amp;&amp; (cmds.size == 1 || !cmds[1].contains(bot.nick) || !cmds[1].endsWith(" (${Ignore.IGNORE_CMD}"))</ID>
<ID>ComplexMethod:Posting.kt$Posting$commandResponse</ID>
<ID>ComplexMethod:UrlMgr.kt$UrlMgr$commandResponse</ID>
<ID>ComplexMethod:View.kt$View$commandResponse</ID>
<ID>LongMethod:Posting.kt$Posting$commandResponse</ID>
<ID>LongMethod:UrlMgr.kt$UrlMgr$commandResponse</ID>
<ID>LongMethod:View.kt$View$commandResponse</ID>
<ID>LongParameterList:AbstractCommand.kt$AbstractCommand$( bot: Mobibot, sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID> <ID>LongParameterList:AbstractCommand.kt$AbstractCommand$( bot: Mobibot, sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID>
<ID>LoopWithTooManyJumpStatements:View.kt$View$while (i &lt; 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 &gt; 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 &gt; 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++ }</ID> <ID>LongParameterList:Comment.kt$Comment$( bot: Mobibot, cmd: String, sender: String, isOp: Boolean, entry: EntryLink, index: Int, commentIndex: Int )</ID>
<ID>LongParameterList:Comment.kt$Comment$(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int)</ID>
<ID>MagicNumber:Comment.kt$Comment$3</ID> <ID>MagicNumber:Comment.kt$Comment$3</ID>
<ID>MagicNumber:Cycle.kt$Cycle$10</ID> <ID>MagicNumber:Cycle.kt$Cycle$10</ID>
<ID>MagicNumber:Recap.kt$Recap.Companion$10</ID> <ID>MagicNumber:Recap.kt$Recap.Companion$10</ID>
<ID>MagicNumber:UrlMgr.kt$UrlMgr$1000L</ID> <ID>MagicNumber:UrlMgr.kt$UrlMgr$1000L</ID>
<ID>MagicNumber:UrlMgr.kt$UrlMgr$60L</ID> <ID>MagicNumber:UrlMgr.kt$UrlMgr$60L</ID>
<ID>MagicNumber:View.kt$View$8</ID> <ID>MagicNumber:View.kt$View$8</ID>
<ID>NestedBlockDepth:Comment.kt$Comment$commandResponse</ID>
<ID>NestedBlockDepth:Ignore.kt$Ignore$commandResponse</ID>
<ID>NestedBlockDepth:Posting.kt$Posting$commandResponse</ID>
<ID>NestedBlockDepth:UrlMgr.kt$UrlMgr$commandResponse</ID> <ID>NestedBlockDepth:UrlMgr.kt$UrlMgr$commandResponse</ID>
<ID>NestedBlockDepth:View.kt$View$commandResponse</ID>
</Whitelist> </Whitelist>
</SmellBaseline> </SmellBaseline>

View file

@ -14,13 +14,13 @@ import java.time.*;
public final class ReleaseInfo { public final class ReleaseInfo {
public static final String PROJECT = "mobibot"; public static final String PROJECT = "mobibot";
public static final LocalDateTime BUILDDATE = 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 MAJOR = 0;
public static final int MINOR = 8; public static final int MINOR = 8;
public static final int PATCH = 0; public static final int PATCH = 0;
public static final String PRERELEASE = "alpha"; public static final String PRERELEASE = "alpha";
public static final String BUILDMETA = "045"; public static final String BUILDMETA = "105";
public static final String VERSION = "0.8.0-alpha+045"; public static final String VERSION = "0.8.0-alpha+105";
/** /**
* Disables the default constructor. * Disables the default constructor.

View file

@ -212,7 +212,7 @@ public class Mobibot extends PircBot {
UrlMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel); UrlMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, ircChannel);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Last feed: {}", UrlMgr.getToday()); logger.debug("Last feed: {}", UrlMgr.getStartDate());
} }
} catch (Exception e) { } catch (Exception e) {
if (logger.isErrorEnabled()) { if (logger.isErrorEnabled()) {

View file

@ -63,5 +63,4 @@ abstract class AbstractCommand {
open fun matches(message: String): Boolean { open fun matches(message: String): Boolean {
return false return false
} }
} }

View file

@ -37,7 +37,7 @@ import net.thauvin.erik.mobibot.Utils
import java.util.* import java.util.*
class Ignore(defaultIgnore: String) : AbstractCommand() { class Ignore(defaultIgnore: String) : AbstractCommand() {
private val keyword = "me" private val me = "me"
init { init {
if (defaultIgnore.isNotBlank()) { if (defaultIgnore.isNotBlank()) {
@ -47,11 +47,20 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
override val command = IGNORE_CMD override val command = IGNORE_CMD
override val help = listOf( 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.bold("To check your ignore status:"),
Utils.helpIndent("%s: $command"), Utils.helpIndent("%s: $command"),
Utils.bold("To toggle your ignore status:"), 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 <nick>|$me [<nick> ...]")
)
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
override val isVisible = true override val isVisible = true
@ -74,38 +83,69 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
isOp: Boolean, isOp: Boolean,
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (isOp) { if (!isOp) {
val nick = sender.toLowerCase() val nick = sender.toLowerCase()
val isMe = args.toLowerCase().startsWith(keyword) val isMe = args.toLowerCase().startsWith(me)
if (isMe) { ignoreNick(bot, nick, isMe)
if (ignored.remove(nick)) { } else {
bot.send(sender, "You are no longer ignored.") ignoreOp(bot, sender, args)
} else { }
ignored.add(nick) }
bot.send(sender, "You are now ignored.")
} 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 { } else {
if (ignored.contains(nick)) { ignored.add(sender)
bot.send(sender, "You are currently ignored.") bot.send(sender, "You are now ignored.")
} else {
bot.send(sender, "You are not currently ignored.")
}
} }
} else { } else {
if (args.isNotEmpty()) { if (ignored.contains(sender)) {
val nicks = args.toLowerCase().split(" ") bot.send(sender, "You are currently ignored.")
for (nick in nicks) { } else {
val ignore = if (keyword == nick) { bot.send(sender, "You are not currently ignored.")
sender.toLowerCase() }
} else { }
nick }
}
if (!ignored.remove(ignore)) { private fun ignoreOp(bot: Mobibot, sender: String, args: String) {
ignored.add(ignore) 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")}.")
} }
} }
} }

View file

@ -71,37 +71,13 @@ class Comment : AbstractCommand() {
if (index < UrlMgr.entriesCount) { if (index < UrlMgr.entriesCount) {
val entry: EntryLink = UrlMgr.getEntry(index) val entry: EntryLink = UrlMgr.getEntry(index)
val cindex = cmds[1].toInt() - 1 val commentIndex = cmds[1].toInt() - 1
if (cindex < entry.commentsCount) { if (commentIndex < entry.commentsCount) {
val cmd = cmds[2].trim() when (val cmd = cmds[2].trim()) {
"" -> showComment(bot, entry, index, commentIndex) // L1.1:
// L1.1: "-" -> deleteComment(bot, entry, index, commentIndex) // L11:-
if (cmd.isEmpty()) { "?" -> changeAuthor(bot, cmd, sender, isOp, entry, index, commentIndex) // L1.1:?<author>
val comment = entry.getComment(cindex) else -> setComment(bot, cmd, sender, entry, index, commentIndex)
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:?<author>
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)
} }
} }
} }
@ -127,4 +103,40 @@ class Comment : AbstractCommand() {
override fun matches(message: String): Boolean { override fun matches(message: String): Boolean {
return message.matches("^${Constants.LINK_CMD}[0-9]+\\.[0-9]+:.*".toRegex()) 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)))
}
} }

View file

@ -69,74 +69,17 @@ class Posting : AbstractCommand() {
val index = cmds[0].toInt() - 1 val index = cmds[0].toInt() - 1
if (index < UrlMgr.entriesCount) { if (index < UrlMgr.entriesCount) {
val cmd = cmds[1].trim() when (val cmd = cmds[1].trim()) {
if (cmd.isEmpty()) { "" -> showEntry(bot, index)
val entry: EntryLink = UrlMgr.getEntry(index) "-" -> removeEntry(bot, sender, login, isOp, index) // L1:-
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) else -> {
if (entry.hasTags()) { when (cmd[0]) {
bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) '|' -> changeTitle(bot, cmd, index) // L1:|<title>
} '=' -> changeUrl(bot, cmd, login, isOp, index) // L1:=<url>
if (entry.hasComments()) { '?' -> changeAuthor(bot, cmd, sender, isOp, index) // L1:?<author>
val comments = entry.comments else -> addComment(bot, cmd, sender, index)
for (i in comments.indices) {
bot.send(bot.channel, EntriesUtils.buildComment(index, i, comments[i]))
} }
} }
} 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 { override fun matches(message: String): Boolean {
return message.matches("${Constants.LINK_CMD}[0-9]+:.*".toRegex()) 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]))
}
}
}
} }

View file

@ -32,7 +32,6 @@
package net.thauvin.erik.mobibot.commands.links 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.Constants
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.TwitterTimer 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 net.thauvin.erik.mobibot.entries.EntryLink
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.IOException import java.io.IOException
import java.util.*
class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
private val tagsKeywords = ArrayList<String>() private val tagsKeywords = ArrayList<String>()
@ -74,7 +72,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
get() = entries.size get() = entries.size
@JvmStatic @JvmStatic
var today: String = Utils.today() var startDate: String = Utils.today()
private set private set
@JvmStatic @JvmStatic
@ -109,16 +107,15 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
@JvmStatic @JvmStatic
fun startup(current: String, backlogs: String, channel: String) { fun startup(current: String, backlogs: String, channel: String) {
today = EntriesMgr.loadEntries(current, channel, entries) startDate = EntriesMgr.loadEntries(current, channel, entries)
if (Utils.today() != today) { if (Utils.today() != startDate) {
this.entries.clear() this.entries.clear()
today = Utils.today() startDate = Utils.today()
} }
EntriesMgr.loadBacklogs(backlogs, history) EntriesMgr.loadBacklogs(backlogs, history)
} }
} }
@SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
override fun commandResponse( override fun commandResponse(
bot: Mobibot, bot: Mobibot,
sender: String, sender: String,
@ -129,56 +126,22 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
) { ) {
val cmds = args.split(" ".toRegex(), 2) val cmds = args.split(" ".toRegex(), 2)
if (Ignore.isNotIgnored(sender) && (cmds.size == 1 if (Ignore.isNotIgnored(sender) && (cmds.size == 1 || !cmds[1].contains(bot.nick))) {
|| !cmds[1].contains(bot.nick)
|| !cmds[1].endsWith(" (${Ignore.IGNORE_CMD}"))) {
val link = cmds[0].trim() val link = cmds[0].trim()
var isBackup = false if (!isDupEntry(bot, sender, link)) {
val dupIndex: Int = findDupEntry(link) val isBackup = saveDayBackup(bot)
if (dupIndex == -1) {
if (Utils.today() != today) {
isBackup = true
saveEntries(bot, true)
entries.clear()
today = Utils.today()
}
val tags: StringBuilder = StringBuilder(defaultTags) val tags: StringBuilder = StringBuilder(defaultTags)
var title = Constants.NO_TITLE var title = Constants.NO_TITLE
if (cmds.size == 2) { if (cmds.size == 2) {
val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2) val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2)
if (data.size == 1) { title = data[0].trim()
title = data[0].trim() if (data.size > 1) {
} else {
if (data[0].isNotBlank()) {
title = data[0].trim()
}
tags.append(' ').append(data[1].trim()) tags.append(' ').append(data[1].trim())
} }
} }
if (Constants.NO_TITLE == title) { tags.append(matchTagKeywords(title))
try { title = fetchTitle(link, title)
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)
}
}
}
entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString())) entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString()))
val index: Int = entries.size - 1 val index: Int = entries.size - 1
val entry: EntryLink = entries[index] val entry: EntryLink = entries[index]
@ -188,13 +151,10 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
bot.addPin(entry) bot.addPin(entry)
// Queue link for posting to twitter // Queue link for posting to twitter
if (bot.isTwitterAutoPost) { twitterPost(bot, index)
bot.twitterAddEntry(index)
Mobibot.timer.schedule(
TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L
)
}
saveEntries(bot, isBackup) saveEntries(bot, isBackup)
if (Constants.NO_TITLE == entry.title) { if (Constants.NO_TITLE == entry.title) {
bot.send(sender, Utils.bold("Please specify a title, by typing:")) bot.send(sender, Utils.bold("Please specify a title, by typing:"))
bot.send( bot.send(
@ -202,11 +162,6 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
Utils.helpIndent(Constants.LINK_CMD + (index + 1) + ":|This is the title") 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 isPrivate: Boolean
): Boolean = false ): 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 { override fun matches(message: String): Boolean {
return message.matches(LINK_MATCH.toRegex()) 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)
}
}
} }

View file

@ -65,57 +65,56 @@ class View : AbstractCommand() {
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (entriesCount != 0) { if (entriesCount != 0) {
val max = entriesCount showPosts(bot, args, sender)
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")
)
}
}
} else { } else {
bot.send(sender, "There is currently nothing to view. Why don't you post something?") 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")
)
}
}
}
} }

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Sat Mar 28 10:22:37 PDT 2020 #Sun Mar 29 00:39:32 PDT 2020
version.buildmeta=045 version.buildmeta=105
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=alpha version.prerelease=alpha
version.project=mobibot version.project=mobibot
version.semver=0.8.0-alpha+045 version.semver=0.8.0-alpha+105