main #8

Closed
limepotato wants to merge 183 commits from main into blog-drafts
3 changed files with 33 additions and 30 deletions
Showing only changes of commit df10a2f941 - Show all commits

View file

@ -38,6 +38,7 @@ fun Application.module() {
anyHost()
}
checkCreate()
checkConfig()
runBlocking {
val client =
HttpClient(CIO) {
@ -45,7 +46,7 @@ fun Application.module() {
install(Auth) {
bearer {
loadTokens {
BearerTokens(Config().botToken, Config().botToken)
BearerTokens(botToken, botToken)
}
}
}

View file

@ -11,14 +11,21 @@ import java.nio.file.Paths
import java.util.*
val prop = Properties()
val confPath = Paths.get("").toAbsolutePath().toString()
val confFile = File("$confPath/bot.cfg")
class Config {
private val confPath = Paths.get("").toAbsolutePath().toString()
private val confFile = File("$confPath/bot.cfg")
var botToken: String = ""
var instanceDomain: String = ""
var ntfyEndpoint: String = ""
var superSecret: String = ""
init {
fun checkConfig() {
if (confFile.exists()) {
FileInputStream(confFile).use { prop.load(it) }
botToken = (prop.getProperty("botToken"))
instanceDomain = (prop.getProperty("instance"))
ntfyEndpoint = (prop.getProperty("ntfyEndpoint"))
superSecret = (prop.getProperty("superSecret"))
} else {
Files.createFile(Paths.get("$confPath/bot.cfg"))
FileInputStream(confFile).use {
@ -30,11 +37,6 @@ class Config {
}
val out: OutputStream = FileOutputStream(confFile)
prop.store(out, "personalSiteAPI")
throw Exception("You need to fill ./bot.cfg !!!")
}
}
val botToken: String = (prop.getProperty("botToken"))
val instanceDomain: String = (prop.getProperty("instance"))
val ntfyEndpoint: String = (prop.getProperty("ntfyEndpoint"))
val superSecret: String = (prop.getProperty("superSecret"))
}

View file

@ -71,9 +71,9 @@ fun Application.configureRouting(client: HttpClient) {
}
// meow button
post {
if (call.receiveText() == Config().superSecret) {
if (call.receiveText() == superSecret) {
call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getMeow(), Config().instanceDomain)
makePost(client, getMeow(), instanceDomain)
call.respondText("meowed with bypass")
logger.info { "meowed with bypass" }
} else {
@ -83,7 +83,7 @@ fun Application.configureRouting(client: HttpClient) {
logger.info { "failed meow" }
} else {
call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getMeow(), Config().instanceDomain)
makePost(client, getMeow(), instanceDomain)
meowTimer()
call.respondText("meow' sent! timed out for some time")
logger.info { "meowed" }
@ -108,9 +108,9 @@ fun Application.configureRouting(client: HttpClient) {
}
// beep button
post {
if (call.receiveText() == Config().superSecret) {
if (call.receiveText() == superSecret) {
call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getBeep(), Config().instanceDomain)
makePost(client, getBeep(), instanceDomain)
call.respondText("beeped with bypass")
logger.info { "beeped with bypass" }
} else {
@ -120,7 +120,7 @@ fun Application.configureRouting(client: HttpClient) {
logger.info { "failed beeped" }
} else {
call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getBeep(), Config().instanceDomain)
makePost(client, getBeep(), instanceDomain)
beepTimer()
call.respondText("beep sent!")
logger.info { "beeped" }
@ -141,7 +141,7 @@ fun Application.configureRouting(client: HttpClient) {
val click = formParameters["Click"].toString()
call.response.status(HttpStatusCode(201, "Received..."))
call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click")
ntfyMsg(client, Config().ntfyEndpoint, title, message, attach, click)
ntfyMsg(client, ntfyEndpoint, title, message, attach, click)
storeMessage(title, message, attach, click)
}
}