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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. val capabilities = CapabilitiesState()
  8. }
  9. class ServerFeatureMap {
  10. private val features = HashMap<ServerFeature<*>, Any?>()
  11. @Suppress("UNCHECKED_CAST")
  12. operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
  13. internal operator fun set(feature: ServerFeature<*>, value: Any) {
  14. require(feature.type.isInstance(value))
  15. features[feature] = value
  16. }
  17. internal fun setAll(featureMap: ServerFeatureMap) = featureMap.features.forEach { feature, value -> features[feature] = value }
  18. internal fun reset(feature: ServerFeature<*>) = features.put(feature, null)
  19. }
  20. data class ModePrefixMapping(val modes: String, val prefixes: String) {
  21. fun isPrefix(char: Char) = prefixes.contains(char)
  22. fun getMode(prefix: Char) = modes[prefixes.indexOf(prefix)]
  23. fun getModes(prefixes: String) = String(prefixes.map(this::getMode).toCharArray())
  24. }
  25. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  26. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  27. object ModePrefixes : ServerFeature<ModePrefixMapping>("PREFIX", ModePrefixMapping::class, ModePrefixMapping("ov", "@+"))
  28. object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
  29. object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
  30. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  31. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  32. }
  33. internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  34. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  35. }