This commit is contained in:
Erik C. Thauvin 2021-05-03 15:29:43 -07:00
parent 9f3dc273f0
commit d6df365e4b
22 changed files with 102 additions and 98 deletions

View file

@ -40,21 +40,22 @@ dependencies {
compileOnly 'pircbot:pircbot:1.5.0:sources'
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation 'com.rometools:rome:1.15.0'
implementation 'commons-cli:commons-cli:1.4'
implementation 'commons-net:commons-net:3.8.0'
implementation 'net.aksingh:owm-japis:2.5.3.0'
implementation 'net.objecthunter:exp4j:0.4.8'
implementation 'net.thauvin.erik:pinboard-poster:1.0.3'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-RC'
implementation 'org.json:json:20210307'
implementation 'org.jsoup:jsoup:1.13.1'
implementation 'org.twitter4j:twitter4j-core:4.0.7'
implementation "org.apache.logging.log4j:log4j-api:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-core:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-slf4j-impl:$versions.log4j"
implementation 'commons-cli:commons-cli:1.4'
implementation 'commons-net:commons-net:3.8.0'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.rometools:rome:1.15.0'
implementation 'net.aksingh:owm-japis:2.5.3.0'
implementation 'net.objecthunter:exp4j:0.4.8'
implementation 'net.thauvin.erik:pinboard-poster:1.0.3'
implementation 'org.json:json:20210307'
implementation 'org.jsoup:jsoup:1.13.1'
implementation 'org.twitter4j:twitter4j-core:4.0.7'
compileOnly "com.github.spotbugs:spotbugs-annotations:$versions.spotbugs"
testCompileOnly "com.github.spotbugs:spotbugs-annotations:$versions.spotbugs"

View file

@ -2,7 +2,6 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>ComplexCondition:Addons.kt$Addons$((isPrivate &amp;&amp; module.isPrivateMsgEnabled) || !isPrivate) &amp;&amp; module.commands.contains(cmd)</ID>
<ID>ComplexMethod:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List&lt;EntryLink&gt;, history: MutableList&lt;String&gt;, isDayBackup: Boolean )</ID>
<ID>ComplexMethod:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>LongMethod:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List&lt;EntryLink&gt;, history: MutableList&lt;String&gt;, isDayBackup: Boolean )</ID>
@ -15,49 +14,14 @@
<ID>LongParameterList:Comment.kt$Comment$(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int)</ID>
<ID>LongParameterList:Mobibot.kt$Mobibot$( nick: String, list: List&lt;String&gt;, maxPerLine: Int, isPrivate: Boolean, isBold: Boolean = false, isIndent: Boolean = false )</ID>
<ID>LongParameterList:Twitter.kt$Twitter.Companion$( consumerKey: String?, consumerSecret: String?, token: String?, tokenSecret: String?, handle: String?, message: String, isDm: Boolean )</ID>
<ID>MagicNumber:AddLog.kt$AddLog$4</ID>
<ID>MagicNumber:Comment.kt$Comment$3</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$11</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$3</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter.Companion$3</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter.Companion$4</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter.Companion$8</ID>
<ID>MagicNumber:Cycle.kt$Cycle$10</ID>
<ID>MagicNumber:Dice.kt$Dice$7</ID>
<ID>MagicNumber:FeedReader.kt$FeedReader$5</ID>
<ID>MagicNumber:Ignore.kt$Ignore$8</ID>
<ID>MagicNumber:Mobibot.kt$Mobibot$10</ID>
<ID>MagicNumber:Mobibot.kt$Mobibot$1000L</ID>
<ID>MagicNumber:Mobibot.kt$Mobibot$3</ID>
<ID>MagicNumber:Mobibot.kt$Mobibot$5</ID>
<ID>MagicNumber:Mobibot.kt$Mobibot$8</ID>
<ID>MagicNumber:Modules.kt$Modules$7</ID>
<ID>MagicNumber:Recap.kt$Recap.Companion$10</ID>
<ID>MagicNumber:Tell.kt$Tell$50</ID>
<ID>MagicNumber:Tell.kt$Tell$7</ID>
<ID>MagicNumber:Twitter.kt$Twitter$1000L</ID>
<ID>MagicNumber:Twitter.kt$Twitter$60L</ID>
<ID>MagicNumber:Users.kt$Users$8</ID>
<ID>MagicNumber:Utils.kt$Utils$30</ID>
<ID>MagicNumber:Utils.kt$Utils$365</ID>
<ID>MagicNumber:Utils.kt$Utils$7</ID>
<ID>MagicNumber:View.kt$View$8</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$1.60934</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$32</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$5</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$9</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime$17</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$3</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$3600</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$60</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$86.4</ID>
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties)</ID>
<ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean): Boolean</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @Suppress("MagicNumber") @JvmStatic fun convertCurrency(query: String): Message</ID>
<ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ @Throws(IOException::class, FeedException::class) fun loadEntries(file: String, channel: String, entries: MutableList&lt;EntryLink&gt;): String</ID>
<ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List&lt;EntryLink&gt;, history: MutableList&lt;String&gt;, isDayBackup: Boolean )</ID>
<ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$// Daily backup private fun dailyBackup( bot: Mobibot, history: MutableList&lt;String&gt; )</ID>
<ID>NestedBlockDepth:EntryLink.kt$EntryLink$ private fun setTags(tags: List&lt;String?&gt;)</ID>
<ID>NestedBlockDepth:FeedReader.kt$FeedReader$ override fun run()</ID>
<ID>NestedBlockDepth:GoogleSearch.kt$GoogleSearch$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID>
@ -85,6 +49,5 @@
<ID>TooGenericExceptionCaught:Weather2.kt$Weather2.Companion$e: NullPointerException</ID>
<ID>TooManyFunctions:Mobibot.kt$Mobibot : PircBot</ID>
<ID>TooManyFunctions:Tell.kt$Tell : AbstractCommand</ID>
<ID>TooManyFunctions:Utils.kt$Utils$Utils</ID>
</CurrentIssues>
</SmellBaseline>

View file

@ -97,8 +97,9 @@ class Addons {
return true
}
}
for (module in modules) {
if (((isPrivate && module.isPrivateMsgEnabled) || !isPrivate) && module.commands.contains(cmd)) {
val mods = if (isPrivate) modules.filter { it.isPrivateMsgEnabled } else modules
for (module in mods) {
if (module.commands.contains(cmd)) {
module.commandResponse(sender, cmd, args, isPrivate)
return true
}

View file

@ -48,6 +48,7 @@ class FeedReader(
private val url: String
) : Runnable {
// Maximum number of feed items to display
@Suppress("MagicNumber")
private val maxItems = 5
/**

View file

@ -207,6 +207,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
}
var retries = 0
while (retries++ < MAX_RECONNECT && !isConnected) {
@Suppress("MagicNumber")
sleep(10)
try {
connect(ircServer, ircPort)
@ -240,6 +241,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
/**
* Responds with the default help.
*/
@Suppress("MagicNumber")
fun helpDefault(sender: String, isOp: Boolean, isPrivate: Boolean) {
send(sender, "Type a URL on $channel to post it.", isPrivate)
send(sender, "For more information on a specific command, type:", isPrivate)
@ -305,6 +307,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (weblogUrl.isNotBlank()) {
version = weblogUrl
}
@Suppress("MagicNumber")
sleep(5)
connect()
}
@ -361,6 +364,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
timer.cancel()
twitter.shutdown()
twitter.notification("$name stopped by $sender on $channel")
@Suppress("MagicNumber")
sleep(3)
quitServer("The Bot Is Out There!")
exitProcess(0)
@ -467,6 +471,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
*/
fun sleep(secs: Int) {
try {
@Suppress("MagicNumber")
Thread.sleep(secs * 1000L)
} catch (ignore: InterruptedException) {
// Do nothing

View file

@ -51,6 +51,7 @@ import java.util.stream.Collectors
/**
* Miscellaneous utilities.
*/
@Suppress("TooManyFunctions")
object Utils {
private val searchFlags = arrayOf("%c", "%n")
@ -234,6 +235,7 @@ object Utils {
/**
* Converts milliseconds to year month week day hour and minutes.
*/
@Suppress("MagicNumber")
@JvmStatic
fun uptime(uptime: Long): String {
val info = StringBuilder()

View file

@ -63,6 +63,7 @@ class AddLog(bot: Mobibot) : AbstractCommand(bot) {
return
}
}
@Suppress("MagicNumber")
bot.sendList(sender, history, 4, isPrivate, isIndent = true)
}
}

View file

@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
class Cycle(bot: Mobibot) : AbstractCommand(bot) {
@Suppress("MagicNumber")
private val wait = 10
override val name = "cycle"
override val help = listOf("To have the bot leave the channel and come back:", Utils.helpFormat("%c $name"))

View file

@ -140,6 +140,7 @@ class Ignore(bot: Mobibot) : AbstractCommand(bot) {
if (ignored.size > 0) {
bot.send(sender, "The following nicks are ignored:", isPrivate)
@Suppress("MagicNumber")
bot.sendList(sender, ignored.sorted(), 8, isPrivate, isIndent = true)
} else {
bot.send(sender, "No one is currently ${Utils.bold("ignored")}.", isPrivate)

View file

@ -58,6 +58,7 @@ class Modules(bot: Mobibot) : AbstractCommand(bot) {
send(sender, "There are no enabled modules.", isPrivate)
} else {
send(sender, "The enabled modules are: ", isPrivate)
@Suppress("MagicNumber")
sendList(sender, modulesNames, 7, isPrivate, isIndent = true)
}
} else {

View file

@ -60,6 +60,7 @@ class Recap(bot: Mobibot) : AbstractCommand(bot) {
Utils.utcDateTime(LocalDateTime.now(Clock.systemUTC()))
+ " - $sender" + (if (isAction) " " else ": ") + message
)
@Suppress("MagicNumber")
if (recaps.size > 10) {
recaps.removeAt(0)
}

View file

@ -62,6 +62,7 @@ class Users(bot: Mobibot) : AbstractCommand(bot) {
}
}
@Suppress("MagicNumber")
bot.sendList(sender, nicks.sorted(), 8, isPrivate, isIndent = true)
}
}

View file

@ -65,6 +65,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
isOp: Boolean,
isPrivate: Boolean
) {
@Suppress("MagicNumber")
val cmds = args.substring(1).split("[.:]".toRegex(), 3)
val index = cmds[0].toInt() - 1

View file

@ -40,6 +40,7 @@ import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink
class View(bot: Mobibot) : AbstractCommand(bot) {
@Suppress("MagicNumber")
private val maxEntries = 8
override val name = VIEW_CMD
override val help = listOf(

View file

@ -53,9 +53,11 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
private val serializedObject: String
// Maximum number of days to keep messages
@Suppress("MagicNumber")
private var maxDays = 7
// Message maximum queue size
@Suppress("MagicNumber")
private var maxSize = 50
/**
@ -227,8 +229,7 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
save()
}
} else if (message.sender.equals(nickname, ignoreCase = true) && message.isReceived
&& !message.isNotified
) {
&& !message.isNotified) {
bot.send(
nickname,
"Your message ${reverseColor("[ID " + message.id + ']')} was sent to " +

View file

@ -71,6 +71,53 @@ object EntriesMgr {
// Maximum number of backlogs to keep
private const val maxBacklogs = 10
// Daily backup
private fun dailyBackup(
bot: Mobibot,
history: MutableList<String>
) {
if (bot.backlogsUrl.isNotBlank()) {
if (!history.contains(bot.today)) {
history.add(bot.today)
while (history.size > maxBacklogs) {
history.removeAt(0)
}
}
OutputStreamWriter(
Files.newOutputStream(Paths.get(bot.logsDir + NAV_XML)), StandardCharsets.UTF_8
).use { fw ->
val output = SyndFeedOutput()
val rss: SyndFeed = SyndFeedImpl()
val items: MutableList<SyndEntry> = mutableListOf()
var item: SyndEntry
with(rss) {
feedType = "rss_2.0"
title = "${bot.channel} IRC Links Backlogs"
description = "Backlogs of Links from ${bot.ircServer} on ${bot.channel}"
link = bot.backlogsUrl
publishedDate = Calendar.getInstance().time
}
var date: String
items.clear()
for (i in history.size - 1 downTo 0) {
date = history[i]
item = SyndEntryImpl()
with(item) {
link = bot.backlogsUrl + date + ".xml"
title = date
description = SyndContentImpl().apply { value = "Links for $date" }
}
items.add(item)
}
rss.entries = items
if (bot.logger.isDebugEnabled) bot.logger.debug("Writing the backlog feed.")
output.output(rss, fw)
}
} else {
bot.logger.warn("Unable to generate the backlogs feed. No property configured.")
}
}
/**
* Loads the backlogs.
*/
@ -139,7 +186,7 @@ object EntriesMgr {
if (bot.logsDir.isNotBlank() && bot.weblogUrl.isNotBlank()) {
try {
val output = SyndFeedOutput()
var rss: SyndFeed = SyndFeedImpl()
val rss: SyndFeed = SyndFeedImpl()
val items: MutableList<SyndEntry> = mutableListOf()
var item: SyndEntry
OutputStreamWriter(
@ -153,12 +200,12 @@ object EntriesMgr {
publishedDate = Calendar.getInstance().time
language = "en"
}
var buff: StringBuilder
val buff: StringBuilder = StringBuilder()
var comment: EntryComment
for (i in entries.size - 1 downTo 0) {
with(entries[i]) {
buff = StringBuilder()
.append("Posted by <b>")
buff.setLength(0)
buff.append("Posted by <b>")
.append(nick)
.append("</b> on <a href=\"irc://")
.append(bot.ircServer).append('/')
@ -199,43 +246,7 @@ object EntriesMgr {
), StandardCharsets.UTF_8
).use { fw -> output.output(rss, fw) }
if (isDayBackup) {
if (bot.backlogsUrl.isNotBlank()) {
if (!history.contains(bot.today)) {
history.add(bot.today)
while (history.size > maxBacklogs) {
history.removeAt(0)
}
}
OutputStreamWriter(
Files.newOutputStream(Paths.get(bot.logsDir + NAV_XML)), StandardCharsets.UTF_8
).use { fw ->
rss = SyndFeedImpl()
with(rss) {
feedType = "rss_2.0"
title = "${bot.channel} IRC Links Backlogs"
description = "Backlogs of Links from ${bot.ircServer} on ${bot.channel}"
link = bot.backlogsUrl
publishedDate = Calendar.getInstance().time
}
var date: String
items.clear()
for (i in history.size - 1 downTo 0) {
date = history[i]
item = SyndEntryImpl()
with(item) {
link = bot.backlogsUrl + date + ".xml"
title = date
description = SyndContentImpl().apply { value = "Links for $date" }
}
items.add(item)
}
rss.entries = items
if (bot.logger.isDebugEnabled) bot.logger.debug("Writing the backlog feed.")
output.output(rss, fw)
}
} else {
bot.logger.warn("Unable to generate the backlogs feed. No property configured.")
}
dailyBackup(bot, history)
}
} catch (e: FeedException) {
if (bot.logger.isWarnEnabled) bot.logger.warn("Unable to generate the entries feed.", e)

View file

@ -86,6 +86,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
}
} else if (args.contains(CURRENCY_RATES_KEYWORD)) {
send(sender, "The currency rates for ${Utils.bold(pubDate)} are:", isPrivate)
@Suppress("MagicNumber")
sendList(sender, currencyRates(), 3, isPrivate, isIndent = true)
} else {
helpResponse(sender, isPrivate)
@ -122,6 +123,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
isPrivate
)
send(sender, "The supported currencies are: ", isPrivate)
@Suppress("MagicNumber")
sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate, isIndent = true)
}
}
@ -150,6 +152,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
/**
* Converts from a currency to another.
*/
@Suppress("MagicNumber")
@JvmStatic
fun convertCurrency(query: String): Message {
val cmds = query.split(" ")
@ -185,6 +188,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
fun currencyRates(): List<String> {
val rates = mutableListOf<String>()
for ((key, value) in EXCHANGE_RATES) {
@Suppress("MagicNumber")
rates.add(" $key: ${StringUtils.leftPad(value, 8)}")
}
return rates

View file

@ -73,6 +73,7 @@ class Dice(bot: Mobibot) : AbstractModule(bot) {
}
private fun roll(): Pair<Int, Int> {
@Suppress("MagicNumber")
return Pair(Random.nextInt(1, 7), Random.nextInt(1, 7))
}

View file

@ -145,6 +145,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
if (bot.logger.isDebugEnabled) {
bot.logger.debug("Scheduling {} for posting on Twitter.", EntriesUtils.buildLinkCmd(index))
}
@Suppress("MagicNumber")
bot.timer.schedule(TwitterTimer(bot, index), Constants.TIMER_DELAY * 60L * 1000L)
}
}

View file

@ -89,6 +89,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
}
private fun getTemps(d: Double?): String {
@Suppress("MagicNumber")
val c = (d!! - 32) * 5 / 9
return "${d.roundToInt()} °F, ${c.roundToInt()} °C"
}
@ -186,6 +187,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
}
private fun wind(w: Double): String {
@Suppress("MagicNumber")
val kmh = w * 1.60934
return "${w.roundToInt()} mph, ${kmh.roundToInt()} km/h"
}

View file

@ -60,6 +60,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
/**
* Returns the current Internet (beat) Time.
*/
@Suppress("MagicNumber")
private fun internetTime(): String {
val zdt = ZonedDateTime.now(ZoneId.of("UTC+01:00"))
val beats = ((zdt[ChronoField.SECOND_OF_MINUTE] + zdt[ChronoField.MINUTE_OF_HOUR] * 60
@ -171,6 +172,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
countries["ZULU"] = "Zulu"
countries["INTERNET"] = BEATS_KEYWORD
countries["BEATS"] = BEATS_KEYWORD
@Suppress("MagicNumber")
ZoneId.getAvailableZoneIds().stream()
.filter { tz: String ->
!tz.contains("/") && tz.length == 3 && !countries.containsKey(tz)
@ -191,6 +193,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
with(bot) {
if (args.isEmpty()) {
send(sender, "The supported countries/zones are: ", isPrivate)
@Suppress("MagicNumber")
sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false)
} else {
val msg = time(args)

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle
#Sat May 01 21:41:40 PDT 2021
version.buildmeta=568
#Mon May 03 14:17:39 PDT 2021
version.buildmeta=576
version.major=0
version.minor=8
version.patch=0
version.prerelease=beta
version.project=mobibot
version.semver=0.8.0-beta+568
version.semver=0.8.0-beta+576