Skip to content

Commit 0f1229d

Browse files
committed
Fix a flaw where no check for config.allowedChats was performed
1 parent 007bc08 commit 0f1229d

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

build.gradle.kts

+13-17
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,32 @@ import java.io.File
55
import java.io.FileInputStream
66

77
buildscript {
8-
repositories {
9-
mavenCentral()
10-
maven("https://plugins.gradle.org/m2/")
11-
}
128
dependencies {
139
classpath("org.yaml:snakeyaml:1.26")
1410
classpath("org.jlleitschuh.gradle:ktlint-gradle:10.1.0")
1511
}
1612
}
1713

1814
plugins {
19-
id("org.jetbrains.kotlin.jvm") version "1.6.10"
20-
id("com.github.johnrengelman.shadow") version "5.2.0"
15+
id("org.jetbrains.kotlin.jvm") version "1.8.20"
16+
id("com.github.johnrengelman.shadow") version "8.1.1"
2117
id("org.jlleitschuh.gradle.ktlint") version "10.1.0"
2218
}
2319

24-
group = "org.kraftwerk28"
25-
26-
val cfg: Map<String, String> = Yaml()
27-
.load(FileInputStream("$projectDir/src/main/resources/plugin.yml"))
20+
val cfg: Map<String, String> = Yaml().run {
21+
val pluginFile = FileInputStream("$projectDir/src/main/resources/plugin.yml")
22+
load(pluginFile)
23+
}
2824
val pluginVersion = cfg.get("version")
2925
val spigotApiVersion = cfg.get("api-version")
3026
val retrofitVersion = "2.7.1"
27+
28+
group = "org.kraftwerk28"
3129
version = pluginVersion as Any
3230

3331
repositories {
3432
mavenCentral()
35-
maven(
36-
url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
37-
)
38-
maven(url = "https://jitpack.io")
33+
maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
3934
maven(url = "https://oss.sonatype.org/content/repositories/snapshots/")
4035
}
4136

@@ -71,9 +66,10 @@ tasks {
7166
dependsOn("shadowJar")
7267
finalizedBy("copyArtifacts")
7368
}
69+
withType<JavaCompile> {
70+
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
71+
}
7472
withType<KotlinCompile> {
75-
kotlinOptions {
76-
jvmTarget = "1.8"
77-
}
73+
kotlinOptions.jvmTarget = "1.8"
7874
}
7975
}

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt

+16-14
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class TgBot(
8989
}
9090

9191
private fun initPolling() = plugin.launch {
92-
loop@while (true) {
92+
loop@ while (true) {
9393
try {
9494
api.getUpdates(
9595
offset = currentOffset,
@@ -124,19 +124,21 @@ class TgBot(
124124
}
125125

126126
private suspend fun handleUpdate(update: Update) {
127-
// Ignore private message or channel post
128-
if (listOf("private", "channel").contains(update.message?.chat?.type))
127+
// Accept only these chat types
128+
if (!listOf("group", "supergroup").contains(update.message?.chat?.type))
129129
return
130-
val ctx = HandlerContext(
131-
update,
132-
update.message,
133-
update.message?.chat,
134-
)
130+
131+
// Check whether the chat is white-listed in config
132+
if (!config.allowedChats.contains(update.message?.chat?.id))
133+
return
134+
135+
val ctx = HandlerContext(update, update.message, update.message?.chat)
135136
update.message?.text?.let {
136137
commandRegex?.matchEntire(it)?.groupValues?.let { matchList ->
137-
commandMap[matchList[1]]?.run {
138-
val args = matchList[2].split("\\s+".toRegex())
139-
this(ctx.copy(commandArgs = args))
138+
commandMap[matchList[1]]?.let { handler ->
139+
val commandArgs = matchList[2].split("""\s+""".toRegex())
140+
val handlerContext = ctx.copy(commandArgs = commandArgs)
141+
handler(handlerContext)
140142
}
141143
} ?: run {
142144
onTextHandler(ctx)
@@ -231,9 +233,9 @@ class TgBot(
231233
api.sendMessage(ctx.message!!.chat.id, "No linked users.")
232234
} else {
233235
val text = "<b>Linked users:</b>\n" +
234-
linkedUsers.mapIndexed { i, dbUser ->
235-
"${i + 1}. ${dbUser.fullName()}"
236-
}.joinToString("\n")
236+
linkedUsers.mapIndexed { i, dbUser ->
237+
"${i + 1}. ${dbUser.fullName()}"
238+
}.joinToString("\n")
237239
api.sendMessage(ctx.message!!.chat.id, text)
238240
}
239241
}

src/main/resources/plugin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: SpigotTGBridge
2-
version: "0.20.1"
2+
version: "0.20.2"
33
api-version: "1.15"
44
main: org.kraftwerk28.spigot_tg_bridge.Plugin
55
description: Telegram <-> Minecraft communication plugin for Spigot.

0 commit comments

Comments
 (0)