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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. import kotlin.reflect.KClass
  4. /**
  5. * Contains the current state of a single IRC server.
  6. */
  7. class ServerState internal constructor(initialNickname: String) {
  8. /** The current status of the server. */
  9. var status = ServerStatus.Connecting
  10. internal set
  11. /** Our present nickname on the server. */
  12. var localNickname: String = initialNickname
  13. internal set
  14. /** The features that the server has declared it supports (from the 005 header). */
  15. val features = ServerFeatureMap()
  16. /** The capabilities we have negotiated with the server (from IRCv3). */
  17. val capabilities = CapabilitiesState()
  18. }
  19. class ServerFeatureMap {
  20. private val features = HashMap<ServerFeature<*>, Any?>()
  21. @Suppress("UNCHECKED_CAST")
  22. operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
  23. internal operator fun set(feature: ServerFeature<*>, value: Any) {
  24. require(feature.type.isInstance(value))
  25. features[feature] = value
  26. }
  27. internal fun setAll(featureMap: ServerFeatureMap) = featureMap.features.forEach { feature, value -> features[feature] = value }
  28. internal fun reset(feature: ServerFeature<*>) = features.put(feature, null)
  29. }
  30. data class ModePrefixMapping(val modes: String, val prefixes: String) {
  31. fun isPrefix(char: Char) = prefixes.contains(char)
  32. fun getMode(prefix: Char) = modes[prefixes.indexOf(prefix)]
  33. fun getModes(prefixes: String) = String(prefixes.map(this::getMode).toCharArray())
  34. }
  35. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  36. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  37. object ModePrefixes : ServerFeature<ModePrefixMapping>("PREFIX", ModePrefixMapping::class, ModePrefixMapping("ov", "@+"))
  38. object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
  39. object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
  40. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  41. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  42. }
  43. internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  44. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  45. }
  46. /**
  47. * Enumeration of the possible states of a server.
  48. */
  49. enum class ServerStatus {
  50. /** We are attempting to connect to the server. It is not yet ready for use. */
  51. Connecting,
  52. /** We are logging in, dealing with capabilities, etc. The server is not yet ready for use. */
  53. Negotiating,
  54. /** We are connected and commands can be sent. */
  55. Ready,
  56. }