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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: Collection<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. send(
  28. inReplyTo?.let { tagMap(MessageTag.Reply to inReplyTo) } ?: emptyMap(),
  29. "PRIVMSG",
  30. target,
  31. message)
  32. /**
  33. * Sends a tag-only message.
  34. *
  35. * If [inReplyTo] is specified then the [MessageTag.Reply] tag will be automatically added.
  36. */
  37. fun IrcClient.sendTagMessage(target: String, tags: Map<MessageTag, String>, inReplyTo: String? = null) {
  38. send(inReplyTo?.let { tags + (MessageTag.Reply to inReplyTo) } ?: tags, "TAGMSG", target)
  39. }
  40. /** Sends a message to register a user with the server. */
  41. internal fun IrcClient.sendUser(userName: String, realName: String) = send("USER", userName, "0", "*", realName)
  42. /** Starts an authentication request. */
  43. internal fun IrcClient.sendAuthenticationMessage(data: String = "+") = send("AUTHENTICATE", data)
  44. /**
  45. * Utility method for creating a map of tags to avoid type inference problems.
  46. */
  47. fun tagMap(vararg tags: Pair<MessageTag, String>) = mapOf(*tags)