Extraction of logic being used repeatedly in sealed class hierarchy, use the strname passed into clientmessage.kt

This commit is contained in:
2026-09-12 20:32:57 +01:00
parent 973a6fa11b
commit 59c96e48be
@@ -1,17 +1,22 @@
package com.shr4pnel.ferretirc.net.messages package com.shr4pnel.ferretirc.net.messages
import io.ktor.network.selector.SelectorManager import com.shr4pnel.ferretirc.irc.IRCChannel
import kotlinx.coroutines.Dispatchers
/** /**
* IRC Messages sent by the client * IRC Messages sent by the client
*/ */
sealed class ClientMessage(val strName: String) { sealed class ClientMessage(val strName: String) {
abstract fun toWireIntermediate(): String
fun toWire() = "${toWireIntermediate()}\r\n".encodeToByteArray() fun toWire() = "${toWireIntermediate()}\r\n".encodeToByteArray()
companion object Util { protected open val params = emptyList<String>()
val selectorManager = SelectorManager(Dispatchers.IO) 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") { 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") { sealed class Cap : ClientMessage("CAP") {
val validSubcommands = listOf("LS", "LIST", "REQ", "END") class LS(val version: Int = 302) : Cap() {
override val params: kotlin.collections.List<String>
override fun toWireIntermediate() = when (this) { get() = listOf("LS", version.toString())
is LS -> "CAP LS $version"
is REQ -> TODO()
is END -> "CAP END"
is LIST -> TODO()
} }
class LS(val version: Int = 302) : Cap() class LIST : Cap() {
class LIST : Cap() override val params: kotlin.collections.List<String>
class REQ(val version: Int?) : Cap() get() = TODO("IMPLEMENT CAP LIST")
class END : Cap() }
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") { class Nick(val nickname: String) : ClientMessage("NICK") {
override fun toWireIntermediate() = "NICK $nickname"
init { init {
require(nickname.length < 10) { "\"$nickname\" exceeds IRCs maximum nickname length of 9" } 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.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" } 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") { 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" } 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") { 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") { class Oper(val name: String, val password: String) : ClientMessage("OPER") {
override fun toWireIntermediate() = "OPER $name $password" override val params: kotlin.collections.List<String>
get() = listOf(name, password)
} }
class Die(): ClientMessage("DIE") { class Die : ClientMessage("DIE")
override fun toWireIntermediate() = "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") { class PrivMsg(val target: IRCChannel, val message: String) : ClientMessage("PRIVMSG") {
override fun toWireIntermediate() = "LIST" 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 val trailing: String
override fun toWireIntermediate() = "" get() = message
} }
} }