瀏覽代碼

Support sending CTCPs and actions

tags/v0.4.0
Chris Smith 5 年之前
父節點
當前提交
90167d4cb0

+ 1
- 0
CHANGELOG 查看文件

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

+ 15
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MessageBuilders.kt 查看文件

4
 
4
 
5
 /** Sends a message to ask the server to list capabilities. */
5
 /** Sends a message to ask the server to list capabilities. */
6
 internal fun IrcClient.sendCapabilityList() = send("CAP LS 302")
6
 internal fun IrcClient.sendCapabilityList() = send("CAP LS 302")
7
+
7
 /** Sends a message indicating the end of capability negotiation. */
8
 /** Sends a message indicating the end of capability negotiation. */
8
 internal fun IrcClient.sendCapabilityEnd() = send("CAP END")
9
 internal fun IrcClient.sendCapabilityEnd() = send("CAP END")
10
+
9
 /** Sends a message requesting the specified caps are enabled. */
11
 /** Sends a message requesting the specified caps are enabled. */
10
 internal fun IrcClient.sendCapabilityRequest(capabilities: List<String>) = send("CAP REQ :${capabilities.joinToString(" ")}")
12
 internal fun IrcClient.sendCapabilityRequest(capabilities: List<String>) = send("CAP REQ :${capabilities.joinToString(" ")}")
13
+
11
 /** Sends a request to join the given channel. */
14
 /** Sends a request to join the given channel. */
12
 fun IrcClient.sendJoin(channel: String) = send("JOIN :$channel")
15
 fun IrcClient.sendJoin(channel: String) = send("JOIN :$channel")
16
+
13
 /** Sends a request to change to the given nickname. */
17
 /** Sends a request to change to the given nickname. */
14
 fun IrcClient.sendNickChange(nick: String) = send("NICK :$nick")
18
 fun IrcClient.sendNickChange(nick: String) = send("NICK :$nick")
19
+
15
 /** Sends the connection password to the server. */
20
 /** Sends the connection password to the server. */
16
 internal fun IrcClient.sendPassword(password: String) = send("PASS :$password")
21
 internal fun IrcClient.sendPassword(password: String) = send("PASS :$password")
22
+
17
 /** Sends a response to a PING event. */
23
 /** Sends a response to a PING event. */
18
 internal fun IrcClient.sendPong(nonce: ByteArray) = send("PONG :${String(nonce)}")
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
 /** Sends a private message to a user or channel. */
33
 /** Sends a private message to a user or channel. */
20
 fun IrcClient.sendMessage(target: String, message: String) = send("PRIVMSG $target :$message")
34
 fun IrcClient.sendMessage(target: String, message: String) = send("PRIVMSG $target :$message")
35
+
21
 /** Sends a message to register a user with the server. */
36
 /** Sends a message to register a user with the server. */
22
 internal fun IrcClient.sendUser(userName: String, localHostName: String, serverHostName: String, realName: String) = send("USER $userName $localHostName $serverHostName :$realName")
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 查看文件

10
     private val mockClient = mock<IrcClient>()
10
     private val mockClient = mock<IrcClient>()
11
 
11
 
12
     @Test
12
     @Test
13
-    fun `CapabilityRequestMessage creates CAP REQ message with single argument`() {
13
+    fun `sendCapabilityRequest sends CAP REQ message with single argument`() {
14
         mockClient.sendCapabilityRequest(listOf("a"))
14
         mockClient.sendCapabilityRequest(listOf("a"))
15
         verify(mockClient).send("CAP REQ :a")
15
         verify(mockClient).send("CAP REQ :a")
16
     }
16
     }
17
 
17
 
18
     @Test
18
     @Test
19
-    fun `CapabilityRequestMessage creates CAP REQ message with multiple args`() {
19
+    fun `sendCapabilityRequest sends CAP REQ message with multiple args`() {
20
         mockClient.sendCapabilityRequest(listOf("a b c"))
20
         mockClient.sendCapabilityRequest(listOf("a b c"))
21
         verify(mockClient).send("CAP REQ :a b c")
21
         verify(mockClient).send("CAP REQ :a b c")
22
     }
22
     }
23
 
23
 
24
     @Test
24
     @Test
25
-    fun `JoinMessage creates correct JOIN message`() {
25
+    fun `sendJoin sends correct JOIN message`() {
26
         mockClient.sendJoin("#TheGibson")
26
         mockClient.sendJoin("#TheGibson")
27
         verify(mockClient).send("JOIN :#TheGibson")
27
         verify(mockClient).send("JOIN :#TheGibson")
28
     }
28
     }
29
 
29
 
30
     @Test
30
     @Test
31
-    fun `NickMessage creates correct NICK message`() {
31
+    fun `sendNickChange sends correct NICK message`() {
32
         mockClient.sendNickChange("AcidBurn")
32
         mockClient.sendNickChange("AcidBurn")
33
         verify(mockClient).send("NICK :AcidBurn")
33
         verify(mockClient).send("NICK :AcidBurn")
34
     }
34
     }
35
 
35
 
36
     @Test
36
     @Test
37
-    fun `PasswordMessage creates correct PASS message`() {
37
+    fun `sendPassword sends correct PASS message`() {
38
         mockClient.sendPassword("hacktheplanet")
38
         mockClient.sendPassword("hacktheplanet")
39
         verify(mockClient).send("PASS :hacktheplanet")
39
         verify(mockClient).send("PASS :hacktheplanet")
40
     }
40
     }
41
 
41
 
42
     @Test
42
     @Test
43
-    fun `PongMessage creates correct PONG message`() {
43
+    fun `sendPong sends correct PONG message`() {
44
         mockClient.sendPong("abcdef".toByteArray())
44
         mockClient.sendPong("abcdef".toByteArray())
45
         verify(mockClient).send("PONG :abcdef")
45
         verify(mockClient).send("PONG :abcdef")
46
     }
46
     }
47
 
47
 
48
     @Test
48
     @Test
49
-    fun `PrivmsgMessage creates correct PRIVMSG message`() {
49
+    fun `sendMessage sends correct PRIVMSG message`() {
50
         mockClient.sendMessage("acidBurn", "Hack the planet!")
50
         mockClient.sendMessage("acidBurn", "Hack the planet!")
51
         verify(mockClient).send("PRIVMSG acidBurn :Hack the planet!")
51
         verify(mockClient).send("PRIVMSG acidBurn :Hack the planet!")
52
     }
52
     }
53
 
53
 
54
     @Test
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
         mockClient.sendUser("AcidBurn", "localhost", "gibson", "Kate")
74
         mockClient.sendUser("AcidBurn", "localhost", "gibson", "Kate")
57
         verify(mockClient).send("USER AcidBurn localhost gibson :Kate")
75
         verify(mockClient).send("USER AcidBurn localhost gibson :Kate")
58
     }
76
     }

Loading…
取消
儲存