Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ServerState.kt 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. import kotlin.reflect.KClass
  4. class ServerState(initialNickname: String) {
  5. var localNickname: String = initialNickname
  6. val features = ServerFeatureMap()
  7. }
  8. class ServerFeatureMap {
  9. private val features = HashMap<ServerFeature<*>, Any?>()
  10. @Suppress("UNCHECKED_CAST")
  11. operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
  12. operator fun set(feature: ServerFeature<*>, value: Any) {
  13. require(feature.type.isInstance(value))
  14. features[feature] = value
  15. }
  16. fun setAll(featureMap: ServerFeatureMap) = featureMap.features.forEach { feature, value -> features[feature] = value }
  17. fun reset(feature: ServerFeature<*>) = features.put(feature, null)
  18. }
  19. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  20. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  21. object MaximumChannels : ServerFeature<Int>("CHANLIMIT", Int::class)
  22. object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
  23. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  24. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  25. }
  26. val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  27. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  28. }