diff --git a/Telegram/SourceFiles/_other/updater_linux.cpp b/Telegram/SourceFiles/_other/updater_linux.cpp index a5743bd08..30091a497 100644 --- a/Telegram/SourceFiles/_other/updater_linux.cpp +++ b/Telegram/SourceFiles/_other/updater_linux.cpp @@ -364,9 +364,12 @@ int main(int argc, char *argv[]) { bool startintray = false; bool customWorkingDir = false; bool justUpdate = false; + bool useEnvApi = true; char *key = 0; char *workdir = 0; + char *customApiId = 0; + char *customApiHash = 0; for (int i = 1; i < argc; ++i) { if (equal(argv[i], "-noupdate")) { needupdate = false; @@ -395,6 +398,12 @@ int main(int argc, char *argv[]) { exePath = argv[i]; } else if (equal(argv[i], "-argv0") && ++i < argc) { argv0 = argv[i]; + } else if (equal(argv[i], "-no-env-api")) { + useEnvApi = false; + } else if (equal(argv[i], "-api-id") && ++i < argc) { + customApiId = argv[i]; + } else if (equal(argv[i], "-api-hash") && ++i < argc) { + customApiHash = argv[i]; } } if (exeName.empty() || exeName.find('/') != string::npos) { @@ -496,6 +505,20 @@ int main(int argc, char *argv[]) { push(workdir); } + auto args = vector(); + for (auto &arg : values) { + args.push_back(arg.data()); + } + args.push_back(nullptr); + + if (!useEnvApi) push("-no-env-api"); + if (customApiId && customApiHash) { + push("-api-id"); + push(customApiId); + push("-api-hash"); + push(customApiHash); + } + auto args = vector(); for (auto &arg : values) { args.push_back(arg.data()); diff --git a/Telegram/SourceFiles/_other/updater_osx.m b/Telegram/SourceFiles/_other/updater_osx.m index 4d841723c..dce3f4b7a 100644 --- a/Telegram/SourceFiles/_other/updater_osx.m +++ b/Telegram/SourceFiles/_other/updater_osx.m @@ -93,8 +93,10 @@ int main(int argc, const char * argv[]) { openLog(); pid_t procId = 0; BOOL update = YES, toSettings = NO, autoStart = NO, startInTray = NO; - BOOL customWorkingDir = NO; + BOOL customWorkingDir = NO, useEnvApi = YES; NSString *key = nil; + NSString *customApiId = nil; + NSString *customApiHash = nil; for (int i = 0; i < argc; ++i) { if ([@"-workpath" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { if (++i < argc) { @@ -120,6 +122,12 @@ int main(int argc, const char * argv[]) { customWorkingDir = YES; } else if ([@"-key" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { if (++i < argc) key = [NSString stringWithUTF8String:argv[i]]; + } else if ([@"-no-env-api" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + useEnvApi = NO; + } else if ([@"-api-id" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + if (++i < argc) customApiId = [NSString stringWithUTF8String:argv[i]]; + } else if ([@"-api-hash" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + if (++i < argc) customApiHash = [NSString stringWithUTF8String:argv[i]]; } } if (!workDir) { @@ -261,6 +269,13 @@ int main(int argc, const char * argv[]) { [args addObject:@"-workdir"]; [args addObject:workDir]; } + if (!useEnvApi) [args addObject:@"-no-env-api"]; + if (customApiId && customApiHash) { + [args addObject:@"-api-id"]; + [args addObject:customApiId]; + [args addObject:@"-api-hash"]; + [args addObject:customApiHash]; + } writeLog([[NSArray arrayWithObjects:@"Running application '", appPath, @"' with args '", [args componentsJoinedByString:@"' '"], @"'..", nil] componentsJoinedByString:@""]); for (int i = 0; i < 5; ++i) { diff --git a/Telegram/SourceFiles/_other/updater_win.cpp b/Telegram/SourceFiles/_other/updater_win.cpp index 3107d650f..bfc17b183 100644 --- a/Telegram/SourceFiles/_other/updater_win.cpp +++ b/Telegram/SourceFiles/_other/updater_win.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL bool _debug = false; wstring updaterName, updaterDir, updateTo, exeName, customWorkingDir, customKeyFile; +wstring customApiId, customApiHash; bool equal(const wstring &a, const wstring &b) { return !_wcsicmp(a.c_str(), b.c_str()); @@ -344,6 +345,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara int argsCount; bool needupdate = false, autostart = false, debug = false, writeprotected = false, startintray = false; + bool useEnvApi = true; args = CommandLineToArgvW(GetCommandLine(), &argsCount); if (args) { for (int i = 1; i < argsCount; ++i) { @@ -381,6 +383,14 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara break; } } + } else if (equal(args[i], L"-no-env-api")) { + useEnvApi = false; + } else if (equal(args[i], L"-api-id") && ++i < argsCount) { + writeLog(std::wstring(L"Argument: ") + args[i]); + customApiId = args[i]; + } else if (equal(args[i], L"-api-hash") && ++i < argsCount) { + writeLog(std::wstring(L"Argument: ") + args[i]); + customApiHash = args[i]; } } if (exeName.empty()) { @@ -432,6 +442,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara if (!customKeyFile.empty()) { targs += L" -key \"" + customKeyFile + L"\""; } + if (!useEnvApi) targs += L" -no-env-api"; + if (!customApiId.empty() && !customApiHash.empty()) { + targs += L" -api-id \"" + customApiId + L"\""; + targs += L" -api-hash \"" + customApiHash + L"\""; + } writeLog(L"Result arguments: " + targs); bool executed = false; diff --git a/Telegram/SourceFiles/config.h b/Telegram/SourceFiles/config.h index ade9f6354..e6b1d7c93 100644 --- a/Telegram/SourceFiles/config.h +++ b/Telegram/SourceFiles/config.h @@ -63,35 +63,6 @@ v1/0UnkegO4jNkSY3ycDqn+T3NjxNxnL0EsKh7MjinyMUe3ZISzaIyrdq/8v4bvB\n\ -----END RSA PUBLIC KEY-----\ "; -#if defined TDESKTOP_API_ID && defined TDESKTOP_API_HASH - -constexpr auto ApiId = TDESKTOP_API_ID; -constexpr auto ApiHash = QT_STRINGIFY(TDESKTOP_API_HASH); - -#else // TDESKTOP_API_ID && TDESKTOP_API_HASH - -// To build your version of Telegram Desktop you're required to provide -// your own 'api_id' and 'api_hash' for the Telegram API access. -// -// How to obtain your 'api_id' and 'api_hash' is described here: -// https://core.telegram.org/api/obtaining_api_id -// -// If you're building the application not for deployment, -// but only for test purposes you can comment out the error below. -// -// This will allow you to use TEST ONLY 'api_id' and 'api_hash' which are -// very limited by the Telegram API server. -// -// Your users will start getting internal server errors on login -// if you deploy an app using those 'api_id' and 'api_hash'. - -#error You are required to provide API_ID and API_HASH. - -constexpr auto ApiId = 17349; -constexpr auto ApiHash = "344583e45741c457fe1862106095a5eb"; - -#endif // TDESKTOP_API_ID && TDESKTOP_API_HASH - #if Q_BYTE_ORDER == Q_BIG_ENDIAN #error "Only little endian is supported!" #endif // Q_BYTE_ORDER == Q_BIG_ENDIAN diff --git a/Telegram/SourceFiles/core/crash_reports.cpp b/Telegram/SourceFiles/core/crash_reports.cpp index e3a4367d3..dd8a4eea3 100644 --- a/Telegram/SourceFiles/core/crash_reports.cpp +++ b/Telegram/SourceFiles/core/crash_reports.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "core/crash_reports.h" +#include "kotato/kotato_settings.h" #include "kotato/kotato_version.h" #include "platform/platform_specific.h" #include "base/platform/base_platform_info.h" @@ -317,7 +318,7 @@ QString PlatformString() { void StartCatching() { #ifndef TDESKTOP_DISABLE_CRASH_REPORTS ProcessAnnotations["Binary"] = cExeName().toUtf8().constData(); - ProcessAnnotations["ApiId"] = QString::number(ApiId).toUtf8().constData(); + ProcessAnnotations["ApiId"] = QString::number(::Kotato::JsonSettings::GetInt("api_id")).toUtf8().constData(); ProcessAnnotations["Version"] = (cAlphaVersion() ? u"%1 %2"_q.arg(cAlphaVersion()).arg(AppKotatoTestBranch) : (AppBetaVersion diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index 7bb3ce74d..b43742dd3 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -27,6 +27,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Core { namespace { +constexpr auto kApiIdVarName = "KTGDESKTOP_API_ID"_cs; +constexpr auto kApiHashVarName = "KTGDESKTOP_API_HASH"_cs; + uint64 InstallationTag = 0; base::options::toggle OptionFreeType({ @@ -395,6 +398,15 @@ int Launcher::exec() { // Must be started before Sandbox is created. Platform::start(); + + if (::Kotato::JsonSettings::GetBool("api_use_env") + && qEnvironmentVariableIsSet(kApiIdVarName.utf8().constData()) + && qEnvironmentVariableIsSet(kApiHashVarName.utf8().constData())) { + ::Kotato::JsonSettings::Set("api_id", qgetenv(kApiIdVarName.utf8().constData()).toInt()); + ::Kotato::JsonSettings::Set("api_hash", QString::fromLatin1(qgetenv(kApiHashVarName.utf8().constData()))); + ::Kotato::JsonSettings::Set("api_start_params", false); + } + auto result = executeApplication(); DEBUG_LOG(("Kotatogram finished, result: %1").arg(result)); @@ -529,6 +541,9 @@ void Launcher::processArguments() { { "-workdir" , KeyFormat::OneValue }, { "--" , KeyFormat::OneValue }, { "-scale" , KeyFormat::OneValue }, + { "-no-env-api" , KeyFormat::NoValues }, + { "-api-id" , KeyFormat::OneValue }, + { "-api-hash" , KeyFormat::OneValue }, }; auto parseResult = QMap(); auto parsingKey = QByteArray(); @@ -583,6 +598,15 @@ void Launcher::processArguments() { ? kScaleAuto : value; } + + ::Kotato::JsonSettings::Set("api_use_env", !parseResult.contains("-no-env-api")); + auto customApiId = parseResult.value("-api-id", {}).join(QString()).toInt(); + auto customApiHash = parseResult.value("-api-hash", {}).join(QString()); + if (customApiId > 0 && !customApiHash.isEmpty()) { + ::Kotato::JsonSettings::Set("api_id", customApiId); + ::Kotato::JsonSettings::Set("api_hash", customApiHash); + ::Kotato::JsonSettings::Set("api_start_params", true); + } } int Launcher::executeApplication() { diff --git a/Telegram/SourceFiles/intro/intro_phone.cpp b/Telegram/SourceFiles/intro/intro_phone.cpp index 7ac420956..7ddd2c7a5 100644 --- a/Telegram/SourceFiles/intro/intro_phone.cpp +++ b/Telegram/SourceFiles/intro/intro_phone.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "intro/intro_phone.h" +#include "kotato/kotato_settings.h" #include "lang/lang_keys.h" #include "intro/intro_code.h" #include "intro/intro_qr.h" @@ -194,8 +195,8 @@ void PhoneWidget::submit() { api().instance().setUserPhone(_sentPhone); _sentRequest = api().request(MTPauth_SendCode( MTP_string(_sentPhone), - MTP_int(ApiId), - MTP_string(ApiHash), + MTP_int(::Kotato::JsonSettings::GetInt("api_id")), + MTP_string(::Kotato::JsonSettings::GetString("api_hash")), MTP_codeSettings( MTP_flags(0), MTPVector(), diff --git a/Telegram/SourceFiles/intro/intro_qr.cpp b/Telegram/SourceFiles/intro/intro_qr.cpp index 3ad8aead8..2c74f529a 100644 --- a/Telegram/SourceFiles/intro/intro_qr.cpp +++ b/Telegram/SourceFiles/intro/intro_qr.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "intro/intro_qr.h" +#include "kotato/kotato_settings.h" #include "boxes/abstract_box.h" #include "intro/intro_phone.h" #include "intro/intro_widget.h" @@ -318,8 +319,8 @@ void QrWidget::refreshCode() { return; } _requestId = api().request(MTPauth_ExportLoginToken( - MTP_int(ApiId), - MTP_string(ApiHash), + MTP_int(::Kotato::JsonSettings::GetInt("api_id")), + MTP_string(::Kotato::JsonSettings::GetString("api_hash")), MTP_vector(0) )).done([=](const MTPauth_LoginToken &result) { handleTokenResult(result); diff --git a/Telegram/SourceFiles/kotato/kotato_settings.cpp b/Telegram/SourceFiles/kotato/kotato_settings.cpp index 3a18489a6..ec354ea85 100644 --- a/Telegram/SourceFiles/kotato/kotato_settings.cpp +++ b/Telegram/SourceFiles/kotato/kotato_settings.cpp @@ -167,6 +167,55 @@ struct Definition { const std::map> DefinitionMap { // Non-stored settings + + // To build your version of Kotatogram Desktop you're required to provide + // your own 'api_id' and 'api_hash' for the Telegram API access. + // + // How to obtain your 'api_id' and 'api_hash' is described here: + // https://core.telegram.org/api/obtaining_api_id + // + // By default Kotatogram provides empty 'api_id' and 'api_hash' + // since you can set it them in runtime. They can be set with + // KTGDESKTOP_API_ID and KTGDESKTOP_API_HASH environment variables. + // You must set both variables for it to work. + // + // As an alternative, you can use -api-id and -api_hash + // start parameters. Note that environment variables have priority + // over start parameters, so you should use -no-env-api if you don't + // want them. And as with environment variables, both -api-id and + // -api_hash must be set for it to work. + // + // If 'api_id' and 'api_hash' are empty, and they're not set by any + // of these parameters, you won't be able to connect to Telegram at all. + // Sessions created on TDesktop + forks (including Kotatogram) might + // work, but it could be risky, so be careful with it. + { "api_id", { + .storage = SettingStorage::None, + .type = SettingType::IntSetting, +#if defined TDESKTOP_API_ID && defined TDESKTOP_API_HASH + .defaultValue = TDESKTOP_API_ID, +#else + .defaultValue = 0, +#endif + }}, + { "api_hash", { + .storage = SettingStorage::None, + .type = SettingType::QStringSetting, +#if defined TDESKTOP_API_ID && defined TDESKTOP_API_HASH + .defaultValue = QT_STRINGIFY(TDESKTOP_API_HASH), +#else + .defaultValue = QString(), +#endif + }}, + { "api_use_env", { + .storage = SettingStorage::None, + .type = SettingType::BoolSetting, + .defaultValue = true, }}, + { "api_start_params", { + .storage = SettingStorage::None, + .type = SettingType::BoolSetting, + .defaultValue = false, }}, + // Stored settings }; diff --git a/Telegram/SourceFiles/mtproto/session_private.cpp b/Telegram/SourceFiles/mtproto/session_private.cpp index 0a36371c5..fe69528ee 100644 --- a/Telegram/SourceFiles/mtproto/session_private.cpp +++ b/Telegram/SourceFiles/mtproto/session_private.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/session_private.h" +#include "kotato/kotato_settings.h" #include "kotato/kotato_version.h" #include "mtproto/details/mtproto_bound_key_creator.h" #include "mtproto/details/mtproto_dcenter.h" @@ -685,7 +686,7 @@ void SessionPrivate::tryToSend() { initWrapper = MTPInitConnection( MTP_flags(Flag::f_params | (mtprotoProxy ? Flag::f_proxy : Flag(0))), - MTP_int(ApiId), + MTP_int(::Kotato::JsonSettings::GetInt("api_id")), MTP_string(deviceModel), MTP_string(systemVersion), MTP_string(appVersion), diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp index 9afde3b19..bf4566cd2 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "platform/linux/launcher_linux.h" +#include "kotato/kotato_settings.h" #include "core/crash_reports.h" #include "core/update_checker.h" #include "webview/platform/linux/webview_linux_webkitgtk.h" @@ -104,6 +105,17 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { } } + if (!::Kotato::JsonSettings::GetBool("api_use_env")) { + argumentsList.push_back("-no-env-api"); + } + + if (::Kotato::JsonSettings::GetBool("api_start_params")) { + argumentsList.push_back("-api-id"); + argumentsList.push_back(std::to_string(::Kotato::JsonSettings::GetInt("api_id"))); + argumentsList.push_back("-api-hash"); + argumentsList.push_back(::Kotato::JsonSettings::GetString("api_hash").toStdString()); + } + Logs::closeMain(); CrashReports::Finish(); diff --git a/Telegram/SourceFiles/platform/mac/launcher_mac.mm b/Telegram/SourceFiles/platform/mac/launcher_mac.mm index 8fecc6bfc..a3dff528e 100644 --- a/Telegram/SourceFiles/platform/mac/launcher_mac.mm +++ b/Telegram/SourceFiles/platform/mac/launcher_mac.mm @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "platform/mac/launcher_mac.h" +#include "kotato/kotato_settings.h" #include "core/crash_reports.h" #include "core/update_checker.h" #include "base/base_file_utilities.h" @@ -67,6 +68,13 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { if (customWorkingDir()) { [args addObject:@"-workdir_custom"]; } + if (!::Kotato::JsonSettings::GetBool("api_use_env")) [args addObject:@"-no-env-api"]; + if (::Kotato::JsonSettings::GetBool("api_start_params")) { + [args addObject:@"-api-id"]; + [args addObject:Q2NSString(QString::number(::Kotato::JsonSettings::GetInt("api_id")))]; + [args addObject:@"-api-hash"]; + [args addObject:Q2NSString(::Kotato::JsonSettings::GetString("api_hash"))]; + } DEBUG_LOG(("Application Info: executing %1 %2").arg(NS2QString(path)).arg(NS2QString([args componentsJoinedByString:@" "]))); Logs::closeMain(); diff --git a/Telegram/SourceFiles/platform/win/launcher_win.cpp b/Telegram/SourceFiles/platform/win/launcher_win.cpp index d6bf912ba..58518d938 100644 --- a/Telegram/SourceFiles/platform/win/launcher_win.cpp +++ b/Telegram/SourceFiles/platform/win/launcher_win.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "platform/win/launcher_win.h" +#include "kotato/kotato_settings.h" #include "core/crash_reports.h" #include "core/update_checker.h" @@ -90,6 +91,16 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { pushArgument('"' + cExeDir() + '"'); } } + + if (!::Kotato::JsonSettings::GetBool("api_use_env")) { + pushArgument(qsl("-no-env-api")); + } + if (::Kotato::JsonSettings::GetBool("api_start_params")) { + pushArgument(qsl("-api-id")); + pushArgument('"' + QString::number(::Kotato::JsonSettings::GetInt("api_id")) + '"'); + pushArgument(qsl("-api-hash")); + pushArgument('"' + ::Kotato::JsonSettings::GetString("api_hash") + '"'); + } return launch(operation, binaryPath, argumentsList); } diff --git a/Telegram/cmake/telegram_options.cmake b/Telegram/cmake/telegram_options.cmake index f11c39964..f3d616a32 100644 --- a/Telegram/cmake/telegram_options.cmake +++ b/Telegram/cmake/telegram_options.cmake @@ -14,29 +14,6 @@ if (TDESKTOP_API_TEST) set(TDESKTOP_API_HASH 344583e45741c457fe1862106095a5eb) endif() -if (TDESKTOP_API_ID STREQUAL "0" OR TDESKTOP_API_HASH STREQUAL "") - message(FATAL_ERROR - " \n" - " PROVIDE: -D TDESKTOP_API_ID=[API_ID] -D TDESKTOP_API_HASH=[API_HASH]\n" - " \n" - " > To build your version of Telegram Desktop you're required to provide\n" - " > your own 'api_id' and 'api_hash' for the Telegram API access.\n" - " >\n" - " > How to obtain your 'api_id' and 'api_hash' is described here:\n" - " > https://core.telegram.org/api/obtaining_api_id\n" - " >\n" - " > If you're building the application not for deployment,\n" - " > but only for test purposes you can use TEST ONLY credentials,\n" - " > which are very limited by the Telegram API server:\n" - " >\n" - " > api_id: 17349\n" - " > api_hash: 344583e45741c457fe1862106095a5eb\n" - " >\n" - " > Your users will start getting internal server errors on login\n" - " > if you deploy an app using those 'api_id' and 'api_hash'.\n" - " ") -endif() - if (DESKTOP_APP_DISABLE_AUTOUPDATE) target_compile_definitions(Telegram PRIVATE TDESKTOP_DISABLE_AUTOUPDATE) endif()