RynthServers/Plugins/RynthCore.Plugin.RynthILT/IltWiki/WikiAddress.cs

73 lines
3.6 KiB
C#

// WikiAddress.cs - The wiki's address, as the player types it (2026-11-27).
//
// The quest guide's wiki address is a setting with no default: RynthILT ships no wiki address. The
// player types the wiki's address (the site, its index.php or its api.php) and this turns it into the
// MediaWiki API address the guide talks to. Only https: the sign-in goes to it.
using System;
namespace RynthCore.Plugin.RynthILT.IltWiki;
internal static class WikiAddress
{
public const int MaxLength = 300;
/// <summary>
/// The API address for what the player typed: "https://wiki.example/" and "https://wiki.example/index.php"
/// become "https://wiki.example/api.php"; an api.php address stays. Query and fragment dropped. False with
/// <paramref name="error"/> for an empty, non-https or malformed address.
/// </summary>
public static bool TryNormalize(string? typed, out string api, out string error)
{
api = string.Empty;
error = string.Empty;
string t = (typed ?? string.Empty).Trim();
if (t.Length == 0) { error = "type the wiki's address first"; return false; }
if (t.Length > MaxLength) { error = "that address is too long"; return false; }
if (!t.Contains("://", StringComparison.Ordinal)) t = "https://" + t;
if (!Uri.TryCreate(t, UriKind.Absolute, out Uri? uri) || uri.Host.Length == 0)
{
error = "that isn't a web address";
return false;
}
if (uri.Scheme != Uri.UriSchemeHttps)
{
error = "the wiki's address must start with https:// (the sign-in is sent to it)";
return false;
}
if (uri.UserInfo.Length > 0)
{
error = "leave the login out of the address; type it in the login boxes";
return false;
}
string path = uri.AbsolutePath;
if (path.EndsWith("/api.php", StringComparison.OrdinalIgnoreCase)) { }
else if (path.EndsWith("/index.php", StringComparison.OrdinalIgnoreCase)) path = path.Substring(0, path.Length - "index.php".Length) + "api.php";
else path = (path.EndsWith('/') ? path : path + "/") + "api.php";
api = uri.GetLeftPart(UriPartial.Authority) + path;
return true;
}
/// <summary>The address's host ("wiki.example"), or "" for junk.</summary>
public static string HostOf(string? address) =>
Uri.TryCreate(address ?? string.Empty, UriKind.Absolute, out Uri? u) ? u.Host.ToLowerInvariant() : string.Empty;
/// <summary>Two API addresses are the same wiki (scheme, host, port and path, any case).</summary>
public static bool Same(string? a, string? b) =>
Uri.TryCreate(a ?? string.Empty, UriKind.Absolute, out Uri? ua) && Uri.TryCreate(b ?? string.Empty, UriKind.Absolute, out Uri? ub)
&& Uri.Compare(ua, ub, UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) == 0;
/// <summary>The site's robots.txt next to the API ("https://wiki.example/robots.txt").</summary>
public static string RobotsOf(string api) =>
Uri.TryCreate(api, UriKind.Absolute, out Uri? u) ? u.GetLeftPart(UriPartial.Authority) + "/robots.txt" : string.Empty;
/// <summary>The folder-safe form of a host, for the guide's cache file ("wiki.example" -> "wiki.example").</summary>
public static string FileKey(string api)
{
string host = HostOf(api);
var chars = host.ToCharArray();
for (int i = 0; i < chars.Length; i++)
if (!(char.IsAsciiLetterOrDigit(chars[i]) || chars[i] is '.' or '-')) chars[i] = '_';
string s = new string(chars).Trim('.');
return s.Length == 0 ? "wiki" : s;
}
}