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

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