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.

IrcClient.kt 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.events.IrcEvent
  3. import com.dmdirc.ktirc.io.CaseMapping
  4. import com.dmdirc.ktirc.messages.sendJoin
  5. import com.dmdirc.ktirc.messages.tagMap
  6. import com.dmdirc.ktirc.model.*
  7. import com.dmdirc.ktirc.util.RemoveIn
  8. import kotlinx.coroutines.Deferred
  9. /**
  10. * Primary interface for interacting with KtIrc.
  11. */
  12. interface IrcClient {
  13. /**
  14. * Holds state relating to the current server, its features, and capabilities.
  15. */
  16. val serverState: ServerState
  17. /**
  18. * Holds the state for each channel we are currently joined to.
  19. */
  20. val channelState: ChannelStateMap
  21. /**
  22. * Holds the state for all known users (those in common channels).
  23. */
  24. val userState: UserState
  25. /**
  26. * The configured behaviour of the client.
  27. */
  28. val behaviour: ClientBehaviour
  29. /**
  30. * The current case-mapping of the server, defining how uppercase and lowercase nicks/channels/etc are mapped
  31. * to one another.
  32. *
  33. * This may change over the lifetime of an [IrcClient]. It should not be stored, and should be checked each
  34. * time it is needed.
  35. */
  36. val caseMapping: CaseMapping
  37. get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
  38. /**
  39. * Begins a connection attempt to the IRC server.
  40. *
  41. * This method will return immediately, and the attempt to connect will be executed in a coroutine on the
  42. * IO scheduler. To check the status of the connection, monitor events using [onEvent].
  43. */
  44. fun connect()
  45. /**
  46. * Disconnect immediately from the IRC server, without sending a QUIT.
  47. */
  48. fun disconnect()
  49. /**
  50. * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
  51. *
  52. * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
  53. * such as [sendJoin].
  54. *
  55. * @param message The line to be sent to the IRC server.
  56. */
  57. @Deprecated("Use structured send instead", ReplaceWith("send(command, arguments)"))
  58. @RemoveIn("2.0.0")
  59. fun send(message: String)
  60. /**
  61. * Sends the given command to the IRC server.
  62. *
  63. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  64. * extension methods in [com.dmdirc.ktirc.messages] such as [sendJoin].
  65. *
  66. * This method will return immediately; the message will be delivered by a coroutine. Messages
  67. * are guaranteed to be delivered in order when this method is called multiple times.
  68. *
  69. * @param tags The IRCv3 tags to prefix the message with, if any.
  70. * @param command The command to be sent.
  71. * @param arguments The arguments to the command.
  72. */
  73. fun send(tags: Map<MessageTag, String>, command: String, vararg arguments: String)
  74. /**
  75. * Sends the given command to the IRC server.
  76. *
  77. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  78. * extension methods in [com.dmdirc.ktirc.messages] such as [sendJoin].
  79. *
  80. * This method will return immediately; the message will be delivered by a coroutine. Messages
  81. * are guaranteed to be delivered in order when this method is called multiple times.
  82. *
  83. * @param command The command to be sent.
  84. * @param arguments The arguments to the command.
  85. */
  86. fun send(command: String, vararg arguments: String) = send(emptyMap(), command, *arguments)
  87. /**
  88. * Registers a new handler for all events on this connection.
  89. *
  90. * All events are subclasses of [IrcEvent]; the idiomatic way to handle them is using a `when` statement:
  91. *
  92. * ```
  93. * client.onEvent {
  94. * when(it) {
  95. * is MessageReceived -> println(it.message)
  96. * }
  97. * }
  98. * ```
  99. *
  100. * *Note*: at present handlers cannot be removed; they last the lifetime of the [IrcClient].
  101. *
  102. * @param handler The method to call when a new event occurs.
  103. */
  104. fun onEvent(handler: (IrcEvent) -> Unit)
  105. /**
  106. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  107. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  108. */
  109. fun isLocalUser(user: User) = isLocalUser(user.nickname)
  110. /**
  111. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  112. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  113. */
  114. fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
  115. /**
  116. * Determines if the given [target] appears to be a channel or not. Should only be used after a
  117. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  118. */
  119. fun isChannel(target: String) = target.isNotEmpty() && serverState.channelTypes.contains(target[0])
  120. }
  121. internal interface ExperimentalIrcClient : IrcClient {
  122. /**
  123. * Sends the given command to the IRC server, and waits for a response back.
  124. *
  125. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  126. * extension methods in [com.dmdirc.ktirc.messages] such as [com.dmdirc.ktirc.messages.sendPartAsync].
  127. *
  128. * This method will return immediately. The returned [Deferred] will eventually be populated with
  129. * the server's response. If the server supports the labeled-responses capability, a label will
  130. * be added to the outgoing message to identify the correct response; otherwise the [matcher]
  131. * will be invoked on all incoming events to select the appropriate response.
  132. *
  133. * If the response times out, `null` will be supplied instead of an event.
  134. *
  135. * @param command The command to be sent.
  136. * @param arguments The arguments to the command.
  137. * @param matcher The matcher to use to find a matching event.
  138. * @return A deferred [IrcEvent]? that contains the server's response to the command.
  139. */
  140. fun sendAsync(command: String, arguments: Array<String>, matcher: (IrcEvent) -> Boolean) = sendAsync(tagMap(), command, arguments, matcher)
  141. /**
  142. * Sends the given command to the IRC server, and waits for a response back.
  143. *
  144. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  145. * extension methods in [com.dmdirc.ktirc.messages] such as [com.dmdirc.ktirc.messages.sendPartAsync].
  146. *
  147. * This method will return immediately. The returned [Deferred] will eventually be populated with
  148. * the server's response. If the server supports the labeled-responses capability, a label will
  149. * be added to the outgoing message to identify the correct response; otherwise the [matcher]
  150. * will be invoked on all incoming events to select the appropriate response.
  151. *
  152. * If the response times out, `null` will be supplied instead of an event.
  153. *
  154. * @param tags The IRCv3 tags to prefix the message with, if any.
  155. * @param command The command to be sent.
  156. * @param arguments The arguments to the command.
  157. * @param matcher The matcher to use to find a matching event.
  158. * @return A deferred [IrcEvent]? that contains the server's response to the command.
  159. */
  160. fun sendAsync(tags: Map<MessageTag, String>, command: String, arguments: Array<String>, matcher: (IrcEvent) -> Boolean): Deferred<IrcEvent?>
  161. }
  162. /**
  163. * Defines the behaviour of an [IrcClient].
  164. */
  165. interface ClientBehaviour {
  166. /** Whether or not to request channel modes when we join a channel. */
  167. val requestModesOnJoin: Boolean
  168. /**
  169. * If enabled, all messages (`PRIVMSG`s) sent by the client will always be "echoed" back as a MessageReceived
  170. * event.
  171. *
  172. * This makes the behaviour consistent across ircds that support the echo-message capability and those that
  173. * don't. If disabled, messages will only be echoed back when the server supports the capability.
  174. */
  175. val alwaysEchoMessages: Boolean
  176. }
  177. /**
  178. * Constructs a new [IrcClient] using a configuration DSL.
  179. *
  180. * See [IrcClientConfigBuilder] for details of all options
  181. */
  182. @IrcClientDsl
  183. @Suppress("FunctionName")
  184. fun IrcClient(block: IrcClientConfigBuilder.() -> Unit): IrcClient =
  185. IrcClientImpl(IrcClientConfigBuilder().apply(block).build())