Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

IrcClient.kt 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * Sends the given command to the IRC server, and waits for a response back.
  81. *
  82. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  83. * extension methods in [com.dmdirc.ktirc.messages] such as TODO: sendJoinAsync.
  84. *
  85. * This method will return immediately. If the server supports the labeled-responses capability,
  86. * the returned [Deferred] will be eventually populated with the response from the server. If
  87. * the server does not support the capability, or the response times out, `null` will be supplied.
  88. *
  89. * @param command The command to be sent.
  90. * @param arguments The arguments to the command.
  91. * @return A deferred [IrcEvent]? that contains the server's response to the command.
  92. */
  93. fun sendAsync(command: String, vararg arguments: String) = sendAsync(emptyMap(), command, *arguments)
  94. /**
  95. * Sends the given command to the IRC server, and waits for a response back.
  96. *
  97. * This should only be needed to send raw/custom commands; standard messages can be sent using the
  98. * extension methods in [com.dmdirc.ktirc.messages] such as TODO: sendJoinAsync.
  99. *
  100. * This method will return immediately. If the server supports the labeled-responses capability,
  101. * the returned [Deferred] will be eventually populated with the response from the server. If
  102. * the server does not support the capability, or the response times out, `null` will be supplied.
  103. *
  104. * @param tags The IRCv3 tags to prefix the message with, if any.
  105. * @param command The command to be sent.
  106. * @param arguments The arguments to the command.
  107. * @return A deferred [IrcEvent]? that contains the server's response to the command.
  108. */
  109. fun sendAsync(tags: Map<MessageTag, String>, command: String, vararg arguments: String): Deferred<IrcEvent?>
  110. /**
  111. * Registers a new handler for all events on this connection.
  112. *
  113. * All events are subclasses of [IrcEvent]; the idiomatic way to handle them is using a `when` statement:
  114. *
  115. * ```
  116. * client.onEvent {
  117. * when(it) {
  118. * is MessageReceived -> println(it.message)
  119. * }
  120. * }
  121. * ```
  122. *
  123. * *Note*: at present handlers cannot be removed; they last the lifetime of the [IrcClient].
  124. *
  125. * @param handler The method to call when a new event occurs.
  126. */
  127. fun onEvent(handler: (IrcEvent) -> Unit)
  128. /**
  129. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  130. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  131. */
  132. fun isLocalUser(user: User) = isLocalUser(user.nickname)
  133. /**
  134. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  135. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  136. */
  137. fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
  138. /**
  139. * Determines if the given [target] appears to be a channel or not. Should only be used after a
  140. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  141. */
  142. fun isChannel(target: String) = target.isNotEmpty() && serverState.channelTypes.contains(target[0])
  143. }
  144. /**
  145. * Defines the behaviour of an [IrcClient].
  146. */
  147. interface ClientBehaviour {
  148. /** Whether or not to request channel modes when we join a channel. */
  149. val requestModesOnJoin: Boolean
  150. /**
  151. * If enabled, all messages (`PRIVMSG`s) sent by the client will always be "echoed" back as a MessageReceived
  152. * event.
  153. *
  154. * This makes the behaviour consistent across ircds that support the echo-message capability and those that
  155. * don't. If disabled, messages will only be echoed back when the server supports the capability.
  156. */
  157. val alwaysEchoMessages: Boolean
  158. }
  159. /**
  160. * Constructs a new [IrcClient] using a configuration DSL.
  161. *
  162. * See [IrcClientConfigBuilder] for details of all options
  163. */
  164. @IrcClientDsl
  165. @Suppress("FunctionName")
  166. fun IrcClient(block: IrcClientConfigBuilder.() -> Unit): IrcClient =
  167. IrcClientImpl(IrcClientConfigBuilder().apply(block).build())