From 646d15b257ac1964bb53c5603d83ac3ab454112a Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 29 Jan 2019 13:09:37 +0300
Subject: [PATCH] Add LSFileQuarantineEnabled to the .plist

---
 Telegram/SourceFiles/_other/updater_osx.m     | 18 ++++
 Telegram/SourceFiles/core/update_checker.cpp  |  6 ++
 .../platform/linux/specific_linux.cpp         | 62 -------------
 .../SourceFiles/platform/mac/launcher_mac.mm  |  2 +
 .../SourceFiles/platform/mac/specific_mac.h   |  2 +
 .../SourceFiles/platform/mac/specific_mac.mm  | 88 ++-----------------
 Telegram/Telegram.plist                       |  2 +
 7 files changed, 39 insertions(+), 141 deletions(-)

diff --git a/Telegram/SourceFiles/_other/updater_osx.m b/Telegram/SourceFiles/_other/updater_osx.m
index 7431908f7..ea6f0d68b 100644
--- a/Telegram/SourceFiles/_other/updater_osx.m
+++ b/Telegram/SourceFiles/_other/updater_osx.m
@@ -6,6 +6,7 @@ For license and copyright information please follow this link:
 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #import <Cocoa/Cocoa.h>
+#include <sys/xattr.h>
 
 NSString *appName = @"Telegram.app";
 NSString *appDir = nil;
@@ -44,6 +45,20 @@ void writeLog(NSString *msg) {
 	[_logFile synchronizeFile];
 }
 
+void RemoveQuarantineAttribute(NSString *path) {
+	const char *kQuarantineAttribute = "com.apple.quarantine";
+
+	writeLog([@"Removing quarantine: " stringByAppendingString:path]);
+	removexattr([path fileSystemRepresentation], kQuarantineAttribute, 0);
+}
+
+void RemoveQuarantineFromBundle(NSString *path) {
+    RemoveQuarantineAttribute(path);
+    RemoveQuarantineAttribute([path stringByAppendingString:@"/Contents/MacOS/Telegram"]);
+    RemoveQuarantineAttribute([path stringByAppendingString:@"/Contents/Helpers/crashpad_handler"]);
+    RemoveQuarantineAttribute([path stringByAppendingString:@"/Contents/Frameworks/Updater"]);
+}
+
 void delFolder() {
 	writeLog([@"Fully clearing old path: " stringByAppendingString:[workDir stringByAppendingString:@"tupdates/ready"]]);
 	if (![[NSFileManager defaultManager] removeItemAtPath:[workDir stringByAppendingString:@"tupdates/ready"] error:nil]) {
@@ -232,6 +247,9 @@ int main(int argc, const char * argv[]) {
 	}
 
 	NSString *appPath = [[NSArray arrayWithObjects:appDir, appRealName, nil] componentsJoinedByString:@""];
+
+	RemoveQuarantineFromBundle(appPath);
+
 	NSMutableArray *args = [[NSMutableArray alloc] initWithObjects: @"-noupdate", nil];
 	if (toSettings) [args addObject:@"-tosettings"];
 	if (_debug) [args addObject:@"-debug"];
diff --git a/Telegram/SourceFiles/core/update_checker.cpp b/Telegram/SourceFiles/core/update_checker.cpp
index 078f974f9..021da0cf5 100644
--- a/Telegram/SourceFiles/core/update_checker.cpp
+++ b/Telegram/SourceFiles/core/update_checker.cpp
@@ -1547,6 +1547,12 @@ bool checkReadyUpdate() {
 		return false;
 	}
 #endif // Q_OS_LINUX
+
+#ifdef Q_OS_MAC
+	Platform::RemoveQuarantine(QFileInfo(curUpdater).absolutePath());
+	Platform::RemoveQuarantine(updater.absolutePath());
+#endif // Q_OS_MAC
+
 	return true;
 }
 
diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp
index d9964a715..9b256e5b2 100644
--- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp
+++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp
@@ -158,68 +158,6 @@ QAbstractNativeEventFilter *psNativeEventFilter() {
 void psWriteDump() {
 }
 
-QString demanglestr(const QString &mangled) {
-	if (mangled.isEmpty()) return mangled;
-
-	QByteArray cmd = ("c++filt -n " + mangled).toUtf8();
-	FILE *f = popen(cmd.constData(), "r");
-	if (!f) return "BAD_SYMBOL_" + mangled;
-
-	QString result;
-	char buffer[4096] = { 0 };
-	while (!feof(f)) {
-		if (fgets(buffer, 4096, f) != NULL) {
-			result += buffer;
-		}
-	}
-	pclose(f);
-	return result.trimmed();
-}
-
-QStringList addr2linestr(uint64 *addresses, int count) {
-	QStringList result;
-	if (!count || cExeName().isEmpty()) return result;
-
-	result.reserve(count);
-	QByteArray cmd = "addr2line -e " + EscapeShell(QFile::encodeName(cExeDir() + cExeName()));
-	for (int i = 0; i < count; ++i) {
-		if (addresses[i]) {
-			cmd += qsl(" 0x%1").arg(addresses[i], 0, 16).toUtf8();
-		}
-	}
-	FILE *f = popen(cmd.constData(), "r");
-
-	QStringList addr2lineResult;
-	if (f) {
-		char buffer[4096] = {0};
-		while (!feof(f)) {
-			if (fgets(buffer, 4096, f) != NULL) {
-				addr2lineResult.push_back(QString::fromUtf8(buffer));
-			}
-		}
-		pclose(f);
-	}
-	for (int i = 0, j = 0; i < count; ++i) {
-		if (addresses[i]) {
-			if (j < addr2lineResult.size() && !addr2lineResult.at(j).isEmpty() && !addr2lineResult.at(j).startsWith(qstr("0x"))) {
-				QString res = addr2lineResult.at(j).trimmed();
-				if (int index = res.indexOf(qstr("/Telegram/"))) {
-					if (index > 0) {
-						res = res.mid(index + qstr("/Telegram/").size());
-					}
-				}
-				result.push_back(res);
-			} else {
-				result.push_back(QString());
-			}
-			++j;
-		} else {
-			result.push_back(QString());
-		}
-	}
-	return result;
-}
-
 bool _removeDirectory(const QString &path) { // from http://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c
 	QByteArray pathRaw = QFile::encodeName(path);
 	DIR *d = opendir(pathRaw.constData());
diff --git a/Telegram/SourceFiles/platform/mac/launcher_mac.mm b/Telegram/SourceFiles/platform/mac/launcher_mac.mm
index 5553d52be..bb3d94470 100644
--- a/Telegram/SourceFiles/platform/mac/launcher_mac.mm
+++ b/Telegram/SourceFiles/platform/mac/launcher_mac.mm
@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "core/crash_reports.h"
 #include "core/update_checker.h"
 #include "platform/mac/mac_utilities.h"
+#include "platform/platform_specific.h"
 
 #include <Cocoa/Cocoa.h>
 #include <CoreFoundation/CFURL.h>
@@ -117,6 +118,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
 			return false;
 		}
 		path = [path stringByAppendingString:@"/Contents/Frameworks/Updater"];
+		RemoveQuarantine(QFile::decodeName([path fileSystemRepresentation]));
 
 		NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:@"-workpath", Q2NSString(cWorkingDir()), @"-procid", nil];
 		[args addObject:[NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]]];
diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.h b/Telegram/SourceFiles/platform/mac/specific_mac.h
index 3f56c1f28..4faaa2ebf 100644
--- a/Telegram/SourceFiles/platform/mac/specific_mac.h
+++ b/Telegram/SourceFiles/platform/mac/specific_mac.h
@@ -19,6 +19,8 @@ inline bool TranslucentWindowsSupported(QPoint globalPosition) {
 
 QString CurrentExecutablePath(int argc, char *argv[]);
 
+void RemoveQuarantine(const QString &path);
+
 namespace ThirdParty {
 
 inline void start() {
diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.mm b/Telegram/SourceFiles/platform/mac/specific_mac.mm
index 30829bc89..2fbf860b9 100644
--- a/Telegram/SourceFiles/platform/mac/specific_mac.mm
+++ b/Telegram/SourceFiles/platform/mac/specific_mac.mm
@@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include <cstdlib>
 #include <execinfo.h>
+#include <sys/xattr.h>
 
 #include <Cocoa/Cocoa.h>
 #include <CoreFoundation/CFURL.h>
@@ -85,85 +86,6 @@ void psWriteDump() {
 #endif // TDESKTOP_DISABLE_CRASH_REPORTS
 }
 
-QString demanglestr(const QString &mangled) {
-	QByteArray cmd = ("c++filt -n " + mangled).toUtf8();
-	FILE *f = popen(cmd.constData(), "r");
-	if (!f) return "BAD_SYMBOL_" + mangled;
-
-	QString result;
-	char buffer[4096] = {0};
-	while (!feof(f)) {
-		if (fgets(buffer, 4096, f) != NULL) {
-			result += buffer;
-		}
-	}
-	pclose(f);
-	return result.trimmed();
-}
-
-QString escapeShell(const QString &str) {
-	QString result;
-	const QChar *b = str.constData(), *e = str.constEnd();
-	for (const QChar *ch = b; ch != e; ++ch) {
-		if (*ch == ' ' || *ch == '"' || *ch == '\'' || *ch == '\\') {
-			if (result.isEmpty()) {
-				result.reserve(str.size() * 2);
-			}
-			if (ch > b) {
-				result.append(b, ch - b);
-			}
-			result.append('\\');
-			b = ch;
-		}
-	}
-	if (result.isEmpty()) return str;
-
-	if (e > b) {
-		result.append(b, e - b);
-	}
-	return result;
-}
-
-QStringList atosstr(uint64 *addresses, int count, uint64 base) {
-	QStringList result;
-	if (!count || cExeName().isEmpty()) return result;
-
-	result.reserve(count);
-	QString cmdstr = "atos -o " + escapeShell(cExeDir() + cExeName()) + qsl("/Contents/MacOS/Telegram -l 0x%1").arg(base, 0, 16);
-	for (int i = 0; i < count; ++i) {
-		if (addresses[i]) {
-			cmdstr += qsl(" 0x%1").arg(addresses[i], 0, 16);
-		}
-	}
-	QByteArray cmd = cmdstr.toUtf8();
-	FILE *f = popen(cmd.constData(), "r");
-
-	QStringList atosResult;
-	if (f) {
-		char buffer[4096] = {0};
-		while (!feof(f)) {
-			if (fgets(buffer, 4096, f) != NULL) {
-				atosResult.push_back(QString::fromUtf8(buffer));
-			}
-		}
-		pclose(f);
-	}
-	for (int i = 0, j = 0; i < count; ++i) {
-		if (addresses[i]) {
-			if (j < atosResult.size() && !atosResult.at(j).isEmpty() && !atosResult.at(j).startsWith(qstr("0x"))) {
-				result.push_back(atosResult.at(j).trimmed());
-			} else {
-				result.push_back(QString());
-			}
-			++j;
-		} else {
-			result.push_back(QString());
-		}
-	}
-	return result;
-
-}
-
 void psDeleteDir(const QString &dir) {
 	objc_deleteDir(dir);
 }
@@ -272,6 +194,14 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
 	return NS2QString([[NSBundle mainBundle] bundlePath]);
 }
 
+void RemoveQuarantine(const QString &path) {
+	const auto kQuarantineAttribute = "com.apple.quarantine";
+
+	DEBUG_LOG(("Removing quarantine attribute: %1").arg(path));
+	const auto local = QFile::encodeName(path);
+	removexattr(local.data(), kQuarantineAttribute, 0);
+}
+
 void RegisterCustomScheme() {
 #ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
 	OSStatus result = LSSetDefaultHandlerForURLScheme(CFSTR("tg"), (CFStringRef)[[NSBundle mainBundle] bundleIdentifier]);
diff --git a/Telegram/Telegram.plist b/Telegram/Telegram.plist
index 47bdf4d77..af4435e35 100644
--- a/Telegram/Telegram.plist
+++ b/Telegram/Telegram.plist
@@ -35,6 +35,8 @@
 	<string>public.app-category.social-networking</string>
 	<key>LSMinimumSystemVersion</key>
 	<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
+	<key>LSFileQuarantineEnabled</key>
+	<true/>
 	<key>NOTE</key>
 	<string></string>
 	<key>NSMicrophoneUsageDescription</key>