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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.SaslConfig
  3. import com.dmdirc.ktirc.io.CaseMapping
  4. import com.dmdirc.ktirc.util.logger
  5. import kotlin.reflect.KClass
  6. /**
  7. * Contains the current state of a single IRC server.
  8. */
  9. class ServerState internal constructor(
  10. private val initialNickname: String,
  11. private val initialServerName: String,
  12. saslConfig: SaslConfig? = null) {
  13. private val log by logger()
  14. /** Whether we've received the 'Welcome to IRC' (001) message. */
  15. internal var receivedWelcome = false
  16. /** The current status of the server. */
  17. var status = ServerStatus.Disconnected
  18. internal set
  19. /**
  20. * What we believe our current nickname to be on the server.
  21. *
  22. * Initially this will be the nickname provided in the config. It will be updated to the actual nickname
  23. * in use when connecting. Once you have received a [com.dmdirc.ktirc.events.ServerWelcome] event you can
  24. * rely on this value being current.
  25. * */
  26. var localNickname: String = initialNickname
  27. internal set
  28. /**
  29. * The name of the server we are connected to.
  30. *
  31. * Initially this will be the hostname or IP address provided in the config. It will be updated to the server's
  32. * self-reported hostname when connecting. Once you have received a [com.dmdirc.ktirc.events.ServerWelcome] event
  33. * you can rely on this value being current.
  34. */
  35. var serverName: String = initialServerName
  36. internal set
  37. /** The features that the server has declared it supports (from the 005 header). */
  38. val features = ServerFeatureMap()
  39. /** The capabilities we have negotiated with the server (from IRCv3). */
  40. val capabilities = CapabilitiesState()
  41. /** The current state of SASL authentication. */
  42. internal val sasl = SaslState(saslConfig)
  43. /**
  44. * Convenience accessor for the [ServerFeature.ModePrefixes] feature, which will always have a value.
  45. */
  46. val channelModePrefixes
  47. get() = features[ServerFeature.ModePrefixes] ?: throw IllegalStateException("lost mode prefixes")
  48. /**
  49. * Determines if the given mode is one applied to a user of a channel, such as 'o' for operator.
  50. */
  51. fun isChannelUserMode(mode: Char) = channelModePrefixes.isMode(mode)
  52. /**
  53. * Determines what type of channel mode the given character is, based on the server features.
  54. *
  55. * If the mode isn't found, or the server hasn't provided modes, it is presumed to be [ChannelModeType.NoParameter].
  56. */
  57. fun channelModeType(mode: Char): ChannelModeType {
  58. features[ServerFeature.ChannelModes]?.forEachIndexed { index, modes ->
  59. if (mode in modes) {
  60. return ChannelModeType.values()[index]
  61. }
  62. }
  63. log.warning { "Unknown channel mode $mode, assuming it's boolean" }
  64. return ChannelModeType.NoParameter
  65. }
  66. internal fun reset() {
  67. receivedWelcome = false
  68. status = ServerStatus.Disconnected
  69. localNickname = initialNickname
  70. serverName = initialServerName
  71. features.clear()
  72. capabilities.reset()
  73. sasl.reset()
  74. }
  75. }
  76. /**
  77. * Maps known features onto their values, enforcing type safety.
  78. */
  79. class ServerFeatureMap {
  80. private val features = HashMap<ServerFeature<*>, Any?>()
  81. /**
  82. * Gets the value, or the default value, of the given feature.
  83. */
  84. @Suppress("UNCHECKED_CAST")
  85. operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
  86. internal operator fun set(feature: ServerFeature<*>, value: Any) {
  87. require(feature.type.isInstance(value)) {
  88. "Value given for feature ${feature::class} must be type ${feature.type} but was ${value::class}"
  89. }
  90. features[feature] = value
  91. }
  92. internal fun setAll(featureMap: ServerFeatureMap) = featureMap.features.forEach { feature, value -> features[feature] = value }
  93. internal fun reset(feature: ServerFeature<*>) = features.put(feature, null)
  94. internal fun clear() = features.clear()
  95. internal fun isEmpty() = features.isEmpty()
  96. }
  97. /**
  98. * Stores the mapping of mode prefixes received from the server.
  99. */
  100. data class ModePrefixMapping(val modes: String, val prefixes: String) {
  101. /** Determines whether the given character is a mode prefix (e.g. "@", "+"). */
  102. fun isPrefix(char: Char) = prefixes.contains(char)
  103. /** Determines whether the given character is a channel user mode (e.g. "o", "v"). */
  104. fun isMode(char: Char) = modes.contains(char)
  105. /** Gets the mode corresponding to the given prefix (e.g. "@" -> "o"). */
  106. fun getMode(prefix: Char) = modes[prefixes.indexOf(prefix)]
  107. /** Gets the modes corresponding to the given prefixes (e.g. "@+" -> "ov"). */
  108. fun getModes(prefixes: String) = String(prefixes.map(this::getMode).toCharArray())
  109. }
  110. /**
  111. * Describes a server feature determined from the 005 response.
  112. */
  113. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  114. /** The network the server says it belongs to. */
  115. object Network : ServerFeature<String>("NETWORK", String::class)
  116. /** The case mapping the server uses, defaulting to RFC. */
  117. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  118. /** The mode prefixes the server uses, defaulting to ov/@+. */
  119. object ModePrefixes : ServerFeature<ModePrefixMapping>("PREFIX", ModePrefixMapping::class, ModePrefixMapping("ov", "@+"))
  120. /** The maximum number of channels a client may join. */
  121. object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
  122. /** The modes supported in channels. */
  123. object ChannelModes : ServerFeature<Array<String>>("CHANMODES", Array<String>::class)
  124. /** The maximum length of a channel name, defaulting to 200. */
  125. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  126. /** Whether or not the server supports extended who. */
  127. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  128. }
  129. /**
  130. * Enumeration of the possible states of a server.
  131. */
  132. enum class ServerStatus {
  133. /** The server is not connected. */
  134. Disconnected,
  135. /** We are attempting to connect to the server. It is not yet ready for use. */
  136. Connecting,
  137. /** We are logging in, dealing with capabilities, etc. The server is not yet ready for use. */
  138. Negotiating,
  139. /** We are connected and commands can be sent. */
  140. Ready,
  141. }
  142. internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  143. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  144. }