106 lines
3.8 KiB
Kotlin
106 lines
3.8 KiB
Kotlin
![]() |
package xyz.maxwellj.chookpen.client
|
||
|
|
||
|
import fuel.Fuel
|
||
|
import fuel.get
|
||
|
import java.io.File
|
||
|
|
||
|
import kotlin.system.exitProcess
|
||
|
|
||
|
import java.math.BigInteger
|
||
|
import java.security.MessageDigest
|
||
|
|
||
|
fun md5(input:String): String {
|
||
|
val md = MessageDigest.getInstance("MD5")
|
||
|
return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
|
||
|
}
|
||
|
|
||
|
suspend fun main(args: Array<String>) {
|
||
|
// Variables
|
||
|
var name = ""
|
||
|
var server = ""
|
||
|
var port = ""
|
||
|
var hasHTTPS = ""
|
||
|
var password = ""
|
||
|
var configFile = File(".chookpen.profile")
|
||
|
|
||
|
if (!configFile.exists()) {
|
||
|
println("You don't have a Chookpen profile set up yet. If you've got a Chookpen profile, type 'l' to login and press enter. Otherwise, just press enter.")
|
||
|
val userInput = readln()
|
||
|
if (userInput == "l") {
|
||
|
println("Username:")
|
||
|
name = readln()
|
||
|
println("Password:")
|
||
|
password = readln()
|
||
|
val request = Fuel.get("http://localhost:8000/api/createAccount/username:{$name}token:{${md5(password)}}").body.string()
|
||
|
if (request == "Invalid token! Please try putting in your password right") {
|
||
|
println("Invalid password! Please rerun the program and put in the right password")
|
||
|
exitProcess(1)
|
||
|
} else {
|
||
|
configFile.createNewFile()
|
||
|
configFile.writeText("$name:$password:localhost:8000:0")
|
||
|
println("Logged in! Run the command again to start talking!")
|
||
|
exitProcess(0)
|
||
|
}
|
||
|
} else {
|
||
|
println("Choose a username:")
|
||
|
val newName = readln()
|
||
|
if (newName == "") {
|
||
|
println("Please choose a username! Rerun the program and try again")
|
||
|
exitProcess(1)
|
||
|
println("Choose a password:")
|
||
|
val newPassword = readln()
|
||
|
if (newPassword == "") {
|
||
|
println("Please choose a password! Rerun the program and try again")
|
||
|
exitProcess(1)
|
||
|
}
|
||
|
val request = Fuel.get("http://localhost:8000/api/createAccount/username:{$newName}token:{${md5(newPassword)}}").body.string()
|
||
|
if (request == "That username already exists on the server! Please choose a different username") {
|
||
|
println("That username already exists on the server! Rerun the program and choose a different username")
|
||
|
exitProcess(1)
|
||
|
} else {
|
||
|
configFile.createNewFile()
|
||
|
configFile.writeText("$newName:$newPassword:localhost:8000:0")
|
||
|
println("Account created!")
|
||
|
exitProcess(0)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var configStage = 0
|
||
|
for (char in configFile.readText()) {
|
||
|
if (char == ':') {configStage ++}
|
||
|
if (configStage == 0) {
|
||
|
name += char
|
||
|
} else if (configStage == 1) {
|
||
|
password += char
|
||
|
} else if (configStage == 2) {
|
||
|
server += char
|
||
|
} else if (configStage == 3) {
|
||
|
port += char
|
||
|
} else if (configStage == 4) {
|
||
|
hasHTTPS += char
|
||
|
}
|
||
|
}
|
||
|
|
||
|
server = server.replace(":", "")
|
||
|
port = port.replace(":", "")
|
||
|
hasHTTPS = hasHTTPS.replace(":", "")
|
||
|
password = password.replace(":", "")
|
||
|
|
||
|
// Actual code
|
||
|
println("Chookpen Client initialised!")
|
||
|
println("Hello $name@$server")
|
||
|
val protocol = "http"
|
||
|
if (hasHTTPS == "1") {
|
||
|
val protocol = "https"
|
||
|
}
|
||
|
if (args.count() == 0) {
|
||
|
println(Fuel.get("$protocol://$server:$port/api/username:{$name}token:{${md5(password)}}").body.string())
|
||
|
} else {
|
||
|
val message = args[0]
|
||
|
println(Fuel.get("$protocol://$server:$port/api/username:{$name}token:{${md5(password)}}message:{$message}").body.string())
|
||
|
}
|
||
|
exitProcess(0)
|
||
|
}
|