Browse Source

Add isChannel method

tags/v0.8.0
Chris Smith 5 years ago
parent
commit
e4276619c0

+ 1
- 0
CHANGELOG View File

@@ -3,6 +3,7 @@ vNEXT (in development)
3 3
  * Added support for SCRAM-SHA-1 and SCRAM-SHA-256 SASL mechanisms
4 4
  * Added MotdLineReceived event
5 5
  * Added topic events and state
6
+ * Add utility method IrcClient.isChannel(String) to identify if a target is a channel or not
6 7
  * (Internal) Move event handlers into their own package
7 8
 
8 9
 v0.7.0

+ 10
- 2
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt View File

@@ -71,15 +71,23 @@ interface IrcClient {
71 71
     fun onEvent(handler: (IrcEvent) -> Unit)
72 72
 
73 73
     /**
74
-     * Utility method to determine if the given user is the one we are connected to IRC as.
74
+     * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
75
+     * [com.dmdirc.ktirc.events.ServerReady] event has been received.
75 76
      */
76 77
     fun isLocalUser(user: User) = isLocalUser(user.nickname)
77 78
 
78 79
     /**
79
-     * Utility method to determine if the given user is the one we are connected to IRC as.
80
+     * Utility method to determine if the given user is the one we are connected to IRC as. Should only be used after a
81
+     * [com.dmdirc.ktirc.events.ServerReady] event has been received.
80 82
      */
81 83
     fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
82 84
 
85
+    /**
86
+     * Determines if the given [target] appears to be a channel or not. Should only be used after a
87
+     * [com.dmdirc.ktirc.events.ServerReady] event has been received.
88
+     */
89
+    fun isChannel(target: String) = target.isNotEmpty() && serverState.channelTypes.contains(target[0])
90
+
83 91
 }
84 92
 
85 93
 /**

+ 8
- 0
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt View File

@@ -57,6 +57,12 @@ class ServerState internal constructor(
57 57
     val channelModePrefixes
58 58
         get() = features[ServerFeature.ModePrefixes] ?: throw IllegalStateException("lost mode prefixes")
59 59
 
60
+    /**
61
+     * Convenience accessor for the [ServerFeature.ChannelTypes] feature, which will always have a value.
62
+     */
63
+    val channelTypes
64
+        get() = features[ServerFeature.ChannelTypes] ?: throw IllegalStateException("lost channel types")
65
+
60 66
     /**
61 67
      * Determines if the given mode is one applied to a user of a channel, such as 'o' for operator.
62 68
      */
@@ -148,6 +154,8 @@ sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val d
148 154
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
149 155
     /** The modes supported in channels. */
150 156
     object ChannelModes : ServerFeature<Array<String>>("CHANMODES", Array<String>::class)
157
+    /** The types of channels supported. */
158
+    object ChannelTypes : ServerFeature<String>("CHANTYPES", String::class, "#&")
151 159
     /** The maximum length of a channel name, defaulting to 200. */
152 160
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
153 161
     /** Whether or not the server supports extended who. */

+ 13
- 0
src/test/kotlin/com/dmdirc/ktirc/IrcClientImplTest.kt View File

@@ -300,6 +300,19 @@ internal class IrcClientImplTest {
300 300
         }
301 301
     }
302 302
 
303
+    @Test
304
+    fun `identifies channels that have a prefix in the chantypes feature`() {
305
+        with(IrcClientImpl(normalConfig)) {
306
+            serverState.features[ServerFeature.ChannelTypes] = "&~"
307
+            assertTrue(isChannel("&dumpsterdiving"))
308
+            assertTrue(isChannel("~hacktheplanet"))
309
+            assertFalse(isChannel("#root"))
310
+            assertFalse(isChannel("acidBurn"))
311
+            assertFalse(isChannel(""))
312
+            assertFalse(isChannel("acidBurn#~"))
313
+        }
314
+    }
315
+
303 316
     private suspend inline fun <reified T : IrcEvent> IrcClient.waitForEvent(): T {
304 317
         val mutex = Mutex(true)
305 318
         val value = AtomicReference<T>()

Loading…
Cancel
Save