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.

MessageHandler.kt 1.1KB

12345678910111213141516171819202122232425262728293031
  1. package com.dmdirc.ktirc.io
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.events.EventHandler
  4. import com.dmdirc.ktirc.messages.MessageProcessor
  5. import com.dmdirc.ktirc.model.IrcMessage
  6. import com.dmdirc.ktirc.util.logger
  7. import kotlinx.coroutines.channels.ReceiveChannel
  8. import kotlinx.coroutines.channels.consumeEach
  9. class MessageHandler(private val processors: List<MessageProcessor>, val handlers: MutableList<EventHandler>) {
  10. private val log by logger()
  11. suspend fun processMessages(ircClient: IrcClient, messages: ReceiveChannel<IrcMessage>) {
  12. messages.consumeEach {
  13. it.process().forEach { event ->
  14. handlers.forEach { handler ->
  15. handler.processEvent(ircClient, event)
  16. }
  17. }
  18. }
  19. }
  20. private fun IrcMessage.process() = this.getProcessor()?.process(this) ?: emptyList()
  21. private fun IrcMessage.getProcessor() = processors.firstOrNull { it.commands.contains(command) } ?: run {
  22. log.warning { "No processor found for $command" }
  23. null
  24. }
  25. }