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.

MessageHandler.kt 1.0KB

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