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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.dmdirc.ktirc
  2. /**
  3. * Dsl marker for [IrcClient] dsl.
  4. */
  5. @DslMarker
  6. annotation class IrcClientDsl
  7. internal data class IrcClientConfig(
  8. val server: ServerConfig,
  9. val profile: ProfileConfig,
  10. val behaviour: ClientBehaviour,
  11. val sasl: SaslConfig?)
  12. /**
  13. * Dsl for configuring an IRC Client.
  14. *
  15. * [server] and [profile] blocks are required. The full range of configuration options are:
  16. *
  17. * ```
  18. * server {
  19. * host = "irc.example.com" // Required
  20. * port = 6667
  21. * useTls = true
  22. * password = "H4ckTh3Pl4n3t"
  23. * }
  24. *
  25. * profile {
  26. * nickname = "MyBot" // Required
  27. * username = "bot"
  28. * realName = "Botomatic v1.2"
  29. * }
  30. *
  31. * behaviour {
  32. * requestModesOnJoin = true
  33. * alwaysEchoMessages = true
  34. * preferIPv6 = false
  35. * }
  36. *
  37. * sasl {
  38. * mechanisms += "PLAIN" // or to set the list from scratch:
  39. * mechanisms("PLAIN")
  40. *
  41. * username = "botaccount"
  42. * password = "s3cur3"
  43. * }
  44. * ```
  45. */
  46. @IrcClientDsl
  47. class IrcClientConfigBuilder {
  48. private var server: ServerConfig? = null
  49. set(value) {
  50. check(field == null) { "server may only be specified once" }
  51. check(!value?.host.isNullOrEmpty()) { "server.host must be specified" }
  52. field = value
  53. }
  54. private var profile: ProfileConfig? = null
  55. set(value) {
  56. check(field == null) { "profile may only be specified once" }
  57. check(!value?.nickname.isNullOrEmpty()) { "profile.nickname must be specified" }
  58. field = value
  59. }
  60. private var behaviour: BehaviourConfig? = null
  61. set(value) {
  62. check(field == null) { "behaviour may only be specified once" }
  63. field = value
  64. }
  65. private var sasl: SaslConfig? = null
  66. set(value) {
  67. check(field == null) { "sasl may only be specified once" }
  68. field = value
  69. }
  70. /**
  71. * Configures the server that the IrcClient will connect to.
  72. *
  73. * See [ServerConfig] for details of each parameter.
  74. *
  75. * @param block Optional additional configuration to apply to the [ServerConfig]
  76. */
  77. @IrcClientDsl
  78. fun server(host: String? = null, port: Int? = null, useTls: Boolean? = null, password: String? = null, block: (ServerConfig.() -> Unit)? = null) {
  79. server = ServerConfig().apply {
  80. host?.let { this.host = it }
  81. port?.let { this.port = it }
  82. useTls?.let { this.useTls = it }
  83. password?.let { this.password = it }
  84. block?.let { apply(it) }
  85. }
  86. }
  87. /**
  88. * Configures the profile of the IrcClient user.
  89. *
  90. * See [ProfileConfig] for details of each parameter.
  91. *
  92. * @param block Optional additional configuration to apply to the [ProfileConfig]
  93. */
  94. @IrcClientDsl
  95. fun profile(nickname: String? = null, username: String? = null, realName: String? = null, block: (ProfileConfig.() -> Unit)? = null) {
  96. profile = ProfileConfig().apply {
  97. nickname?.let { this.nickname = it }
  98. username?.let { this.username = it }
  99. realName?.let { this.realName = it }
  100. block?.let { apply(it) }
  101. }
  102. }
  103. /**
  104. * Configures the behaviour of the client (optional).
  105. */
  106. @IrcClientDsl
  107. fun behaviour(block: BehaviourConfig.() -> Unit) {
  108. behaviour = BehaviourConfig().apply(block)
  109. }
  110. /**
  111. * Configures SASL authentication (optional).
  112. */
  113. @IrcClientDsl
  114. fun sasl(block: SaslConfig.() -> Unit) {
  115. sasl = SaslConfig().apply(block)
  116. }
  117. internal fun build() =
  118. IrcClientConfig(
  119. checkNotNull(server) { "Server must be specified " },
  120. checkNotNull(profile) { "Profile must be specified" },
  121. behaviour ?: BehaviourConfig(),
  122. sasl)
  123. }
  124. /**
  125. * Dsl for configuring a server.
  126. */
  127. @IrcClientDsl
  128. class ServerConfig {
  129. /** The hostname (or IP address) of the server to connect to. */
  130. var host: String = ""
  131. /** The port to connect on. Defaults to 6697. */
  132. var port: Int = 6697
  133. /** Whether or not to use TLS (an encrypted connection). */
  134. var useTls: Boolean = true
  135. /** The password required to connect to the server, if any. */
  136. var password: String? = null
  137. }
  138. /**
  139. * Dsl for configuring a profile.
  140. */
  141. @IrcClientDsl
  142. class ProfileConfig {
  143. /** The initial nickname to use when connecting. */
  144. var nickname: String = ""
  145. /** The username (used in place of an ident response) to provide to the server. */
  146. var username: String = "KtIrc"
  147. /** The "real name" to provide to the server. */
  148. var realName: String = "KtIrc User"
  149. }
  150. /**
  151. * Dsl for configuring SASL authentication.
  152. *
  153. * By default the `PLAIN`, `SCRAM-SHA-1`, and `SCRAM-SHA-256` methods will be enabled if SASL is configured.
  154. *
  155. * You can modify the mechanisms either by editing the [mechanisms] collection:
  156. *
  157. * ```
  158. * mechanisms += "EXTERNAL"
  159. * mechanisms.remove("PLAIN")
  160. * ```
  161. *
  162. * or by calling the [mechanisms] function with all the mechanisms you wish
  163. * to enable:
  164. *
  165. * ```
  166. * mechanisms("PLAIN", "EXTERNAL")
  167. * ```
  168. *
  169. * Priority of mechanisms is determined by KtIrc, regardless of the order
  170. * they are specified in here.
  171. */
  172. @IrcClientDsl
  173. class SaslConfig {
  174. /** The SASL mechanisms to enable. */
  175. val mechanisms: MutableCollection<String> = mutableSetOf("PLAIN", "SCRAM-SHA-1", "SCRAM-SHA-256")
  176. /** The username to provide when authenticating using SASL. */
  177. var username: String = ""
  178. /** The username to provide when authenticating using SASL. */
  179. var password: String = ""
  180. /**
  181. * Replaces all enabled SASL mechanisms with the given ones.
  182. */
  183. @IrcClientDsl
  184. fun mechanisms(vararg methods: String) {
  185. with(this.mechanisms) {
  186. clear()
  187. addAll(methods)
  188. }
  189. }
  190. }
  191. /**
  192. * Dsl for configuring the behaviour of an [IrcClient].
  193. */
  194. @IrcClientDsl
  195. class BehaviourConfig : ClientBehaviour {
  196. override var requestModesOnJoin = false
  197. override var alwaysEchoMessages = false
  198. override var preferIPv6 = true
  199. }