Move message classes to ferretirc.net.messages

This commit is contained in:
2026-08-12 13:26:44 +01:00
parent 5d7847ee89
commit fcac3d5431
4 changed files with 79 additions and 67 deletions
@@ -1,64 +0,0 @@
package com.shr4pnel.ferretirc.net
import io.ktor.network.selector.SelectorManager
import kotlinx.coroutines.Dispatchers
/**
* IRC Messages sent by the client
*/
sealed class ClientMessage(val strName: String) {
abstract fun toWire(): ByteArray
companion object Util {
val selectorManager = SelectorManager(Dispatchers.IO)
}
class Ping(val token: String?) : ClientMessage("PING") {
init {
require(token?.isNotEmpty() ?: true) {
"Token to PING should be null or a non-empty string"
}
}
override fun toWire() = "$strName $token\r\n".encodeToByteArray()
}
sealed class Cap : ClientMessage("CAP") {
val validSubcommands = listOf("LS", "LIST", "REQ", "END")
override fun toWire() = when (this) {
is LS -> "CAP LS $version\r\n".encodeToByteArray()
is REQ -> TODO()
}
class LS(val version: Int = 302) : Cap() {
val subcommand = "LS"
}
class REQ(val version: Int?) : Cap() {
val subcommand = "REQ"
}
class END {
val subcommand = "END"
}
// init {
// require(validSubcommands.contains(subcommand.toUpperCasePreservingASCIIRules())) {
// "$subcommand is not a valid subcommand to CAP"
// }
// }
}
class Nick(val nickname: String) : ClientMessage("NICK") {
override fun toWire() = "NICK $nickname\r\n".encodeToByteArray()
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" }
}
}
}
@@ -0,0 +1,69 @@
package com.shr4pnel.ferretirc.net.messages
import io.ktor.network.selector.SelectorManager
import kotlinx.coroutines.Dispatchers
/**
* 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)
}
class Ping(val token: String? = null) : ClientMessage("PING") {
init {
require(token?.isNotEmpty() ?: true) {
"Token to PING should be null or a non-empty string"
}
}
override fun toWireIntermediate() = "$strName ${token.orEmpty()}"
}
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()
class LIST : Cap()
class REQ(val version: Int?) : Cap()
class END : Cap()
}
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" }
}
}
class User(val username: String, val realName: String) : ClientMessage("USER") {
// TODO GET USERNAME INITIALISATION CHECK PARAMETERS FROM USERLEN RPL_ISUPPORT
init {
require(username.isNotEmpty()) { "Username may not be blank" }
}
override fun toWireIntermediate() = "USER $username 0 * :$realName"
}
class Pass(val password: String) : ClientMessage("PASS") {
override fun toWireIntermediate() = "PASS $password"
}
class UNIMPLEMENTED() : ClientMessage("") {
override fun toWireIntermediate() = ""
}
}
@@ -1,12 +1,12 @@
package com.shr4pnel.ferretirc.net package com.shr4pnel.ferretirc.net.messages
import io.ktor.network.sockets.Socket import io.ktor.network.sockets.Socket
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
abstract class MessageIO(val socket: Socket, val scope: CoroutineScope) { abstract class MessageIO(val socket: Socket, val scope: CoroutineScope) {
val incomingMessages = Channel<String>(Channel.BUFFERED) val incomingMessages = Channel<String>(Channel.BUFFERED)
val outgoingMessages = Channel<ClientMessage>(Channel.BUFFERED) val outgoingMessages = Channel<ClientMessage>(Channel.BUFFERED)
abstract fun start(): Job
abstract suspend fun start()
} }
@@ -0,0 +1,7 @@
package com.shr4pnel.ferretirc.net.messages
sealed class ServerMessage {
class Cap() : ServerMessage()
class Pong(val token: String? = null) : ServerMessage()
class UNIMPLEMENTED(val msg: String) : ServerMessage()
}