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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. val caseMapping: CaseMapping
  23. get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
  24. /**
  25. * Begins a connection attempt to the IRC server.
  26. *
  27. * This method will return immediately, and the attempt to connect will be executed in a coroutine on the
  28. * IO scheduler. To check the status of the connection, monitor events using [onEvent].
  29. */
  30. fun connect()
  31. /**
  32. * Disconnect immediately from the IRC server, without sending a QUIT.
  33. */
  34. fun disconnect()
  35. /**
  36. * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
  37. *
  38. * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
  39. * such as [sendJoin].
  40. *
  41. * @param message The line to be sent to the IRC server.
  42. */
  43. fun send(message: String)
  44. /**
  45. * Registers a new handler for all events on this connection.
  46. *
  47. * All events are subclasses of [IrcEvent]; the idiomatic way to handle them is using a `when` statement:
  48. *
  49. * ```
  50. * client.onEvent {
  51. * when(it) {
  52. * is MessageReceived -> println(it.message)
  53. * }
  54. * }
  55. * ```
  56. *
  57. * *Note*: at present handlers cannot be removed; they last the lifetime of the [IrcClient].
  58. *
  59. * @param handler The method to call when a new event occurs.
  60. */
  61. fun onEvent(handler: (IrcEvent) -> Unit)
  62. /**
  63. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  64. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  65. */
  66. fun isLocalUser(user: User) = isLocalUser(user.nickname)
  67. /**
  68. * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
  69. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  70. */
  71. fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
  72. /**
  73. * Determines if the given [target] appears to be a channel or not. Should only be used after a
  74. * [com.dmdirc.ktirc.events.ServerReady] event has been received.
  75. */
  76. fun isChannel(target: String) = target.isNotEmpty() && serverState.channelTypes.contains(target[0])
  77. }
  78. /**
  79. * Constructs a new [IrcClient] using a configuration DSL.
  80. *
  81. * See [IrcClientConfigBuilder] for details of all options
  82. */
  83. @IrcClientDsl
  84. @Suppress("FunctionName")
  85. fun IrcClient(block: IrcClientConfigBuilder.() -> Unit): IrcClient =
  86. IrcClientImpl(IrcClientConfigBuilder().apply(block).build())