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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.dmdirc.ktirc.messages.processors
  2. import com.dmdirc.ktirc.events.ServerFeaturesUpdated
  3. import com.dmdirc.ktirc.io.CaseMapping
  4. import com.dmdirc.ktirc.messages.RPL_ISUPPORT
  5. import com.dmdirc.ktirc.model.IrcMessage
  6. import com.dmdirc.ktirc.model.ModePrefixMapping
  7. import com.dmdirc.ktirc.model.ServerFeatureMap
  8. import com.dmdirc.ktirc.model.serverFeatures
  9. import com.dmdirc.ktirc.util.logger
  10. import kotlin.reflect.KClass
  11. internal class ISupportProcessor : MessageProcessor {
  12. private val log by logger()
  13. override val commands = arrayOf(RPL_ISUPPORT)
  14. override fun process(message: IrcMessage) = listOf(ServerFeaturesUpdated(message.metadata, ServerFeatureMap().apply {
  15. // Ignore the first (nickname) and last ("are supported by this server") params
  16. for (i in 1 until message.params.size - 1) {
  17. parseParam(message.params[i])
  18. }
  19. }))
  20. private fun ServerFeatureMap.parseParam(param: ByteArray) = when (param[0]) {
  21. '-'.toByte() -> resetFeature(param.sliceArray(1 until param.size))
  22. else -> when (val equals = param.indexOf('='.toByte())) {
  23. -1 -> enableFeatureWithDefault(param)
  24. else -> enableFeature(param.sliceArray(0 until equals), param.sliceArray(equals + 1 until param.size))
  25. }
  26. }
  27. private fun ServerFeatureMap.resetFeature(name: ByteArray) = name.asFeature()?.let {
  28. reset(it)
  29. log.finer { "Reset feature ${it::class}" }
  30. }
  31. @Suppress("UNCHECKED_CAST")
  32. private fun ServerFeatureMap.enableFeature(name: ByteArray, value: ByteArray) {
  33. name.asFeature()?.let { feature ->
  34. set(feature, value.cast(feature.type))
  35. log.finer { "Set feature ${feature::class} to ${String(value)}" }
  36. }
  37. }
  38. private fun ServerFeatureMap.enableFeatureWithDefault(name: ByteArray) {
  39. name.asFeature()?.let { feature ->
  40. when (feature.type) {
  41. Boolean::class -> set(feature, true)
  42. else -> TODO("not implemented")
  43. }
  44. }
  45. }
  46. private fun ByteArray.asFeature() = serverFeatures[String(this)]
  47. ?: run {
  48. log.warning { "Unknown feature in 005: ${String(this)}" }
  49. null
  50. }
  51. private fun ByteArray.cast(to: KClass<out Any>): Any = with (String(this)) {
  52. when (to) {
  53. Int::class -> toInt()
  54. String::class -> this
  55. CaseMapping::class -> CaseMapping.fromName(this)
  56. ModePrefixMapping::class -> indexOf(')').let { ModePrefixMapping(substring(1 until it), substring(it + 1)) }
  57. Array<String>::class -> split(',').toTypedArray()
  58. else -> TODO("not implemented")
  59. }
  60. }
  61. }