Skip to content

Commit

Permalink
Fix a flaw where no check for config.allowedChats was performed
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftwerk28 committed Jul 7, 2023
1 parent 007bc08 commit 0f1229d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
30 changes: 13 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,32 @@ import java.io.File
import java.io.FileInputStream

buildscript {
repositories {
mavenCentral()
maven("https://plugins.gradle.org/m2/")
}
dependencies {
classpath("org.yaml:snakeyaml:1.26")
classpath("org.jlleitschuh.gradle:ktlint-gradle:10.1.0")
}
}

plugins {
id("org.jetbrains.kotlin.jvm") version "1.6.10"
id("com.github.johnrengelman.shadow") version "5.2.0"
id("org.jetbrains.kotlin.jvm") version "1.8.20"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("org.jlleitschuh.gradle.ktlint") version "10.1.0"
}

group = "org.kraftwerk28"

val cfg: Map<String, String> = Yaml()
.load(FileInputStream("$projectDir/src/main/resources/plugin.yml"))
val cfg: Map<String, String> = Yaml().run {
val pluginFile = FileInputStream("$projectDir/src/main/resources/plugin.yml")
load(pluginFile)
}
val pluginVersion = cfg.get("version")
val spigotApiVersion = cfg.get("api-version")
val retrofitVersion = "2.7.1"

group = "org.kraftwerk28"
version = pluginVersion as Any

repositories {
mavenCentral()
maven(
url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
)
maven(url = "https://jitpack.io")
maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
maven(url = "https://oss.sonatype.org/content/repositories/snapshots/")
}

Expand Down Expand Up @@ -71,9 +66,10 @@ tasks {
dependsOn("shadowJar")
finalizedBy("copyArtifacts")
}
withType<JavaCompile> {
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
}
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
kotlinOptions.jvmTarget = "1.8"
}
}
30 changes: 16 additions & 14 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class TgBot(
}

private fun initPolling() = plugin.launch {
loop@while (true) {
loop@ while (true) {
try {
api.getUpdates(
offset = currentOffset,
Expand Down Expand Up @@ -124,19 +124,21 @@ class TgBot(
}

private suspend fun handleUpdate(update: Update) {
// Ignore private message or channel post
if (listOf("private", "channel").contains(update.message?.chat?.type))
// Accept only these chat types
if (!listOf("group", "supergroup").contains(update.message?.chat?.type))
return
val ctx = HandlerContext(
update,
update.message,
update.message?.chat,
)

// Check whether the chat is white-listed in config
if (!config.allowedChats.contains(update.message?.chat?.id))
return

val ctx = HandlerContext(update, update.message, update.message?.chat)
update.message?.text?.let {
commandRegex?.matchEntire(it)?.groupValues?.let { matchList ->
commandMap[matchList[1]]?.run {
val args = matchList[2].split("\\s+".toRegex())
this(ctx.copy(commandArgs = args))
commandMap[matchList[1]]?.let { handler ->
val commandArgs = matchList[2].split("""\s+""".toRegex())
val handlerContext = ctx.copy(commandArgs = commandArgs)
handler(handlerContext)
}
} ?: run {
onTextHandler(ctx)
Expand Down Expand Up @@ -231,9 +233,9 @@ class TgBot(
api.sendMessage(ctx.message!!.chat.id, "No linked users.")
} else {
val text = "<b>Linked users:</b>\n" +
linkedUsers.mapIndexed { i, dbUser ->
"${i + 1}. ${dbUser.fullName()}"
}.joinToString("\n")
linkedUsers.mapIndexed { i, dbUser ->
"${i + 1}. ${dbUser.fullName()}"
}.joinToString("\n")
api.sendMessage(ctx.message!!.chat.id, text)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: SpigotTGBridge
version: "0.20.1"
version: "0.20.2"
api-version: "1.15"
main: org.kraftwerk28.spigot_tg_bridge.Plugin
description: Telegram <-> Minecraft communication plugin for Spigot.
Expand Down

0 comments on commit 0f1229d

Please sign in to comment.