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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * mechanisms += "PLAIN" // or to set the list from scratch:
  29. * mechanisms("PLAIN")
  30. *
  31. * username = "botaccount"
  32. * password = "s3cur3"
  33. * }
  34. * ```
  35. */
  36. @IrcClientDsl
  37. class IrcClientConfigBuilder {
  38. private var server: ServerConfig? = null
  39. private var profile: ProfileConfig? = null
  40. private var sasl: SaslConfig? = null
  41. /**
  42. * Configures the server that the IrcClient will connect to.
  43. *
  44. * At a minimum, [ServerConfig.host] must be supplied.
  45. */
  46. @IrcClientDsl
  47. fun server(block: ServerConfig.() -> Unit) {
  48. check(server == null) { "server may only be specified once" }
  49. server = ServerConfig().apply(block).also { check(it.host.isNotEmpty()) { "server.host must be specified" } }
  50. }
  51. /**
  52. * Configures the profile of the IrcClient user.
  53. *
  54. * At a minimum, [ProfileConfig.nickname] must be supplied.
  55. */
  56. @IrcClientDsl
  57. fun profile(block: ProfileConfig.() -> Unit) {
  58. check(profile == null) { "profile may only be specified once" }
  59. profile = ProfileConfig().apply(block).also { check(it.nickname.isNotEmpty()) { "profile.nickname must be specified" } }
  60. }
  61. /**
  62. * Configures SASL authentication (optional).
  63. */
  64. @IrcClientDsl
  65. fun sasl(block: SaslConfig.() -> Unit) {
  66. check(sasl == null) { "sasl may only be specified once" }
  67. sasl = SaslConfig().apply(block)
  68. }
  69. internal fun build() =
  70. IrcClientConfig(
  71. checkNotNull(server) { "Server must be specified " },
  72. checkNotNull(profile) { "Profile must be specified" },
  73. sasl)
  74. }
  75. /**
  76. * Dsl for configuring a server.
  77. */
  78. @IrcClientDsl
  79. class ServerConfig {
  80. /** The hostname (or IP address) of the server to connect to. */
  81. var host: String = ""
  82. /** The port to connect on. Defaults to 6667. */
  83. var port: Int = 6667
  84. /** Whether or not to use TLS (an encrypted connection). */
  85. var useTls: Boolean = false
  86. /** The password required to connect to the server, if any. */
  87. var password: String? = null
  88. }
  89. /**
  90. * Dsl for configuring a profile.
  91. */
  92. @IrcClientDsl
  93. class ProfileConfig {
  94. /** The initial nickname to use when connecting. */
  95. var nickname: String = ""
  96. /** The username (used in place of an ident response) to provide to the server. */
  97. var username: String = "KtIrc"
  98. /** The "real name" to provide to the server. */
  99. var realName: String = "KtIrc User"
  100. }
  101. /**
  102. * Dsl for configuring SASL authentication.
  103. *
  104. * By default the `PLAIN` method will be enabled if SASL is configured.
  105. *
  106. * You can modify the mechanisms either by editing the [mechanisms] collection:
  107. *
  108. * ```
  109. * mechanisms += "EXTERNAL"
  110. * mechanisms.remove("PLAIN")
  111. * ```
  112. *
  113. * or by calling the [mechanisms] function with all the mechanisms you wish
  114. * to enable:
  115. *
  116. * ```
  117. * mechanisms("PLAIN", "EXTERNAL")
  118. * ```
  119. *
  120. * Priority of mechanisms is determined by KtIrc, regardless of the order
  121. * they are specified in here.
  122. */
  123. @IrcClientDsl
  124. class SaslConfig {
  125. /** The SASL mechanisms to enable. */
  126. val mechanisms: MutableCollection<String> = mutableSetOf("PLAIN")
  127. /** The username to provide when authenticating using SASL. */
  128. var username: String = ""
  129. /** The username to provide when authenticating using SASL. */
  130. var password: String = ""
  131. @IrcClientDsl
  132. fun mechanisms(vararg methods: String) {
  133. with (this.mechanisms) {
  134. clear()
  135. addAll(methods)
  136. }
  137. }
  138. }