Add Gambatte GB/GBC emulator, Call/SMS anti-spam blocklist, and Lua/bc/dc/Yabasic

This commit is contained in:
Luxferre committed 2026-10-26 15:17:55 +02:00
1 parent b7f0dc08c9
commit 6db14be799
113 files changed
+20006 -237

No files matched your search

+351 -17
View File
@@ -93,6 +93,9 @@ constexpr const char* kIconToggleOff = "\xef\x88\x84"; // U+F204 toggle off
constexpr const char* kIconKey = "\xef\x82\x84"; // U+F084 key / PIN
constexpr const char* kIconSim = "\xf3\xb0\x99\x95"; // U+F0655 nf-md-sim
constexpr const char* kIconBell = "\xef\x83\xb3"; // U+F0F3 bell / ringtone
constexpr const char* kIconBan = "\xef\x81\x9e"; // U+F05E ban / block
constexpr const char* kIconShield = "\xef\x84\xb2"; // U+F132 shield / anti-spam
constexpr const char* kIconGamepad = "\xef\x84\x9b"; // U+F11B gamepad / emulator
inline int64_t nowMs() {
struct timespec ts {};
@@ -947,10 +950,247 @@ class ContactBook {
std::unordered_map<std::string, std::string> suffixMap_;
};
inline std::string getBlockFilePath() {
const char* env = std::getenv("SXMO_BLOCKFILE");
if (env && env[0]) {
return env;
}
const char* xdg = std::getenv("XDG_CONFIG_HOME");
if (xdg && xdg[0]) {
return std::string(xdg) + "/sxmo/block.tsv";
}
const char* home = std::getenv("HOME");
return std::string(home && home[0] ? home : "/home/user") +
"/.config/sxmo/block.tsv";
}
inline std::string getBlockDirPath() {
const char* env = std::getenv("SXMO_BLOCKDIR");
if (env && env[0]) {
return env;
}
const char* xdg = std::getenv("XDG_DATA_HOME");
if (xdg && xdg[0]) {
return std::string(xdg) + "/sxmo/block";
}
const char* home = std::getenv("HOME");
return std::string(home && home[0] ? home : "/home/user") +
"/.local/share/sxmo/block";
}
inline std::string getBlockedModemLogFilePath() {
return getBlockDirPath() + "/modemlog.tsv";
}
struct BlockEntry {
std::string number;
std::string normNumber;
std::string label;
};
class BlockList {
public:
void load() {
entries_.clear();
exactSet_.clear();
suffixSet_.clear();
std::ifstream bf(getBlockFilePath());
std::string line;
while (std::getline(bf, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty()) {
continue;
}
size_t tab = line.find('\t');
std::string num = trimStr(tab == std::string::npos ? line : line.substr(0, tab));
std::string lbl = tab == std::string::npos ? "" : trimStr(line.substr(tab + 1));
std::string norm = normalizePhone(num);
if (norm.empty()) {
continue;
}
BlockEntry e;
e.number = num;
e.normNumber = norm;
e.label = lbl.empty() ? "Blocked" : lbl;
entries_.push_back(e);
exactSet_.insert(norm);
std::string sfx = phoneSuffixKey(norm);
if (sfx.size() >= 7) {
suffixSet_.insert(sfx);
}
}
}
bool isBlocked(const std::string& rawNumber) const {
std::string norm = normalizePhone(rawNumber);
if (norm.empty()) {
return false;
}
if (exactSet_.count(norm) > 0) {
return true;
}
std::string sfx = phoneSuffixKey(norm);
if (sfx.size() >= 7 && suffixSet_.count(sfx) > 0) {
return true;
}
for (const auto& e : entries_) {
if (!e.normNumber.empty() && e.normNumber.back() == '*' &&
e.normNumber.size() > 1) {
std::string prefix = e.normNumber.substr(0, e.normNumber.size() - 1);
if (norm.rfind(prefix, 0) == 0) {
return true;
}
}
}
return false;
}
bool blockNumber(const std::string& rawNumber,
const std::string& labelRaw = "") {
std::string num = trimStr(rawNumber);
std::string norm = normalizePhone(num);
if (norm.empty()) {
return false;
}
if (num.find('\t') != std::string::npos ||
num.find('\n') != std::string::npos) {
num = norm;
}
std::string label = trimStr(labelRaw);
for (char& ch : label) {
if (ch == '\t' || ch == '\r' || ch == '\n') {
ch = ' ';
}
}
if (label.empty()) {
label = "Blocked";
}
std::string path = getBlockFilePath();
ContactBook::ensureParentDir(path);
std::vector<std::pair<std::string, std::string>> rows;
{
std::ifstream in(path);
std::string line;
while (std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty()) continue;
size_t tab = line.find('\t');
std::string rNum = trimStr(tab == std::string::npos ? line : line.substr(0, tab));
std::string rLbl = tab == std::string::npos ? "Blocked" : trimStr(line.substr(tab + 1));
if (!normalizePhone(rNum).empty()) {
rows.push_back({rNum, rLbl.empty() ? "Blocked" : rLbl});
}
}
}
std::string sfx = phoneSuffixKey(norm);
bool updated = false;
for (auto& r : rows) {
std::string rNorm = normalizePhone(r.first);
std::string rSfx = phoneSuffixKey(rNorm);
if (rNorm == norm || (sfx.size() >= 7 && rSfx == sfx)) {
r.first = num;
if (!labelRaw.empty()) {
r.second = label;
}
updated = true;
break;
}
}
if (!updated) {
rows.push_back({num, label});
}
std::ofstream out(path, std::ios::trunc);
if (!out.is_open()) {
return false;
}
for (const auto& r : rows) {
out << r.first << "\t" << r.second << "\n";
}
out.close();
load();
return true;
}
bool unblockNumber(const std::string& rawNumber) {
std::string norm = normalizePhone(rawNumber);
if (norm.empty()) {
return false;
}
std::string sfx = phoneSuffixKey(norm);
std::string path = getBlockFilePath();
std::vector<std::pair<std::string, std::string>> rows;
bool removed = false;
{
std::ifstream in(path);
std::string line;
while (std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty()) continue;
size_t tab = line.find('\t');
std::string rNum = trimStr(tab == std::string::npos ? line : line.substr(0, tab));
std::string rLbl = tab == std::string::npos ? "Blocked" : trimStr(line.substr(tab + 1));
std::string rNorm = normalizePhone(rNum);
if (rNorm.empty()) continue;
std::string rSfx = phoneSuffixKey(rNorm);
if (rNorm == norm || (sfx.size() >= 7 && rSfx == sfx)) {
removed = true;
continue;
}
rows.push_back({rNum, rLbl.empty() ? "Blocked" : rLbl});
}
}
if (!removed) {
return false;
}
ContactBook::ensureParentDir(path);
std::ofstream out(path, std::ios::trunc);
if (!out.is_open()) {
return false;
}
for (const auto& r : rows) {
out << r.first << "\t" << r.second << "\n";
}
out.close();
load();
return true;
}
bool clearAll() {
std::string path = getBlockFilePath();
ContactBook::ensureParentDir(path);
std::ofstream out(path, std::ios::trunc);
if (!out.is_open()) {
return false;
}
out.close();
load();
return true;
}
const std::vector<BlockEntry>& entries() const { return entries_; }
private:
std::vector<BlockEntry> entries_;
std::unordered_set<std::string> exactSet_;
std::unordered_set<std::string> suffixSet_;
};
enum class CallDirection {
Incoming = 0,
Outgoing = 1,
Missed = 2,
Blocked = 3,
};
struct CallLogItem {
@@ -959,6 +1199,7 @@ struct CallLogItem {
std::string contactName;
std::string displayTime;
int durationSec = 0;
bool isBlockedNumber = false;
};
inline time_t parseIsoTime(const std::string& iso) {
@@ -999,8 +1240,10 @@ inline std::string formatDuration(int sec) {
return buf;
}
inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
std::ifstream lf(getModemLogFilePath());
inline std::vector<CallLogItem> loadCallLogFromFile(const ContactBook& book,
const std::string& logPath,
bool forceBlockedDir = false) {
std::ifstream lf(logPath);
std::string line;
struct ActiveSession {
@@ -1022,7 +1265,7 @@ inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
return;
}
CallLogItem item;
item.direction = cur.dir;
item.direction = forceBlockedDir ? CallDirection::Blocked : cur.dir;
item.number = cur.number;
item.contactName = book.resolveName(cur.number);
item.displayTime = formatShortTime(cur.startTimeStr);
@@ -1053,10 +1296,24 @@ inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
time_t epoch = parseIsoTime(ts);
if (evt == "call_ring") {
if (evt == "call_blocked") {
if (cur.active && normalizePhone(cur.number) == normalizePhone(num)) {
cur.dir = CallDirection::Blocked;
flushSession();
} else {
flushSession();
CallLogItem item;
item.direction = CallDirection::Blocked;
item.number = num;
item.contactName = book.resolveName(num);
item.displayTime = formatShortTime(ts);
item.durationSec = 0;
history.push_back(item);
}
} else if (evt == "call_ring") {
flushSession();
cur.active = true;
cur.dir = CallDirection::Missed;
cur.dir = forceBlockedDir ? CallDirection::Blocked : CallDirection::Missed;
cur.number = num;
cur.startTimeStr = ts;
cur.startEpoch = epoch;
@@ -1088,12 +1345,12 @@ inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
}
} else if (evt == "call_missed") {
if (cur.active && normalizePhone(cur.number) == normalizePhone(num)) {
cur.dir = CallDirection::Missed;
cur.dir = forceBlockedDir ? CallDirection::Blocked : CallDirection::Missed;
flushSession();
} else {
flushSession();
CallLogItem item;
item.direction = CallDirection::Missed;
item.direction = forceBlockedDir ? CallDirection::Blocked : CallDirection::Missed;
item.number = num;
item.contactName = book.resolveName(num);
item.displayTime = formatShortTime(ts);
@@ -1105,10 +1362,10 @@ inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
if (cur.active && normalizePhone(cur.number) == normalizePhone(num)) {
if (evt == "call_discard" && !cur.answered &&
cur.dir != CallDirection::Outgoing) {
cur.dir = CallDirection::Missed;
cur.dir = forceBlockedDir ? CallDirection::Blocked : CallDirection::Missed;
}
CallLogItem item;
item.direction = cur.dir;
item.direction = forceBlockedDir ? CallDirection::Blocked : cur.dir;
item.number = cur.number;
item.contactName = book.resolveName(cur.number);
item.displayTime = formatShortTime(cur.startTimeStr);
@@ -1126,6 +1383,44 @@ inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
return history;
}
inline std::vector<CallLogItem> loadCallLog(const ContactBook& book) {
return loadCallLogFromFile(book, getModemLogFilePath(), false);
}
inline std::vector<CallLogItem> loadBlockedCallLog(const ContactBook& book) {
return loadCallLogFromFile(book, getBlockedModemLogFilePath(), true);
}
inline bool clearCallLogFile(const std::string& logPath) {
std::ifstream in(logPath);
if (!in.is_open()) {
return true;
}
std::vector<std::string> keep;
std::string line;
while (std::getline(in, line)) {
if (line.empty()) continue;
size_t t1 = line.find('\t');
if (t1 == std::string::npos) continue;
size_t t2 = line.find('\t', t1 + 1);
if (t2 == std::string::npos) continue;
std::string evt = trimStr(line.substr(t1 + 1, t2 - t1 - 1));
if (evt.rfind("call_", 0) == 0 || evt == "ring_muted") {
continue;
}
keep.push_back(line);
}
in.close();
std::ofstream out(logPath, std::ios::trunc);
if (!out.is_open()) {
return false;
}
for (const auto& l : keep) {
out << l << "\n";
}
return true;
}
inline void placePhoneCall(const std::string& rawNumber) {
std::string num = normalizePhone(rawNumber);
if (num.empty()) {
@@ -1285,6 +1580,7 @@ struct SmsThread {
time_t lastEpoch = 0;
bool lastIncoming = true;
bool hasDraft = false;
bool isBlocked = false;
int messageCount = 0;
};
@@ -1319,9 +1615,12 @@ inline bool parseSmsHeaderLine(const std::string& line,
return false;
}
inline std::vector<SmsMessage> loadSmsThreadMessages(const std::string& number) {
inline std::vector<SmsMessage> loadSmsThreadMessages(
const std::string& number,
const std::string& customBaseDir = "") {
std::vector<SmsMessage> msgs;
std::string path = getModemLogDirPath() + "/" + number + "/sms.txt";
std::string baseDir = customBaseDir.empty() ? getModemLogDirPath() : customBaseDir;
std::string path = baseDir + "/" + number + "/sms.txt";
std::ifstream in(path);
if (!in.is_open()) {
return msgs;
@@ -1424,11 +1723,13 @@ inline void removeSmsDraft(const std::string& number) {
::unlink(path.c_str());
}
inline bool deleteSmsThread(const std::string& number) {
inline bool deleteSmsThread(const std::string& number,
const std::string& customBaseDir = "") {
if (number.empty()) {
return false;
}
std::string dir = getModemLogDirPath() + "/" + number;
std::string baseDir = customBaseDir.empty() ? getModemLogDirPath() : customBaseDir;
std::string dir = baseDir + "/" + number;
std::string smsPath = dir + "/sms.txt";
std::string draftPath = dir + "/draft.txt";
::unlink(smsPath.c_str());
@@ -1437,9 +1738,42 @@ inline bool deleteSmsThread(const std::string& number) {
return true;
}
inline std::vector<SmsThread> loadSmsThreads(const ContactBook& book) {
inline bool moveSmsThreadBetweenDirs(const std::string& number,
const std::string& fromBaseDir,
const std::string& toBaseDir) {
if (number.empty()) {
return false;
}
std::string srcPath = fromBaseDir + "/" + number + "/sms.txt";
std::ifstream in(srcPath);
if (!in.is_open()) {
return false;
}
std::ostringstream ss;
ss << in.rdbuf();
in.close();
std::string content = ss.str();
if (content.empty()) {
return false;
}
std::string dstPath = toBaseDir + "/" + number + "/sms.txt";
ContactBook::ensureParentDir(dstPath);
std::ofstream out(dstPath, std::ios::app);
if (!out.is_open()) {
return false;
}
out << content;
out.close();
deleteSmsThread(number, fromBaseDir);
return true;
}
inline std::vector<SmsThread> loadSmsThreads(
const ContactBook& book,
const std::string& customBaseDir = "") {
std::vector<SmsThread> threads;
std::string baseDir = getModemLogDirPath();
bool isSpamDir = !customBaseDir.empty();
std::string baseDir = isSpamDir ? customBaseDir : getModemLogDirPath();
DIR* d = ::opendir(baseDir.c_str());
if (!d) {
return threads;
@@ -1459,13 +1793,13 @@ inline std::vector<SmsThread> loadSmsThreads(const ContactBook& book) {
std::string smsPath = subDir + "/sms.txt";
std::string draftPath = subDir + "/draft.txt";
bool hasSms = (::access(smsPath.c_str(), R_OK) == 0);
std::string draft = loadSmsDraft(name);
std::string draft = isSpamDir ? "" : loadSmsDraft(name);
bool hasDraft = !draft.empty();
if (!hasSms && !hasDraft) {
continue;
}
auto msgs = loadSmsThreadMessages(name);
auto msgs = loadSmsThreadMessages(name, baseDir);
if (msgs.empty() && !hasDraft) {
continue;
}