Apply ktlint standard formatting

This commit is contained in:
2026-09-13 21:26:25 +01:00
parent aa1e862201
commit e8fc28a239
15 changed files with 192 additions and 142 deletions
+4 -4
View File
@@ -13,10 +13,10 @@ repositories {
dependencies { dependencies {
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
implementation ("io.ktor:ktor-client-core:${ktorVersion}") implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-cio:${ktorVersion}") implementation("io.ktor:ktor-client-cio:$ktorVersion")
implementation("io.ktor:ktor-network:${ktorVersion}") implementation("io.ktor:ktor-network:$ktorVersion")
implementation("io.ktor:ktor-network-tls:${ktorVersion}") implementation("io.ktor:ktor-network-tls:$ktorVersion")
implementation("io.github.oshai:kotlin-logging-jvm:8.0.4") implementation("io.github.oshai:kotlin-logging-jvm:8.0.4")
implementation("ch.qos.logback:logback-classic:1.6.1") implementation("ch.qos.logback:logback-classic:1.6.1")
} }
@@ -2,8 +2,8 @@ package com.shr4pnel.ferretirc
import com.shr4pnel.ferretirc.irc.Server import com.shr4pnel.ferretirc.irc.Server
import com.shr4pnel.ferretirc.irc.util.Helpers import com.shr4pnel.ferretirc.irc.util.Helpers
import com.shr4pnel.ferretirc.net.messages.ClientMessage
import com.shr4pnel.ferretirc.net.Connection import com.shr4pnel.ferretirc.net.Connection
import com.shr4pnel.ferretirc.net.messages.ClientMessage
import com.shr4pnel.ferretirc.net.messages.ServerMessage import com.shr4pnel.ferretirc.net.messages.ServerMessage
import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -64,13 +64,21 @@ class IrcClient(hostname: String, port: Int, enableLogging: Boolean = true) {
suspend inline fun <reified T : ServerMessage> waitForNext(noinline predicate: (T) -> Boolean = { true }): T = suspend inline fun <reified T : ServerMessage> waitForNext(noinline predicate: (T) -> Boolean = { true }): T =
connection.reader.waitForNext(T::class, predicate) connection.reader.waitForNext(T::class, predicate)
suspend fun register(nick: String, realName: String? = null, password: String? = null) { suspend fun register(
val messages = buildList { nick: String,
realName: String? = null,
password: String? = null,
) {
val messages =
buildList {
if (!password.isNullOrEmpty()) add(ClientMessage.Pass(password)) if (!password.isNullOrEmpty()) add(ClientMessage.Pass(password))
add(ClientMessage.Cap.LS()) add(ClientMessage.Cap.LS())
add(ClientMessage.Nick(nick)) add(ClientMessage.Nick(nick))
if (!realName.isNullOrEmpty()) add(ClientMessage.User(nick, realName)) if (!realName.isNullOrEmpty()) {
else add(ClientMessage.User(nick, nick)) add(ClientMessage.User(nick, realName))
} else {
add(ClientMessage.User(nick, nick))
}
add(ClientMessage.Cap.END()) add(ClientMessage.Cap.END())
} }
queueMessages(*messages.toTypedArray()) queueMessages(*messages.toTypedArray())
@@ -1,5 +1,3 @@
package com.shr4pnel.ferretirc.irc package com.shr4pnel.ferretirc.irc
class IRCChannel(val chanName: String, val clientCount: Int, val topic: String) { class IRCChannel(val chanName: String, val clientCount: Int, val topic: String)
}
@@ -9,7 +9,11 @@ import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.onSubscription import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
class Server(private val msgBuffer: SharedFlow<ServerMessage>, private val outgoingMessages: Channel<ClientMessage>, private val scope: CoroutineScope) { class Server(
private val msgBuffer: SharedFlow<ServerMessage>,
private val outgoingMessages: Channel<ClientMessage>,
private val scope: CoroutineScope,
) {
lateinit var features: ISupportFeatures lateinit var features: ISupportFeatures
private set private set
@@ -31,6 +35,5 @@ class Server(private val msgBuffer: SharedFlow<ServerMessage>, private val outgo
} }
fun fetchFeatures() { fun fetchFeatures() {
} }
} }
@@ -18,6 +18,7 @@ class MessageParser(val incoming: Channel<String>) {
abstract class Command(open val command: String, open val parameters: List<String>) { abstract class Command(open val command: String, open val parameters: List<String>) {
abstract fun toServerMessage(): ServerMessage abstract fun toServerMessage(): ServerMessage
fun getTrailingParameterIndex() = parameters.indexOfFirst { it.startsWith(":") } fun getTrailingParameterIndex() = parameters.indexOfFirst { it.startsWith(":") }
fun getTrailingParameterString() = fun getTrailingParameterString() =
@@ -35,21 +36,25 @@ class MessageParser(val incoming: Channel<String>) {
fun getNonTrailingParameters() = getNonTrailingParameterString().split(" ") fun getNonTrailingParameters() = getNonTrailingParameterString().split(" ")
class NamedCommand(override val command: String, override val parameters: List<String>) : Command(command, parameters) { class NamedCommand(override val command: String, override val parameters: List<String>) : Command(command, parameters) {
override fun toServerMessage() = when (command.uppercase()) { override fun toServerMessage() =
when (command.uppercase()) {
"PONG" -> ServerMessage.Pong(getTrailingParameterString().removePrefix(":")) "PONG" -> ServerMessage.Pong(getTrailingParameterString().removePrefix(":"))
"MODE" -> ServerMessage.Mode( "MODE" ->
ServerMessage.Mode(
parameters.first(), parameters.first(),
parameters.last().removePrefix(":") parameters.last().removePrefix(":"),
) // TODO MODE, CHANMODE, LOCALMODE ) // TODO MODE, CHANMODE, LOCALMODE
"NOTICE" -> ServerMessage.Notice( "NOTICE" ->
ServerMessage.Notice(
getNonTrailingParameters(), getNonTrailingParameters(),
getTrailingParameterString() getTrailingParameterString(),
) )
"PRIVMSG" -> ServerMessage.PrivMsg( "PRIVMSG" ->
ServerMessage.PrivMsg(
getNonTrailingParameters(), getNonTrailingParameters(),
getTrailingParameterString() getTrailingParameterString(),
) )
else -> { else -> {
@@ -59,13 +64,15 @@ class MessageParser(val incoming: Channel<String>) {
} }
class NumericCommand(override val command: String, override val parameters: List<String>) : Command(command, parameters) { class NumericCommand(override val command: String, override val parameters: List<String>) : Command(command, parameters) {
override fun toServerMessage() = when (command.toInt()) { override fun toServerMessage() =
when (command.toInt()) {
5 -> ServerMessage.Numeric.RPL_ISUPPORT(getNonTrailingParameterString()) 5 -> ServerMessage.Numeric.RPL_ISUPPORT(getNonTrailingParameterString())
321 -> ServerMessage.Numeric.RPL_LISTSTART() 321 -> ServerMessage.Numeric.RPL_LISTSTART()
322 -> ServerMessage.Numeric.RPL_LIST( 322 ->
ServerMessage.Numeric.RPL_LIST(
parameters[1], parameters[1],
parameters[2].toInt(), parameters[2].toInt(),
getTrailingParameterString() getTrailingParameterString(),
) )
323 -> ServerMessage.Numeric.RPL_LISTEND() 323 -> ServerMessage.Numeric.RPL_LISTEND()
else -> ServerMessage.UNIMPLEMENTED(parameters.toString()) else -> ServerMessage.UNIMPLEMENTED(parameters.toString())
@@ -99,21 +106,31 @@ class MessageParser(val incoming: Channel<String>) {
val tokens = commandList.toMutableList() val tokens = commandList.toMutableList()
// remove tags from strlist if present // remove tags from strlist if present
tags = if (tokens.first().startsWith("@")) { tags =
if (tokens.first().startsWith("@")) {
tokens.removeFirst() tokens.removeFirst()
} else null } else {
null
}
// remove client prefix (nick&opt hostname) if present // remove client prefix (nick&opt hostname) if present
prefix = if (tokens.first().startsWith(":")) { prefix =
if (tokens.first().startsWith(":")) {
tokens.removeFirst() tokens.removeFirst()
} else null } else {
null
}
// get command e.g. PING // get command e.g. PING
val commandStr = tokens.removeFirst() val commandStr = tokens.removeFirst()
// Check if command is a numeric (RPL/ERR) or named // Check if command is a numeric (RPL/ERR) or named
command = if (commandStr.toIntOrNull() == null) Command.NamedCommand(commandStr, tokens) command =
else Command.NumericCommand(commandStr, tokens) if (commandStr.toIntOrNull() == null) {
Command.NamedCommand(commandStr, tokens)
} else {
Command.NumericCommand(commandStr, tokens)
}
return command.toServerMessage() return command.toServerMessage()
} }
@@ -122,7 +139,6 @@ class MessageParser(val incoming: Channel<String>) {
} }
suspend fun start() { suspend fun start() {
val builder = MessageBuilder() val builder = MessageBuilder()
for (msg in incoming) { for (msg in incoming) {
logger.trace { "Receiving: $msg" } logger.trace { "Receiving: $msg" }
@@ -27,7 +27,6 @@ class MessageReader(socket: Socket, scope: CoroutineScope) : MessageIO(socket, s
val parser = MessageParser(incomingMessages) val parser = MessageParser(incomingMessages)
private val logger = if (Helpers.enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER) private val logger = if (Helpers.enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
init { init {
scope.launch { scope.launch {
parser.start() parser.start()
@@ -49,7 +48,10 @@ class MessageReader(socket: Socket, scope: CoroutineScope) : MessageIO(socket, s
} }
} }
suspend fun <T : ServerMessage> waitForNext(kClass: KClass<T>, predicate: (T) -> Boolean = { true }): T { suspend fun <T : ServerMessage> waitForNext(
kClass: KClass<T>,
predicate: (T) -> Boolean = { true },
): T {
return messageBuffer return messageBuffer
.filter { .filter {
kClass.isInstance(it) kClass.isInstance(it)
@@ -16,7 +16,8 @@ class MessageWriter(socket: Socket, scope: CoroutineScope) : MessageIO(socket, s
private lateinit var send: ByteWriteChannel private lateinit var send: ByteWriteChannel
private val logger = if (Helpers.enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER) private val logger = if (Helpers.enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
override fun start() = scope.launch { override fun start() =
scope.launch {
send = socket.openWriteChannel() send = socket.openWriteChannel()
for (msg in outgoingMessages) { for (msg in outgoingMessages) {
@@ -55,7 +55,9 @@ sealed class ClientMessage(val strName: String) {
class Nick(val nickname: String) : ClientMessage("NICK") { class Nick(val nickname: String) : ClientMessage("NICK") {
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" }
} }
@@ -97,7 +99,10 @@ sealed class ClientMessage(val strName: String) {
class PrivMsg(val target: IRCChannel, val message: String) : ClientMessage("PRIVMSG") { class PrivMsg(val target: IRCChannel, val message: String) : ClientMessage("PRIVMSG") {
override val params: kotlin.collections.List<String> 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 get() =
listOf(
target.chanName,
) // TODO RPL_ISUPPORT REQUIREMENTS IN FUN EG LINELEN STATUSMSG - ALSO ADD FUCKING STUPID STATUSMSG & SUPPORT FOR MASKS. FUCK ME
override val trailing: String override val trailing: String
get() = message get() = message
@@ -7,5 +7,6 @@ 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(): Any abstract fun start(): Any
} }
@@ -5,16 +5,24 @@ package com.shr4pnel.ferretirc.net.messages
*/ */
sealed class ServerMessage { sealed class ServerMessage {
data class Cap(val placeholder: String? = null) : ServerMessage() data class Cap(val placeholder: String? = null) : ServerMessage()
data class Pong(val token: String? = null) : ServerMessage() data class Pong(val token: String? = null) : ServerMessage()
data class Mode(val operatorName: String, val pMask: String) : ServerMessage() data class Mode(val operatorName: String, val pMask: String) : ServerMessage()
data class PrivMsg(val targets: List<String>, val message: String) : ServerMessage() data class PrivMsg(val targets: List<String>, val message: String) : ServerMessage()
data class Notice(val targets: List<String>, val message: String) : ServerMessage() data class Notice(val targets: List<String>, val message: String) : ServerMessage()
data class UNIMPLEMENTED(val msg: String) : ServerMessage() data class UNIMPLEMENTED(val msg: String) : ServerMessage()
sealed class Numeric(val number: Short) : ServerMessage() { sealed class Numeric(val number: Short) : ServerMessage() {
data class RPL_ISUPPORT(val keypairs: String) : Numeric(5) data class RPL_ISUPPORT(val keypairs: String) : Numeric(5)
class RPL_LISTSTART : Numeric(321) class RPL_LISTSTART : Numeric(321)
data class RPL_LIST(val chanName: String, val clientCount: Int, val topic: String) : Numeric(322) data class RPL_LIST(val chanName: String, val clientCount: Int, val topic: String) : Numeric(322)
class RPL_LISTEND : Numeric(323) class RPL_LISTEND : Numeric(323)
} }
} }
@@ -5,7 +5,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import com.shr4pnel.ferretirc.net.messages.ClientMessage as Message
import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@@ -14,6 +13,7 @@ import java.lang.ProcessBuilder
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import com.shr4pnel.ferretirc.net.messages.ClientMessage as Message
class IrcClientTest { class IrcClientTest {
private val logger = KotlinLogging.logger {} private val logger = KotlinLogging.logger {}
@@ -25,19 +25,21 @@ class IrcClientTest {
@JvmStatic @JvmStatic
@BeforeAll @BeforeAll
fun setup() = runBlocking { fun setup() =
runBlocking {
val logFile = File("src/test/resources/logs/ngircd.log") val logFile = File("src/test/resources/logs/ngircd.log")
val ngircdConfigPath = javaClass.classLoader.getResource("ngircd.conf")!!.path val ngircdConfigPath = javaClass.classLoader.getResource("ngircd.conf")!!.path
logFile.createNewFile() logFile.createNewFile()
processes = ProcessBuilder.startPipeline( processes =
ProcessBuilder.startPipeline(
listOf<ProcessBuilder>( listOf<ProcessBuilder>(
// Run IRC server in [-d] debug log mode, with [-n] no daemon, using config [-f] file at path // Run IRC server in [-d] debug log mode, with [-n] no daemon, using config [-f] file at path
ProcessBuilder("ngircd", "-dnf", ngircdConfigPath).redirectErrorStream(true), ProcessBuilder("ngircd", "-dnf", ngircdConfigPath).redirectErrorStream(true),
// Remove unhelpful default ngircd info [PID. relative time, GID?] // Remove unhelpful default ngircd info [PID. relative time, GID?]
ProcessBuilder("sed", "-ue", "s/\\[[^][]*\\]//g"), ProcessBuilder("sed", "-ue", "s/\\[[^][]*\\]//g"),
// Add relative timestamp // Add relative timestamp
ProcessBuilder("ts", "-s", "%H:%M:%.S").redirectOutput(logFile) ProcessBuilder("ts", "-s", "%H:%M:%.S").redirectOutput(logFile),
) ),
) )
client.connect() client.connect()
client.register("shr4p", "Tyler D", "password") client.register("shr4p", "Tyler D", "password")
@@ -45,7 +47,8 @@ class IrcClientTest {
@JvmStatic @JvmStatic
@AfterAll @AfterAll
fun shutdown() = runBlocking { fun shutdown() =
runBlocking {
logger.info { "Closing server." } logger.info { "Closing server." }
client.queueMessage(Message.Die()) client.queueMessage(Message.Die())
client.waitForNext<ServerMessage.Notice>() client.waitForNext<ServerMessage.Notice>()
@@ -56,18 +59,19 @@ class IrcClientTest {
@Test @Test
fun sample() { fun sample() {
client.scope.launch { client.scope.launch {
} }
} }
@Test @Test
fun pingGetsPong() = runBlocking { fun pingGetsPong() =
runBlocking {
val token = "ACK" val token = "ACK"
logger.info { "Sending PING $token" } logger.info { "Sending PING $token" }
client.queueMessage(Message.Ping(token)) client.queueMessage(Message.Ping(token))
logger.info { "Waiting to receive PONG" } logger.info { "Waiting to receive PONG" }
val pong = withTimeoutOrNull(1.seconds) { val pong =
withTimeoutOrNull(1.seconds) {
client.waitForNext<ServerMessage.Pong> { it.token.equals(token) } client.waitForNext<ServerMessage.Pong> { it.token.equals(token) }
} }
assertNotNull(pong, "Reached timeout while waiting for PONG") assertNotNull(pong, "Reached timeout while waiting for PONG")
@@ -76,9 +80,11 @@ class IrcClientTest {
} }
@Test @Test
fun oper() = runBlocking { fun oper() =
runBlocking {
client.queueMessage(Message.Oper("shr4p", "password")) client.queueMessage(Message.Oper("shr4p", "password"))
val mode = withTimeoutOrNull(1.seconds) { val mode =
withTimeoutOrNull(1.seconds) {
client.waitForNext<ServerMessage.Mode>() client.waitForNext<ServerMessage.Mode>()
} }
assertNotNull(mode, "Timed out waiting for OPER MODE response") assertNotNull(mode, "Timed out waiting for OPER MODE response")
@@ -88,14 +94,16 @@ class IrcClientTest {
} }
@Test @Test
fun listChannels() = runBlocking { fun listChannels() =
runBlocking {
val channels = client.server.fetchChannels() val channels = client.server.fetchChannels()
assert(channels.isNotEmpty()) assert(channels.isNotEmpty())
client.queueMessage(Message.Join(channels.first())) client.queueMessage(Message.Join(channels.first()))
} }
@Test @Test
fun messageLands() = runBlocking { fun messageLands() =
runBlocking {
val guestUser = IrcClient("localhost", 6667, false) val guestUser = IrcClient("localhost", 6667, false)
guestUser.connect() guestUser.connect()
guestUser.register("guest", "guest", "pass") guestUser.register("guest", "guest", "pass")