Browse Source

Add isChannel method

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

+ 1
- 0
CHANGELOG View File

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

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

71
     fun onEvent(handler: (IrcEvent) -> Unit)
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
     fun isLocalUser(user: User) = isLocalUser(user.nickname)
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
     fun isLocalUser(nickname: String) = caseMapping.areEquivalent(nickname, serverState.localNickname)
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
     val channelModePrefixes
57
     val channelModePrefixes
58
         get() = features[ServerFeature.ModePrefixes] ?: throw IllegalStateException("lost mode prefixes")
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
      * Determines if the given mode is one applied to a user of a channel, such as 'o' for operator.
67
      * Determines if the given mode is one applied to a user of a channel, such as 'o' for operator.
62
      */
68
      */
148
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
154
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
149
     /** The modes supported in channels. */
155
     /** The modes supported in channels. */
150
     object ChannelModes : ServerFeature<Array<String>>("CHANMODES", Array<String>::class)
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
     /** The maximum length of a channel name, defaulting to 200. */
159
     /** The maximum length of a channel name, defaulting to 200. */
152
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
160
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
153
     /** Whether or not the server supports extended who. */
161
     /** Whether or not the server supports extended who. */

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

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
     private suspend inline fun <reified T : IrcEvent> IrcClient.waitForEvent(): T {
316
     private suspend inline fun <reified T : IrcEvent> IrcClient.waitForEvent(): T {
304
         val mutex = Mutex(true)
317
         val mutex = Mutex(true)
305
         val value = AtomicReference<T>()
318
         val value = AtomicReference<T>()

Loading…
Cancel
Save