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() anyHost()
} }
checkCreate() checkCreate()
checkConfig()
runBlocking { runBlocking {
val client = val client =
HttpClient(CIO) { HttpClient(CIO) {
@ -45,7 +46,7 @@ fun Application.module() {
install(Auth) { install(Auth) {
bearer { bearer {
loadTokens { loadTokens {
BearerTokens(Config().botToken, Config().botToken) BearerTokens(botToken, botToken)
} }
} }
} }

View file

@ -11,30 +11,32 @@ import java.nio.file.Paths
import java.util.* import java.util.*
val prop = Properties() val prop = Properties()
val confPath = Paths.get("").toAbsolutePath().toString()
val confFile = File("$confPath/bot.cfg")
class Config { var botToken: String = ""
private val confPath = Paths.get("").toAbsolutePath().toString() var instanceDomain: String = ""
private val confFile = File("$confPath/bot.cfg") var ntfyEndpoint: String = ""
var superSecret: String = ""
init { fun checkConfig() {
if (confFile.exists()) { if (confFile.exists()) {
FileInputStream(confFile).use { prop.load(it) } FileInputStream(confFile).use { prop.load(it) }
} else { botToken = (prop.getProperty("botToken"))
Files.createFile(Paths.get("$confPath/bot.cfg")) instanceDomain = (prop.getProperty("instance"))
FileInputStream(confFile).use { ntfyEndpoint = (prop.getProperty("ntfyEndpoint"))
prop.load(it) superSecret = (prop.getProperty("superSecret"))
prop.setProperty("botToken", "YourTokenHere") } else {
prop.setProperty("instance", "instance.domain") Files.createFile(Paths.get("$confPath/bot.cfg"))
prop.setProperty("ntfyEndpoint", "ntfy.example.com/topic") FileInputStream(confFile).use {
prop.setProperty("superSecret", "secretKey") prop.load(it)
} prop.setProperty("botToken", "YourTokenHere")
val out: OutputStream = FileOutputStream(confFile) prop.setProperty("instance", "instance.domain")
prop.store(out, "personalSiteAPI") prop.setProperty("ntfyEndpoint", "ntfy.example.com/topic")
prop.setProperty("superSecret", "secretKey")
} }
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 // meow button
post { post {
if (call.receiveText() == Config().superSecret) { if (call.receiveText() == superSecret) {
call.response.status(HttpStatusCode(201, "Meow Posted")) call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getMeow(), Config().instanceDomain) makePost(client, getMeow(), instanceDomain)
call.respondText("meowed with bypass") call.respondText("meowed with bypass")
logger.info { "meowed with bypass" } logger.info { "meowed with bypass" }
} else { } else {
@ -83,7 +83,7 @@ fun Application.configureRouting(client: HttpClient) {
logger.info { "failed meow" } logger.info { "failed meow" }
} else { } else {
call.response.status(HttpStatusCode(201, "Meow Posted")) call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getMeow(), Config().instanceDomain) makePost(client, getMeow(), instanceDomain)
meowTimer() meowTimer()
call.respondText("meow' sent! timed out for some time") call.respondText("meow' sent! timed out for some time")
logger.info { "meowed" } logger.info { "meowed" }
@ -108,9 +108,9 @@ fun Application.configureRouting(client: HttpClient) {
} }
// beep button // beep button
post { post {
if (call.receiveText() == Config().superSecret) { if (call.receiveText() == superSecret) {
call.response.status(HttpStatusCode(201, "Meow Posted")) call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getBeep(), Config().instanceDomain) makePost(client, getBeep(), instanceDomain)
call.respondText("beeped with bypass") call.respondText("beeped with bypass")
logger.info { "beeped with bypass" } logger.info { "beeped with bypass" }
} else { } else {
@ -120,7 +120,7 @@ fun Application.configureRouting(client: HttpClient) {
logger.info { "failed beeped" } logger.info { "failed beeped" }
} else { } else {
call.response.status(HttpStatusCode(201, "Meow Posted")) call.response.status(HttpStatusCode(201, "Meow Posted"))
makePost(client, getBeep(), Config().instanceDomain) makePost(client, getBeep(), instanceDomain)
beepTimer() beepTimer()
call.respondText("beep sent!") call.respondText("beep sent!")
logger.info { "beeped" } logger.info { "beeped" }
@ -141,7 +141,7 @@ fun Application.configureRouting(client: HttpClient) {
val click = formParameters["Click"].toString() val click = formParameters["Click"].toString()
call.response.status(HttpStatusCode(201, "Received...")) call.response.status(HttpStatusCode(201, "Received..."))
call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click") 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) storeMessage(title, message, attach, click)
} }
} }