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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  27. }
  28. val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  29. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  30. }