Removed complexity (detekt)
This commit is contained in:
parent
02fae4d8e4
commit
fbe36d50b7
10 changed files with 328 additions and 271 deletions
|
@ -2,25 +2,15 @@
|
|||
<SmellBaseline>
|
||||
<Blacklist></Blacklist>
|
||||
<Whitelist>
|
||||
<ID>ComplexCondition:UrlMgr.kt$UrlMgr$Ignore.isNotIgnored(sender) && (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>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++ }</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:Cycle.kt$Cycle$10</ID>
|
||||
<ID>MagicNumber:Recap.kt$Recap.Companion$10</ID>
|
||||
<ID>MagicNumber:UrlMgr.kt$UrlMgr$1000L</ID>
|
||||
<ID>MagicNumber:UrlMgr.kt$UrlMgr$60L</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:View.kt$View$commandResponse</ID>
|
||||
</Whitelist>
|
||||
</SmellBaseline>
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -63,5 +63,4 @@ abstract class AbstractCommand {
|
|||
open fun matches(message: String): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 <nick>|$me [<nick> ...]")
|
||||
)
|
||||
|
||||
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")}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:?<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)
|
||||
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:?<author>
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:|<title>
|
||||
'=' -> 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]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue