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.

MessageBuilders.kt 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.dmdirc.ktirc.messages
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.model.MessageTag
  4. /** Sends a message to ask the server to list capabilities. */
  5. internal fun IrcClient.sendCapabilityList() = send("CAP LS 302")
  6. /** Sends a message indicating the end of capability negotiation. */
  7. internal fun IrcClient.sendCapabilityEnd() = send("CAP END")
  8. /** Sends a message requesting the specified caps are enabled. */
  9. internal fun IrcClient.sendCapabilityRequest(capabilities: List<String>) = send("CAP REQ :${capabilities.joinToString(" ")}")
  10. /** Sends a request to join the given channel. */
  11. fun IrcClient.sendJoin(channel: String) = send("JOIN :$channel")
  12. /** Sends a request to see the modes of a given target. */
  13. fun IrcClient.sendModeRequest(target: String) = send("MODE :$target")
  14. /** Sends a request to change to the given nickname. */
  15. fun IrcClient.sendNickChange(nick: String) = send("NICK :$nick")
  16. /** Sends the connection password to the server. */
  17. internal fun IrcClient.sendPassword(password: String) = send("PASS :$password")
  18. /** Sends a response to a PING event. */
  19. internal fun IrcClient.sendPong(nonce: ByteArray) = send("PONG :${String(nonce)}")
  20. /** Sends a CTCP message of the specified [type] and with optional [data] to [target] (a user or a channel). */
  21. fun IrcClient.sendCtcp(target: String, type: String, data: String? = null) =
  22. sendMessage(target, "\u0001${type.toUpperCase()}${data?.let { " $it" } ?: ""}\u0001")
  23. /** Sends an action to the given [target] (a user or a channel). */
  24. fun IrcClient.sendAction(target: String, action: String) = sendCtcp(target, "ACTION", action)
  25. /** Sends a private message to a user or channel. */
  26. fun IrcClient.sendMessage(target: String, message: String, inReplyTo: String? = null) =
  27. sendWithTags(mapOf(MessageTag.Reply to inReplyTo), "PRIVMSG $target :$message")
  28. /**
  29. * Sends a tag-only message.
  30. *
  31. * If [inReplyTo] is specified then the [MessageTag.Reply] tag will be automatically added.
  32. */
  33. fun IrcClient.sendTagMessage(target: String, tags: Map<MessageTag, String>, inReplyTo: String? = null) {
  34. sendWithTags(inReplyTo?.let { tags + (MessageTag.Reply to inReplyTo) } ?: tags, "TAGMSG $target")
  35. }
  36. /** Sends a message to register a user with the server. */
  37. internal fun IrcClient.sendUser(userName: String, realName: String) = send("USER $userName 0 * :$realName")
  38. /** Starts an authentication request. */
  39. internal fun IrcClient.sendAuthenticationMessage(data: String = "+") = send("AUTHENTICATE $data")
  40. /**
  41. * Sends a message prefixed with some IRCv3 tags.
  42. *
  43. * For convenience, if the value of a tag is `null`, the tag will be omitted. If no tags are present the
  44. * message is sent directly with no prefix.
  45. */
  46. internal fun IrcClient.sendWithTags(tags: Map<MessageTag, String?>, message: String) = tags
  47. .filterValues { it != null }
  48. .map { (key, value) -> "${key.name}=${value?.escapeTagValue()}" }
  49. .joinToString(";")
  50. .let {
  51. if (it.isEmpty()) send(message) else send("@$it $message")
  52. }
  53. internal fun String.escapeTagValue() = replace("\\", "\\\\")
  54. .replace("\n", "\\n")
  55. .replace("\r", "\\r")
  56. .replace(";", "\\:")
  57. .replace(" ", "\\s")