Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ServerState.kt 4.0KB

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