From 59c96e48be625735e7f62505c119f0d7dee6f137 Mon Sep 17 00:00:00 2001 From: observer Date: Sat, 12 Sep 2026 20:32:57 +0100 Subject: [PATCH] Extraction of logic being used repeatedly in sealed class hierarchy, use the strname passed into clientmessage.kt --- .../ferretirc/net/messages/ClientMessage.kt | 85 ++++++++++++------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/shr4pnel/ferretirc/net/messages/ClientMessage.kt b/src/main/kotlin/com/shr4pnel/ferretirc/net/messages/ClientMessage.kt index c490673..86d06f8 100644 --- a/src/main/kotlin/com/shr4pnel/ferretirc/net/messages/ClientMessage.kt +++ b/src/main/kotlin/com/shr4pnel/ferretirc/net/messages/ClientMessage.kt @@ -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() + 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 + 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 + 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 + get() = TODO("IMPLEMENT CAP LIST") + } + + class REQ(val version: Int?) : Cap() { + override val params: kotlin.collections.List + get() = TODO("IMPL CAP REQ") + } + + class END : Cap() { + override val params: kotlin.collections.List + 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 + 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 + 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 + 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 + 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 + 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 + 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 } } \ No newline at end of file