RynthServers/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs
Daniel Wallace 5e732cecb7 RynthDreamWeave: link ChatSource into the plugin, strict alphabet tests, harden the launch stem
The plugin project did not compile (ChatSource not linked). LaunchWire now treats any line starting with the success stem as success-shaped: malformed, it is dropped, never returned as refusal text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:20:38 -08:00

56 lines
2.7 KiB
C#

// The wire text of DreamWeave's website sign-in: the "/weblogin" request and ACE's reply line.
// The code comes from chat; nothing else does. The line that carries it is eaten by the caller
// and the code is never logged, saved or chatted.
// Success: "[WEB] Launch code: <43 chars A-Za-z0-9_-> (single use, expires in 60 seconds)".
// Any other "[WEB] " line is a refusal worded for the player.
using System;
using RynthCore.Plugin.Shared;
namespace RynthCore.Plugin.RynthDreamWeave;
internal static class LaunchWire
{
public const string RequestCommand = "/weblogin";
public const string ReplyLead = "[WEB] ";
public const string SuccessLead = "[WEB] Launch code: ";
/// <summary>Any line starting like this has the success shape: malformed, it is dropped, never shown as a refusal.</summary>
private const string SuccessStem = "[WEB] Launch code";
public const int CodeLength = 43;
public const long ReplyTimeoutMs = 10_000, MinIntervalMs = 3_000;
/// <summary>The chat line to send for a destination. One place to add an argument later.</summary>
public static string RequestFor(WebDestination d) => RequestCommand;
public static bool IsCodeChar(char c)
=> (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_';
/// <summary>
/// True when <paramref name="text"/> is a [WEB] reply from the server: then either
/// <paramref name="code"/> is set (success) or <paramref name="refusal"/> is (a refusal).
/// False for anything else, and for a player's copy of the line. A line that starts like a
/// success but whose code is malformed is false, never a refusal and never a code.
/// </summary>
public static bool TryParse(string? text, int chatType, out string code, out string refusal)
{
code = "";
refusal = "";
if (text == null || ChatSource.IsPlayerAuthored(chatType)) return false;
if (!ChatSource.StartsWithMarker(text, ReplyLead)) return false;
int start = ChatSource.ContentStart(text);
ReadOnlySpan<char> line = text.AsSpan(start);
if (line.StartsWith(SuccessStem.AsSpan(), StringComparison.Ordinal))
{
if (!line.StartsWith(SuccessLead.AsSpan(), StringComparison.Ordinal)) return false;
ReadOnlySpan<char> rest = line.Slice(SuccessLead.Length);
if (rest.Length <= CodeLength || rest[CodeLength] != ' ') return false;
for (int i = 0; i < CodeLength; i++)
if (!IsCodeChar(rest[i])) return false;
code = rest.Slice(0, CodeLength).ToString();
return true;
}
refusal = line.Slice(ReplyLead.Length).ToString();
return true;
}
}