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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.dmdirc.ktirc.messages
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.model.MessageTag
  4. /** Sends a request to join the given channel. */
  5. fun IrcClient.sendJoin(channel: String) = send("JOIN", channel)
  6. /** Sends a request to part the given channel. */
  7. fun IrcClient.sendPart(channel: String, reason: String? = null) =
  8. reason?.let { send("PART", channel, reason) } ?: send("PART", channel)
  9. /** Sends a request to see the modes of a given target. */
  10. fun IrcClient.sendModeRequest(target: String) = send("MODE", target)
  11. /** Sends a request to change to the given nickname. */
  12. fun IrcClient.sendNickChange(nick: String) = send("NICK", nick)
  13. /** Sends a request to set or unset our away state. */
  14. fun IrcClient.sendAway(reason: String? = null) =
  15. // We need to pass emptyMap() in the second case to avoid using the deprecated send(String) method
  16. // Once that's removed, the redundant map can be taken out.
  17. reason?.let { send("AWAY", reason) } ?: send(emptyMap(), "AWAY")
  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. send(
  26. inReplyTo?.let { tagMap(MessageTag.Reply to inReplyTo) } ?: emptyMap(),
  27. "PRIVMSG",
  28. target,
  29. message)
  30. /**
  31. * Sends a tag-only message.
  32. *
  33. * If [inReplyTo] is specified then the [MessageTag.Reply] tag will be automatically added.
  34. */
  35. fun IrcClient.sendTagMessage(target: String, tags: Map<MessageTag, String>, inReplyTo: String? = null) {
  36. send(inReplyTo?.let { tags + (MessageTag.Reply to inReplyTo) } ?: tags, "TAGMSG", target)
  37. }
  38. /**
  39. * Utility method for creating a map of tags to avoid type inference problems.
  40. */
  41. fun tagMap(vararg tags: Pair<MessageTag, String>) = mapOf(*tags)