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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.events.*
  3. import com.dmdirc.ktirc.io.*
  4. import com.dmdirc.ktirc.messages.*
  5. import com.dmdirc.ktirc.model.*
  6. import kotlinx.coroutines.channels.map
  7. import kotlinx.coroutines.coroutineScope
  8. import kotlinx.coroutines.runBlocking
  9. import java.util.logging.Level
  10. import java.util.logging.LogManager
  11. interface IrcClient {
  12. suspend fun send(message: String)
  13. val serverState: ServerState
  14. val channelState: ChannelStateMap
  15. val userState: UserState
  16. fun onEvent(handler: (IrcEvent) -> Unit)
  17. val caseMapping: CaseMapping
  18. get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
  19. fun isLocalUser(user: User): Boolean = caseMapping.areEquivalent(user.nickname, serverState.localNickname)
  20. }
  21. // TODO: How should alternative nicknames work?
  22. // TODO: Should IRC Client take a pool of servers and rotate through, or make the caller do that?
  23. // TODO: Should there be a default profile?
  24. class IrcClientImpl(private val server: Server, private val profile: Profile) : IrcClient {
  25. var socketFactory: (String, Int, Boolean) -> LineBufferedSocket = ::KtorLineBufferedSocket
  26. override val serverState = ServerState(profile.initialNick)
  27. override val channelState = ChannelStateMap { caseMapping }
  28. override val userState = UserState { caseMapping }
  29. private val messageHandler = MessageHandler(messageProcessors.toList(), eventHandlers.toMutableList())
  30. private val parser = MessageParser()
  31. private var socket: LineBufferedSocket? = null
  32. // TODO: It would be cleaner if this didn't suspend but returned immediately
  33. override suspend fun send(message: String) {
  34. socket?.sendLine(message)
  35. }
  36. suspend fun connect() {
  37. // TODO: Concurrency!
  38. check(socket == null)
  39. coroutineScope {
  40. with(socketFactory(server.host, server.port, server.tls)) {
  41. socket = this
  42. connect()
  43. sendLine("CAP LS 302")
  44. server.password?.let { pass -> sendLine(passwordMessage(pass)) }
  45. sendLine(nickMessage(profile.initialNick))
  46. // TODO: Send correct host
  47. sendLine(userMessage(profile.userName, "localhost", server.host, profile.realName))
  48. // TODO: This should be elsewhere
  49. messageHandler.processMessages(this@IrcClientImpl, readLines(this@coroutineScope).map { parser.parse(it) })
  50. }
  51. }
  52. }
  53. fun disconnect() {
  54. socket?.disconnect()
  55. }
  56. override fun onEvent(handler: (IrcEvent) -> Unit) {
  57. messageHandler.handlers.add(object : EventHandler {
  58. override suspend fun processEvent(client: IrcClient, event: IrcEvent) {
  59. handler(event)
  60. }
  61. })
  62. }
  63. }
  64. fun main() {
  65. val rootLogger = LogManager.getLogManager().getLogger("")
  66. rootLogger.level = Level.FINEST
  67. for (h in rootLogger.handlers) {
  68. h.level = Level.FINEST
  69. }
  70. runBlocking {
  71. val client = IrcClientImpl(Server("testnet.inspircd.org", 6667), Profile("KtIrc", "Kotlin!", "kotlin"))
  72. client.onEvent { event ->
  73. runBlocking {
  74. when (event) {
  75. is ServerWelcome -> client.send(joinMessage("#ktirc"))
  76. is MessageReceived -> if (event.message == "!test") client.send(privmsgMessage(event.target, "Test successful!"))
  77. }
  78. }
  79. }
  80. client.connect()
  81. }
  82. }