Browse Source

Quit handling

tags/v0.1.0
Chris Smith 5 years ago
parent
commit
0cbac7dd11

+ 5
- 0
src/main/kotlin/com/dmdirc/ktirc/events/ChannelStateHandler.kt View File

@@ -16,6 +16,7 @@ class ChannelStateHandler : EventHandler {
16 16
             is ChannelParted -> handlePart(client, event)
17 17
             is ChannelNamesReceived -> handleNamesReceived(client, event)
18 18
             is ChannelNamesFinished -> handleNamesFinished(client, event)
19
+            is UserQuit -> handleQuit(client, event)
19 20
         }
20 21
     }
21 22
 
@@ -63,4 +64,8 @@ class ChannelStateHandler : EventHandler {
63 64
         }
64 65
     }
65 66
 
67
+    private fun handleQuit(client: IrcClient, event: UserQuit) {
68
+        client.channelState.forEach { it.users -= event.user.nickname }
69
+    }
70
+
66 71
 }

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

@@ -34,3 +34,6 @@ data class ChannelNamesFinished(val channel: String) : IrcEvent()
34 34
 
35 35
 /** Raised when a message is received. */
36 36
 data class MessageReceived(val user: User, val target: String, val message: String) : IrcEvent()
37
+
38
+/** Raised when a user quits. */
39
+data class UserQuit(val user: User, val reason: String = "") : IrcEvent()

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

@@ -24,5 +24,6 @@ val messageProcessors = setOf(
24 24
         PartProcessor(),
25 25
         PingProcessor(),
26 26
         PrivmsgProcessor(),
27
+        QuitProcessor(),
27 28
         WelcomeProcessor()
28 29
 )

+ 18
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/QuitProcessor.kt View File

@@ -0,0 +1,18 @@
1
+package com.dmdirc.ktirc.messages
2
+
3
+import com.dmdirc.ktirc.events.UserQuit
4
+import com.dmdirc.ktirc.io.IrcMessage
5
+import com.dmdirc.ktirc.model.asUser
6
+
7
+internal class QuitProcessor : MessageProcessor {
8
+
9
+    override val commands = arrayOf("QUIT")
10
+
11
+    override fun process(message: IrcMessage) = message.prefix?.let {
12
+        listOf(UserQuit(it.asUser(), message.reason))
13
+    } ?: emptyList()
14
+
15
+    private val IrcMessage.reason
16
+        get() = if (params.isNotEmpty()) String(params[0]) else ""
17
+
18
+}

+ 25
- 0
src/test/kotlin/com/dmdirc/ktirc/events/ChannelStateHandlerTest.kt View File

@@ -121,4 +121,29 @@ internal class ChannelStateHandlerTest {
121 121
         assertFalse("zerocool" in channel.users)
122 122
     }
123 123
 
124
+    @Test
125
+    fun `ChannelStateHandler removes user from all channel member lists for quits`() = runBlocking {
126
+        with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
127
+            users += ChannelUser("ZeroCool")
128
+            channelStateMap += this
129
+        }
130
+
131
+        with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
132
+            users += ChannelUser("ZeroCool")
133
+            channelStateMap += this
134
+        }
135
+
136
+        with (ChannelState("#chat") { CaseMapping.Rfc }) {
137
+            users += ChannelUser("AcidBurn")
138
+            channelStateMap += this
139
+        }
140
+
141
+        handler.processEvent(ircClient, UserQuit(User("zerocool", "dade", "root.localhost")))
142
+
143
+        assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
144
+        assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
145
+        assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
146
+        assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
147
+    }
148
+
124 149
 }

+ 34
- 0
src/test/kotlin/com/dmdirc/ktirc/messages/QuitProcessorTest.kt View File

@@ -0,0 +1,34 @@
1
+package com.dmdirc.ktirc.messages
2
+
3
+import com.dmdirc.ktirc.events.UserQuit
4
+import com.dmdirc.ktirc.io.IrcMessage
5
+import com.dmdirc.ktirc.model.User
6
+import org.junit.jupiter.api.Assertions
7
+import org.junit.jupiter.api.Test
8
+
9
+internal class QuitProcessorTest {
10
+
11
+    @Test
12
+    fun `QuitProcessor raises quit event without message`() {
13
+        val events = QuitProcessor().process(
14
+                IrcMessage(null, "acidburn!libby@root.localhost".toByteArray(), "QUIT", emptyList()))
15
+        Assertions.assertEquals(1, events.size)
16
+        Assertions.assertEquals(UserQuit(User("acidburn", "libby", "root.localhost")), events[0])
17
+    }
18
+
19
+    @Test
20
+    fun `QuitProcessor raises quit event with message`() {
21
+        val events = QuitProcessor().process(
22
+                IrcMessage(null, "acidburn!libby@root.localhost".toByteArray(), "QUIT", listOf("Hack the planet!".toByteArray())))
23
+        Assertions.assertEquals(1, events.size)
24
+        Assertions.assertEquals(UserQuit(User("acidburn", "libby", "root.localhost"), "Hack the planet!"), events[0])
25
+    }
26
+
27
+    @Test
28
+    fun `QuitProcessor does nothing if prefix missing`() {
29
+        val events = QuitProcessor().process(
30
+                IrcMessage(null, null, "QUIT", listOf("Hack the planet!".toByteArray())))
31
+        Assertions.assertEquals(0, events.size)
32
+    }
33
+
34
+}

Loading…
Cancel
Save