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 857B

1234567891011121314151617181920212223
  1. package com.dmdirc.ktirc.io
  2. import com.dmdirc.ktirc.events.IrcEvent
  3. import com.dmdirc.ktirc.messages.MessageProcessor
  4. import com.dmdirc.ktirc.util.logger
  5. import kotlinx.coroutines.channels.ReceiveChannel
  6. import kotlinx.coroutines.channels.consumeEach
  7. class MessageHandler(private val processors: Collection<MessageProcessor>, private val eventHandler: (IrcEvent) -> Unit) {
  8. private val log by logger()
  9. suspend fun processMessages(messages: ReceiveChannel<IrcMessage>) {
  10. messages.consumeEach { it.process().forEach(eventHandler) }
  11. }
  12. private fun IrcMessage.process() = this.getProcessor()?.process(this) ?: emptyList()
  13. private fun IrcMessage.getProcessor() = processors.firstOrNull { it.commands.contains(command) } ?: run {
  14. log.warning { "No processor found for $command" }
  15. null
  16. }
  17. }