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.

IrcMessage.kt 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.events.EventMetadata
  3. import com.dmdirc.ktirc.util.currentTimeProvider
  4. import com.dmdirc.ktirc.util.currentTimeZoneProvider
  5. import java.time.Instant
  6. import java.time.LocalDateTime
  7. /**
  8. * Represents an IRC protocol message.
  9. */
  10. internal class IrcMessage(val tags: Map<MessageTag, String>, val prefix: ByteArray?, val command: String, val params: List<ByteArray>) {
  11. /** The time at which the message was sent, or our best guess at it. */
  12. val metadata = EventMetadata(time, batchId)
  13. /** The user that generated the message, if any. */
  14. val sourceUser by lazy {
  15. prefix?.asUser()?.apply {
  16. tags[MessageTag.AccountName]?.let { account = it }
  17. }
  18. }
  19. private val time
  20. get() = if (MessageTag.ServerTime in tags) {
  21. LocalDateTime.ofInstant(Instant.parse(tags[MessageTag.ServerTime]), currentTimeZoneProvider())
  22. } else {
  23. currentTimeProvider()
  24. }
  25. private val batchId
  26. get() = tags[MessageTag.Batch]
  27. }
  28. /**
  29. * Supported tags that may be applied to messages.
  30. */
  31. @Suppress("unused")
  32. sealed class MessageTag(val name: String) {
  33. /** Specifies the account name of the user, if the `account-tag` capability is negotiated. */
  34. object AccountName : MessageTag("account")
  35. /** Specifies the ID that a batch message belongs to. */
  36. object Batch : MessageTag("batch")
  37. /** Specifies the time the server received the message, if the `server-time` capability is negotiated. */
  38. object ServerTime : MessageTag("time")
  39. /** A unique ID for the message, used to reply, react, edit, delete, etc. */
  40. object MessageId : MessageTag("draft/msgid")
  41. /** Used to identify a message ID that was replied to, to enable threaded conversations. */
  42. object Reply : MessageTag("+draft/reply")
  43. /** Used to specify a slack-like reaction to another message. */
  44. object React : MessageTag("+draft/react")
  45. }
  46. internal val messageTags: Map<String, MessageTag> by lazy {
  47. MessageTag::class.nestedClasses.map { it.objectInstance as MessageTag }.associateBy { it.name }
  48. }