mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-20 05:43:54 -07:00
Adds OS-level URL-scheme handlers so users can click a link in a browser,
chat client, or third-party tool to launch Cockatrice straight into a
server / game / Oracle update.
Supported URL forms:
cockatrice://joingame?hostname=H&port=P&roomid=R&gameid=G[&spectate=1]
cockatrice-oracle://update[?spoilers=1]
Credentials passed via URL (username/password query params) are deliberately
ignored — URLs leak through shell history, browser history, EDR capture, etc.
If the target server requires auth and no saved credentials match, the Connect
dialog opens pre-filled with the URL's host/port so the user types their
password locally.
OS integration
- Linux: MimeType=x-scheme-handler/cockatrice (and -oracle) added to the
.desktop files; Exec=cockatrice %u passes the URL through.
- Windows: NSIS installer writes HKCR\cockatrice and HKCR\cockatrice-oracle
registry entries; uninstaller removes them.
- macOS: per-app Info.cockatrice.plist / Info.oracle.plist declare
CFBundleURLTypes; a QFileOpenEvent filter is installed on QApplication
before any nested event loop so cold-start URLs aren't lost.
New abstractions
- Intent (libcockatrice_utility/libcockatrice/utility/intent.h): abstract base
for chained async actions. Guarantees finished() fires at most once,
execute() is idempotent, self-deletes via deleteLater, and
startTimeoutSafetyNet() arms a configurable per-stage deadline. Concrete
intents (IntentConnectToServer, IntentLogin, IntentJoinServerRoom,
IntentJoinServerGame) compose the joingame flow via UrlParser.
- SingleInstanceManager: async per-user local-socket primary/secondary
handshake; URL forwarded from secondary to primary with QDataStream framing
both ways. shared_ptr-backed resolved flag survives every lambda capture.
- UrlSchemeEventFilter (new libcockatrice_utility_gui sibling library): QObject
event filter that translates macOS QFileOpenEvent into a urlReceived(QString)
signal. Lives in its own Gui-bearing lib so libcockatrice_utility stays
Core+Network only and doesn't drag Qt::Gui into servatrice.
- UrlUtils (header-only): pure URL parsing, fully unit-tested.
Wiring
- MainWindow::handleUrl(QString) — single entry point for any URL source.
- DlgConnect::prefillNewHost(host, port) — pre-fills new-host inputs.
- ServersSettings::findSavedCredsByHostPort — case-insensitive saved-creds
lookup.
- TabSupervisor::requestJoinRoom + roomJoinedById / roomJoinFailedById signals,
TabServer::roomAlreadyJoined for the short-circuit "already in this room"
path — single source of truth for duplicate-join handling.
Tests
- 36 new unit tests across four single-purpose targets in tests/:
- url_utils_test (22 tests) — scheme matching, port/room/game validation,
spectator flag, credentials ignored, case-insensitivity.
- url_scheme_event_filter_test (3 tests) — QFileOpenEvent capture.
- intent_test (7 tests) — self-delete, abort propagation, parent-destruction-
mid-flight, finish-once gate, execute() idempotence.
- single_instance_manager_test (4 tests) — per-user socket naming, becoming-
primary alone, forwarding to an existing primary, single-emission of
roleResolved.
Build tooling (incidental)
- Dockerfile.format, docker-compose.format.yml, Makefile — a docker-based
runner for format.sh that mirrors CI's desktop-lint step.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
136 lines
3.9 KiB
C++
136 lines
3.9 KiB
C++
#ifndef LIBCOCKATRICE_URL_UTILS_H
|
|
#define LIBCOCKATRICE_URL_UTILS_H
|
|
|
|
#include <QStringList>
|
|
#include <QUrl>
|
|
#include <QUrlQuery>
|
|
#include <optional>
|
|
|
|
namespace UrlUtils
|
|
{
|
|
|
|
/**
|
|
* @brief Scans @p args and returns the first entry that starts with
|
|
* @p schemePrefix (case-insensitive, per RFC 3986), or an empty string
|
|
* if none is found. Only the first match is returned; subsequent
|
|
* matching args are ignored.
|
|
*
|
|
* Use this to extract a custom-scheme URL from QCommandLineParser positional
|
|
* arguments or raw argv arrays.
|
|
*/
|
|
inline QString findUrlArgument(const QStringList &args, const QString &schemePrefix)
|
|
{
|
|
for (const QString &arg : args) {
|
|
if (arg.startsWith(schemePrefix, Qt::CaseInsensitive))
|
|
return arg;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
/**
|
|
* @brief Parsed shape of a cockatrice-oracle:// URL.
|
|
*
|
|
* Currently only @c update is recognised; other hosts are ignored.
|
|
*/
|
|
struct OracleUrlAction
|
|
{
|
|
bool isUpdate{false};
|
|
bool spoilersOnly{false};
|
|
};
|
|
|
|
/**
|
|
* @brief Parse a cockatrice-oracle:// URL into an OracleUrlAction.
|
|
*
|
|
* Recognised forms:
|
|
* cockatrice-oracle://update
|
|
* cockatrice-oracle://update?spoilers=1
|
|
*
|
|
* Returns a default-constructed action (@c isUpdate == false) for any URL
|
|
* whose host is not @c update. Host matching is case-insensitive.
|
|
*/
|
|
inline OracleUrlAction parseOracleUrl(const QString &url)
|
|
{
|
|
OracleUrlAction action;
|
|
const QUrl parsed(url);
|
|
if (parsed.host().toLower() != QStringLiteral("update"))
|
|
return action;
|
|
action.isUpdate = true;
|
|
action.spoilersOnly = QUrlQuery(parsed.query()).queryItemValue(QStringLiteral("spoilers")) == QStringLiteral("1");
|
|
return action;
|
|
}
|
|
|
|
/**
|
|
* @brief Parsed parameters from a cockatrice://joingame URL.
|
|
*/
|
|
struct JoinGameUrlParams
|
|
{
|
|
QString hostname;
|
|
quint16 port;
|
|
int roomId;
|
|
int gameId;
|
|
bool spectator;
|
|
};
|
|
|
|
/**
|
|
* @brief Parse a cockatrice://joingame URL into its parameters.
|
|
*
|
|
* Recognised forms:
|
|
* cockatrice://joingame?hostname=H&port=P&roomid=R&gameid=G
|
|
* cockatrice://joingame?hostname=H&port=P&roomid=R&gameid=G&spectate=1
|
|
*
|
|
* Validation:
|
|
* - scheme must be "cockatrice" (case-insensitive)
|
|
* - host must be "joingame" (case-insensitive)
|
|
* - hostname query param required
|
|
* - port required, 1..65535
|
|
* - roomid required, >= 0
|
|
* - gameid required, >= 0
|
|
* - spectate=1 sets spectator true; any other value (including absence) is false
|
|
*
|
|
* Credentials in the query (username/password) are intentionally ignored.
|
|
*
|
|
* @return std::nullopt for unrecognised or malformed URLs.
|
|
*/
|
|
inline std::optional<JoinGameUrlParams> parseJoinGameUrl(const QString &url)
|
|
{
|
|
const QUrl parsed(url);
|
|
if (!parsed.isValid())
|
|
return std::nullopt;
|
|
if (parsed.scheme().toLower() != QStringLiteral("cockatrice"))
|
|
return std::nullopt;
|
|
if (parsed.host().toLower() != QStringLiteral("joingame"))
|
|
return std::nullopt;
|
|
|
|
const QUrlQuery query(parsed.query());
|
|
|
|
const QString hostname = query.queryItemValue(QStringLiteral("hostname"));
|
|
if (hostname.isEmpty())
|
|
return std::nullopt;
|
|
|
|
bool portOk = false;
|
|
const uint portVal = query.queryItemValue(QStringLiteral("port")).toUInt(&portOk);
|
|
if (!portOk || portVal == 0 || portVal > 65535)
|
|
return std::nullopt;
|
|
|
|
bool roomOk = false;
|
|
const int roomId = query.queryItemValue(QStringLiteral("roomid")).toInt(&roomOk);
|
|
if (!roomOk || roomId < 0)
|
|
return std::nullopt;
|
|
|
|
bool gameOk = false;
|
|
const int gameId = query.queryItemValue(QStringLiteral("gameid")).toInt(&gameOk);
|
|
if (!gameOk || gameId < 0)
|
|
return std::nullopt;
|
|
|
|
JoinGameUrlParams params;
|
|
params.hostname = hostname;
|
|
params.port = static_cast<quint16>(portVal);
|
|
params.roomId = roomId;
|
|
params.gameId = gameId;
|
|
params.spectator = query.queryItemValue(QStringLiteral("spectate")) == QStringLiteral("1");
|
|
return params;
|
|
}
|
|
|
|
} // namespace UrlUtils
|
|
|
|
#endif // LIBCOCKATRICE_URL_UTILS_H
|