bug fix for scala port

This commit is contained in:
A.C.Sukazyo Eyre 2023-09-17 15:18:57 +08:00
parent a8b7562b51
commit 6e82227447
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
25 changed files with 69 additions and 46 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@
#build
/build/
/bin/
/out/
.metals/
.bloop/
.project

View File

@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur
MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono
MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s
VERSION = 1.0.0-RC4
VERSION = 1.0.0-RC5
USE_DELTA = true
VERSION_DELTA = scalaport4

View File

@ -17,6 +17,7 @@ object MornySystem {
@BuildConfigField val VERSION_DELTA: String = BuildConfig.VERSION_DELTA
@BuildConfigField val CODENAME: String = BuildConfig.CODENAME
@BuildConfigField val CODE_STORE: String = BuildConfig.CODE_STORE
//noinspection ScalaWeakerAccess
@BuildConfigField val COMMIT_PATH: String = BuildConfig.COMMIT_PATH
@BuildConfigField

View File

@ -25,6 +25,10 @@ object DirectMsgClear extends ISimpleCommand {
logger trace "message is not outdated(48 hrs ago)"
val isTrusted = MornyCoeur.trusted isTrusted event.message.from.id
// todo:
// it does not work. due to the Telegram Bot API doesn't provide
// nested replyToMessage, so currently the trusted check by
// replyToMessage.replyToMessage will not work!
def _isReplyTrusted: Boolean =
if (event.message.replyToMessage.replyToMessage == null) false
else if (event.message.replyToMessage.replyToMessage.from.id == event.message.from.id) true

View File

@ -14,6 +14,7 @@ import java.net.InetAddress
import java.rmi.UnknownHostException
import scala.language.postfixOps
// todo: maybe move some utils method outside
object MornyInformation extends ITelegramCommand {
private case object Subs {
@ -46,6 +47,7 @@ object MornyInformation extends ITelegramCommand {
}
//noinspection ScalaWeakerAccess
def getVersionGitTagHTML: String = {
if (!MornySystem.isGitBuild) return ""
val g = StringBuilder()
@ -61,11 +63,12 @@ object MornyInformation extends ITelegramCommand {
val v = StringBuilder()
v ++= s"<code>${MornySystem VERSION_BASE}</code>"
if (MornySystem isUseDelta) v++=s"-δ<code>${MornySystem VERSION_DELTA}</code>"
if (MornySystem isGitBuild) v++="+"++=getVersionGitTagHTML
if (MornySystem isGitBuild) v++="+git."++=getVersionGitTagHTML
v ++= s"*<code>${MornySystem.CODENAME toUpperCase}</code>"
v toString
}
//noinspection ScalaWeakerAccess
def getRuntimeHostname: String|Null = {
try InetAddress.getLocalHost.getHostName
catch case _:UnknownHostException => null
@ -154,13 +157,13 @@ object MornyInformation extends ITelegramCommand {
event.message.chat.id,
/* language=html */
s"""system:
|- Morny <code>${h(if (getRuntimeHostname == null) "<unknown-host>" else getRuntimeHostname)}</code>
|- <code>${h(if (getRuntimeHostname == null) "<unknown-host>" else getRuntimeHostname)}</code>
|- <code>${h(sysprop("os.name"))}</code> <code>${h(sysprop("os.arch"))}</code> <code>${h(sysprop("os.version"))}</code>
|java runtime:
|- <code>${h(sysprop("java.vm.vendor"))}.${h(sysprop("java.vm.name"))}</code>
|- <code>${h(sysprop("java.vm.version"))}</code>
|vm memory:
|- <code>${Runtime.getRuntime.totalMemory/1024/1024}</code> / <code>${Runtime.getRuntime.maxMemory/1024/1024}</code>
|- <code>${Runtime.getRuntime.totalMemory/1024/1024}</code> / <code>${Runtime.getRuntime.maxMemory/1024/1024}</code> MB
|- <code>${Runtime.getRuntime.availableProcessors}</code> cores
|coeur version:
|- $getVersionAllFullTagHTML

View File

@ -28,7 +28,7 @@ object MornyOldJrrp extends ITelegramCommand {
MornyCoeur.extra exec SendMessage(
event.message.chat.id,
// language=html
f"${user.fullnameRefHTML} 在(utc的)今天的运气指数是———— <code>$jrrp%.2f%%</code>${h(ending)}"
f"${user.fullnameRefHTML} 在(utc的)今天的运气指数是———— <code>$jrrp%.2f%%</code> ${h(ending)}"
).replyToMessageId(event.message.messageId).parseMode(ParseMode HTML)
}

View File

@ -25,10 +25,10 @@ object Nbnhhsh extends ITelegramCommand {
override def execute (using command: InputCommand, event: Update): Unit = {
val queryTarget: String|Null =
if (event.message.replyToMessage != null && event.message.replyToMessage.text != null)
event.message.replyToMessage.text
else if command.args nonEmpty then
if command.args nonEmpty then
command.args mkString " "
else if (event.message.replyToMessage != null && event.message.replyToMessage.text != null)
event.message.replyToMessage.text
else null
if (queryTarget == null)

View File

@ -3,15 +3,15 @@ package cc.sukazyo.cono.morny.bot.command
import cc.sukazyo.cono.morny.MornyCoeur
import cc.sukazyo.cono.morny.data.TelegramStickers
import cc.sukazyo.cono.morny.util.tgapi.InputCommand
import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.model.{Message, Update}
import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.request.{SendMessage, SendSticker}
import javax.swing.text.html.HTML
import scala.annotation.unused
import scala.language.postfixOps
@SuppressWarnings(Array("NonAsciiCharacters"))
//noinspection NonAsciiCharacters
object 喵呜 {
object 抱抱 extends ISimpleCommand {
@ -56,7 +56,7 @@ object 喵呜 {
}
private def replyingSet (whileRec: String, whileNew: String)(using event: Update): Unit = {
val isNew = event.message.replyToMessage == null;
val isNew = event.message.replyToMessage == null
val target = if (isNew) event.message else event.message.replyToMessage
MornyCoeur.extra exec new SendMessage(
event.message.chat.id,

View File

@ -7,6 +7,7 @@ import cc.sukazyo.cono.morny.util.UseRandom.*
import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.request.SendMessage
//noinspection NonAsciiCharacters
object 私わね extends ISimpleCommand {
override val name: String = "me"

View File

@ -52,6 +52,8 @@ object OnCallMe extends EventListener {
var isAllowed = false
var lastDinnerData: Message|Null = null
if (MornyCoeur.trusted isTrusted_dinnerReader req.from.id) {
// todo: have issues
// i dont want to test it anymore... it might be deprecated soon
lastDinnerData = (MornyCoeur.extra exec GetChat(MornyCoeur.config.dinnerChatId)).chat.pinnedMessage
val sendResp = MornyCoeur.extra exec ForwardMessage(
req.from.id,

View File

@ -52,9 +52,9 @@ object OnEventHackHandle extends EventListener {
override def onEditedMessage (using update: Update): Boolean =
onEventHacked(update.editedMessage.chat.id, update.editedMessage.from.id)
override def onChannelPost (using update: Update): Boolean =
onEventHacked(update.channelPost.chat.id, update.channelPost.from.id)
onEventHacked(update.channelPost.chat.id, 0)
override def onEditedChannelPost (using update: Update): Boolean =
onEventHacked(update.editedChannelPost.chat.id, update.editedChannelPost.from.id)
onEventHacked(update.editedChannelPost.chat.id, 0)
override def onInlineQuery (using update: Update): Boolean =
onEventHacked(0, update.inlineQuery.from.id)
override def onChosenInlineResult (using update: Update): Boolean =

View File

@ -9,7 +9,14 @@ import scala.language.postfixOps
object OnQuestionMarkReply extends EventListener {
private def QUESTION_MARKS = Set('?', '', '¿', '⁈', '⁇', '‽', '❔', '❓')
private val QUESTION_MARKS = Set('?', '', '¿', '⁈', '⁇', '‽', '❔', '❓')
def isAllMessageMark (using text: String): Boolean = {
var isAll = true
for (c <- text)
if !(QUESTION_MARKS contains c) then isAll = false
isAll
}
override def onMessage (using event: Update): Boolean = {
@ -18,8 +25,7 @@ object OnQuestionMarkReply extends EventListener {
import cc.sukazyo.cono.morny.util.UseMath.over
import cc.sukazyo.cono.morny.util.UseRandom.chance_is
if (1 over 8) chance_is false then return false
for (c <- event.message.text toCharArray)
if !(QUESTION_MARKS contains c) then return false
if !isAllMessageMark(using event.message.text) then return false
MornyCoeur.extra exec SendMessage(
event.message.chat.id, event.message.text

View File

@ -22,7 +22,7 @@ object OnTelegramCommand extends EventListener {
if (!(inputCommand.command matches "^\\w+$"))
logger debug "not command"
false
else if ((inputCommand.target ne null) && (inputCommand.target ne MornyCoeur.username))
else if ((inputCommand.target ne null) && (inputCommand.target != MornyCoeur.username))
logger debug "not morny command"
false
else

View File

@ -9,13 +9,13 @@ import scala.language.postfixOps
object OnUserRandom extends EventListener {
private val USER_OR_QUERY = "(.+)(?:还是|or)(.+)"r
private val USER_IF_QUERY = "(.+)[吗?|]+$"r
private val USER_OR_QUERY = "^(.+)(?:还是|or)(.+)$"r
private val USER_IF_QUERY = "^(.+)(?:吗\\?||\\?|吗?)$"r
override def onMessage(using update: Update): Boolean = {
if update.message.text == null then return false
if update.message.text startsWith "/" then return false
if !(update.message.text startsWith "/") then return false
import cc.sukazyo.cono.morny.util.UseRandom.rand_half
val query = update.message.text substring 1
@ -23,6 +23,8 @@ object OnUserRandom extends EventListener {
case USER_OR_QUERY(_con1, _con2) =>
if rand_half then _con1 else _con2
case USER_IF_QUERY(_con) =>
// for capability with [[OnQuestionMarkReply]]
if OnQuestionMarkReply.isAllMessageMark(using _con) then return false
(if rand_half then "不" else "") + _con
case _ => null

View File

@ -22,14 +22,24 @@ object OnUserSlashAction extends EventListener {
if (text startsWith "/") {
// there has to be some special conditions for DP7
// due to I have left DP7, I closed those special
// conditions.
// that is 2022, May 28th
// when one year goes, These code have rewrite with
// scala, those commented code is removed permanently.
// these message, here to remember the old DP7.
val actions = UniversalCommand(text)
actions(0) = actions(0) substring 1
actions(0)
actions(0) match
// ignore Telegram command like
case TG_FORMAT(_) =>
return false
// ignore Path link
case x if x contains "/" => return false
case _ =>
@ -55,7 +65,7 @@ object OnUserSlashAction extends EventListener {
h(v_verb), if hasObject then "" else "了",
if (origin == target)
s"<a href='tg://user?id=${origin.sender_id}'>自己</a>"
else origin.sender_firstnameRefHTML,
else target.sender_firstnameRefHTML,
if hasObject then h(v_object+" ") else ""
)
).parseMode(ParseMode HTML).replyToMessageId(update.message.messageId)

View File

@ -14,7 +14,7 @@ object MyInformation extends ITelegramQuery {
override def query (event: Update): List[InlineQueryUnit[_]] | Null = {
if ((event.inlineQuery.query ne null) || (event.inlineQuery.query nonEmpty)) return null
if !((event.inlineQuery.query eq null) || (event.inlineQuery.query isEmpty)) then return null
List(
InlineQueryUnit(InlineQueryResultArticle(

View File

@ -9,11 +9,11 @@ import scala.util.matching.Regex
object ShareToolTwitter extends ITelegramQuery {
val TITLE_VX = "[tweet] Share as VxTwitter"
val TITLE_VX_COMBINED = "[tweet] Share as VxTwitter(combination)"
val ID_PREFIX_VX = "[morny/share/twitter/vxtwi]"
val ID_PREFIX_VX_COMBINED = "[morny/share/twitter/vxtwi_combine]"
val REGEX_TWEET_LINK: Regex = "^(?:https?://)?((?:(?:c\\.)?vx|fx|www\\.)?twitter\\.com)/((\\w+)/status/(\\d+)(?:/photo/(\\d+))?)/?(\\?[\\w&=-]+)?$"r
private val TITLE_VX = "[tweet] Share as VxTwitter"
private val TITLE_VX_COMBINED = "[tweet] Share as VxTwitter(combination)"
private val ID_PREFIX_VX = "[morny/share/twitter/vxtwi]"
private val ID_PREFIX_VX_COMBINED = "[morny/share/twitter/vxtwi_combine]"
private val REGEX_TWEET_LINK: Regex = "^(?:https?://)?((?:(?:c\\.)?vx|fx|www\\.)?twitter\\.com)/((\\w+)/status/(\\d+)(?:/photo/(\\d+))?)/?(\\?[\\w&=-]+)?$"r
override def query (event: Update): List[InlineQueryUnit[_]] | Null = {
@ -21,7 +21,7 @@ object ShareToolTwitter extends ITelegramQuery {
event.inlineQuery.query match
case REGEX_TWEET_LINK(_1, _2, _) =>
case REGEX_TWEET_LINK(_, _2, _, _, _, _) =>
List(
InlineQueryUnit(InlineQueryResultArticle(
inlineQueryId(ID_PREFIX_VX+event.inlineQuery.query), TITLE_VX,

View File

@ -74,6 +74,7 @@ object MornyReport {
).parseMode(ParseMode HTML))
}
//noinspection ScalaWeakerAccess
def sectionConfigFields (config: MornyConfig): String = {
val echo = StringBuilder()
for (field <- config.getClass.getFields) {

View File

@ -12,6 +12,6 @@ object MornyJrrp {
private def jrrp_v_xmomi (identifier: Long, dayStamp: Long): Double =
import cc.sukazyo.cono.morny.util.CommonEncrypt.MD5
import cc.sukazyo.cono.morny.util.ConvertByteHex.toHex
(java.lang.Long parseLong MD5(s"$identifier@$dayStamp").toHex.substring(0, 4)) / (0xffff toDouble)
java.lang.Long.parseLong(MD5(s"$identifier@$dayStamp").toHex.substring(0, 4), 16) / (0xffff toDouble)
}

View File

@ -19,6 +19,7 @@ object IP186QueryHandler {
commonQuery(SITE_URL + ip, QUERY_PARAM_IP)
@throws[IOException]
//noinspection ScalaWeakerAccess
def query_whois (domain: String): IP186Response =
commonQuery(SITE_URL+"whois/"+domain, QUERY_PARAM_WHOIS)

View File

@ -1,12 +0,0 @@
package cc.sukazyo.cono.morny.internal
import scala.jdk.CollectionConverters._
import scala.collection.immutable as simm
import java.util as j
object ScalaJavaConv {
def jSetInteger2simm (data: j.Set[Integer]): simm.Set[Int] =
data.asScala.toSet.map(_.intValue)
}

View File

@ -36,8 +36,7 @@ object UniversalCommand {
arg = arg.empty
} else if (input(i) isQuote) {
val _inside_tag = input(i)
var _inside = true
boundary { while (_inside) {
boundary { while (true) {
i=i+1
if (i >= input.length) throw IllegalArgumentException("UniversalCommand: unclosed quoted text")
if (input(i) == _inside_tag)

View File

@ -32,7 +32,7 @@ public class ExtraAction {
public <T extends BaseRequest<T, R>, R extends BaseResponse> R exec (T req, String errorMessage) {
final R resp = bot.execute(req);
if (!resp.isOk()) throw new EventRuntimeException.ActionFailed(
(errorMessage.equals("") ? String.valueOf(resp.errorCode()) : errorMessage),
(errorMessage.isEmpty() ? String.valueOf(resp.errorCode()) : errorMessage),
resp
);
return resp;

View File

@ -22,6 +22,7 @@ object TelegramFormatter {
case _ => s"<a href='${_link_chat(chat.id_tdLib)}'>@[c/${chat.id}]</a>"
else s"@${h(chat.username)}"
//noinspection ScalaWeakerAccess
def safe_firstnameRefHTML: String =
chat.`type` match
// language=html
@ -29,6 +30,7 @@ object TelegramFormatter {
// language=html
case _ => s"<a href='${_link_chat(chat.id_tdLib)}'>${h(chat.title)}</a>"
//noinspection ScalaWeakerAccess
def id_tdLib: Long =
if chat.id < 0 then (chat.id - MASK_BOTAPI_ID)abs else chat.id
@ -42,12 +44,14 @@ object TelegramFormatter {
extension (user: User) {
//noinspection ScalaWeakerAccess
def fullname: String = _connectName(user.firstName, user.lastName)
def fullnameRefHTML: String =
// language=html
s"<a href='${_link_user(user.id)}'>${h(user.fullname)}</a>"
//noinspection ScalaWeakerAccess
def firstnameRefHTML: String =
// language=html
s"<a href='${_link_user(user.id)}'>${h(user.firstName)}</a>"

View File

@ -9,8 +9,8 @@ import scala.util.Using
object TelegramUserInformation {
val DC_QUERY_SOURCE_SITE = "https://t.me/"
val DC_QUERY_PROCESSOR_REGEX: Regex = "(cdn[1-9]).tele(sco.pe|gram-cdn.org)"r
private val DC_QUERY_SOURCE_SITE = "https://t.me/"
private val DC_QUERY_PROCESSOR_REGEX: Regex = "(cdn[1-9]).tele(sco.pe|gram-cdn.org)"r
private val httpClient = OkHttpClient()