Browse Source

Implement MotdFinished, improve some docs

tags/v0.5.0
Chris Smith 5 years ago
parent
commit
44d87b68c0

+ 3
- 0
CHANGELOG View File

@@ -1,5 +1,8 @@
1 1
 vNEXT (in development)
2 2
 
3
+ * Added MotdFinished event
4
+ * Improved some documentation
5
+
3 6
 v0.4.0
4 7
 
5 8
  * Added CtcpReceived and ActionReceived events

+ 1
- 1
README.md View File

@@ -31,7 +31,7 @@ simple bot might look like:
31 31
 with(IrcClientImpl(Server("my.server.com", 6667), Profile("nick", "realName", "userName"))) {
32 32
     onEvent { event ->
33 33
         when (event) {
34
-            is ServerWelcome -> sendJoin("#ktirc")
34
+            is ServerReady -> sendJoin("#ktirc")
35 35
             is MessageReceived ->
36 36
                 if (event.message == "!test")
37 37
                     reply(event, "Test successful!")

+ 8
- 4
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt View File

@@ -41,7 +41,7 @@ interface IrcClient {
41 41
      * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
42 42
      *
43 43
      * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
44
-     * such as [joinMessage].
44
+     * such as [sendJoin].
45 45
      *
46 46
      * @param message The line to be sent to the IRC server.
47 47
      */
@@ -50,7 +50,7 @@ interface IrcClient {
50 50
     /**
51 51
      * Registers a new handler for all events on this connection.
52 52
      *
53
-     * All events are subclasses of [IrcEvent]; the idomatic way to handle them is using a `when` statement:
53
+     * All events are subclasses of [IrcEvent]; the idiomatic way to handle them is using a `when` statement:
54 54
      *
55 55
      * ```
56 56
      * client.onEvent {
@@ -114,9 +114,13 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
114 114
 
115 115
                 connect()
116 116
 
117
-                with (Channel<ByteArray>(Channel.UNLIMITED)) {
117
+                with(Channel<ByteArray>(Channel.UNLIMITED)) {
118 118
                     writeChannel = this
119
-                    scope.launch { writeLines(this@with) }
119
+                    scope.launch {
120
+                        writeChannel?.let {
121
+                            writeLines(it)
122
+                        }
123
+                    }
120 124
                 }
121 125
 
122 126
                 emitEvent(ServerConnected(currentTimeProvider()))

+ 3
- 0
src/main/kotlin/com/dmdirc/ktirc/events/Events.kt View File

@@ -58,3 +58,6 @@ class ServerCapabilitiesAcknowledged(time: LocalDateTime, val capabilities: Map<
58 58
 
59 59
 /** Raised when the server has finished sending us capabilities. */
60 60
 class ServerCapabilitiesFinished(time: LocalDateTime) : IrcEvent(time)
61
+
62
+/** Raised when a Message Of the Day has completed. */
63
+class MotdFinished(time: LocalDateTime, val missing: Boolean = false): IrcEvent(time)

+ 1
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MessageProcessor.kt View File

@@ -21,6 +21,7 @@ internal val messageProcessors = setOf(
21 21
         CapabilityProcessor(),
22 22
         ISupportProcessor(),
23 23
         JoinProcessor(),
24
+        MotdProcessor(),
24 25
         NamesProcessor(),
25 26
         PartProcessor(),
26 27
         PingProcessor(),

+ 17
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MotdProcessor.kt View File

@@ -0,0 +1,17 @@
1
+package com.dmdirc.ktirc.messages
2
+
3
+import com.dmdirc.ktirc.events.MotdFinished
4
+import com.dmdirc.ktirc.model.IrcMessage
5
+
6
+internal class MotdProcessor : MessageProcessor {
7
+
8
+    override val commands = arrayOf("422", "376")
9
+
10
+    override fun process(message: IrcMessage) = sequence {
11
+        when (message.command) {
12
+            "422" -> yield(MotdFinished(message.time, missing = true))
13
+            "376" -> yield(MotdFinished(message.time))
14
+        }
15
+    }.toList()
16
+
17
+}

+ 14
- 2
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt View File

@@ -15,11 +15,23 @@ class ServerState internal constructor(initialNickname: String, initialServerNam
15 15
     var status = ServerStatus.Connecting
16 16
         internal set
17 17
 
18
-    /** Our present nickname on the server. */
18
+    /**
19
+     * What we believe our current nickname to be on the server.
20
+     *
21
+     * Initially this will be the nickname provided in the [Profile]. It will be updated to the actual nickname
22
+     * in use when connecting. Once you have received a [com.dmdirc.ktirc.events.ServerWelcome] event you can
23
+     * rely on this value being current.
24
+     * */
19 25
     var localNickname: String = initialNickname
20 26
         internal set
21 27
 
22
-    /** The server's name, as reported by it. */
28
+    /**
29
+     * The name of the server we are connected to.
30
+     *
31
+     * Initially this will be the hostname or IP address provided in the [Server]. It will be updated to the server's
32
+     * self-reported hostname when connecting. Once you have received a [com.dmdirc.ktirc.events.ServerWelcome] event
33
+     * you can rely on this value being current.
34
+     */
23 35
     var serverName: String = initialServerName
24 36
         internal set
25 37
 

+ 40
- 0
src/test/kotlin/com/dmdirc/ktirc/messages/MotdProcessorTest.kt View File

@@ -0,0 +1,40 @@
1
+package com.dmdirc.ktirc.messages
2
+
3
+import com.dmdirc.ktirc.TestConstants
4
+import com.dmdirc.ktirc.events.MotdFinished
5
+import com.dmdirc.ktirc.model.IrcMessage
6
+import com.dmdirc.ktirc.util.currentTimeProvider
7
+import org.junit.jupiter.api.Assertions.*
8
+import org.junit.jupiter.api.BeforeEach
9
+import org.junit.jupiter.api.Test
10
+
11
+internal class MotdProcessorTest {
12
+
13
+    @BeforeEach
14
+    fun setUp() {
15
+        currentTimeProvider = { TestConstants.time }
16
+    }
17
+
18
+    @Test
19
+    fun `MotdProcessor raises motdFinished when not found numeric received`() {
20
+        val events = MotdProcessor().process(
21
+                IrcMessage(emptyMap(), "the.gibson".toByteArray(), "422", listOf("MOTD missing".toByteArray())))
22
+        assertEquals(1, events.size)
23
+
24
+        val event = events[0] as MotdFinished
25
+        assertEquals(TestConstants.time, events[0].time)
26
+        assertTrue(event.missing)
27
+    }
28
+
29
+    @Test
30
+    fun `MotdProcessor raises motdFinished when MOTD finishes normally`() {
31
+        val events = MotdProcessor().process(
32
+                IrcMessage(emptyMap(), "the.gibson".toByteArray(), "376", listOf("acidBurn".toByteArray(), "End of /MOTD command.".toByteArray())))
33
+        assertEquals(1, events.size)
34
+
35
+        val event = events[0] as MotdFinished
36
+        assertEquals(TestConstants.time, events[0].time)
37
+        assertFalse(event.missing)
38
+    }
39
+
40
+}

Loading…
Cancel
Save