Browse Source

Document IrcClient, make lots of things internal.

tags/v0.2.1
Chris Smith 5 years ago
parent
commit
1eb9259e30

+ 1
- 0
CHANGELOG View File

@@ -1,5 +1,6 @@
1 1
 vNEXT (in development)
2 2
 
3
+  * Added documentation and reduced visibility of some internal methods/classes
3 4
   * (Internal) Enabled Travis, Codacy and Coveralls
4 5
 
5 6
 v0.2.0

+ 60
- 11
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt View File

@@ -10,30 +10,79 @@ import java.util.concurrent.atomic.AtomicBoolean
10 10
 import java.util.logging.Level
11 11
 import java.util.logging.LogManager
12 12
 
13
-
13
+/**
14
+ * Primary interface for interacting with KtIrc.
15
+ */
14 16
 interface IrcClient {
15 17
 
16
-    fun send(message: String)
17
-
18 18
     val serverState: ServerState
19 19
     val channelState: ChannelStateMap
20 20
     val userState: UserState
21 21
 
22
-    fun onEvent(handler: (IrcEvent) -> Unit)
23
-
24 22
     val caseMapping: CaseMapping
25 23
         get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
26 24
 
27
-    fun isLocalUser(user: User): Boolean = caseMapping.areEquivalent(user.nickname, serverState.localNickname)
25
+    /**
26
+     * Begins a connection attempt to the IRC server.
27
+     *
28
+     * This method will return immediately, and the attempt to connect will be executed in a coroutine on the
29
+     * IO scheduler. To check the status of the connection, monitor events using [onEvent].
30
+     */
31
+    fun connect()
32
+
33
+    /**
34
+     * Disconnect immediately from the IRC server, without sending a QUIT.
35
+     */
36
+    fun disconnect()
37
+
38
+    /**
39
+     * Sends the given raw line to the IRC server, followed by a carriage return and line feed.
40
+     *
41
+     * Standard IRC messages can be constructed using the methods in [com.dmdirc.ktirc.messages]
42
+     * such as [joinMessage].
43
+     *
44
+     * @param message The line to be sent to the IRC server.
45
+     */
46
+    fun send(message: String)
47
+
48
+    /**
49
+     * Registers a new handler for all events on this connection.
50
+     *
51
+     * All events are subclasses of [IrcEvent]; the idomatic way to handle them is using a `when` statement:
52
+     *
53
+     * ```
54
+     * client.onEvent {
55
+     *     when(it) {
56
+     *         is MessageReceived -> println(it.message)
57
+     *     }
58
+     * }
59
+     * ```
60
+     *
61
+     * *Note*: at present handlers cannot be removed; they last the lifetime of the [IrcClient].
62
+     *
63
+     * @param handler The method to call when a new event occurs.
64
+     */
65
+    fun onEvent(handler: (IrcEvent) -> Unit)
66
+
67
+    /**
68
+     * Utility method to determine if the given user is the one we are connected to IRC as.
69
+     */
70
+    fun isLocalUser(user: User) = caseMapping.areEquivalent(user.nickname, serverState.localNickname)
28 71
 
29 72
 }
30 73
 
74
+/**
75
+ * Concrete implementation of an [IrcClient].
76
+ *
77
+ * @param server The server to connect to.
78
+ * @param profile The user details to use when connecting.
79
+ */
31 80
 // TODO: How should alternative nicknames work?
32 81
 // TODO: Should IRC Client take a pool of servers and rotate through, or make the caller do that?
33 82
 // TODO: Should there be a default profile?
34 83
 class IrcClientImpl(private val server: Server, private val profile: Profile) : IrcClient {
35 84
 
36
-    var socketFactory: (String, Int, Boolean) -> LineBufferedSocket = ::KtorLineBufferedSocket
85
+    internal var socketFactory: (String, Int, Boolean) -> LineBufferedSocket = ::KtorLineBufferedSocket
37 86
 
38 87
     override val serverState = ServerState(profile.initialNick)
39 88
     override val channelState = ChannelStateMap { caseMapping }
@@ -55,7 +104,7 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
55 104
         }
56 105
     }
57 106
 
58
-    fun connect() {
107
+    override fun connect() {
59 108
         check(!connecting.getAndSet(true))
60 109
         connectionJob = scope.launch {
61 110
             with(socketFactory(server.host, server.port, server.tls)) {
@@ -72,7 +121,7 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
72 121
         }
73 122
     }
74 123
 
75
-    fun disconnect() {
124
+    override fun disconnect() {
76 125
         socket?.disconnect()
77 126
     }
78 127
 
@@ -89,7 +138,7 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
89 138
     }
90 139
 }
91 140
 
92
-fun main() {
141
+internal fun main() {
93 142
     val rootLogger = LogManager.getLogManager().getLogger("")
94 143
     rootLogger.level = Level.FINEST
95 144
     for (h in rootLogger.handlers) {
@@ -109,4 +158,4 @@ fun main() {
109 158
         client.connect()
110 159
         client.join()
111 160
     }
112
-}
161
+}

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/CapabilitiesHandler.kt View File

@@ -8,7 +8,7 @@ import com.dmdirc.ktirc.model.CapabilitiesState
8 8
 import com.dmdirc.ktirc.model.Capability
9 9
 import com.dmdirc.ktirc.util.logger
10 10
 
11
-class CapabilitiesHandler : EventHandler {
11
+internal class CapabilitiesHandler : EventHandler {
12 12
 
13 13
     private val log by logger()
14 14
 

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

@@ -5,7 +5,7 @@ import com.dmdirc.ktirc.model.ChannelState
5 5
 import com.dmdirc.ktirc.model.ChannelUser
6 6
 import com.dmdirc.ktirc.util.logger
7 7
 
8
-class ChannelStateHandler : EventHandler {
8
+internal class ChannelStateHandler : EventHandler {
9 9
 
10 10
     private val log by logger()
11 11
 

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/events/EventHandler.kt View File

@@ -3,13 +3,13 @@ package com.dmdirc.ktirc.events
3 3
 import com.dmdirc.ktirc.IrcClient
4 4
 
5 5
 @FunctionalInterface
6
-interface EventHandler {
6
+internal interface EventHandler {
7 7
 
8 8
     fun processEvent(client: IrcClient, event: IrcEvent)
9 9
 
10 10
 }
11 11
 
12
-val eventHandlers = listOf(
12
+internal val eventHandlers = listOf(
13 13
         CapabilitiesHandler(),
14 14
         ChannelStateHandler(),
15 15
         PingHandler(),

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/EventUtils.kt View File

@@ -4,7 +4,7 @@ import com.dmdirc.ktirc.IrcClient
4 4
 import com.dmdirc.ktirc.model.ServerFeature
5 5
 import com.dmdirc.ktirc.model.asUser
6 6
 
7
-fun ChannelNamesReceived.toModesAndUsers(client: IrcClient) = sequence {
7
+internal fun ChannelNamesReceived.toModesAndUsers(client: IrcClient) = sequence {
8 8
     val modePrefixes = client.serverState.features[ServerFeature.ModePrefixes]!!
9 9
     for (user in names) {
10 10
         user.takeWhile { modePrefixes.isPrefix(it) }.let { prefix ->

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/PingHandler.kt View File

@@ -3,7 +3,7 @@ package com.dmdirc.ktirc.events
3 3
 import com.dmdirc.ktirc.IrcClient
4 4
 import com.dmdirc.ktirc.messages.pongMessage
5 5
 
6
-class PingHandler : EventHandler {
6
+internal class PingHandler : EventHandler {
7 7
 
8 8
     override fun processEvent(client: IrcClient, event: IrcEvent) {
9 9
         when (event) {

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/ServerStateHandler.kt View File

@@ -2,7 +2,7 @@ package com.dmdirc.ktirc.events
2 2
 
3 3
 import com.dmdirc.ktirc.IrcClient
4 4
 
5
-class ServerStateHandler : EventHandler {
5
+internal class ServerStateHandler : EventHandler {
6 6
 
7 7
     override fun processEvent(client: IrcClient, event: IrcEvent) {
8 8
         when (event) {

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/UserStateHandler.kt View File

@@ -3,7 +3,7 @@ package com.dmdirc.ktirc.events
3 3
 import com.dmdirc.ktirc.IrcClient
4 4
 import com.dmdirc.ktirc.model.UserState
5 5
 
6
-class UserStateHandler : EventHandler {
6
+internal class UserStateHandler : EventHandler {
7 7
 
8 8
     override fun processEvent(client: IrcClient, event: IrcEvent) {
9 9
         when (event) {

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/io/CaseMapping.kt View File

@@ -7,7 +7,7 @@ enum class CaseMapping(private val lowerToUpperMapping: Pair<IntRange, IntRange>
7 7
     RfcStrict(97..125 to 65..93);
8 8
 
9 9
     companion object {
10
-        fun fromName(name: String) = when(name.toLowerCase()) {
10
+        internal fun fromName(name: String) = when(name.toLowerCase()) {
11 11
             "ascii" -> Ascii
12 12
             "rfc1459" -> Rfc
13 13
             "rfc1459-strict" -> RfcStrict

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/io/LineBufferedSocket.kt View File

@@ -19,7 +19,7 @@ import java.net.InetSocketAddress
19 19
 import java.security.SecureRandom
20 20
 import javax.net.ssl.X509TrustManager
21 21
 
22
-interface LineBufferedSocket {
22
+internal interface LineBufferedSocket {
23 23
 
24 24
     suspend fun connect()
25 25
     fun disconnect()
@@ -35,7 +35,7 @@ interface LineBufferedSocket {
35 35
  * Asynchronous socket that buffers incoming data and emits individual lines.
36 36
  */
37 37
 // TODO: Expose advanced TLS options
38
-class KtorLineBufferedSocket(private val host: String, private val port: Int, private val tls: Boolean = false): LineBufferedSocket {
38
+internal class KtorLineBufferedSocket(private val host: String, private val port: Int, private val tls: Boolean = false): LineBufferedSocket {
39 39
 
40 40
     companion object {
41 41
         const val CARRIAGE_RETURN = '\r'.toByte()

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/io/MessageHandler.kt View File

@@ -8,7 +8,7 @@ import com.dmdirc.ktirc.util.logger
8 8
 import kotlinx.coroutines.channels.ReceiveChannel
9 9
 import kotlinx.coroutines.channels.consumeEach
10 10
 
11
-class MessageHandler(private val processors: List<MessageProcessor>, val handlers: MutableList<EventHandler>) {
11
+internal class MessageHandler(private val processors: List<MessageProcessor>, val handlers: MutableList<EventHandler>) {
12 12
 
13 13
     private val log by logger()
14 14
 

+ 5
- 1
src/main/kotlin/com/dmdirc/ktirc/io/MessageParser.kt View File

@@ -17,17 +17,21 @@ import com.dmdirc.ktirc.util.logger
17 17
  *
18 18
  * For example:
19 19
  *
20
+ * ```
20 21
  * @aaa=bbb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG #someChannel :This is a test message
21 22
  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
22 23
  * IRCv3 tags                       Prefix               Cmd     Param #1     Trailing parameter
24
+ * ```
23 25
  *
24 26
  * or:
25 27
  *
28
+ * ```
26 29
  * PING 12345678
27 30
  * ^^^^ ^^^^^^^^
28 31
  * Cmd  Param #1
32
+ * ```
29 33
  */
30
-class MessageParser {
34
+internal class MessageParser {
31 35
 
32 36
     companion object {
33 37
         private const val AT = '@'.toByte()

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

@@ -8,7 +8,7 @@ import com.dmdirc.ktirc.model.capabilities
8 8
 import com.dmdirc.ktirc.util.logger
9 9
 import java.time.LocalDateTime
10 10
 
11
-class CapabilityProcessor : MessageProcessor {
11
+internal class CapabilityProcessor : MessageProcessor {
12 12
 
13 13
     private val log by logger()
14 14
 

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

@@ -3,7 +3,7 @@ package com.dmdirc.ktirc.messages
3 3
 import com.dmdirc.ktirc.events.IrcEvent
4 4
 import com.dmdirc.ktirc.model.IrcMessage
5 5
 
6
-interface MessageProcessor {
6
+internal interface MessageProcessor {
7 7
 
8 8
     /**
9 9
      * The messages which this handler can process.
@@ -17,7 +17,7 @@ interface MessageProcessor {
17 17
 
18 18
 }
19 19
 
20
-val messageProcessors = setOf(
20
+internal val messageProcessors = setOf(
21 21
         CapabilityProcessor(),
22 22
         ISupportProcessor(),
23 23
         JoinProcessor(),

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/model/CapabilitiesState.kt View File

@@ -35,6 +35,6 @@ sealed class Capability(val name: String) {
35 35
     object HostChangeMessages : Capability("chghost") // TODO: Add processor
36 36
 }
37 37
 
38
-val capabilities: Map<String, Capability> by lazy {
38
+internal val capabilities: Map<String, Capability> by lazy {
39 39
     Capability::class.nestedClasses.map { it.objectInstance as Capability }.associateBy { it.name }
40 40
 }

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/model/IrcMessage.kt View File

@@ -30,6 +30,6 @@ sealed class MessageTag(val name: String) {
30 30
     object ServerTime : MessageTag("time")
31 31
 }
32 32
 
33
-val messageTags: Map<String, MessageTag> by lazy {
33
+internal val messageTags: Map<String, MessageTag> by lazy {
34 34
     MessageTag::class.nestedClasses.map { it.objectInstance as MessageTag }.associateBy { it.name }
35 35
 }

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

@@ -45,6 +45,6 @@ sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val d
45 45
     object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
46 46
 }
47 47
 
48
-val serverFeatures: Map<String, ServerFeature<*>> by lazy {
48
+internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
49 49
     ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
50 50
 }

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

@@ -18,9 +18,9 @@ data class User(
18 18
     }
19 19
 }
20 20
 
21
-fun ByteArray.asUser() = String(this).asUser()
21
+internal fun ByteArray.asUser() = String(this).asUser()
22 22
 
23
-fun String.asUser(): User {
23
+internal fun String.asUser(): User {
24 24
     val identOffset = indexOf('!')
25 25
     return if (identOffset >= 0) {
26 26
         val hostOffset = indexOf('@', identOffset)

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/util/Logging.kt View File

@@ -7,6 +7,6 @@ private fun <T: Any> logger(forClass: KClass<T>): Logger {
7 7
     return Logger.getLogger(forClass.qualifiedName)
8 8
 }
9 9
 
10
-fun <R : Any> R.logger(): Lazy<Logger> {
10
+internal fun <R : Any> R.logger(): Lazy<Logger> {
11 11
     return lazy { logger(this::class) }
12 12
 }

Loading…
Cancel
Save