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

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