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.1KB

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