Game bot confirmations added.

This commit is contained in:
John Preston 2016-09-15 22:15:49 +03:00
parent 578cf4ed61
commit 6d2fc5c642
11 changed files with 4316 additions and 4167 deletions

View file

@ -764,6 +764,8 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
"lng_open_this_link" = "Open this link?";
"lng_open_link" = "Open";
"lng_allow_bot_pass" = "Do you allow {bot_name} to pass your Telegram name and id to the web pages you open via this bot?";
"lng_allow_bot" = "Allow";
"lng_bot_start" = "Start";
"lng_bot_choose_group" = "Choose Group";

View file

@ -19,15 +19,16 @@ Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "lang.h"
#include "boxes/confirmbox.h"
#include "confirmbox.h"
#include "lang.h"
#include "mainwidget.h"
#include "mainwindow.h"
#include "apiwrap.h"
#include "application.h"
#include "core/click_handler_types.h"
#include "styles/style_boxes.h"
#include "localstorage.h"
TextParseOptions _confirmBoxTextOptions = {
TextParseLinks | TextParseMultiline | TextParseRichText, // flags
@ -195,6 +196,18 @@ void ConfirmLinkBox::onOpenLink() {
UrlClickHandler::doOpen(_url);
}
ConfirmBotGameBox::ConfirmBotGameBox(UserData *bot, const QString &url) : ConfirmBox(lng_allow_bot_pass(lt_bot_name, bot->name), lang(lng_allow_bot))
, _bot(bot)
, _url(url) {
connect(this, SIGNAL(confirmed()), this, SLOT(onOpenLink()));
}
void ConfirmBotGameBox::onOpenLink() {
Ui::hideLayer();
Local::makeBotTrusted(_bot);
UrlClickHandler::doOpen(_url);
}
MaxInviteBox::MaxInviteBox(const QString &link) : AbstractBox(st::boxWidth)
, _close(this, lang(lng_box_ok), st::defaultBoxButton)
, _text(st::boxTextFont, lng_participant_invite_sorry(lt_count, Global::ChatSizeMax()), _confirmBoxTextOptions, st::boxWidth - st::boxPadding.left() - st::boxButtonPadding.right())

View file

@ -125,6 +125,21 @@ private:
};
class ConfirmBotGameBox : public ConfirmBox {
Q_OBJECT
public:
ConfirmBotGameBox(UserData *bot, const QString &url);
public slots:
void onOpenLink();
private:
UserData *_bot;
QString _url;
};
class MaxInviteBox : public AbstractBox {
Q_OBJECT

View file

@ -26,6 +26,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "boxes/confirmbox.h"
#include "core/qthelp_regex.h"
#include "core/qthelp_url.h"
#include "localstorage.h"
QString UrlClickHandler::copyToClipboardContextItemText() const {
return lang(isEmail() ? lng_context_copy_email : lng_context_copy_link);
@ -113,6 +114,20 @@ void HiddenUrlClickHandler::onClick(Qt::MouseButton button) const {
}
}
void BotGameUrlClickHandler::onClick(Qt::MouseButton button) const {
auto u = url();
u = tryConvertUrlToLocal(u);
if (u.startsWith(qstr("tg://"))) {
App::openLocalUrl(u);
} else if (!_bot || Local::isBotTrusted(_bot)) {
doOpen(u);
} else {
Ui::showLayer(new ConfirmBotGameBox(_bot, u));
}
}
QString HiddenUrlClickHandler::getExpandedLinkText(ExpandLinksMode mode, const QStringRef &textPart) const {
QString result;
if (mode == ExpandLinksAll) {

View file

@ -122,6 +122,17 @@ public:
};
class BotGameUrlClickHandler : public UrlClickHandler {
public:
BotGameUrlClickHandler(UserData *bot, QString url) : UrlClickHandler(url, false), _bot(bot) {
}
void onClick(Qt::MouseButton button) const override;
private:
UserData *_bot;
};
class MentionClickHandler : public TextClickHandler {
public:
MentionClickHandler(const QString &tag) : _tag(tag) {

View file

@ -55,7 +55,7 @@ void activateBotCommand(const HistoryItem *msg, int row, int col) {
const HistoryMessageReplyMarkup::Button *button = nullptr;
if (auto markup = msg->Get<HistoryMessageReplyMarkup>()) {
if (row < markup->rows.size()) {
const auto &buttonRow(markup->rows.at(row));
auto &buttonRow = markup->rows[row];
if (col < buttonRow.size()) {
button = &buttonRow.at(col);
}
@ -81,7 +81,7 @@ void activateBotCommand(const HistoryItem *msg, int row, int col) {
case ButtonType::Url: {
auto url = QString::fromUtf8(button->data);
UrlClickHandler(url).onClick(Qt::LeftButton);
HiddenUrlClickHandler(url).onClick(Qt::LeftButton);
} break;
case ButtonType::RequestLocation: {

View file

@ -5783,8 +5783,16 @@ void HistoryWidget::app_sendBotCallback(const HistoryMessageReplyMarkup::Button
bool lastKeyboardUsed = (_keyboard.forMsgId() == FullMsgId(_channel, _history->lastKeyboardId)) && (_keyboard.forMsgId() == FullMsgId(_channel, msg->id));
auto bot = msg->viaBot();
if (!bot) {
bot = msg->from()->asUser();
if (bot && !bot->botInfo) {
bot = nullptr;
}
}
using ButtonType = HistoryMessageReplyMarkup::Button::Type;
BotCallbackInfo info = { msg->fullId(), row, col, (button->type == ButtonType::Game) };
BotCallbackInfo info = { bot, msg->fullId(), row, col, (button->type == ButtonType::Game) };
MTPmessages_GetBotCallbackAnswer::Flags flags = 0;
QByteArray sendData;
int32 sendGameId = 0;
@ -5833,11 +5841,13 @@ void HistoryWidget::botCallbackDone(BotCallbackInfo info, const MTPmessages_BotC
auto url = qs(answerData.vurl);
if (info.game) {
url = appendShareGameScoreUrl(url, info.msgId);
}
BotGameUrlClickHandler(info.bot, url).onClick(Qt::LeftButton);
} else {
UrlClickHandler(url).onClick(Qt::LeftButton);
}
}
}
}
bool HistoryWidget::botCallbackFail(BotCallbackInfo info, const RPCError &error, mtpRequestId req) {
// show error?

View file

@ -961,6 +961,7 @@ private:
void addMessagesToBack(PeerData *peer, const QVector<MTPMessage> &messages);
struct BotCallbackInfo {
UserData *bot;
FullMsgId msgId;
int row, col;
bool game;

View file

@ -34,7 +34,9 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "playerwidget.h"
#include "apiwrap.h"
namespace Local {
namespace {
typedef quint64 FileKey;
static const char tdfMagic[] = { 'T', 'D', 'F', '$' };
@ -54,8 +56,8 @@ namespace {
QString _basePath, _userBasePath;
bool _started = false;
_local_inner::Manager *_manager = 0;
TaskQueue *_localLoader = 0;
internal::Manager *_manager = nullptr;
TaskQueue *_localLoader = nullptr;
bool _working() {
return _manager && !_basePath.isEmpty();
@ -486,6 +488,7 @@ namespace {
lskSavedGifsOld = 0x0e, // no data
lskSavedGifs = 0x0f, // no data
lskStickersKeys = 0x10, // no data
lskTrustedBots = 0x11, // no data
};
enum {
@ -572,7 +575,11 @@ namespace {
typedef QMap<QString, FileDesc> WebFilesMap;
WebFilesMap _webFilesMap;
uint64 _storageWebFilesSize = 0;
FileKey _locationsKey = 0, _reportSpamStatusesKey = 0;
FileKey _locationsKey = 0, _reportSpamStatusesKey = 0, _trustedBotsKey = 0;
using TrustedBots = OrderedSet<uint64>;
TrustedBots _trustedBots;
bool _trustedBotsRead = false;
FileKey _recentStickersKeyOld = 0;
FileKey _installedStickersKey = 0, _featuredStickersKey = 0, _recentStickersKey = 0, _archivedStickersKey = 0;
@ -1699,7 +1706,7 @@ namespace {
}
}
Local::ReadMapState _readMap(const QByteArray &pass) {
ReadMapState _readMap(const QByteArray &pass) {
uint64 ms = getms();
QByteArray dataNameUtf8 = (cDataFile() + (cTestMode() ? qsl(":/test/") : QString())).toUtf8();
FileKey dataNameHash[2];
@ -1709,31 +1716,31 @@ namespace {
FileReadDescriptor mapData;
if (!readFile(mapData, qsl("map"))) {
return Local::ReadMapFailed;
return ReadMapFailed;
}
LOG(("App Info: reading map..."));
QByteArray salt, keyEncrypted, mapEncrypted;
mapData.stream >> salt >> keyEncrypted >> mapEncrypted;
if (!_checkStreamStatus(mapData.stream)) {
return Local::ReadMapFailed;
return ReadMapFailed;
}
if (salt.size() != LocalEncryptSaltSize) {
LOG(("App Error: bad salt in map file, size: %1").arg(salt.size()));
return Local::ReadMapFailed;
return ReadMapFailed;
}
createLocalKey(pass, &salt, &_passKey);
EncryptedDescriptor keyData, map;
if (!decryptLocal(keyData, keyEncrypted, _passKey)) {
LOG(("App Info: could not decrypt pass-protected key from map file, maybe bad password..."));
return Local::ReadMapPassNeeded;
return ReadMapPassNeeded;
}
uchar key[LocalEncryptKeySize] = { 0 };
if (keyData.stream.readRawData((char*)key, LocalEncryptKeySize) != LocalEncryptKeySize || !keyData.stream.atEnd()) {
LOG(("App Error: could not read pass-protected key from map file"));
return Local::ReadMapFailed;
return ReadMapFailed;
}
_localKey.setKey(key);
@ -1742,7 +1749,7 @@ namespace {
if (!decryptLocal(map, mapEncrypted)) {
LOG(("App Error: could not decrypt map."));
return Local::ReadMapFailed;
return ReadMapFailed;
}
LOG(("App Info: reading encrypted map..."));
@ -1750,7 +1757,7 @@ namespace {
DraftsNotReadMap draftsNotReadMap;
StorageMap imagesMap, stickerImagesMap, audiosMap;
qint64 storageImagesSize = 0, storageStickersSize = 0, storageAudiosSize = 0;
quint64 locationsKey = 0, reportSpamStatusesKey = 0;
quint64 locationsKey = 0, reportSpamStatusesKey = 0, trustedBotsKey = 0;
quint64 recentStickersKeyOld = 0;
quint64 installedStickersKey = 0, featuredStickersKey = 0, recentStickersKey = 0, archivedStickersKey = 0;
quint64 savedGifsKey = 0;
@ -1822,6 +1829,9 @@ namespace {
case lskReportSpamStatuses: {
map.stream >> reportSpamStatusesKey;
} break;
case lskTrustedBots: {
map.stream >> trustedBotsKey;
} break;
case lskRecentStickersOld: {
map.stream >> recentStickersKeyOld;
} break;
@ -1852,10 +1862,10 @@ namespace {
} break;
default:
LOG(("App Error: unknown key type in encrypted map: %1").arg(keyType));
return Local::ReadMapFailed;
return ReadMapFailed;
}
if (!_checkStreamStatus(map.stream)) {
return Local::ReadMapFailed;
return ReadMapFailed;
}
}
@ -1872,6 +1882,7 @@ namespace {
_locationsKey = locationsKey;
_reportSpamStatusesKey = reportSpamStatusesKey;
_trustedBotsKey = trustedBotsKey;
_recentStickersKeyOld = recentStickersKeyOld;
_installedStickersKey = installedStickersKey;
_featuredStickersKey = featuredStickersKey;
@ -1902,9 +1913,9 @@ namespace {
LOG(("Map read time: %1").arg(getms() - ms));
if (_oldSettingsVersion < AppVersion) {
Local::writeSettings();
writeSettings();
}
return Local::ReadMapDone;
return ReadMapDone;
}
void _writeMap(WriteMapWhen when) {
@ -1948,6 +1959,7 @@ namespace {
if (!_audiosMap.isEmpty()) mapSize += sizeof(quint32) * 2 + _audiosMap.size() * (sizeof(quint64) * 3 + sizeof(qint32));
if (_locationsKey) mapSize += sizeof(quint32) + sizeof(quint64);
if (_reportSpamStatusesKey) mapSize += sizeof(quint32) + sizeof(quint64);
if (_trustedBotsKey) mapSize += sizeof(quint32) + sizeof(quint64);
if (_recentStickersKeyOld) mapSize += sizeof(quint32) + sizeof(quint64);
if (_installedStickersKey || _featuredStickersKey || _recentStickersKey || _archivedStickersKey) {
mapSize += sizeof(quint32) + 4 * sizeof(quint64);
@ -1994,6 +2006,9 @@ namespace {
if (_reportSpamStatusesKey) {
mapData.stream << quint32(lskReportSpamStatuses) << quint64(_reportSpamStatusesKey);
}
if (_trustedBotsKey) {
mapData.stream << quint32(lskTrustedBots) << quint64(_trustedBotsKey);
}
if (_recentStickersKeyOld) {
mapData.stream << quint32(lskRecentStickersOld) << quint64(_recentStickersKeyOld);
}
@ -2021,61 +2036,7 @@ namespace {
_mapChanged = false;
}
}
namespace _local_inner {
Manager::Manager() {
_mapWriteTimer.setSingleShot(true);
connect(&_mapWriteTimer, SIGNAL(timeout()), this, SLOT(mapWriteTimeout()));
_locationsWriteTimer.setSingleShot(true);
connect(&_locationsWriteTimer, SIGNAL(timeout()), this, SLOT(locationsWriteTimeout()));
}
void Manager::writeMap(bool fast) {
if (!_mapWriteTimer.isActive() || fast) {
_mapWriteTimer.start(fast ? 1 : WriteMapTimeout);
} else if (_mapWriteTimer.remainingTime() <= 0) {
mapWriteTimeout();
}
}
void Manager::writingMap() {
_mapWriteTimer.stop();
}
void Manager::writeLocations(bool fast) {
if (!_locationsWriteTimer.isActive() || fast) {
_locationsWriteTimer.start(fast ? 1 : WriteMapTimeout);
} else if (_locationsWriteTimer.remainingTime() <= 0) {
locationsWriteTimeout();
}
}
void Manager::writingLocations() {
_locationsWriteTimer.stop();
}
void Manager::mapWriteTimeout() {
_writeMap(WriteMapNow);
}
void Manager::locationsWriteTimeout() {
_writeLocations(WriteMapNow);
}
void Manager::finish() {
if (_mapWriteTimer.isActive()) {
mapWriteTimeout();
}
if (_locationsWriteTimer.isActive()) {
locationsWriteTimeout();
}
}
}
namespace Local {
} // namespace
void finish() {
if (_manager) {
@ -2091,7 +2052,7 @@ namespace Local {
void start() {
t_assert(_manager == 0);
_manager = new _local_inner::Manager();
_manager = new internal::Manager();
_localLoader = new TaskQueue(0, FileLoaderQueueStopTimeout);
_basePath = cWorkingDir() + qsl("tdata/");
@ -2284,7 +2245,7 @@ namespace Local {
_storageImagesSize = _storageStickersSize = _storageAudiosSize = 0;
_webFilesMap.clear();
_storageWebFilesSize = 0;
_locationsKey = _reportSpamStatusesKey = 0;
_locationsKey = _reportSpamStatusesKey = _trustedBotsKey = 0;
_recentStickersKeyOld = 0;
_installedStickersKey = _featuredStickersKey = _recentStickersKey = _archivedStickersKey = 0;
_savedGifsKey = 0;
@ -4010,8 +3971,8 @@ namespace Local {
}
void addSavedPeer(PeerData *peer, const QDateTime &position) {
SavedPeers &savedPeers(cRefSavedPeers());
SavedPeers::iterator i = savedPeers.find(peer);
auto &savedPeers = cRefSavedPeers();
auto i = savedPeers.find(peer);
if (i == savedPeers.cend()) {
savedPeers.insert(peer, position);
} else if (i.value() != position) {
@ -4023,10 +3984,10 @@ namespace Local {
}
void removeSavedPeer(PeerData *peer) {
SavedPeers &savedPeers(cRefSavedPeers());
auto &savedPeers = cRefSavedPeers();
if (savedPeers.isEmpty()) return;
SavedPeers::iterator i = savedPeers.find(peer);
auto i = savedPeers.find(peer);
if (i != savedPeers.cend()) {
cRefSavedPeersByTime().remove(i.value(), peer);
savedPeers.erase(i);
@ -4039,6 +4000,69 @@ namespace Local {
_writeReportSpamStatuses();
}
void writeTrustedBots() {
if (!_working()) return;
if (_trustedBots.isEmpty()) {
if (_trustedBotsKey) {
clearKey(_trustedBotsKey);
_trustedBotsKey = 0;
_mapChanged = true;
_writeMap();
}
} else {
if (!_trustedBotsKey) {
_trustedBotsKey = genKey();
_mapChanged = true;
_writeMap(WriteMapFast);
}
quint32 size = sizeof(qint32) + _trustedBots.size() * sizeof(quint64);
EncryptedDescriptor data(size);
data.stream << qint32(_trustedBots.size());
for_const (auto botId, _trustedBots) {
data.stream << quint64(botId);
}
FileWriteDescriptor file(_trustedBotsKey);
file.writeEncrypted(data);
}
}
void readTrustedBots() {
if (!_trustedBotsKey) return;
FileReadDescriptor trusted;
if (!readEncryptedFile(trusted, _trustedBotsKey)) {
clearKey(_trustedBotsKey);
_trustedBotsKey = 0;
_writeMap();
return;
}
qint32 size = 0;
trusted.stream >> size;
for (int i = 0; i < size; ++i) {
quint64 botId = 0;
trusted.stream >> botId;
_trustedBots.insert(botId);
}
}
void makeBotTrusted(UserData *bot) {
if (!isBotTrusted(bot)) {
_trustedBots.insert(bot->id);
writeTrustedBots();
}
}
bool isBotTrusted(UserData *bot) {
if (!_trustedBotsRead) {
readTrustedBots();
_trustedBotsRead = true;
}
return _trustedBots.contains(bot->id);
}
bool encrypt(const void *src, void *dst, uint32 len, const void *key128) {
if (!_localKey.created()) {
return false;
@ -4107,6 +4131,10 @@ namespace Local {
_reportSpamStatusesKey = 0;
_mapChanged = true;
}
if (_trustedBotsKey) {
_trustedBotsKey = 0;
_mapChanged = true;
}
if (_recentStickersKeyOld) {
_recentStickersKeyOld = 0;
_mapChanged = true;
@ -4304,4 +4332,55 @@ namespace Local {
}
}
namespace internal {
Manager::Manager() {
_mapWriteTimer.setSingleShot(true);
connect(&_mapWriteTimer, SIGNAL(timeout()), this, SLOT(mapWriteTimeout()));
_locationsWriteTimer.setSingleShot(true);
connect(&_locationsWriteTimer, SIGNAL(timeout()), this, SLOT(locationsWriteTimeout()));
}
void Manager::writeMap(bool fast) {
if (!_mapWriteTimer.isActive() || fast) {
_mapWriteTimer.start(fast ? 1 : WriteMapTimeout);
} else if (_mapWriteTimer.remainingTime() <= 0) {
mapWriteTimeout();
}
}
void Manager::writingMap() {
_mapWriteTimer.stop();
}
void Manager::writeLocations(bool fast) {
if (!_locationsWriteTimer.isActive() || fast) {
_locationsWriteTimer.start(fast ? 1 : WriteMapTimeout);
} else if (_locationsWriteTimer.remainingTime() <= 0) {
locationsWriteTimeout();
}
}
void Manager::writingLocations() {
_locationsWriteTimer.stop();
}
void Manager::mapWriteTimeout() {
_writeMap(WriteMapNow);
}
void Manager::locationsWriteTimeout() {
_writeLocations(WriteMapNow);
}
void Manager::finish() {
if (_mapWriteTimer.isActive()) {
mapWriteTimeout();
}
if (_locationsWriteTimer.isActive()) {
locationsWriteTimeout();
}
}
} // namespace internal
} // namespace Local

View file

@ -22,35 +22,6 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "core/basic_types.h"
namespace _local_inner {
class Manager : public QObject {
Q_OBJECT
public:
Manager();
void writeMap(bool fast);
void writingMap();
void writeLocations(bool fast);
void writingLocations();
void finish();
public slots:
void mapWriteTimeout();
void locationsWriteTimeout();
private:
QTimer _mapWriteTimer;
QTimer _locationsWriteTimer;
};
}
namespace Local {
void start();
@ -183,7 +154,38 @@ namespace Local {
void writeReportSpamStatuses();
void makeBotTrusted(UserData *bot);
bool isBotTrusted(UserData *bot);
bool encrypt(const void *src, void *dst, uint32 len, const void *key128);
bool decrypt(const void *src, void *dst, uint32 len, const void *key128);
namespace internal {
class Manager : public QObject {
Q_OBJECT
public:
Manager();
void writeMap(bool fast);
void writingMap();
void writeLocations(bool fast);
void writingLocations();
void finish();
public slots:
void mapWriteTimeout();
void locationsWriteTimeout();
private:
QTimer _mapWriteTimer;
QTimer _locationsWriteTimer;
};
} // namespace internal
} // namespace Local

View file

@ -4680,7 +4680,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) {
auto &set = d.vstickerset.c_messages_stickerSet();
if (set.vset.type() == mtpc_stickerSet) {
auto &s = set.vset.c_stickerSet();
if (!s.is_masks()) {
auto &sets = Global::RefStickerSets();
auto it = sets.find(s.vid.v);
if (it == sets.cend()) {
@ -4696,7 +4696,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) {
auto &v = set.vdocuments.c_vector().v;
it->stickers.clear();
it->stickers.reserve(v.size());
for (int32 i = 0, l = v.size(); i < l; ++i) {
for (int i = 0, l = v.size(); i < l; ++i) {
auto doc = App::feedDocument(v.at(i));
if (!doc || !doc->sticker()) continue;
@ -4707,15 +4707,15 @@ void MainWidget::feedUpdate(const MTPUpdate &update) {
}
it->emoji.clear();
auto &packs = set.vpacks.c_vector().v;
for (int32 i = 0, l = packs.size(); i < l; ++i) {
for (int i = 0, l = packs.size(); i < l; ++i) {
if (packs.at(i).type() != mtpc_stickerPack) continue;
auto &pack = packs.at(i).c_stickerPack();
if (EmojiPtr e = emojiGetNoColor(emojiFromText(qs(pack.vemoticon)))) {
if (auto e = emojiGetNoColor(emojiFromText(qs(pack.vemoticon)))) {
auto &stickers = pack.vdocuments.c_vector().v;
StickerPack p;
p.reserve(stickers.size());
for (int32 j = 0, c = stickers.size(); j < c; ++j) {
DocumentData *doc = App::document(stickers.at(j).v);
for (int j = 0, c = stickers.size(); j < c; ++j) {
auto doc = App::document(stickers.at(j).v);
if (!doc || !doc->sticker()) continue;
p.push_back(doc);
@ -4748,6 +4748,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) {
emit stickersUpdated();
}
}
}
} break;
case mtpc_updateStickerSetsOrder: {
@ -4756,7 +4757,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) {
auto &order = d.vorder.c_vector().v;
auto &sets = Global::StickerSets();
Stickers::Order result;
for (int32 i = 0, l = order.size(); i < l; ++i) {
for (int i = 0, l = order.size(); i < l; ++i) {
if (sets.constFind(order.at(i).v) == sets.cend()) {
break;
}