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

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