#include "mologram/FakeTelegramService.hpp"
#include <algorithm>
#include <filesystem>
namespace mologram {
using js::Array;
using js::Object;
using js::Value;
static Peer P(PeerKind k, std::int64_t id) { Peer p; p.kind = k; p.id = id; return p; }
static const Peer kMe = P(PeerKind::User, 1);
void FakeTelegramService::seed() {
struct S { PeerKind k; std::int64_t id; const char* name; const char* user; bool bot, verified, online, forum; };
const S rows[] = {
{PeerKind::User, 1, "Saved Messages", "", false, false, true, false},
{PeerKind::User, 100, "Alice", "alice", false, false, true, false},
{PeerKind::User, 101, "Bob", "bob", false, false, false, false},
{PeerKind::User, 102, "Carol \xE6\xBC\xA2\xE5\xAD\x97", "carol", false, false, false, false},
{PeerKind::User, 103, "BuildBot", "buildbot", true, false, true, false},
{PeerKind::Chat, 200, "Family", "", false, false, false, false},
{PeerKind::Chat, 201, "Project \xF0\x9F\x9A\x80", "", false, false, false, false},
{PeerKind::Channel, 300, "Telegram News", "telegram", false, true, false, false},
{PeerKind::Channel, 301, "Dev Forum", "devforum", false, false, false, true},
};
int n = 0;
for (const auto& r : rows) {
Peer p = P(r.k, r.id);
PeerInfo info; info.peer = p; info.name = r.name; info.username = r.user; info.isBot = r.bot; info.verified = r.verified;
info.online = r.online; info.status = r.online ? "online" : "last seen recently"; info.members = r.k == PeerKind::User ? 0 : 12;
infos_[p] = info;
Dialog d; d.peer = p; d.name = r.name; d.verified = r.verified; d.online = r.online; d.isBot = r.bot; d.forum = r.forum; d.pinned = (n == 0);
dialogs_.push_back(d);
for (int i = 0; i < 40; i++) {
Message m; m.id = ++nextId_; m.peer = p; m.date = clock_ - (40 - i) * 60 - n * 3600;
m.outgoing = (i % 3 == 0);
m.sender = m.outgoing ? kMe : p; m.senderName = m.outgoing ? "You" : r.name;
m.text = "Message " + std::to_string(i + 1) + " in " + r.name;
messages_[p].push_back(m);
}
refreshPreview(p);
n++;
}
folders_ = {{1, "Personal", ""}, {2, "Work", ""}};
dialogs_[1].folders = {1}; dialogs_[2].folders = {1}; dialogs_[6].folders = {2}; dialogs_[4].folders = {2};
dialogs_[1].unread = 2; dialogs_[7].unread = 5;
}
void FakeTelegramService::refreshPreview(const Peer& p) {
Dialog* d = dialogFor(p);
auto it = messages_.find(p);
if (!d || it == messages_.end() || it->second.empty()) return;
const Message& m = it->second.back();
d->preview = m.text; d->date = m.date; d->topMessageId = m.id;
}
Dialog* FakeTelegramService::dialogFor(const Peer& p) {
for (auto& d : dialogs_) if (d.peer == p) return &d;
return nullptr;
}
const std::vector<Message>& FakeTelegramService::history(const Peer& p) const {
static const std::vector<Message> empty;
auto it = messages_.find(p);
return it == messages_.end() ? empty : it->second;
}
Message& FakeTelegramService::append(const Peer& p, Message m) {
m.id = ++nextId_; m.peer = p; m.date = ++clock_;
auto& v = messages_[p];
v.push_back(std::move(m));
touchDialog(p, v.back());
return v.back();
}
void FakeTelegramService::touchDialog(const Peer& p, const Message& m) {
if (Dialog* d = dialogFor(p)) {
d->preview = m.text.empty() ? "[media]" : m.text; d->date = m.date; d->topMessageId = m.id;
if (!m.outgoing) d->unread++;
}
}
bool FakeTelegramService::gated(RpcResult& out) const {
if (serverPremium_) return false;
out = RpcResult::failure(ErrorCode::PremiumRequired, "PREMIUM_ACCOUNT_REQUIRED");
return true;
}
void FakeTelegramService::setPremium(PremiumState s) {
serverPremium_ = (s == PremiumState::Premium);
told_ = s;
Object o; o["state"] = Value(premiumName(s));
emit({"premium.changed", Value(std::move(o))});
}
Message FakeTelegramService::simulateIncoming(const Peer& peer, const std::string& text) {
Message m; m.sender = peer; m.senderName = infos_.count(peer) ? infos_[peer].name : "?"; m.text = text;
Message& stored = append(peer, m);
Object o; o["message"] = stored.toJson();
Message copy = stored;
emit({"message.new", Value(std::move(o))});
return copy;
}
static Value ok() { return RpcResult::success().value; }
void FakeTelegramService::registerHandlers() {
on("hello", [this](const Value&, const CancelFlag&) {
Object o; o["proto_version"] = Value(1); o["account"] = Value(std::string("Fake Account")); o["premium"] = Value(premiumName(told_)); o["auth"] = Value(authState_);
return RpcResult::success(Value(std::move(o)));
});
on("auth.state", [this](const Value&, const CancelFlag&) { Object o; o["state"] = Value(authState_); return RpcResult::success(Value(std::move(o))); });
on("auth.send_phone", [this](const Value&, const CancelFlag&) {
authState_ = "need_code";
Object o;
o["sent"] = Value(true); o["type"] = Value("app"); o["next_type"] = Value("sms"); o["timeout"] = Value(60); // like Telegram: the app first, SMS on resend
return RpcResult::success(Value(std::move(o)));
});
on("auth.send_code", [this](const Value& p, const CancelFlag&) {
if (js::getStr(p, "code") != "12345") return RpcResult::failure(ErrorCode::RpcError, "PHONE_CODE_INVALID");
authState_ = "need_password"; Object o; o["state"] = Value(authState_); return RpcResult::success(Value(std::move(o)));
});
on("auth.send_password", [this](const Value& p, const CancelFlag&) {
if (js::getStr(p, "password") != "secret") return RpcResult::failure(ErrorCode::RpcError, "PASSWORD_HASH_INVALID");
authState_ = "ready"; loggedIn_ = true; Object o; o["state"] = Value(authState_); return RpcResult::success(Value(std::move(o)));
});
on("auth.qr_start", [](const Value&, const CancelFlag&) { Object o; o["url"] = Value("tg://login?token=FAKE"); return RpcResult::success(Value(std::move(o))); });
on("auth.logout", [this](const Value&, const CancelFlag&) { setLoggedIn(false); return RpcResult::success(ok()); });
on("dialogs.list", [this](const Value& p, const CancelFlag&) {
std::string folder = js::getStr(p, "folder");
int offset = js::getInt(p, "offset");
std::vector<Dialog> v;
for (const auto& d : dialogs_) {
if (folder == "archive") { if (!d.archived) continue; }
else {
if (d.archived) continue;
if (!folder.empty() && folder != "all" && std::find(d.folders.begin(), d.folders.end(), std::atoi(folder.c_str())) == d.folders.end()) continue;
}
v.push_back(d);
}
std::stable_sort(v.begin(), v.end(), [](const Dialog& a, const Dialog& b) { return a.pinned != b.pinned ? a.pinned : a.date > b.date; });
Array a;
for (size_t i = (size_t)std::max(0, offset); i < v.size(); i++) a.push_back(v[i].toJson());
Object o; o["dialogs"] = Value(std::move(a));
return RpcResult::success(Value(std::move(o)));
});
on("dialogs.folders", [this](const Value&, const CancelFlag&) {
Array a; for (const auto& f : folders_) a.push_back(f.toJson());
Object o; o["folders"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
auto dialogFlag = [this](const char* name, std::function<void(Dialog&, const Value&)> fn) {
on(name, [this, fn](const Value& p, const CancelFlag&) {
Dialog* d = dialogFor(Peer::fromJson(*p.find("peer")));
if (!d) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
fn(*d, p);
Object o; o["dialog"] = d->toJson(); emit({"dialog.updated", Value(std::move(o))});
return RpcResult::success(ok());
});
};
dialogFlag("dialogs.pin", [](Dialog& d, const Value& p) { d.pinned = js::getBool(p, "on"); });
dialogFlag("dialogs.mute", [](Dialog& d, const Value& p) { d.muted = js::getId(p, "until") != 0; });
dialogFlag("dialogs.mark_read", [](Dialog& d, const Value&) { d.unread = 0; });
dialogFlag("dialogs.archive", [](Dialog& d, const Value& p) { d.archived = js::getBool(p, "on"); });
on("dialogs.delete", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
dialogs_.erase(std::remove_if(dialogs_.begin(), dialogs_.end(), [&](const Dialog& d) { return d.peer == peer; }), dialogs_.end());
messages_.erase(peer);
return RpcResult::success(ok());
});
on("messages.history", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
auto it = messages_.find(peer);
if (it == messages_.end()) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
std::int64_t offsetId = js::getId(p, "offset_id");
std::string dir = js::getStr(p, "direction", "older");
int count = js::getInt(p, "count", 30);
std::vector<const Message*> sel;
if (dir == "newer") { for (const auto& m : it->second) if (m.id > offsetId) sel.push_back(&m); if ((int)sel.size() > count) sel.resize((size_t)count); }
else { for (const auto& m : it->second) if (offsetId == 0 || m.id < offsetId) sel.push_back(&m); if ((int)sel.size() > count) sel.erase(sel.begin(), sel.end() - count); }
Array a; for (auto* m : sel) a.push_back(m->toJson());
Object o; o["messages"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("messages.send", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
if (!dialogFor(peer)) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
std::string text = js::getStr(p, "text");
if (text.empty()) return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_EMPTY");
// Premium only: custom emoji entities and message effects
bool custom = false;
if (const Array* ents = js::getArr(p, "entities")) for (const auto& e : *ents) if (js::getStr(e, "type") == "custom_emoji") custom = true;
RpcResult gp;
if ((custom || js::getId(p, "effect") != 0) && gated(gp)) return gp;
Message m; m.effectId = js::getId(p, "effect"); m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.text = text; m.replyTo = js::getId(p, "reply_to");
std::int64_t when = js::getId(p, "schedule");
if (when > clock_) { // scheduled: kept aside until "sent now", not part of the history
m.id = ++nextId_; m.peer = peer; m.date = when;
scheduled_[peer].push_back(m);
return RpcResult::success(m.toJson());
}
Message& s = append(peer, m);
Object o; o["message"] = s.toJson();
emit({"message.new", Value(Object(o))});
return RpcResult::success(s.toJson());
});
on("messages.edit", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")); std::int64_t id = js::getId(p, "id");
for (auto& m : messages_[peer]) if (m.id == id) {
if (!m.outgoing) return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_AUTHOR_REQUIRED");
m.text = js::getStr(p, "text"); m.edited = true; refreshPreview(peer);
Object o; o["message"] = m.toJson(); emit({"message.edited", Value(Object(o))});
return RpcResult::success(m.toJson());
}
return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_ID_INVALID");
});
on("messages.delete", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
lastRevoke_ = js::getBool(p, "revoke", true);
std::vector<std::int64_t> ids;
if (const Array* a = js::getArr(p, "ids")) for (const auto& e : *a) ids.push_back(e.isStr() ? std::stoll(e.asStr()) : (std::int64_t)e.asNum());
auto& v = messages_[peer];
v.erase(std::remove_if(v.begin(), v.end(), [&](const Message& m) { return std::find(ids.begin(), ids.end(), m.id) != ids.end(); }), v.end());
refreshPreview(peer);
Array deleted; for (auto i : ids) deleted.push_back(js::id(i));
Object o; o["peer"] = peer.toJson(); o["ids"] = Value(std::move(deleted)); emit({"message.deleted", Value(std::move(o))});
return RpcResult::success(ok());
});
on("messages.forward", [this](const Value& p, const CancelFlag&) {
Peer from = Peer::fromJson(*p.find("from")), to = Peer::fromJson(*p.find("to"));
Array out;
if (const Array* a = js::getArr(p, "ids")) for (const auto& e : *a) {
std::int64_t id = e.isStr() ? std::stoll(e.asStr()) : (std::int64_t)e.asNum();
for (const auto& m : messages_[from]) if (m.id == id) {
Message c = m; c.outgoing = true; c.sender = kMe; c.senderName = "You"; c.forwardFrom = infos_[from].name;
out.push_back(append(to, c).toJson());
}
}
Object o; o["messages"] = Value(std::move(out)); return RpcResult::success(Value(std::move(o)));
});
on("messages.pin", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")); std::int64_t id = js::getId(p, "id");
for (auto& m : messages_[peer]) if (m.id == id) { m.pinned = js::getBool(p, "on"); return RpcResult::success(ok()); }
return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_ID_INVALID");
});
on("messages.react", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")); std::int64_t id = js::getId(p, "id");
std::vector<Reaction> want;
if (const Array* a = js::getArr(p, "reactions")) for (const auto& e : *a) want.push_back(Reaction::fromJson(e));
RpcResult g;
bool needsPremium = want.size() > 1;
for (const auto& r : want) if (r.isCustom()) needsPremium = true;
if (needsPremium && gated(g)) return g;
for (auto& m : messages_[peer]) if (m.id == id) {
m.reactions.clear();
for (auto r : want) { r.count = 1; r.mine = true; m.reactions.push_back(r); }
return RpcResult::success(ok());
}
return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_ID_INVALID");
});
on("messages.search", [this](const Value& p, const CancelFlag&) {
std::string q = js::getStr(p, "query"); std::string pk = js::getStr(p, "peer");
Array a;
for (const auto& kv : messages_) {
if (!pk.empty() && kv.first != Peer::fromKey(pk)) continue;
for (const auto& m : kv.second) if (m.text.find(q) != std::string::npos) a.push_back(m.toJson());
}
Object o; o["messages"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("messages.draft", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")); drafts_[peer] = js::getStr(p, "text");
if (Dialog* d = dialogFor(peer)) {
d->draft = drafts_[peer];
Object o; o["dialog"] = d->toJson(); emit({"dialog.updated", Value(std::move(o))});
}
return RpcResult::success(ok());
});
on("messages.send_file", [this](const Value& p, const CancelFlag& c) {
Peer peer = Peer::fromJson(*p.find("peer")); std::string path = js::getStr(p, "path");
std::error_code ec; auto size = std::filesystem::file_size(path, ec);
if (ec) return RpcResult::failure(ErrorCode::RpcError, "FILE_NOT_FOUND");
if (c.cancelled()) return RpcResult::failure(ErrorCode::Cancelled, "cancelled");
RpcResult g;
if (size > 2000000000ull && gated(g)) return g;
Message m; m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.text = js::getStr(p, "caption");
std::string kind = js::getStr(p, "kind", "document");
m.media.kind = kind == "photo" ? MediaKind::Photo : kind == "video" ? MediaKind::Video : kind == "voice" ? MediaKind::Voice : kind == "audio" ? MediaKind::Audio : MediaKind::Document;
m.media.fileName = std::filesystem::path(path).filename().string(); m.media.size = (std::int64_t)size;
m.media.token = "fake:" + m.media.fileName;
Message& s = append(peer, m);
Object o; o["message"] = s.toJson(); emit({"message.new", Value(Object(o))});
return RpcResult::success(s.toJson());
});
on("messages.send_album", [this](const Value& p, const CancelFlag& c) {
Peer peer = Peer::fromJson(*p.find("peer"));
Array out;
const Array* paths = js::getArr(p, "paths");
if (!paths || paths->empty()) return RpcResult::failure(ErrorCode::RpcError, "ALBUM_EMPTY");
bool first = true;
for (const auto& e : *paths) {
std::string path = e.asStr();
std::error_code ec; auto size = std::filesystem::file_size(path, ec);
if (ec) return RpcResult::failure(ErrorCode::RpcError, "FILE_NOT_FOUND");
if (c.cancelled()) return RpcResult::failure(ErrorCode::Cancelled, "cancelled");
Message m; m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.text = first ? js::getStr(p, "caption") : "";
std::string ext = std::filesystem::path(path).extension().string();
m.media.kind = (ext == ".mp4" || ext == ".mov" || ext == ".mkv") ? MediaKind::Video : MediaKind::Photo;
m.media.fileName = std::filesystem::path(path).filename().string(); m.media.size = (std::int64_t)size; m.media.token = "fake:" + m.media.fileName;
Message& s = append(peer, m);
Object o; o["message"] = s.toJson(); emit({"message.new", Value(Object(o))});
out.push_back(s.toJson());
first = false;
}
Object o; o["messages"] = Value(std::move(out));
return RpcResult::success(Value(std::move(o)));
});
on("messages.send_poll", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
Message m; m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.hasPoll = true; m.poll.question = js::getStr(p, "question");
if (const Array* a = js::getArr(p, "options")) { int i = 0; for (const auto& e : *a) m.poll.options.push_back({e.asStr(), std::to_string(i++), 0, false, false}); }
if (m.poll.options.size() < 2) return RpcResult::failure(ErrorCode::RpcError, "POLL_OPTION_INVALID");
m.poll.multiple = js::getBool(p, "multiple"); m.poll.quiz = js::getBool(p, "quiz");
if (m.poll.quiz) { int c = js::getInt(p, "correct", -1); if (c < 0 || c >= (int)m.poll.options.size()) return RpcResult::failure(ErrorCode::RpcError, "QUIZ_CORRECT_ANSWERS_EMPTY"); m.poll.options[(size_t)c].correct = true; }
m.media.kind = MediaKind::Poll;
Message& s = append(peer, m);
return RpcResult::success(s.toJson());
});
on("messages.vote", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")); std::int64_t id = js::getId(p, "id");
for (auto& m : messages_[peer]) if (m.id == id && m.hasPoll) {
if (m.poll.closed) return RpcResult::failure(ErrorCode::RpcError, "POLL_CLOSED");
if (const Array* a = js::getArr(p, "options")) for (const auto& e : *a) {
size_t i = (size_t)(e.isStr() ? std::stoi(e.asStr()) : e.asInt());
if (i < m.poll.options.size()) { m.poll.options[i].votes++; m.poll.options[i].chosen = true; m.poll.totalVoters++; }
}
return RpcResult::success(ok());
}
return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_ID_INVALID");
});
on("messages.click_button", [](const Value& p, const CancelFlag&) { Object o; o["answer"] = Value("clicked " + js::getStr(p, "data")); return RpcResult::success(Value(std::move(o))); });
on("media.download", [](const Value& p, const CancelFlag& c) {
if (c.cancelled()) return RpcResult::failure(ErrorCode::Cancelled, "cancelled");
std::string dest = js::getStr(p, "dest");
Object o; o["path"] = Value(dest.empty() ? std::string("/tmp/mologram-fake-download") : dest); return RpcResult::success(Value(std::move(o)));
});
on("peer.info", [this](const Value& p, const CancelFlag&) {
auto it = infos_.find(Peer::fromJson(*p.find("peer")));
if (it == infos_.end()) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
return RpcResult::success(it->second.toJson());
});
on("peer.members", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
Array a;
if (peer.kind != PeerKind::User) {
auto it = members_.find(peer);
std::vector<Peer> list;
if (it != members_.end()) list = it->second;
else for (std::int64_t id = 100; id < 104; id++) list.push_back(P(PeerKind::User, id));
for (const auto& m : list) {
auto inf = infos_.find(m);
if (inf == infos_.end()) continue;
PeerInfo pi = inf->second;
pi.role = m.id == 100 ? "owner" : m.id == 101 ? "admin" : "";
a.push_back(pi.toJson());
}
}
Object o; o["members"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("contacts.list", [this](const Value&, const CancelFlag&) {
Array a; for (auto& kv : infos_) if (kv.first.kind == PeerKind::User && kv.second.contact) a.push_back(kv.second.toJson());
Object o; o["contacts"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("contacts.search", [this](const Value& p, const CancelFlag&) {
std::string q = js::getStr(p, "query"); Array a;
for (auto& kv : infos_) if (kv.second.name.find(q) != std::string::npos || kv.second.username.find(q) != std::string::npos) a.push_back(kv.second.toJson());
Object o; o["peers"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("contacts.add", [this](const Value& p, const CancelFlag&) {
auto it = infos_.find(Peer::fromJson(*p.find("user")));
if (it == infos_.end()) return RpcResult::failure(ErrorCode::RpcError, "USER_ID_INVALID");
it->second.contact = true; return RpcResult::success(ok());
});
on("contacts.block", [this](const Value& p, const CancelFlag&) {
auto it = infos_.find(Peer::fromJson(*p.find("user")));
if (it == infos_.end()) return RpcResult::failure(ErrorCode::RpcError, "USER_ID_INVALID");
it->second.blocked = js::getBool(p, "on"); return RpcResult::success(ok());
});
on("chat.create", [this](const Value& p, const CancelFlag&) {
std::string kind = js::getStr(p, "kind");
Peer peer = P(kind == "channel" ? PeerKind::Channel : PeerKind::Chat, 400 + (std::int64_t)dialogs_.size());
PeerInfo info; info.peer = peer; info.name = js::getStr(p, "title"); infos_[peer] = info;
Dialog d; d.peer = peer; d.name = info.name; d.date = ++clock_; dialogs_.push_back(d);
messages_[peer];
return RpcResult::success(peer.toJson());
});
on("chat.join", [](const Value& p, const CancelFlag&) {
if (js::getStr(p, "link_or_name").empty()) return RpcResult::failure(ErrorCode::RpcError, "INVITE_HASH_EMPTY");
return RpcResult::success(P(PeerKind::Channel, 300).toJson());
});
on("chat.leave", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
dialogs_.erase(std::remove_if(dialogs_.begin(), dialogs_.end(), [&](const Dialog& d) { return d.peer == peer; }), dialogs_.end());
return RpcResult::success(ok());
});
on("chat.invite_link", [](const Value&, const CancelFlag&) { Object o; o["link"] = Value("https://t.me/+FAKEINVITE"); return RpcResult::success(Value(std::move(o))); });
on("account.profile", [this](const Value&, const CancelFlag&) {
Object o; o["name"] = Value("Fake Account"); o["username"] = Value("fake"); o["bio"] = Value(""); o["premium"] = Value(premiumName(told_));
return RpcResult::success(Value(std::move(o)));
});
on("account.update_profile", [](const Value& p, const CancelFlag&) { return RpcResult::success(*p.find("fields")); });
on("account.sessions", [](const Value&, const CancelFlag&) {
Object s; s["hash"] = Value("1"); s["device"] = Value("mologram"); s["current"] = Value(true);
Array a; a.push_back(Value(std::move(s)));
Object o; o["sessions"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("account.terminate_session", [](const Value&, const CancelFlag&) { return RpcResult::success(ok()); });
on("account.privacy", [this](const Value& p, const CancelFlag&) {
std::string key = js::getStr(p, "key");
auto it = privacy_.find(key);
Object o; o["key"] = Value(key); o["rule"] = Value(it == privacy_.end() ? std::string("everybody") : it->second);
return RpcResult::success(Value(std::move(o)));
});
on("messages.pinned", [this](const Value& p, const CancelFlag&) {
Array a;
for (const auto& m : messages_[Peer::fromJson(*p.find("peer"))]) if (m.pinned) a.push_back(m.toJson());
Object o; o["messages"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("messages.scheduled", [this](const Value& p, const CancelFlag&) {
Array a;
for (const auto& m : scheduled_[Peer::fromJson(*p.find("peer"))]) a.push_back(m.toJson());
Object o; o["messages"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("messages.scheduled_delete", [this](const Value& p, const CancelFlag&) {
auto& list = scheduled_[Peer::fromJson(*p.find("peer"))];
std::vector<std::int64_t> ids;
if (const Array* a = js::getArr(p, "ids")) for (const auto& e : *a) ids.push_back(e.isStr() ? std::stoll(e.asStr()) : (std::int64_t)e.asNum());
list.erase(std::remove_if(list.begin(), list.end(), [&](const Message& m) { return std::find(ids.begin(), ids.end(), m.id) != ids.end(); }), list.end());
return RpcResult::success(ok());
});
on("messages.scheduled_send_now", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
auto& list = scheduled_[peer];
std::vector<std::int64_t> ids;
if (const Array* a = js::getArr(p, "ids")) for (const auto& e : *a) ids.push_back(e.isStr() ? std::stoll(e.asStr()) : (std::int64_t)e.asNum());
std::vector<Message> due;
for (auto it = list.begin(); it != list.end();) {
if (std::find(ids.begin(), ids.end(), it->id) != ids.end()) { due.push_back(*it); it = list.erase(it); } else ++it;
}
for (auto& m : due) {
Message& s = append(peer, m);
Object o; o["message"] = s.toJson(); emit({"message.new", Value(std::move(o))});
}
return RpcResult::success(ok());
});
on("chat.delete", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
if (peer.kind == PeerKind::User) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
dialogs_.erase(std::remove_if(dialogs_.begin(), dialogs_.end(), [&](const Dialog& d) { return d.peer == peer; }), dialogs_.end());
messages_.erase(peer); scheduled_.erase(peer); infos_.erase(peer); members_.erase(peer);
return RpcResult::success(ok());
});
on("messages.send_sticker", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
if (!dialogFor(peer)) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
Message m; m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.media.kind = MediaKind::Sticker; m.media.token = "sticker:" + js::getStr(p, "sticker");
Message& s = append(peer, m);
Object o; o["message"] = s.toJson(); emit({"message.new", Value(Object(o))});
return RpcResult::success(s.toJson());
});
on("bot.commands", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
Array a;
auto it = infos_.find(peer);
if (it != infos_.end() && it->second.isBot) {
for (const char* c : {"start", "help", "status"}) { Object co; co["command"] = Value(c); co["description"] = Value(std::string("Bot command ") + c); a.push_back(Value(std::move(co))); }
}
Object o; o["commands"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("bot.inline", [](const Value& p, const CancelFlag&) {
std::string q = js::getStr(p, "query");
Array a;
for (int i = 1; i <= 3; i++) { Object r; r["id"] = Value(std::to_string(i)); r["title"] = Value("Result " + std::to_string(i) + " for " + q); r["description"] = Value("inline result"); a.push_back(Value(std::move(r))); }
Object o; o["query_id"] = Value("q1"); o["results"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("bot.inline_send", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
if (!dialogFor(peer)) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
Message m; m.outgoing = true; m.sender = kMe; m.senderName = "You"; m.viaBot = "inlinebot"; m.text = "inline result " + js::getStr(p, "result_id");
Message& s = append(peer, m);
Object o; o["message"] = s.toJson(); emit({"message.new", Value(Object(o))});
return RpcResult::success(s.toJson());
});
on("contacts.import", [this](const Value& p, const CancelFlag&) {
int n = 0;
if (const Array* a = js::getArr(p, "contacts")) for (const auto& e : *a) {
Peer np = P(PeerKind::User, ++accountIdCounter_);
PeerInfo i; i.peer = np; i.name = js::getStr(e, "name", "Imported"); i.phone = js::getStr(e, "phone"); i.contact = true; i.status = "last seen recently";
infos_[np] = i; n++;
}
Object o; o["imported"] = Value(n); return RpcResult::success(Value(std::move(o)));
});
on("stickers.search", [](const Value& p, const CancelFlag&) {
std::string q = js::getStr(p, "query");
Array a;
for (const char* n : {"hello", "love", "laugh", "sad", "thanks", "wow"}) {
if (!q.empty() && std::string(n).find(q) == std::string::npos) continue;
Object s; s["id"] = Value(std::string("st-") + n); s["emoji"] = Value("\xF0\x9F\x98\x80"); s["title"] = Value(n); s["animated"] = Value(false); a.push_back(Value(std::move(s)));
}
Object o; o["stickers"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("emoji.custom", [](const Value& p, const CancelFlag&) {
std::string q = js::getStr(p, "query");
Array a;
for (int i = 1; i <= 6; i++) { std::string n = "custom" + std::to_string(i); if (!q.empty() && n.find(q) == std::string::npos) continue; Object e; e["id"] = js::id(5000 + i); e["fallback"] = Value("\xE2\xAD\x90"); e["title"] = Value(n); a.push_back(Value(std::move(e))); }
Object o; o["emoji"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("stickers.sets", [](const Value& p, const CancelFlag&) {
bool emoji = js::getBool(p, "emoji");
Array a;
Object s;
s["id"] = Value(emoji ? "901" : "900"); s["short_name"] = Value(emoji ? "fakeemoji" : "fakeset");
s["title"] = Value(emoji ? "Fake emoji" : "Fake stickers"); s["count"] = Value(6); s["emojis"] = Value(emoji);
s["masks"] = Value(false); s["official"] = Value(false); s["archived"] = Value(false);
a.push_back(Value(std::move(s)));
Object o; o["sets"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("stickers.set", [](const Value& p, const CancelFlag&) {
std::string name = js::getStr(p, "short_name");
if (name != "fakeset" && name != "fakeemoji") return RpcResult::failure(ErrorCode::RpcError, "STICKERSET_INVALID");
Array a;
for (const char* n : {"hello", "love", "laugh", "sad", "thanks", "wow"}) {
Object st; st["id"] = Value(std::string("st-") + n); st["emoji"] = Value("\xF0\x9F\x98\x80"); st["title"] = Value(name);
st["mime"] = Value("image/webp"); st["animated"] = Value(false); st["video"] = Value(false); st["premium"] = Value(false); st["custom_emoji"] = Value(name == "fakeemoji");
a.push_back(Value(std::move(st)));
}
Object o; o["stickers"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("stickers.premium", [](const Value&, const CancelFlag&) {
Array a;
for (const char* n : {"star-burst", "fireworks"}) {
Object st; st["id"] = Value(std::string("pst-") + n); st["emoji"] = Value("\xE2\xAD\x90"); st["title"] = Value("Premium stickers");
st["mime"] = Value("application/x-tgsticker"); st["animated"] = Value(true); st["video"] = Value(false); st["premium"] = Value(true); st["custom_emoji"] = Value(false);
a.push_back(Value(std::move(st)));
}
Object o; o["stickers"] = Value(std::move(a)); return RpcResult::success(Value(std::move(o)));
});
on("account.set_privacy", [this](const Value& p, const CancelFlag&) {
std::string rule = js::getStr(p, "rule");
if (rule != "everybody" && rule != "contacts" && rule != "nobody") return RpcResult::failure(ErrorCode::RpcError, "PRIVACY_VALUE_INVALID");
std::string key = js::getStr(p, "key");
RpcResult g;
if ((key == "voice_messages" || key == "read_time") && gated(g)) return g; // Premium-only privacy keys (SPEC 6.5)
privacy_[key] = rule;
return RpcResult::success(ok());
});
on("account.notifications", [this](const Value&, const CancelFlag&) {
Object o;
for (const char* sc : {"private", "groups", "channels"}) { Object s; s["muted"] = Value(notifyMuted_[sc]); o[sc] = Value(std::move(s)); }
return RpcResult::success(Value(std::move(o)));
});
on("account.set_notifications", [this](const Value& p, const CancelFlag&) {
std::string scope = js::getStr(p, "scope");
if (scope != "private" && scope != "groups" && scope != "channels") return RpcResult::failure(ErrorCode::RpcError, "SCOPE_INVALID");
notifyMuted_[scope] = js::getBool(p, "muted");
return RpcResult::success(ok());
});
on("auth.resend_code", [this](const Value&, const CancelFlag&) {
authState_ = "need_code";
Object o;
o["sent"] = Value(true); o["type"] = Value("sms"); o["next_type"] = Value("call"); o["timeout"] = Value(120);
return RpcResult::success(Value(std::move(o)));
});
on("chat.add_members", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer"));
if (peer.kind == PeerKind::User) return RpcResult::failure(ErrorCode::RpcError, "PEER_ID_INVALID");
auto& list = members_[peer];
if (list.empty()) for (std::int64_t id = 100; id < 104; id++) list.push_back(P(PeerKind::User, id));
if (const Array* a = js::getArr(p, "users")) for (const auto& e : *a) {
Peer u = Peer::fromJson(e);
if (!infos_.count(u)) return RpcResult::failure(ErrorCode::RpcError, "USER_ID_INVALID");
if (std::find(list.begin(), list.end(), u) == list.end()) list.push_back(u);
}
return RpcResult::success(ok());
});
on("chat.remove_member", [this](const Value& p, const CancelFlag&) {
Peer peer = Peer::fromJson(*p.find("peer")), u = Peer::fromJson(*p.find("user"));
auto& list = members_[peer];
if (list.empty()) for (std::int64_t id = 100; id < 104; id++) list.push_back(P(PeerKind::User, id));
list.erase(std::remove(list.begin(), list.end(), u), list.end());
return RpcResult::success(ok());
});
on("chat.set_admin", [this](const Value& p, const CancelFlag&) {
Peer user = Peer::fromJson(*p.find("user"));
if (!infos_.count(user)) return RpcResult::failure(ErrorCode::RpcError, "USER_ID_INVALID");
return RpcResult::success(ok());
});
on("premium.state", [this](const Value&, const CancelFlag&) { Object o; o["state"] = Value(premiumName(told_)); return RpcResult::success(Value(std::move(o))); });
on("premium.translate", [this](const Value& p, const CancelFlag&) {
RpcResult g; if (gated(g)) return g;
Peer peer = Peer::fromJson(*p.find("peer")); std::int64_t id = js::getId(p, "id");
for (const auto& m : messages_[peer]) if (m.id == id) { Object o; o["text"] = Value("[" + js::getStr(p, "to") + "] " + m.text); return RpcResult::success(Value(std::move(o))); }
return RpcResult::failure(ErrorCode::RpcError, "MESSAGE_ID_INVALID");
});
on("premium.transcribe", [this](const Value&, const CancelFlag&) {
RpcResult g; if (gated(g)) return g;
Object o; o["text"] = Value("transcribed voice"); return RpcResult::success(Value(std::move(o)));
});
on("premium.emoji_status", [this](const Value&, const CancelFlag&) { RpcResult g; if (gated(g)) return g; return RpcResult::success(ok()); });
on("premium.profile_color", [this](const Value&, const CancelFlag&) { RpcResult g; if (gated(g)) return g; return RpcResult::success(ok()); });
}
} // namespace mologram