diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt index 94a29ee..a97cfdc 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt @@ -73,6 +73,9 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) val addresses: AddressRepository @JvmName("addresses") get + val labels: LabelRepository + @JvmName("labels") get + val messages: MessageRepository @JvmName("messages") get @@ -301,6 +304,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) var nodeRegistry by Delegates.notNull() var networkHandler by Delegates.notNull() var addressRepo by Delegates.notNull() + var labelRepo by Delegates.notNull() var messageRepo by Delegates.notNull() var proofOfWorkRepo by Delegates.notNull() var proofOfWorkEngine: ProofOfWorkEngine? = null @@ -330,6 +334,11 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) return this } + fun labelRepo(labelRepo: LabelRepository): Builder { + this.labelRepo = labelRepo + return this + } + fun messageRepo(messageRepo: MessageRepository): Builder { this.messageRepo = messageRepo return this @@ -386,6 +395,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) builder.nodeRegistry, builder.networkHandler, builder.addressRepo, + builder.labelRepo, builder.messageRepo, builder.proofOfWorkRepo, builder.proofOfWorkEngine ?: MultiThreadedPOWEngine(), @@ -400,6 +410,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) builder.preferences ) this.addresses = builder.addressRepo + this.labels = builder.labelRepo this.messages = builder.messageRepo (builder.listener as? Listener.WithContext)?.setContext(this) internals.proofOfWorkService.doMissingProofOfWork(builder.preferences.doMissingProofOfWorkDelayInSeconds * 1000L) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt index 6415b8d..c7b303d 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt @@ -40,11 +40,12 @@ import java.util.concurrent.Executors */ class InternalContext( val cryptography: Cryptography, - val inventory: ch.dissem.bitmessage.ports.Inventory, + val inventory: Inventory, val nodeRegistry: NodeRegistry, val networkHandler: NetworkHandler, val addressRepository: AddressRepository, - val messageRepository: ch.dissem.bitmessage.ports.MessageRepository, + val labelRepository: LabelRepository, + val messageRepository: MessageRepository, val proofOfWorkRepository: ProofOfWorkRepository, val proofOfWorkEngine: ProofOfWorkEngine, val customCommandHandler: CustomCommandHandler, @@ -216,7 +217,9 @@ class InternalContext( companion object { private val LOG = LoggerFactory.getLogger(InternalContext::class.java) - @JvmField val NETWORK_NONCE_TRIALS_PER_BYTE: Long = 1000 - @JvmField val NETWORK_EXTRA_BYTES: Long = 1000 + @JvmField + val NETWORK_NONCE_TRIALS_PER_BYTE: Long = 1000 + @JvmField + val NETWORK_EXTRA_BYTES: Long = 1000 } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt index 236d581..d382436 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt @@ -35,6 +35,7 @@ import java.nio.ByteBuffer import java.util.* import java.util.Collections import kotlin.collections.HashSet +import kotlin.collections.LinkedHashSet private fun message(encoding: Plaintext.Encoding, subject: String, body: String): ByteArray = when (encoding) { SIMPLE -> "Subject:$subject\nBody:$body".toByteArray() @@ -254,7 +255,7 @@ class Plaintext private constructor( received = builder.received, initialHash = null, ttl = builder.ttl, - labels = builder.labels, + labels = LinkedHashSet(builder.labels), status = builder.status ?: Status.RECEIVED ) { id = builder.id @@ -278,28 +279,30 @@ class Plaintext private constructor( get() { val s = Scanner(ByteArrayInputStream(message), "UTF-8") val firstLine = s.nextLine() - if (encodingCode == EXTENDED.code) { - if (Message.TYPE == extendedData?.type) { - return (extendedData!!.content as? Message)?.subject + return when (encodingCode) { + EXTENDED.code -> if (Message.TYPE == extendedData?.type) { + (extendedData!!.content as? Message)?.subject } else { - return null + null + } + SIMPLE.code -> firstLine.substring("Subject:".length).trim { it <= ' ' } + else -> { + if (firstLine.length > 50) { + firstLine.substring(0, 50).trim { it <= ' ' } + "..." + } else { + firstLine + } } - } else if (encodingCode == SIMPLE.code) { - return firstLine.substring("Subject:".length).trim { it <= ' ' } - } else if (firstLine.length > 50) { - return firstLine.substring(0, 50).trim { it <= ' ' } + "..." - } else { - return firstLine } } val text: String? get() { if (encodingCode == EXTENDED.code) { - if (Message.TYPE == extendedData?.type) { - return (extendedData?.content as Message?)?.body + return if (Message.TYPE == extendedData?.type) { + (extendedData?.content as Message?)?.body } else { - return null + null } } else { val text = String(message) @@ -322,20 +325,20 @@ class Plaintext private constructor( val parents: List get() { val extendedData = extendedData ?: return emptyList() - if (Message.TYPE == extendedData.type) { - return (extendedData.content as Message).parents + return if (Message.TYPE == extendedData.type) { + (extendedData.content as Message).parents } else { - return emptyList() + emptyList() } } val files: List get() { val extendedData = extendedData ?: return emptyList() - if (Message.TYPE == extendedData.type) { - return (extendedData.content as Message).files + return if (Message.TYPE == extendedData.type) { + (extendedData.content as Message).files } else { - return emptyList() + emptyList() } } @@ -378,8 +381,8 @@ class Plaintext private constructor( override fun toString(): String { val subject = subject - if (subject?.isNotEmpty() ?: false) { - return subject!! + if (subject?.isNotEmpty() == true) { + return subject } else { return Strings.hex( initialHash ?: return super.toString() @@ -529,32 +532,43 @@ class Plaintext private constructor( } class Builder(internal val type: Type) { - internal var id: Any? = null - internal var inventoryVector: InventoryVector? = null - internal var from: BitmessageAddress? = null - internal var to: BitmessageAddress? = null - private var addressVersion: Long = 0 - private var stream: Long = 0 - private var behaviorBitfield: Int = 0 - private var publicSigningKey: ByteArray? = null - private var publicEncryptionKey: ByteArray? = null - private var nonceTrialsPerByte: Long = 0 - private var extraBytes: Long = 0 - private var destinationRipe: ByteArray? = null - private var preventAck: Boolean = false - internal var encoding: Long = 0 - internal var message = ByteArray(0) - internal var ackData: ByteArray? = null - internal var ackMessage: ByteArray? = null - internal var signature: ByteArray? = null - internal var sent: Long? = null - internal var received: Long? = null - internal var status: Status? = null - internal val labels = LinkedHashSet