Ver código fonte

Implement MotdFinished, improve some docs

tags/v0.5.0
Chris Smith 5 anos atrás
pai
commit
44d87b68c0

+ 3
- 0
CHANGELOG Ver arquivo

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

+ 1
- 1
README.md Ver arquivo

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

+ 8
- 4
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt Ver arquivo

41
      * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
41
      * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
42
      *
42
      *
43
      * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
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
      * @param message The line to be sent to the IRC server.
46
      * @param message The line to be sent to the IRC server.
47
      */
47
      */
50
     /**
50
     /**
51
      * Registers a new handler for all events on this connection.
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
      * client.onEvent {
56
      * client.onEvent {
114
 
114
 
115
                 connect()
115
                 connect()
116
 
116
 
117
-                with (Channel<ByteArray>(Channel.UNLIMITED)) {
117
+                with(Channel<ByteArray>(Channel.UNLIMITED)) {
118
                     writeChannel = this
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
                 emitEvent(ServerConnected(currentTimeProvider()))
126
                 emitEvent(ServerConnected(currentTimeProvider()))

+ 3
- 0
src/main/kotlin/com/dmdirc/ktirc/events/Events.kt Ver arquivo

58
 
58
 
59
 /** Raised when the server has finished sending us capabilities. */
59
 /** Raised when the server has finished sending us capabilities. */
60
 class ServerCapabilitiesFinished(time: LocalDateTime) : IrcEvent(time)
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 Ver arquivo

21
         CapabilityProcessor(),
21
         CapabilityProcessor(),
22
         ISupportProcessor(),
22
         ISupportProcessor(),
23
         JoinProcessor(),
23
         JoinProcessor(),
24
+        MotdProcessor(),
24
         NamesProcessor(),
25
         NamesProcessor(),
25
         PartProcessor(),
26
         PartProcessor(),
26
         PingProcessor(),
27
         PingProcessor(),

+ 17
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MotdProcessor.kt Ver arquivo

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 Ver arquivo

15
     var status = ServerStatus.Connecting
15
     var status = ServerStatus.Connecting
16
         internal set
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
     var localNickname: String = initialNickname
25
     var localNickname: String = initialNickname
20
         internal set
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
     var serverName: String = initialServerName
35
     var serverName: String = initialServerName
24
         internal set
36
         internal set
25
 
37
 

+ 40
- 0
src/test/kotlin/com/dmdirc/ktirc/messages/MotdProcessorTest.kt Ver arquivo

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
+}

Carregando…
Cancelar
Salvar