Переглянути джерело

Document IrcClient, make lots of things internal.

tags/v0.2.1
Chris Smith 5 роки тому
джерело
коміт
1eb9259e30

+ 1
- 0
CHANGELOG Переглянути файл

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

+ 60
- 11
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt Переглянути файл

10
 import java.util.logging.Level
10
 import java.util.logging.Level
11
 import java.util.logging.LogManager
11
 import java.util.logging.LogManager
12
 
12
 
13
-
13
+/**
14
+ * Primary interface for interacting with KtIrc.
15
+ */
14
 interface IrcClient {
16
 interface IrcClient {
15
 
17
 
16
-    fun send(message: String)
17
-
18
     val serverState: ServerState
18
     val serverState: ServerState
19
     val channelState: ChannelStateMap
19
     val channelState: ChannelStateMap
20
     val userState: UserState
20
     val userState: UserState
21
 
21
 
22
-    fun onEvent(handler: (IrcEvent) -> Unit)
23
-
24
     val caseMapping: CaseMapping
22
     val caseMapping: CaseMapping
25
         get() = serverState.features[ServerFeature.ServerCaseMapping] ?: CaseMapping.Rfc
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
 // TODO: How should alternative nicknames work?
80
 // TODO: How should alternative nicknames work?
32
 // TODO: Should IRC Client take a pool of servers and rotate through, or make the caller do that?
81
 // TODO: Should IRC Client take a pool of servers and rotate through, or make the caller do that?
33
 // TODO: Should there be a default profile?
82
 // TODO: Should there be a default profile?
34
 class IrcClientImpl(private val server: Server, private val profile: Profile) : IrcClient {
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
     override val serverState = ServerState(profile.initialNick)
87
     override val serverState = ServerState(profile.initialNick)
39
     override val channelState = ChannelStateMap { caseMapping }
88
     override val channelState = ChannelStateMap { caseMapping }
55
         }
104
         }
56
     }
105
     }
57
 
106
 
58
-    fun connect() {
107
+    override fun connect() {
59
         check(!connecting.getAndSet(true))
108
         check(!connecting.getAndSet(true))
60
         connectionJob = scope.launch {
109
         connectionJob = scope.launch {
61
             with(socketFactory(server.host, server.port, server.tls)) {
110
             with(socketFactory(server.host, server.port, server.tls)) {
72
         }
121
         }
73
     }
122
     }
74
 
123
 
75
-    fun disconnect() {
124
+    override fun disconnect() {
76
         socket?.disconnect()
125
         socket?.disconnect()
77
     }
126
     }
78
 
127
 
89
     }
138
     }
90
 }
139
 }
91
 
140
 
92
-fun main() {
141
+internal fun main() {
93
     val rootLogger = LogManager.getLogManager().getLogger("")
142
     val rootLogger = LogManager.getLogManager().getLogger("")
94
     rootLogger.level = Level.FINEST
143
     rootLogger.level = Level.FINEST
95
     for (h in rootLogger.handlers) {
144
     for (h in rootLogger.handlers) {
109
         client.connect()
158
         client.connect()
110
         client.join()
159
         client.join()
111
     }
160
     }
112
-}
161
+}

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/CapabilitiesHandler.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/ChannelStateHandler.kt Переглянути файл

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

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/events/EventHandler.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/EventUtils.kt Переглянути файл

4
 import com.dmdirc.ktirc.model.ServerFeature
4
 import com.dmdirc.ktirc.model.ServerFeature
5
 import com.dmdirc.ktirc.model.asUser
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
     val modePrefixes = client.serverState.features[ServerFeature.ModePrefixes]!!
8
     val modePrefixes = client.serverState.features[ServerFeature.ModePrefixes]!!
9
     for (user in names) {
9
     for (user in names) {
10
         user.takeWhile { modePrefixes.isPrefix(it) }.let { prefix ->
10
         user.takeWhile { modePrefixes.isPrefix(it) }.let { prefix ->

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/PingHandler.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/ServerStateHandler.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/UserStateHandler.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/io/CaseMapping.kt Переглянути файл

7
     RfcStrict(97..125 to 65..93);
7
     RfcStrict(97..125 to 65..93);
8
 
8
 
9
     companion object {
9
     companion object {
10
-        fun fromName(name: String) = when(name.toLowerCase()) {
10
+        internal fun fromName(name: String) = when(name.toLowerCase()) {
11
             "ascii" -> Ascii
11
             "ascii" -> Ascii
12
             "rfc1459" -> Rfc
12
             "rfc1459" -> Rfc
13
             "rfc1459-strict" -> RfcStrict
13
             "rfc1459-strict" -> RfcStrict

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/io/LineBufferedSocket.kt Переглянути файл

19
 import java.security.SecureRandom
19
 import java.security.SecureRandom
20
 import javax.net.ssl.X509TrustManager
20
 import javax.net.ssl.X509TrustManager
21
 
21
 
22
-interface LineBufferedSocket {
22
+internal interface LineBufferedSocket {
23
 
23
 
24
     suspend fun connect()
24
     suspend fun connect()
25
     fun disconnect()
25
     fun disconnect()
35
  * Asynchronous socket that buffers incoming data and emits individual lines.
35
  * Asynchronous socket that buffers incoming data and emits individual lines.
36
  */
36
  */
37
 // TODO: Expose advanced TLS options
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
     companion object {
40
     companion object {
41
         const val CARRIAGE_RETURN = '\r'.toByte()
41
         const val CARRIAGE_RETURN = '\r'.toByte()

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/io/MessageHandler.kt Переглянути файл

8
 import kotlinx.coroutines.channels.ReceiveChannel
8
 import kotlinx.coroutines.channels.ReceiveChannel
9
 import kotlinx.coroutines.channels.consumeEach
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
     private val log by logger()
13
     private val log by logger()
14
 
14
 

+ 5
- 1
src/main/kotlin/com/dmdirc/ktirc/io/MessageParser.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/messages/CapabilityProcessor.kt Переглянути файл

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

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/messages/MessageProcessor.kt Переглянути файл

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

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/model/CapabilitiesState.kt Переглянути файл

35
     object HostChangeMessages : Capability("chghost") // TODO: Add processor
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
     Capability::class.nestedClasses.map { it.objectInstance as Capability }.associateBy { it.name }
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 Переглянути файл

30
     object ServerTime : MessageTag("time")
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
     MessageTag::class.nestedClasses.map { it.objectInstance as MessageTag }.associateBy { it.name }
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 Переглянути файл

45
     object WhoxSupport : ServerFeature<Boolean>("WHOX", Boolean::class, false)
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
     ServerFeature::class.nestedClasses.map { it.objectInstance as ServerFeature<*> }.associateBy { it.name }
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 Переглянути файл

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
     val identOffset = indexOf('!')
24
     val identOffset = indexOf('!')
25
     return if (identOffset >= 0) {
25
     return if (identOffset >= 0) {
26
         val hostOffset = indexOf('@', identOffset)
26
         val hostOffset = indexOf('@', identOffset)

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/util/Logging.kt Переглянути файл

7
     return Logger.getLogger(forClass.qualifiedName)
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
     return lazy { logger(this::class) }
11
     return lazy { logger(this::class) }
12
 }
12
 }

Завантаження…
Відмінити
Зберегти