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 1.7KB

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