Skip to content

Commit

Permalink
Update Kotlin and API.
Browse files Browse the repository at this point in the history
  • Loading branch information
libalex committed Dec 21, 2022
1 parent 005b13e commit 4d01e9f
Show file tree
Hide file tree
Showing 98 changed files with 713 additions and 681 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
plugins {
id 'maven-publish'
id 'signing'
id 'org.jetbrains.kotlin.jvm' version '1.7.20'
id 'org.jetbrains.kotlin.jvm' version '1.7.21'
}

group = 'io.github.obimp'
version = '0.1.6'
version = '0.1.7'

repositories {
mavenCentral()
Expand Down Expand Up @@ -38,8 +38,6 @@ java {
dependencies {
implementation 'io.github.obimp:obimp4j-core:0.1.7'
implementation 'org.bouncycastle:bctls-jdk18on:1.72'
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.7.20'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
}

Expand Down Expand Up @@ -83,7 +81,7 @@ publishing {
}
pom {
name = 'OBIMP4J'
description = 'Java OBIMP (Open Binary Instant Messaging Protocol) implementation.'
description = 'Java OBIMP (Open Binary Instant Messaging Protocol) implementation, client library.'
url = 'https://github.com/obimp/obimp4j-client'
licenses {
license {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 6 additions & 0 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
14 changes: 8 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,7 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +75,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
11 changes: 4 additions & 7 deletions src/main/kotlin/io/github/obimp/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

package io.github.obimp

import io.github.obimp.connection.Connection
import io.github.obimp.listener.OBIMPEventListener

/**
* Client
* @author Alexander Krysin
*/
sealed interface Client {
val configuration: ClientConfiguration

fun configure(block: ClientConfiguration.() -> Unit) = configuration.block()

fun createConnection(secure: Boolean = false): Connection<*>
internal sealed interface Client {
fun connect(hostname: String, port: Int)
fun login(username: String, password: String)
}
65 changes: 60 additions & 5 deletions src/main/kotlin/io/github/obimp/OBIMPClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,70 @@

package io.github.obimp

import io.github.obimp.connection.PlainObimpConnection
import io.github.obimp.connection.SecureObimpConnection
import io.github.obimp.connection.PlainOBIMPConnection
import io.github.obimp.connection.SecureOBIMPConnection
import io.github.obimp.listener.*

/**
* OBIMP Client
* @author Alexander Krysin
*/
object OBIMPClient : Client {
override val configuration: ClientConfiguration = OBIMPClientConfiguration
class OBIMPClient(secure: Boolean, configure: ClientConfiguration.() -> Unit = {}) : Client {
private val configuration = OBIMPClientConfiguration()
private val connection = when {
secure -> SecureOBIMPConnection(configuration)
else -> PlainOBIMPConnection(configuration)
}

override fun createConnection(secure: Boolean) = if (secure) SecureObimpConnection() else PlainObimpConnection()
init {
configuration.configure()
}

fun addCommonListener(listener: CommonListener) = connection.addListener<CommonListener>(listener)

fun addContactListListener(listener: ContactListListener) = connection.addListener<ContactListListener>(listener)

fun addPresenceInfoListener(listener: PresenceInfoListener) = connection.addListener<PresenceInfoListener>(listener)

fun addInstantMessagingListener(listener: InstantMessagingListener) =
connection.addListener<InstantMessagingListener>(listener)

fun addUsersDirectoryListener(listener: UsersDirectoryListener) =
connection.addListener<UsersDirectoryListener>(listener)

fun addUserAvatarsListener(listener: UserAvatarsListener) = connection.addListener<UserAvatarsListener>(listener)

fun addFileTransferListener(listener: FileTransferListener) = connection.addListener<FileTransferListener>(listener)

fun addTransportsListener(listener: TransportsListener) = connection.addListener<TransportsListener>(listener)

fun removeCommonListener(listener: CommonListener) = connection.removeListener<CommonListener>(listener)

fun removeContactListListener(listener: ContactListListener) =
connection.removeListener<ContactListListener>(listener)

fun removePresenceInfoListener(listener: PresenceInfoListener) =
connection.removeListener<PresenceInfoListener>(listener)

fun removeInstantMessagingListener(listener: InstantMessagingListener) =
connection.removeListener<InstantMessagingListener>(listener)

fun removeUsersDirectoryListener(listener: UsersDirectoryListener) =
connection.removeListener<UsersDirectoryListener>(listener)

fun removeUserAvatarsListener(listener: UserAvatarsListener) =
connection.removeListener<UserAvatarsListener>(listener)

fun removeFileTransferListener(listener: FileTransferListener) =
connection.removeListener<FileTransferListener>(listener)

fun removeTransportsListener(listener: TransportsListener) = connection.removeListener<TransportsListener>(listener)

override fun connect(hostname: String, port: Int) {
connection.connect(hostname, port)
}

override fun login(username: String, password: String) {
connection.login(username, password)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.github.obimp.util.Version
* OBIMP Client configuration
* @author Alexander Krysin
*/
object OBIMPClientConfiguration : ClientConfiguration {
internal class OBIMPClientConfiguration : ClientConfiguration {
override var clientType: ClientType = ClientType.USER
override var clientName: String = LibVersion.NAME
override var clientVersion: Version = LibVersion.VERSION
Expand Down
107 changes: 107 additions & 0 deletions src/main/kotlin/io/github/obimp/connection/AbstractOBIMPConnection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* OBIMP4J - Java OBIMP Lib
* Copyright (C) 2013—2022 Alexander Krysin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.github.obimp.connection

import io.github.obimp.ClientConfiguration
import io.github.obimp.common.DisconnectReason
import io.github.obimp.data.structure.DataStructure
import io.github.obimp.data.structure.WTLD
import io.github.obimp.data.type.BLK
import io.github.obimp.data.type.LongWord
import io.github.obimp.data.type.OctaWord
import io.github.obimp.data.type.UTF8
import io.github.obimp.listener.CommonListener
import io.github.obimp.packet.ObimpPacket
import io.github.obimp.packet.Packet
import io.github.obimp.packet.handle.OBIMPPacketHandler.Companion.OBIMP_BEX_COM
import io.github.obimp.packet.handle.common.CommonPacketHandler.Companion.OBIMP_BEX_COM_CLI_HELLO
import io.github.obimp.packet.handle.common.CommonPacketHandler.Companion.OBIMP_BEX_COM_CLI_LOGIN
import io.github.obimp.util.HashUtils
import java.nio.ByteBuffer
import java.nio.channels.SocketChannel

/**
* Abstract OBIMP connection
* @author Alexander Krysin
*/
internal abstract class AbstractOBIMPConnection(
internal val configuration: ClientConfiguration
) : OBIMPConnection, ListenerManager() {
protected val channel: SocketChannel = SocketChannel.open()
protected val outputCache = mutableListOf<Packet<out DataStructure<*>>>()
private var sequence = 0
get() = field++
internal lateinit var username: String
private lateinit var password: String

override fun sendPacket(packet: Packet<out DataStructure<*>>) {
try {
packet.header.sequence = sequence
packet.header.contentLength = packet.body.getLength()
outputCache.add(packet)
} catch (e: Exception) {
for (commonListener in getListeners<CommonListener>()) {
commonListener.onDisconnect(DisconnectReason.NETWORK_ERROR)
}
}
}

override fun login(username: String, password: String) {
this.username = username
this.password = password
val packet = ObimpPacket(OBIMP_BEX_COM, OBIMP_BEX_COM_CLI_HELLO)
packet.addItem(WTLD(LongWord(0x0001), UTF8(username)))
sendPacket(packet)
}

override fun plaintextLogin() {
val login = ObimpPacket(OBIMP_BEX_COM, OBIMP_BEX_COM_CLI_LOGIN)
login.addItem(WTLD(LongWord(0x0001), UTF8(username)))
login.addItem(WTLD(LongWord(0x0003), BLK(ByteBuffer.wrap(HashUtils.base64(password)))))
sendPacket(login)
}

override fun hashLogin(serverKey: ByteArray) {
val login = ObimpPacket(OBIMP_BEX_COM, OBIMP_BEX_COM_CLI_LOGIN)
login.addItem(WTLD(LongWord(0x0001), UTF8(username)))
login.addItem(
WTLD(
LongWord(0x0002),
OctaWord(ByteBuffer.wrap(HashUtils.md5(HashUtils.md5(username + SALT + password) + serverKey)))
)
)
sendPacket(login)
}

override fun disconnect() {
close()
for (commonListener in getListeners<CommonListener>()) {
commonListener.onDisconnect(DisconnectReason.DISCONNECTED_BY_USER)
}
}

protected fun close() {
channel.close()
Selector.stop()
}

companion object {
internal const val SALT = "OBIMPSALT"
}
}
50 changes: 0 additions & 50 deletions src/main/kotlin/io/github/obimp/connection/Connection.kt

This file was deleted.

Loading

0 comments on commit 4d01e9f

Please sign in to comment.