You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ISupportProcessor.kt 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.dmdirc.ktirc.messages
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. import com.dmdirc.ktirc.io.IrcMessage
  4. import com.dmdirc.ktirc.state.ServerFeature
  5. import com.dmdirc.ktirc.state.ServerState
  6. import com.dmdirc.ktirc.state.serverFeatures
  7. import kotlin.reflect.KClass
  8. class ISupportProcessor(val serverState: ServerState) : MessageProcessor {
  9. override val commands = arrayOf("005")
  10. override fun process(message: IrcMessage) {
  11. // Ignore the first (nickname) and last ("are supported by this server") params
  12. for (i in 1 until message.params.size - 1) {
  13. parseParam(message.params[i])
  14. }
  15. }
  16. private fun parseParam(param: ByteArray) = when (param[0]) {
  17. '-'.toByte() -> resetFeature(param.sliceArray(1 until param.size))
  18. else -> when (val equals = param.indexOf('='.toByte())) {
  19. -1 -> enableFeatureWithDefault(param)
  20. else -> enableFeature(param.sliceArray(0 until equals), param.sliceArray(equals + 1 until param.size))
  21. }
  22. }
  23. private fun resetFeature(name: ByteArray) = name.asFeature()?.let { serverState.resetFeature(it) }
  24. @Suppress("UNCHECKED_CAST")
  25. private fun enableFeature(name: ByteArray, value: ByteArray) {
  26. name.asFeature()?.let { feature ->
  27. serverState.setFeature(feature, value.cast(feature.type))
  28. }
  29. }
  30. private fun enableFeatureWithDefault(name: ByteArray) {
  31. TODO("not implemented")
  32. }
  33. private fun ByteArray.asFeature() = serverFeatures[String(this)]
  34. private fun ByteArray.cast(to: KClass<out Any>): Any = when (to) {
  35. Int::class -> String(this).toInt()
  36. String::class -> String(this)
  37. CaseMapping::class -> CaseMapping.fromName(String(this))
  38. else -> TODO("not implemented")
  39. }
  40. }