Skip to content

Commit

Permalink
feat: pangu
Browse files Browse the repository at this point in the history
Thanks to @NullgramClient

Co-authored-by: qwq233 <[email protected]>
  • Loading branch information
omg-xtao and qwq233 committed Oct 7, 2024
1 parent 97d35cc commit f20ce51
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1460,11 +1460,11 @@ private String getStringInternal(String key, String fallback, int fallbackRes, i
}
}
}
if (value == null || "".equals(value)) {
if (value == null || value.isEmpty()) {
value = "LOC_ERR:" + key;
if (getFallbackResources() != null)
value = getFallbackResources().getString(res);
} else {
} else if (NekoConfig.localeToDBC.Bool()) {
value = LocFiltersKt.filter(value);
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import androidx.collection.LongSparseArray;
import androidx.core.graphics.ColorUtils;

import top.qwq2333.nullgram.utils.StringUtils;
import xyz.nextalone.nagram.NaConfig;
import xyz.nextalone.nagram.helper.MessageHelper;
import xyz.nextalone.nagram.ui.syntaxhighlight.SyntaxHighlight;
Expand Down Expand Up @@ -1754,6 +1755,12 @@ public MessageObject(int accountNum, TLRPC.Message message, MessageObject replyT
fromUser = getUser(users, sUsers, message.from_id.user_id);
}

if (generateLayout && messageOwner.message != null && NaConfig.INSTANCE.getEnablePanguOnReceiving().Bool()) {
var pair = StringUtils.spacingText(messageOwner.message, messageOwner.entities);
messageOwner.message = pair.getFirst();
messageOwner.entities = pair.getSecond();
}

updateMessageText(users, chats, sUsers, sChats);
setType();
if (generateLayout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import kotlin.Pair;
import top.qwq2333.nullgram.utils.StringUtils;
import xyz.nextalone.nagram.NaConfig;

public class SendMessagesHelper extends BaseController implements NotificationCenter.NotificationCenterDelegate {
Expand Down Expand Up @@ -1945,7 +1947,16 @@ public int sendMessage(ArrayList<MessageObject> messages, final long peer, boole
if (msgObj.getId() <= 0 || msgObj.needDrawBluredPreview()) {
if (msgObj.type == MessageObject.TYPE_TEXT && !TextUtils.isEmpty(msgObj.messageText)) {
TLRPC.WebPage webPage = msgObj.messageOwner.media != null ? msgObj.messageOwner.media.webpage : null;
SendMessageParams params = SendMessageParams.of(msgObj.messageText.toString(), peer, null, replyToTopMsg, webPage, webPage != null, msgObj.messageOwner.entities, null, null, notify, scheduleDate, null, false);

var messageText = msgObj.messageText.toString();
var entities = msgObj.messageOwner.entities;
if (!msgObj.isForwarded() && NaConfig.INSTANCE.getEnablePanguOnSending().Bool()) {
var pair = StringUtils.spacingText(messageText, msgObj.messageOwner.entities);
messageText = pair.getFirst();
entities = pair.getSecond();
}

SendMessageParams params = SendMessageParams.of(messageText, peer, null, replyToTopMsg, webPage, webPage != null, entities, null, null, notify, scheduleDate, null, false);
params.quick_reply_shortcut = msgObj.getQuickReplyName();
params.quick_reply_shortcut_id = msgObj.getQuickReplyId();
sendMessage(params);
Expand Down Expand Up @@ -2107,9 +2118,18 @@ public int sendMessage(ArrayList<MessageObject> messages, final long peer, boole
if (newMsg.message == null) {
newMsg.message = "";
}

if (!((newMsg.params.containsKey("fwd_id") || newMsg.params.containsKey("fwd_peer")) || msgObj.isForwarded() || MessageObject.isForwardedMessage(newMsg)) && NaConfig.INSTANCE.getEnablePanguOnSending().Bool()) {
var pair = StringUtils.spacingText(newMsg.message, msgObj.messageOwner.entities);
newMsg.message = pair.getFirst();
newMsg.entities = pair.getSecond();
} else {
newMsg.entities = msgObj.messageOwner.entities;
}

newMsg.fwd_msg_id = msgObj.getId();
newMsg.attachPath = msgObj.messageOwner.attachPath;
newMsg.entities = msgObj.messageOwner.entities;
// newMsg.entities = msgObj.messageOwner.entities;
if (msgObj.messageOwner.reply_markup instanceof TLRPC.TL_replyInlineMarkup) {
newMsg.reply_markup = new TLRPC.TL_replyInlineMarkup();
boolean dropMarkup = false;
Expand Down Expand Up @@ -3760,6 +3780,14 @@ public void sendMessage(SendMessageParams sendMessageParams) {
} else {
newMsg = new TLRPC.TL_message();
}

if (poll.poll != null && NaConfig.INSTANCE.getEnablePanguOnSending().Bool()) {
poll.poll.question = StringUtils.spacingText(poll.poll.question);
for (int i = 0; i < poll.poll.answers.size(); i++) {
poll.poll.answers.get(i).text = StringUtils.spacingText(poll.poll.answers.get(i).text);
}
}

newMsg.media = poll;
type = 10;
} else if (location != null) {
Expand Down Expand Up @@ -4227,6 +4255,21 @@ public void sendMessage(SendMessageParams sendMessageParams) {
isFinalGroupMedia = params.get("final") != null;
}

if (!((params != null && params.containsKey("fwd_id")) || MessageObject.isForwardedMessage(newMsg)) && NaConfig.INSTANCE.getEnablePanguOnSending().Bool()) {
Pair<String, ArrayList<TLRPC.MessageEntity>> pair;
if (caption != null) {
pair = StringUtils.spacingText(caption, entities);
caption = pair.getFirst();
} else {
pair = StringUtils.spacingText(message, entities);
message = pair.getFirst();
}
entities = pair.getSecond();

newMsg.message = pair.getFirst();
newMsg.entities = pair.getSecond();
}

if (stars > 0) {
TLRPC.MessageMedia media = newMsg.media;
TLRPC.TL_messageMediaPaidMedia paidMedia = new TLRPC.TL_messageMediaPaidMedia();
Expand Down
154 changes: 154 additions & 0 deletions TMessagesProj/src/main/java/top/qwq2333/nullgram/utils/StringUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2019-2024 qwq233 <[email protected]>
* https://github.com/qwq233/Nullgram
*
* 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 2
* 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 software.
* If not, see
* <https://www.gnu.org/licenses/>
*/
package top.qwq2333.nullgram.utils

import org.telegram.tgnet.TLRPC
import ws.vinta.pangu.Pangu
import kotlin.math.ceil

object StringUtils {
private val pangu = Pangu()

/**
*
* 字符串是否为空白,空白的定义如下:
*
* 1. `null`
* 1. 空字符串:`""`
* 1. 空格、全角空格、制表符、换行符,等不可见字符
*
*
*
* 例:
*
* * `StringUtils.isBlank(null) // true`
* * `StringUtils.isBlank("") // true`
* * `StringUtils.isBlank(" \t\n") // true`
* * `StringUtils.isBlank("abc") // false`
*
*
* @param str 被检测的字符串
* @return 若为空白,则返回 true
*/
@JvmStatic
fun isBlank(str: CharSequence?): Boolean {
if (str.isNullOrEmpty()) {
return true
}
val length: Int = str.length
for (i in 0 until length) {
// 只要有一个非空字符即为非空字符串
if (!isBlankChar(str[i])) {
return false
}
}
return true
}

/**
* 是否空白符<br></br>
* 空白符包括空格、制表符、全角空格和不间断空格<br></br>
*
* @param c 字符
* @return 是否空白符
* @see Character.isWhitespace
* @see Character.isSpaceChar
*/
fun isBlankChar(c: Int): Boolean {
return Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\ufeff'.code || c == '\u202a'.code || c == '\u0000'.code
}

/**
* 是否空白符<br></br>
* 空白符包括空格、制表符、全角空格和不间断空格<br></br>
*
* @param c 字符
* @return 是否空白符
* @see Character.isWhitespace
* @see Character.isSpaceChar
*/
fun isBlankChar(c: Char): Boolean {
return isBlankChar(c.code)
}

/**
* Return a string with a maximum length of `length` characters.
* If there are more than `length` characters, then string ends with an ellipsis ("...").
*
* @param text text
* @param length maximum length you want
* @return Return a string with a maximum length of `length` characters.
*/
@JvmStatic
@Suppress("NAME_SHADOWING")
fun ellipsis(text: String, length: Int): String {
// The letters [iIl1] are slim enough to only count as half a character.
var length = length
length += ceil(text.replace("[^iIl]".toRegex(), "").length / 2.0).toInt()
return if (text.length > length) {
text.substring(0, length - 3) + "..."
} else text
}

@JvmStatic
fun spacingText(message: TLRPC.TL_textWithEntities): TLRPC.TL_textWithEntities {
return TLRPC.TL_textWithEntities().apply {
val pair = spacingText(message.text, message.entities)
text = pair.first
entities = pair.second
}
}

@JvmStatic
fun spacingText(text: String, entities: ArrayList<TLRPC.MessageEntity>?): Pair<String, ArrayList<TLRPC.MessageEntity>?> {
if (text.startsWith("/")) return Pair(text, entities) // command
if (entities.isNullOrEmpty()) return Pair(pangu.spacingText(text), entities)

val panguText = pangu.spacingText(text)

if (panguText.length == text.length) return Pair(panguText, entities) // processed or unnecessary

var skip = 0
for (i in text.indices) {
if (i + skip >= panguText.length) break
if (text[i] == panguText[i + skip]) continue

entities.forEach {
if (it.offset >= i + skip) { // text is after this entity
it.offset += 1
} else if (it.offset + it.length >= i + skip) { // text is in this entity
it.length += 1
} // text is before this entity
}
skip += 1
}

// prevent out of bound
entities.forEach {
if (it.offset >= panguText.length) {
it.offset = panguText.length - 1
}
if (it.offset + it.length > panguText.length) {
it.length = panguText.length - it.offset
}
}

return Pair(panguText, entities)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ public class NekoConfig {
// priv branch changes
public static ConfigItem localPremium = addConfig("localPremium", configTypeBool, false);

public static ConfigItem localeToDBC = addConfig("LocaleToDBC", configTypeBool, false);

static {
loadConfig(false);
checkMigrate(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tw.nekomimi.nekogram.parts

private val mapArr = mapOf(
'' to '(',
'' to ')',
'' to '.',
'' to ',',
'' to '?',
'' to ';'
'' to '(',
'' to ')',
'' to '.',
'' to ',',
'' to '?',
'' to ';'
)

fun filter(input: String) = input.toCharArray().let { c ->
Expand All @@ -21,4 +21,4 @@ fun filter(input: String) = input.toCharArray().let { c ->
listOf(char)
}
}.filterNotNull().toCharArray().let(::String)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity
private final AbstractConfigCell disableSendReadStoriesRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableSendReadStories()));
private final AbstractConfigCell dividerStory = cellGroup.appendCell(new ConfigCellDivider());

// Sticker Cache
private final AbstractConfigCell header3 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString(R.string.ExternalStickerCache)));
private final AbstractConfigCell externalStickerCacheRow = cellGroup.appendCell(new ConfigCellAutoTextCheck(
NaConfig.INSTANCE.getExternalStickerCache(), LocaleController.getString(R.string.ExternalStickerCacheHint), this::onExternalStickerCacheButtonClick));
Expand All @@ -136,6 +137,13 @@ public class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity
private final AbstractConfigCell externalStickerCacheDeleteAllRow = cellGroup.appendCell(new ConfigCellText("ExternalStickerCacheDeleteAll", ExternalStickerCacheHelper::deleteAllCaches));
private final AbstractConfigCell divider2 = cellGroup.appendCell(new ConfigCellDivider());

// Pangu
private final AbstractConfigCell header4 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString(R.string.Pangu)));
private final AbstractConfigCell enablePanguOnSendingRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getEnablePanguOnSending(), LocaleController.getString(R.string.PanguInfo)));
private final AbstractConfigCell enablePanguOnReceivingRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getEnablePanguOnReceiving()));
private final AbstractConfigCell localeToDBCRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.localeToDBC));
private final AbstractConfigCell divider3 = cellGroup.appendCell(new ConfigCellDivider());

private UndoView tooltip;

private static final int INTENT_PICK_CUSTOM_EMOJI_PACK = 114;
Expand Down Expand Up @@ -359,6 +367,8 @@ public void onItemClick(int id) {
intent.setType("application/zip");
Activity act = getParentActivity();
act.startActivityFromChild(act, intent, INTENT_PICK_CUSTOM_EMOJI_PACK);
} else if (key.equals(NekoConfig.localeToDBC.getKey())) {
tooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
}
};

Expand Down
Loading

0 comments on commit f20ce51

Please sign in to comment.