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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 com.dmdirc.ktirc.util.logger
  6. import java.time.Instant
  7. import java.time.LocalDateTime
  8. import java.util.logging.Level
  9. /**
  10. * Represents an IRC protocol message.
  11. */
  12. internal class IrcMessage(val tags: Map<MessageTag, String>, val prefix: ByteArray?, val command: String, val params: List<ByteArray>) {
  13. private val log by logger()
  14. /** The time at which the message was sent, or our best guess at it. */
  15. val metadata = EventMetadata(
  16. time = tags[MessageTag.ServerTime]?.toLocalDateOrNull() ?: currentTimeProvider(),
  17. batchId = tags[MessageTag.Batch],
  18. messageId = tags[MessageTag.MessageId],
  19. label = tags[MessageTag.Label])
  20. /** The user that generated the message, if any. */
  21. val sourceUser by lazy {
  22. prefix?.asUser()?.apply {
  23. tags[MessageTag.AccountName]?.let { account = it }
  24. }
  25. }
  26. private fun String.toLocalDateOrNull() = try {
  27. LocalDateTime.ofInstant(Instant.parse(this), currentTimeZoneProvider())
  28. } catch(e: Exception) {
  29. log.log(Level.WARNING, e) { "Received unparsable server-time tag: $this" }
  30. null
  31. }
  32. }
  33. /**
  34. * Supported tags that may be applied to messages.
  35. */
  36. @Suppress("unused")
  37. sealed class MessageTag(val name: String) {
  38. /** Specifies the account name of the user, if the `account-tag` capability is negotiated. */
  39. object AccountName : MessageTag("account")
  40. /** Specifies the ID that a batch message belongs to. */
  41. object Batch : MessageTag("batch")
  42. /** An arbitrary label to identify the response to messages we generate. */
  43. object Label : MessageTag("draft/label")
  44. /** A unique ID for the message, used to reply, react, edit, delete, etc. */
  45. object MessageId : MessageTag("draft/msgid")
  46. /** Used to identify a message ID that was replied to, to enable threaded conversations. */
  47. object Reply : MessageTag("+draft/reply")
  48. /** Used to specify a slack-like reaction to another message. */
  49. object React : MessageTag("+draft/react")
  50. /** Specifies the time the server received the message, if the `server-time` capability is negotiated. */
  51. object ServerTime : MessageTag("time")
  52. }
  53. internal val messageTags: Map<String, MessageTag> by lazy {
  54. MessageTag::class.nestedClasses.map { it.objectInstance as MessageTag }.associateBy { it.name }
  55. }