Chris Smith 6fb728f7e2 Support for notices and CTCPRs | vor 5 Jahren | |
---|---|---|
docs | vor 5 Jahren | |
gradle/wrapper | vor 5 Jahren | |
src | vor 5 Jahren | |
.gitignore | vor 5 Jahren | |
.travis.yml | vor 5 Jahren | |
CHANGELOG | vor 5 Jahren | |
LICENCE | vor 5 Jahren | |
README.md | vor 5 Jahren | |
build.gradle.kts | vor 5 Jahren | |
gradlew | vor 5 Jahren | |
gradlew.bat | vor 5 Jahren |
KtIrc is a Kotlin JVM library for connecting to and interacting with IRC servers. It is still in an early stage of development.
KtIrc is published to JCenter, so adding it to a gradle build is as simple as:
repositories {
jcenter()
}
dependencies {
implementation("com.dmdirc:ktirc:<VERSION>")
}
The main interface for interacting with KtIrc is the IrcClientImpl
class. A
simple bot might look like:
with(IrcClientImpl(Server("my.server.com", 6667), Profile("nick", "realName", "userName"))) {
onEvent { event ->
when (event) {
is ServerReady -> sendJoin("#ktirc")
is MessageReceived ->
if (event.message == "!test")
reply(event, "Test successful!")
}
}
connect()
}
java.lang.IllegalStateException: Check failed
when connecting to some serversThis happens when the IRC server requests an optional client certificate (for use in SASL auth, usually). At present there is no support for client certificates in the networking library used by KtIrc. This is tracked upstream in ktor#641. There is no workaround other than using an insecure connection.
This is an issue with the Java standard library. You can change its behaviour by
defining the system property java.net.preferIPv6Addresses
to true
, e.g. by
running Java with -Djava.net.preferIPv6Addresses=true
or calling
System.setProperty("java.net.preferIPv6Addresses","true");
in code.
The LineBufferedSocket
class receives bytes from the IRC server. Whenever it
encounters a complete line (terminated by a CR
, LF
or CRLF
), it passes it
to the IrcClient
as a ByteArray
. The MessageParser
breaks up the line
into its component parts (tags, prefixes, commands, and parameters) and returns
them as an IrcMessage
.
The IrcMessage
is given to the MessageHandler
, which tries to find a
processor that can handle the command in the message. The processor’s job is
to convert the message into an IrcEvent
subclass. Processors do not get
given any contextual information or state, their job is simply to convert
the message as received into an event.
The events are returned to the MessageHandler
which then passes them on
to all registered event handlers. The job of the event handlers is twofold:
firstly, use the events to update the state of KtIrc (for example, after
receiving a JOIN
message, the ChannelStateHandler
will add the user
to the list of users in the channel, while the UserStateHandler
may update
the user’s hostname if we hadn’t previously seen it). Secondly, the event
handlers may themselves raise events. This is useful for higher-order
events such as ServerReady
that depend on a variety of factors and
states.
All the generated events (from processors or from event handlers) are
passed to the IrcClient
, which in turn passes them to the library
user via the delegates passed to the onEvent
method.
Contributing is welcomed and encouraged! Please try to add unit tests for new features, and maintain a code style consistent with the existing code.
The code in this repository is released under the MIT licence. See the LICENCE file for more info.