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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /**
  7. * Primary interface for interacting with KtIrc.
  8. */
  9. interface IrcClient {
  10. /**
  11. * Holds state relating to the current server, its features, and capabilities.
  12. */
  13. val serverState: ServerState
  14. /**
  15. * Holds the state for each channel we are currently joined to.
  16. */
  17. val channelState: ChannelStateMap
  18. /**
  19. * Holds the state for all known users (those in common channels).
  20. */
  21. val userState: UserState
  22. /**
  23. * The configured behaviour of the client.
  24. */
  25. val behaviour: ClientBehaviour
  26. val caseMapping: CaseMapping
  27. get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
  28. /**
  29. * Begins a connection attempt to the IRC server.
  30. *
  31. * This method will return immediately, and the attempt to connect will be executed in a coroutine on the
  32. * IO scheduler. To check the status of the connection, monitor events using [onEvent].
  33. */
  34. fun connect()
  35. /**
  36. * Disconnect immediately from the IRC server, without sending a QUIT.
  37. */
  38. fun disconnect()
  39. /**
  40. * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
  41. *
  42. * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
  43. * such as [sendJoin].
  44. *
  45. * @param message The line to be sent to the IRC server.
  46. */
  47. fun send(message: String)
  48. /**
  49. * Registers a new handler for all events on this connection.
  50. *
  51. * All events are subclasses of [IrcEvent]; the idiomatic way to handle them is using a `when` statement:
  52. *
  53. * ```
  54. * client.onEvent {
  55. * when(it) {
  56. * is MessageReceived -> println(it.message)
  57. * }
  58. * }
  59. * ```
  60. *
  61. * *Note*: at present handlers cannot be removed; they last the lifetime of the [IrcClient].
  62. *
  63. * @param handler The method to call when a new event occurs.
  64. */
  65. fun onEvent(handler: (IrcEvent) -> Unit)
  66. /**
  67. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  68. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  69. */
  70. fun isLocalUser(user: User) = isLocalUser(user.nickname)
  71. /**
  72. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  73. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  74. */
  75. fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
  76. /**
  77. * Determines if the given [target] appears to be a channel or not. Should only be used after a
  78. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  79. */
  80. fun isChannel(target: String) = target.isNotEmpty() && serverState.channelTypes.contains(target[0])
  81. }
  82. /**
  83. * Defines the behaviour of an [IrcClient].
  84. */
  85. interface ClientBehaviour {
  86. /** Whether or not to request channel modes when we join a channel. */
  87. val requestModesOnJoin: Boolean
  88. }
  89. /**
  90. * Constructs a new [IrcClient] using a configuration DSL.
  91. *
  92. * See [IrcClientConfigBuilder] for details of all options
  93. */
  94. @IrcClientDsl
  95. @Suppress("FunctionName")
  96. fun IrcClient(block: IrcClientConfigBuilder.() -> Unit): IrcClient =
  97. IrcClientImpl(IrcClientConfigBuilder().apply(block).build())