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.

Dsl.kt 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.dmdirc.ktirc
  2. /**
  3. * Dsl marker for [IrcClient] dsl.
  4. */
  5. @DslMarker
  6. annotation class IrcClientDsl
  7. internal data class IrcClientConfig(val server: ServerConfig, val profile: ProfileConfig, val sasl: SaslConfig?)
  8. /**
  9. * Dsl for configuring an IRC Client.
  10. *
  11. * [server] and [profile] blocks are required. The full range of configuration options are:
  12. *
  13. * ```
  14. * server {
  15. * host = "irc.example.com" // Required
  16. * port = 6667
  17. * useTls = true
  18. * password = "H4ckTh3Pl4n3t"
  19. * }
  20. *
  21. * profile {
  22. * nickname = "MyBot" // Required
  23. * username = "bot
  24. * realName = "Botomatic v1.2"
  25. * }
  26. *
  27. * sasl {
  28. * username = "botaccount"
  29. * password = "s3cur3"
  30. * }
  31. * ```
  32. */
  33. @IrcClientDsl
  34. class IrcClientConfigBuilder {
  35. private var server: ServerConfig? = null
  36. private var profile: ProfileConfig? = null
  37. private var sasl: SaslConfig? = null
  38. /**
  39. * Configures the server that the IrcClient will connect to.
  40. *
  41. * At a minimum, [ServerConfig.host] must be supplied.
  42. */
  43. @IrcClientDsl
  44. fun server(block: ServerConfig.() -> Unit) {
  45. check(server == null) { "server may only be specified once" }
  46. server = ServerConfig().apply(block).also { check(it.host.isNotEmpty()) { "server.host must be specified" } }
  47. }
  48. /**
  49. * Configures the profile of the IrcClient user.
  50. *
  51. * At a minimum, [ProfileConfig.nickName] must be supplied.
  52. */
  53. @IrcClientDsl
  54. fun profile(block: ProfileConfig.() -> Unit) {
  55. check(profile == null) { "profile may only be specified once" }
  56. profile = ProfileConfig().apply(block).also { check(it.nickname.isNotEmpty()) { "profile.nickname must be specified" } }
  57. }
  58. /**
  59. * Configures SASL authentication (optional).
  60. */
  61. @IrcClientDsl
  62. fun sasl(block: SaslConfig.() -> Unit) {
  63. check(sasl == null) { "sasl may only be specified once" }
  64. sasl = SaslConfig().apply(block)
  65. }
  66. internal fun build() =
  67. IrcClientConfig(
  68. checkNotNull(server) { "Server must be specified " },
  69. checkNotNull(profile) { "Profile must be specified" },
  70. sasl)
  71. }
  72. /**
  73. * Dsl for configuring a server.
  74. */
  75. @IrcClientDsl
  76. class ServerConfig {
  77. /** The hostname (or IP address) of the server to connect to. */
  78. var host: String = ""
  79. /** The port to connect on. Defaults to 6667. */
  80. var port: Int = 6667
  81. /** Whether or not to use TLS (an encrypted connection). */
  82. var useTls: Boolean = false
  83. /** The password required to connect to the server, if any. */
  84. var password: String? = null
  85. }
  86. /**
  87. * Dsl for configuring a profile.
  88. */
  89. @IrcClientDsl
  90. class ProfileConfig {
  91. /** The initial nickname to use when connecting. */
  92. var nickname: String = ""
  93. /** The username (used in place of an ident response) to provide to the server. */
  94. var username: String = "KtIrc"
  95. /** The "real name" to provide to the server. */
  96. var realName: String = "KtIrc User"
  97. }
  98. /**
  99. * Dsl for configuring SASL authentication.
  100. */
  101. @IrcClientDsl
  102. class SaslConfig {
  103. /** The username to provide when authenticating using SASL. */
  104. var username: String = ""
  105. /** The username to provide when authenticating using SASL. */
  106. var password: String = ""
  107. }