Extraction of logic being used repeatedly in sealed class hierarchy, use the strname passed into clientmessage.kt
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
package com.shr4pnel.ferretirc.net.messages
|
||||
|
||||
import io.ktor.network.selector.SelectorManager
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import com.shr4pnel.ferretirc.irc.IRCChannel
|
||||
|
||||
/**
|
||||
* IRC Messages sent by the client
|
||||
*/
|
||||
sealed class ClientMessage(val strName: String) {
|
||||
abstract fun toWireIntermediate(): String
|
||||
fun toWire() = "${toWireIntermediate()}\r\n".encodeToByteArray()
|
||||
|
||||
companion object Util {
|
||||
val selectorManager = SelectorManager(Dispatchers.IO)
|
||||
protected open val params = emptyList<String>()
|
||||
protected open val trailing: String? = null
|
||||
|
||||
fun toWireIntermediate(): String {
|
||||
return buildString {
|
||||
append(strName)
|
||||
params.forEach { append(" $it") }
|
||||
trailing?.let { append(" :$it") }
|
||||
}
|
||||
}
|
||||
|
||||
class Ping(val token: String? = null) : ClientMessage("PING") {
|
||||
@@ -21,33 +26,41 @@ sealed class ClientMessage(val strName: String) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun toWireIntermediate() = "$strName ${token.orEmpty()}"
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOfNotNull(token)
|
||||
}
|
||||
|
||||
sealed class Cap : ClientMessage("CAP") {
|
||||
val validSubcommands = listOf("LS", "LIST", "REQ", "END")
|
||||
|
||||
override fun toWireIntermediate() = when (this) {
|
||||
is LS -> "CAP LS $version"
|
||||
is REQ -> TODO()
|
||||
is END -> "CAP END"
|
||||
is LIST -> TODO()
|
||||
class LS(val version: Int = 302) : Cap() {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf("LS", version.toString())
|
||||
}
|
||||
|
||||
class LS(val version: Int = 302) : Cap()
|
||||
class LIST : Cap()
|
||||
class REQ(val version: Int?) : Cap()
|
||||
class END : Cap()
|
||||
class LIST : Cap() {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = TODO("IMPLEMENT CAP LIST")
|
||||
}
|
||||
|
||||
class REQ(val version: Int?) : Cap() {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = TODO("IMPL CAP REQ")
|
||||
}
|
||||
|
||||
class END : Cap() {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf("END")
|
||||
}
|
||||
}
|
||||
|
||||
class Nick(val nickname: String) : ClientMessage("NICK") {
|
||||
override fun toWireIntermediate() = "NICK $nickname"
|
||||
|
||||
init {
|
||||
require(nickname.length < 10) { "\"$nickname\" exceeds IRCs maximum nickname length of 9" }
|
||||
require(!nickname.startsWith(":") && !nickname.startsWith("#")) { "\"$nickname\" may not begin with : or #" } // TODO THIS SHOULD BLACKLIST ALL PREFIXES NAMED IN CHANTYPES PARAMETER
|
||||
require(!nickname.contains(" ")) { "$nickname may not contain a space" }
|
||||
}
|
||||
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(nickname)
|
||||
}
|
||||
|
||||
class User(val username: String, val realName: String) : ClientMessage("USER") {
|
||||
@@ -56,27 +69,37 @@ sealed class ClientMessage(val strName: String) {
|
||||
require(username.isNotEmpty()) { "Username may not be blank" }
|
||||
}
|
||||
|
||||
override fun toWireIntermediate() = "USER $username 0 * :$realName"
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(username, "0", "*")
|
||||
|
||||
override val trailing: String
|
||||
get() = realName
|
||||
}
|
||||
|
||||
class Pass(val password: String) : ClientMessage("PASS") {
|
||||
override fun toWireIntermediate() = "PASS $password"
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(password)
|
||||
}
|
||||
|
||||
class Oper(val name: String, val password: String): ClientMessage("OPER") {
|
||||
override fun toWireIntermediate() = "OPER $name $password"
|
||||
|
||||
class Oper(val name: String, val password: String) : ClientMessage("OPER") {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(name, password)
|
||||
}
|
||||
|
||||
class Die(): ClientMessage("DIE") {
|
||||
override fun toWireIntermediate() = "DIE"
|
||||
class Die : ClientMessage("DIE")
|
||||
|
||||
class List : ClientMessage("LIST")
|
||||
|
||||
class Join(val channel: IRCChannel) : ClientMessage("JOIN") {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(channel.chanName)
|
||||
}
|
||||
|
||||
class List(): ClientMessage("LIST") {
|
||||
override fun toWireIntermediate() = "LIST"
|
||||
}
|
||||
class PrivMsg(val target: IRCChannel, val message: String) : ClientMessage("PRIVMSG") {
|
||||
override val params: kotlin.collections.List<String>
|
||||
get() = listOf(target.chanName) // TODO RPL_ISUPPORT REQUIREMENTS IN FUN EG LINELEN STATUSMSG - ALSO ADD FUCKING STUPID STATUSMSG & SUPPORT FOR MASKS. FUCK ME
|
||||
|
||||
class UNIMPLEMENTED() : ClientMessage("") {
|
||||
override fun toWireIntermediate() = ""
|
||||
override val trailing: String
|
||||
get() = message
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user