Browse Source

Support sending CTCPs and actions

tags/v0.4.0
Chris Smith 5 years ago
parent
commit
90167d4cb0

+ 1
- 0
CHANGELOG View File

@@ -1,6 +1,7 @@
1 1
 vNEXT (in development)
2 2
 
3 3
  * Added CtcpReceived and ActionReceived events
4
+ * Added sendCtcp and sendAction message builders
4 5
 
5 6
 v0.3.1
6 7
 

+ 15
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MessageBuilders.kt View File

@@ -4,19 +4,34 @@ import com.dmdirc.ktirc.IrcClient
4 4
 
5 5
 /** Sends a message to ask the server to list capabilities. */
6 6
 internal fun IrcClient.sendCapabilityList() = send("CAP LS 302")
7
+
7 8
 /** Sends a message indicating the end of capability negotiation. */
8 9
 internal fun IrcClient.sendCapabilityEnd() = send("CAP END")
10
+
9 11
 /** Sends a message requesting the specified caps are enabled. */
10 12
 internal fun IrcClient.sendCapabilityRequest(capabilities: List<String>) = send("CAP REQ :${capabilities.joinToString(" ")}")
13
+
11 14
 /** Sends a request to join the given channel. */
12 15
 fun IrcClient.sendJoin(channel: String) = send("JOIN :$channel")
16
+
13 17
 /** Sends a request to change to the given nickname. */
14 18
 fun IrcClient.sendNickChange(nick: String) = send("NICK :$nick")
19
+
15 20
 /** Sends the connection password to the server. */
16 21
 internal fun IrcClient.sendPassword(password: String) = send("PASS :$password")
22
+
17 23
 /** Sends a response to a PING event. */
18 24
 internal fun IrcClient.sendPong(nonce: ByteArray) = send("PONG :${String(nonce)}")
25
+
26
+/** Sends a CTCP message of the specified [type] and with optional [data] to [target] (a user or a channel). */
27
+fun IrcClient.sendCtcp(target: String, type: String, data: String? = null) =
28
+        sendMessage(target, "\u0001${type.toUpperCase()}${data?.let { " $it" } ?: ""}\u0001")
29
+
30
+/** Sends an action to the given [target] (a user or a channel). */
31
+fun IrcClient.sendAction(target: String, action: String) = sendCtcp(target, "ACTION", action)
32
+
19 33
 /** Sends a private message to a user or channel. */
20 34
 fun IrcClient.sendMessage(target: String, message: String) = send("PRIVMSG $target :$message")
35
+
21 36
 /** Sends a message to register a user with the server. */
22 37
 internal fun IrcClient.sendUser(userName: String, localHostName: String, serverHostName: String, realName: String) = send("USER $userName $localHostName $serverHostName :$realName")

+ 26
- 8
src/test/kotlin/com/dmdirc/ktirc/messages/MessageBuildersTest.kt View File

@@ -10,49 +10,67 @@ internal class MessageBuildersTest {
10 10
     private val mockClient = mock<IrcClient>()
11 11
 
12 12
     @Test
13
-    fun `CapabilityRequestMessage creates CAP REQ message with single argument`() {
13
+    fun `sendCapabilityRequest sends CAP REQ message with single argument`() {
14 14
         mockClient.sendCapabilityRequest(listOf("a"))
15 15
         verify(mockClient).send("CAP REQ :a")
16 16
     }
17 17
 
18 18
     @Test
19
-    fun `CapabilityRequestMessage creates CAP REQ message with multiple args`() {
19
+    fun `sendCapabilityRequest sends CAP REQ message with multiple args`() {
20 20
         mockClient.sendCapabilityRequest(listOf("a b c"))
21 21
         verify(mockClient).send("CAP REQ :a b c")
22 22
     }
23 23
 
24 24
     @Test
25
-    fun `JoinMessage creates correct JOIN message`() {
25
+    fun `sendJoin sends correct JOIN message`() {
26 26
         mockClient.sendJoin("#TheGibson")
27 27
         verify(mockClient).send("JOIN :#TheGibson")
28 28
     }
29 29
 
30 30
     @Test
31
-    fun `NickMessage creates correct NICK message`() {
31
+    fun `sendNickChange sends correct NICK message`() {
32 32
         mockClient.sendNickChange("AcidBurn")
33 33
         verify(mockClient).send("NICK :AcidBurn")
34 34
     }
35 35
 
36 36
     @Test
37
-    fun `PasswordMessage creates correct PASS message`() {
37
+    fun `sendPassword sends correct PASS message`() {
38 38
         mockClient.sendPassword("hacktheplanet")
39 39
         verify(mockClient).send("PASS :hacktheplanet")
40 40
     }
41 41
 
42 42
     @Test
43
-    fun `PongMessage creates correct PONG message`() {
43
+    fun `sendPong sends correct PONG message`() {
44 44
         mockClient.sendPong("abcdef".toByteArray())
45 45
         verify(mockClient).send("PONG :abcdef")
46 46
     }
47 47
 
48 48
     @Test
49
-    fun `PrivmsgMessage creates correct PRIVMSG message`() {
49
+    fun `sendMessage sends correct PRIVMSG message`() {
50 50
         mockClient.sendMessage("acidBurn", "Hack the planet!")
51 51
         verify(mockClient).send("PRIVMSG acidBurn :Hack the planet!")
52 52
     }
53 53
 
54 54
     @Test
55
-    fun `UserMessage creates correct USER message`() {
55
+    fun `sendCtcp sends correct CTCP message with no arguments`() {
56
+        mockClient.sendCtcp("acidBurn", "ping")
57
+        verify(mockClient).send("PRIVMSG acidBurn :\u0001PING\u0001")
58
+    }
59
+
60
+    @Test
61
+    fun `sendCtcp sends correct CTCP message with arguments`() {
62
+        mockClient.sendCtcp("acidBurn", "ping", "12345")
63
+        verify(mockClient).send("PRIVMSG acidBurn :\u0001PING 12345\u0001")
64
+    }
65
+
66
+    @Test
67
+    fun `sendAction sends correct action`() {
68
+        mockClient.sendAction("acidBurn", "hacks the planet")
69
+        verify(mockClient).send("PRIVMSG acidBurn :\u0001ACTION hacks the planet\u0001")
70
+    }
71
+
72
+    @Test
73
+    fun `sendUser sends correct USER message`() {
56 74
         mockClient.sendUser("AcidBurn", "localhost", "gibson", "Kate")
57 75
         verify(mockClient).send("USER AcidBurn localhost gibson :Kate")
58 76
     }

Loading…
Cancel
Save