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.

ServerState.kt 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.dmdirc.ktirc.state
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. import kotlin.reflect.KClass
  4. interface ServerState {
  5. var localNickname: String
  6. fun <T : Any> getFeature(feature: ServerFeature<T>): T?
  7. fun setFeature(feature: ServerFeature<*>, value: Any)
  8. fun resetFeature(feature: ServerFeature<*>): Any?
  9. }
  10. class IrcServerState(initialNickname: String) : ServerState {
  11. override var localNickname: String = initialNickname
  12. private val features = HashMap<ServerFeature<*>, Any>()
  13. @Suppress("UNCHECKED_CAST")
  14. override fun <T : Any> getFeature(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T?
  15. override fun setFeature(feature: ServerFeature<*>, value: Any) {
  16. require(feature.type.isInstance(value))
  17. features[feature] = value
  18. }
  19. override fun resetFeature(feature: ServerFeature<*>) = features.remove(feature)
  20. }
  21. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  22. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  23. object MaximumChannels : ServerFeature<Int>("CHANLIMIT", Int::class)
  24. object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
  25. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  26. }
  27. val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  28. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  29. }