Fix parsing of SSK links without document name
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / text / SoneTextParser.kt
index 64125c1..690ce6d 100644 (file)
@@ -15,7 +15,6 @@ import net.pterodactylus.sone.text.LinkType.POST
 import net.pterodactylus.sone.text.LinkType.SONE
 import net.pterodactylus.sone.text.LinkType.SSK
 import net.pterodactylus.sone.text.LinkType.USK
-import net.pterodactylus.sone.utils.let
 import org.bitpedia.util.Base32
 import java.net.MalformedURLException
 
@@ -68,7 +67,9 @@ class SoneTextParser(private val soneProvider: SoneProvider?, private val postPr
                }
                SSK, USK ->
                        try {
-                                FreenetLinkPart(link, FreenetURI(link).docName, trusted = context?.routingKey?.contentEquals(FreenetURI(link).routingKey) == true)
+                               FreenetURI(link).let { uri ->
+                                       uri.docName ?: "${uri.keyType}@${uri.routingKey.freenetBase64}"
+                               }.let { FreenetLinkPart(link, it, trusted = context?.routingKey?.contentEquals(FreenetURI(link).routingKey) == true) }
                        } catch (e: MalformedURLException) {
                                PlainTextPart(link)
                        }
@@ -195,3 +196,5 @@ private fun isPunctuation(char: Char) = char in punctuationChars
 private val whitespace = Regex("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]")
 
 private data class NextLink(val position: Int, val linkType: LinkType, val link: String, val remainder: String)
+
+private val ByteArray.freenetBase64 get() = Base64.encode(this)!!