Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow /openurl to force use private/non-private mode #5149

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Minor: Chatters from recent-messages are now added to autocompletion. (#5116)
- Minor: Added a _System_ theme that updates according to the system's color scheme (requires Qt 6.5). (#5118)
- Minor: Added icons for newer versions of macOS. (#5148)
- Minor: Added the `--incognito/--no-incognito` options to the `/openurl` command, allowing you to override the "Open links in incognito/private mode" setting. (#5149)
- Minor: Added support for the `{input.text}` placeholder in the **Split** -> **Run a command** hotkey. (#5130)
- Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840)
- Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848)
Expand Down
78 changes: 74 additions & 4 deletions src/controllers/commands/builtin/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "widgets/splits/SplitContainer.hpp"
#include "widgets/Window.hpp"

#include <QCommandLineParser>
#include <QDesktopServices>
#include <QString>
#include <QUrl>
Expand Down Expand Up @@ -414,26 +415,95 @@ QString clearmessages(const CommandContext &ctx)

QString openURL(const CommandContext &ctx)
{
/**
* The /openurl command
* Takes a positional argument as the URL to open
*
* Accepts the option --private or --no-private (or --incognito or --no-incognito).
* These options will force the URL to be opened in private or non-private mode, regardless of the
* default incognito mode setting.
*
* Examples:
* - /openurl https://twitch.tv/forsen
* with the setting "Open links in incognito/private mode" enabled
* Opens https://twitch.tv/forsen in private mode
* - /openurl https://twitch.tv/forsen
* with the setting "Open links in incognito/private mode" disabled
* Opens https://twitch.tv/forsen in normal mode
* - /openurl https://twitch.tv/forsen --private
* with the setting "Open links in incognito/private mode" disabled
* Opens https://twitch.tv/forsen in private mode
* - /openurl https://twitch.tv/forsen --no-private
* with the setting "Open links in incognito/private mode" enabled
* Opens https://twitch.tv/forsen in normal mode
*/
if (ctx.channel == nullptr)
{
return "";
}

if (ctx.words.size() < 2)
QCommandLineParser parser;
parser.addPositionalArgument("URL", "The URL to open");
QCommandLineOption privateModeOption(
{
"private",
"incognito",
},
"Force private mode. Cannot be used together with --no-private");
QCommandLineOption noPrivateModeOption(
{
"no-private",
"no-incognito",
},
"Force non-private mode. Cannot be used together with --private");
parser.addOptions({
privateModeOption,
noPrivateModeOption,
});
parser.parse(ctx.words);

const auto &positionalArguments = parser.positionalArguments();
if (positionalArguments.isEmpty())
{
ctx.channel->addMessage(makeSystemMessage("Usage: /openurl <URL>"));
ctx.channel->addMessage(makeSystemMessage(
"Usage: /openurl <URL> [--incognito/--no-incognito]"));
return "";
}
auto urlString = parser.positionalArguments().at(0);

QUrl url = QUrl::fromUserInput(ctx.words.mid(1).join(" "));
QUrl url = QUrl::fromUserInput(urlString);
if (!url.isValid())
{
ctx.channel->addMessage(makeSystemMessage("Invalid URL specified."));
return "";
}

auto preferPrivateMode = getSettings()->openLinksIncognito.getValue();
auto forcePrivateMode = parser.isSet(privateModeOption);
auto forceNonPrivateMode = parser.isSet(noPrivateModeOption);

if (forcePrivateMode && forceNonPrivateMode)
{
ctx.channel->addMessage(makeSystemMessage(
"Error: /openurl may only be called with --incognito or "
"--no-incognito, not both at the same time."));
return "";
}

bool usePrivateMode = false;

if (forceNonPrivateMode)
{
usePrivateMode = false;
}
else if (supportsIncognitoLinks() &&
(forcePrivateMode || preferPrivateMode))
{
usePrivateMode = true;
}

bool res = false;
if (supportsIncognitoLinks() && getSettings()->openLinksIncognito)
if (usePrivateMode)
{
res = openLinkIncognito(url.toString(QUrl::FullyEncoded));
}
Expand Down
Loading