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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, initialServerName: String) {
  8. /** Whether we've received the 'Welcome to IRC' (001) message. */
  9. internal var receivedWelcome = false
  10. /** The current status of the server. */
  11. var status = ServerStatus.Connecting
  12. internal set
  13. /** Our present nickname on the server. */
  14. var localNickname: String = initialNickname
  15. internal set
  16. /** The server's name, as reported by it. */
  17. var serverName: String = initialServerName
  18. internal set
  19. /** The features that the server has declared it supports (from the 005 header). */
  20. val features = ServerFeatureMap()
  21. /** The capabilities we have negotiated with the server (from IRCv3). */
  22. val capabilities = CapabilitiesState()
  23. }
  24. /**
  25. * Maps known features onto their values, enforcing type safety.
  26. */
  27. class ServerFeatureMap {
  28. private val features = HashMap<ServerFeature<*>, Any?>()
  29. /**
  30. * Gets the value, or the default value, of the given feature.
  31. */
  32. @Suppress("UNCHECKED_CAST")
  33. operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
  34. internal operator fun set(feature: ServerFeature<*>, value: Any) {
  35. require(feature.type.isInstance(value))
  36. features[feature] = value
  37. }
  38. internal fun setAll(featureMap: ServerFeatureMap) = featureMap.features.forEach { feature, value -> features[feature] = value }
  39. internal fun reset(feature: ServerFeature<*>) = features.put(feature, null)
  40. }
  41. /**
  42. * Stores the mapping of mode prefixes received from the server.
  43. */
  44. data class ModePrefixMapping(val modes: String, val prefixes: String) {
  45. /** Determines whether the given character is a mode prefix (e.g. "@", "+"). */
  46. fun isPrefix(char: Char) = prefixes.contains(char)
  47. /** Gets the mode corresponding to the given prefix (e.g. "@" -> "o"). */
  48. fun getMode(prefix: Char) = modes[prefixes.indexOf(prefix)]
  49. /** Gets the modes corresponding to the given prefixes (e.g. "@+" -> "ov"). */
  50. fun getModes(prefixes: String) = String(prefixes.map(this::getMode).toCharArray())
  51. }
  52. /**
  53. * Describes a server feature determined from the 005 response.
  54. */
  55. sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val default: T? = null) {
  56. /** The network the server says it belongs to. */
  57. object Network : ServerFeature<String>("NETWORK", String::class)
  58. /** The case mapping the server uses, defaulting to RFC. */
  59. object ServerCaseMapping : ServerFeature<CaseMapping>("CASEMAPPING", CaseMapping::class, CaseMapping.Rfc)
  60. /** The mode prefixes the server uses, defaulting to ov/@+. */
  61. object ModePrefixes : ServerFeature<ModePrefixMapping>("PREFIX", ModePrefixMapping::class, ModePrefixMapping("ov", "@+"))
  62. /** The maximum number of channels a client may join. */
  63. object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
  64. /** The modes supported in channels. */
  65. object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
  66. /** The maximum length of a channel name, defaulting to 200. */
  67. object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
  68. /** Whether or not the server supports extended who. */
  69. object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
  70. }
  71. internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
  72. ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
  73. }
  74. /**
  75. * Enumeration of the possible states of a server.
  76. */
  77. enum class ServerStatus {
  78. /** We are attempting to connect to the server. It is not yet ready for use. */
  79. Connecting,
  80. /** We are logging in, dealing with capabilities, etc. The server is not yet ready for use. */
  81. Negotiating,
  82. /** We are connected and commands can be sent. */
  83. Ready,
  84. }