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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 change to the given nickname. */
  13. fun IrcClient.sendNickChange(nick: String) = send("NICK :$nick")
  14. /** Sends the connection password to the server. */
  15. internal fun IrcClient.sendPassword(password: String) = send("PASS :$password")
  16. /** Sends a response to a PING event. */
  17. internal fun IrcClient.sendPong(nonce: ByteArray) = send("PONG :${String(nonce)}")
  18. /** Sends a CTCP message of the specified [type] and with optional [data] to [target] (a user or a channel). */
  19. fun IrcClient.sendCtcp(target: String, type: String, data: String? = null) =
  20. sendMessage(target, "\u0001${type.toUpperCase()}${data?.let { " $it" } ?: ""}\u0001")
  21. /** Sends an action to the given [target] (a user or a channel). */
  22. fun IrcClient.sendAction(target: String, action: String) = sendCtcp(target, "ACTION", action)
  23. /** Sends a private message to a user or channel. */
  24. fun IrcClient.sendMessage(target: String, message: String, inReplyTo: String? = null) =
  25. if (inReplyTo == null)
  26. send("PRIVMSG $target :$message")
  27. else
  28. send("@${MessageTag.Reply.name}=$inReplyTo PRIVMSG $target :$message")
  29. // TODO ^ Proper tag building/serializing
  30. /** Sends a message to register a user with the server. */
  31. internal fun IrcClient.sendUser(userName: String, realName: String) = send("USER $userName 0 * :$realName")
  32. /** Starts an authentication request. */
  33. internal fun IrcClient.sendAuthenticationMessage(data: String = "+") = send("AUTHENTICATE $data")