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.

EventUtils.kt 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.dmdirc.ktirc.events
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.messages.sendMessage
  4. import com.dmdirc.ktirc.messages.sendTagMessage
  5. import com.dmdirc.ktirc.model.MessageTag
  6. import com.dmdirc.ktirc.model.ServerFeature
  7. import com.dmdirc.ktirc.model.asUser
  8. internal fun ChannelNamesReceived.toModesAndUsers(client: IrcClient) = sequence {
  9. val modePrefixes = client.serverState.features[ServerFeature.ModePrefixes]!!
  10. for (user in names) {
  11. user.takeWhile { modePrefixes.isPrefix(it) }.let { prefix ->
  12. yield(modePrefixes.getModes(prefix) to user.substring(prefix.length).asUser())
  13. }
  14. }
  15. }.toList()
  16. /**
  17. * Replies in the appropriate place to a message received.
  18. *
  19. * Messages sent direct to the client will be responded to in direct message back; messages sent to a channel
  20. * will be replied to in the channel. If [prefixWithNickname] is `true`, channel messages will be prefixed
  21. * with the other user's nickname (separated from the message by a colon and space).
  22. *
  23. * If the given [message] has a [com.dmdirc.ktirc.model.MessageTag.MessageId] tag then the reply will include
  24. * the message ID to tell other IRCv3 capable clients what message is being replied to.
  25. */
  26. fun IrcClient.reply(message: MessageReceived, response: String, prefixWithNickname: Boolean = false) {
  27. if (isToMe(message)) {
  28. sendMessage(message.user.nickname, response, message.metadata.messageId)
  29. } else {
  30. sendMessage(message.target, if (prefixWithNickname) "${message.user.nickname}: $response" else response, message.metadata.messageId)
  31. }
  32. }
  33. /**
  34. * "React" in the appropriate place to a message received.
  35. */
  36. fun IrcClient.react(message: MessageReceived, reaction: String) = sendTagMessage(
  37. if (isToMe(message)) message.user.nickname else message.target,
  38. mapOf(MessageTag.React to reaction),
  39. message.metadata.messageId)
  40. /**
  41. * Utility to determine whether the given message is to our local user or not.
  42. */
  43. internal fun IrcClient.isToMe(message: MessageReceived) =
  44. caseMapping.areEquivalent(message.target, localUser.nickname)