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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(
  13. time = time,
  14. batchId = tags[MessageTag.Batch],
  15. messageId = tags[MessageTag.MessageId],
  16. label = tags[MessageTag.Label])
  17. /** The user that generated the message, if any. */
  18. val sourceUser by lazy {
  19. prefix?.asUser()?.apply {
  20. tags[MessageTag.AccountName]?.let { account = it }
  21. }
  22. }
  23. private val time
  24. get() = when (MessageTag.ServerTime in tags) {
  25. true -> LocalDateTime.ofInstant(Instant.parse(tags[MessageTag.ServerTime]), currentTimeZoneProvider())
  26. false -> currentTimeProvider()
  27. }
  28. }
  29. /**
  30. * Supported tags that may be applied to messages.
  31. */
  32. @Suppress("unused")
  33. sealed class MessageTag(val name: String) {
  34. /** Specifies the account name of the user, if the `account-tag` capability is negotiated. */
  35. object AccountName : MessageTag("account")
  36. /** Specifies the ID that a batch message belongs to. */
  37. object Batch : MessageTag("batch")
  38. /** An arbitrary label to identify the response to messages we generate. */
  39. object Label : MessageTag("draft/label")
  40. /** A unique ID for the message, used to reply, react, edit, delete, etc. */
  41. object MessageId : MessageTag("draft/msgid")
  42. /** Used to identify a message ID that was replied to, to enable threaded conversations. */
  43. object Reply : MessageTag("+draft/reply")
  44. /** Used to specify a slack-like reaction to another message. */
  45. object React : MessageTag("+draft/react")
  46. /** Specifies the time the server received the message, if the `server-time` capability is negotiated. */
  47. object ServerTime : MessageTag("time")
  48. }
  49. internal val messageTags: Map<String, MessageTag> by lazy {
  50. MessageTag::class.nestedClasses.map { it.objectInstance as MessageTag }.associateBy { it.name }
  51. }