選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ISupportProcessor.kt 2.6KB

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