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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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>) =
  10. send("CAP", "REQ", capabilities.joinToString(" "))
  11. /** Sends a request to join the given channel. */
  12. fun IrcClient.sendJoin(channel: String) = send("JOIN", channel)
  13. /** Sends a request to part the given channel. */
  14. fun IrcClient.sendPart(channel: String, reason: String? = null) =
  15. reason?.let { send("PART", channel, reason) } ?: send("PART", channel)
  16. /** Sends a request to see the modes of a given target. */
  17. fun IrcClient.sendModeRequest(target: String) = send("MODE", target)
  18. /** Sends a request to change to the given nickname. */
  19. fun IrcClient.sendNickChange(nick: String) = send("NICK", nick)
  20. /** Sends the connection password to the server. */
  21. internal fun IrcClient.sendPassword(password: String) = send("PASS", password)
  22. /** Sends a response to a PING event. */
  23. internal fun IrcClient.sendPong(nonce: ByteArray) = send("PONG", String(nonce))
  24. /** Sends a CTCP message of the specified [type] and with optional [data] to [target] (a user or a channel). */
  25. fun IrcClient.sendCtcp(target: String, type: String, data: String? = null) =
  26. sendMessage(target, "\u0001${type.toUpperCase()}${data?.let { " $it" } ?: ""}\u0001")
  27. /** Sends an action to the given [target] (a user or a channel). */
  28. fun IrcClient.sendAction(target: String, action: String) = sendCtcp(target, "ACTION", action)
  29. /** Sends a private message to a user or channel. */
  30. fun IrcClient.sendMessage(target: String, message: String, inReplyTo: String? = null) =
  31. send(
  32. inReplyTo?.let { tagMap(MessageTag.Reply to inReplyTo) } ?: emptyMap(),
  33. "PRIVMSG",
  34. target,
  35. message)
  36. /**
  37. * Sends a tag-only message.
  38. *
  39. * If [inReplyTo] is specified then the [MessageTag.Reply] tag will be automatically added.
  40. */
  41. fun IrcClient.sendTagMessage(target: String, tags: Map<MessageTag, String>, inReplyTo: String? = null) {
  42. send(inReplyTo?.let { tags + (MessageTag.Reply to inReplyTo) } ?: tags, "TAGMSG", target)
  43. }
  44. /** Sends a message to register a user with the server. */
  45. internal fun IrcClient.sendUser(userName: String, realName: String) = send("USER", userName, "0", "*", realName)
  46. /** Starts an authentication request. */
  47. internal fun IrcClient.sendAuthenticationMessage(data: String = "+") = send("AUTHENTICATE", data)
  48. /**
  49. * Utility method for creating a map of tags to avoid type inference problems.
  50. */
  51. fun tagMap(vararg tags: Pair<MessageTag, String>) = mapOf(*tags)