Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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