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

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