`Shamrock`: fix #49

Signed-off-by: FQL <ydic001@vip.qq.com>
This commit is contained in:
FQL 2023-10-08 15:22:53 +08:00
parent b5f1bd8b8b
commit 5e0589cd27
8 changed files with 63 additions and 11 deletions

View File

@ -244,6 +244,16 @@ object ShamrockConfig {
preferences.edit().putBoolean("enable_auto_start", v).apply()
}
fun enableSelfMsg(ctx: Context): Boolean {
val preferences = ctx.getSharedPreferences("config", 0)
return preferences.getBoolean("enable_self_msg", false)
}
fun setEnableSelfMsg(ctx: Context, v: Boolean) {
val preferences = ctx.getSharedPreferences("config", 0)
preferences.edit().putBoolean("enable_self_msg", v).apply()
}
fun getConfigMap(ctx: Context): Map<String, Any?> {
val preferences = ctx.getSharedPreferences("config", 0)
return mapOf(
@ -266,6 +276,7 @@ object ShamrockConfig {
"auto_clear" to preferences.getBoolean("auto_clear", false),
"ssl_private_pwd" to preferences.getString("ssl_private_pwd", ""),
"key_store" to preferences.getString("key_store", ""),
"enable_self_msg" to preferences.getBoolean("enable_self_msg", false),
)
}

View File

@ -155,7 +155,31 @@ fun LabFragment() {
return@Function false
}
}
}
ActionBox(
modifier = Modifier.padding(top = 12.dp),
painter = painterResource(id = R.drawable.round_logo_dev_24),
title = "消息相关"
) {
Column {
Divider(
modifier = Modifier,
color = GlobalColor.Divider,
thickness = 0.2.dp
)
Function(
title = "自发消息推送",
desc = "推送Bot发送的消息未做特殊处理请勿打开。",
descColor = it,
isSwitch = ShamrockConfig.enableSelfMsg(ctx)
) {
ShamrockConfig.setEnableSelfMsg(ctx, it)
ShamrockConfig.pushUpdate(ctx)
return@Function true
}
}
}
}
}

View File

@ -69,7 +69,6 @@ object DashboardInitializer {
} else if (result.retcode != 0) {
log("尝试从接口获取账号信息失败,未知错误。", Level.ERROR)
} else {
log("心跳请求发送已发送。")
AccountInfo.let { account ->
account.uin.value = result.data.uin.toString()
account.nick.value = result.data.nick
@ -82,18 +81,19 @@ object DashboardInitializer {
}
} catch (e: ConnectException) {
state.isFined.value = false
log("检测到Service死亡正在尝试重新启动")
context.broadcastToModule {
putExtra("__cmd", "checkAndStartService")
}
if (ShamrockConfig.enableAutoStart(context)) {
log("检测到Service死亡正在尝试重新启动")
GlobalScope.launch(Dispatchers.Main) {
val packageName = "com.tencent.mobileqq"
val className = "com.tencent.mobileqq.activity.SplashActivity"
val intent = Intent()
intent.component = ComponentName(packageName, className)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("from", "shamrock")
startActivity(context, intent, Bundle.EMPTY)
}

View File

@ -19,7 +19,7 @@ import kotlin.math.roundToInt
object DownloadUtils {
private const val MAX_THREAD = 4
suspend fun download(urlAdr: String, dest: File) {
suspend fun download(urlAdr: String, dest: File): Boolean {
val url = URL(urlAdr)
val connection = withContext(Dispatchers.IO) { url.openConnection() } as HttpURLConnection
connection.requestMethod = "GET"
@ -60,7 +60,9 @@ object DownloadUtils {
}
return@withTimeoutOrNull true
} ?: dest.delete()
return true
}
return false
}
private suspend fun reallyDownload(url: URL, start: Int, end: Int, dest: File, channel: Channel<Int>) {

View File

@ -49,6 +49,8 @@ internal object ShamrockConfig {
putBoolean("auto_clear", intent.getBooleanExtra("auto_clear", false)) // 自动清理
putBoolean("enable_self_msg", intent.getBooleanExtra("enable_self_msg", false)) // 推送自己发的消息
putBoolean("isInit", true)
}
}
@ -68,6 +70,12 @@ internal object ShamrockConfig {
return Config.privateRule
}
fun enableSelfMsg(): Boolean {
val mmkv = MMKVFetcher.mmkvWithId("shamrock_config")
return mmkv.getBoolean("enable_self_msg", false)
}
fun openWebSocketClient(): Boolean {
val mmkv = MMKVFetcher.mmkvWithId("shamrock_config")
return mmkv.getBoolean("ws_client", false)

View File

@ -89,6 +89,9 @@ internal object AioListener: IKernelMsgListener {
time = record.msgTime
)
if (!ShamrockConfig.enableSelfMsg())
return@launch
when (record.chatType) {
MsgConstant.KCHATTYPEGROUP -> {
GlobalPusher.forEach {

View File

@ -29,7 +29,7 @@ internal object MessageHelper {
var uniseq = generateMsgId(chatType)
var nonMsg: Boolean
val msg = messageArrayToMessageElements(chatType, uniseq.second, peerId, message).also {
if (it.isEmpty()) error("message is empty, unable to send")
if (it.isEmpty()) error("消息合成失败,请查看日志或者检查输入。")
}.filter {
it.elementType != -1
}.also {

View File

@ -39,18 +39,22 @@ internal object FileUtils {
File(file.substring(8)).inputStream().use {
saveFileToCache( it )
}
} else {
} else if (file.startsWith("http://") || file.startsWith("https://")) {
kotlin.run {
val tmp = getTmpFile()
DownloadUtils.download(file, tmp)
tmp.inputStream().use {
saveFileToCache(it)
}.also {
if(DownloadUtils.download(file, tmp)) {
tmp.inputStream().use {
saveFileToCache(it)
}.also {
tmp.delete()
LogCenter.log({ "文件下载完成: ${it.absolutePath}, 地址: $file" }, Level.DEBUG)
}
} else {
tmp.delete()
LogCenter.log({ "文件下载完成: ${it.absolutePath}, 地址: $file" }, Level.DEBUG)
error("文件下载失败: $file")
}
}
}
} else error("不支持的文件地址: $file")
}
fun renameByMd5(file: File): File {