Parse network on a chatTabs entry

kind must be dereth, command optional (default dn, 1-32 of a-z0-9); a bad
network is dropped and noted in the parse error, the entry is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-15 10:20:33 -08:00
commit fea33eb41b
2 changed files with 66 additions and 5 deletions

View file

@ -74,7 +74,11 @@
// Roleplay, the room the Dereth Network README names as usual). An
// entry with a bad field is dropped and reported in the parse error (the
// other keys still parse); at most MaxChatTabs entries of MaxChatRules
// rules each. Engines before this ignore the key.
// rules each. An entry may add "network": { "kind": "dereth",
// "command": "dn" } (the chat network the tab belongs to): "kind" must be
// dereth, "command" is optional (default dn) and 1-32 of a-z and 0-9; a
// bad network is dropped and reported but the entry is kept. Engines
// before this ignore the key.
//
// Everything from a plugin is checked here: ids are short lower-case words,
// text is capped, a command must be a single "/..." chat command, and the lists
@ -163,7 +167,14 @@ internal sealed record SectionWindowDecl(
internal sealed record SectionChatRuleDecl(string Action, string When, string Mode, string Pattern, bool GlobalRoom);
/// <summary>A default chat tab a server's plugin declares ("chatTabs", 2026-11-15); owner key = Server + "/" + Id.</summary>
internal sealed record SectionChatTabDecl(string Server, string Id, string Name, bool Pinned, string GlobalRoomFallback, SectionChatRuleDecl[] Rules);
internal sealed record SectionChatTabDecl(string Server, string Id, string Name, bool Pinned, string GlobalRoomFallback, SectionChatRuleDecl[] Rules)
{
/// <summary>The chat network this tab belongs to ("network", 2026-11-15), or null.</summary>
public SectionNetworkDecl? Network { get; init; }
}
/// <summary>A chat network a chatTabs entry declares: Kind is "dereth"; Command is the mod's chat command word (no slash).</summary>
internal sealed record SectionNetworkDecl(string Kind, string Command);
/// <summary>One plugin's parsed manifest.</summary>
internal sealed record SectionManifest(
@ -308,8 +319,9 @@ internal static class SectionManifestParser
foreach (JsonElement e in Array(root, "chatTabs"))
{
if (chatTabs.Count >= MaxChatTabs) { Note(ref error, $"chatTabs: more than {MaxChatTabs} entries, the rest dropped"); break; }
SectionChatTabDecl? decl = ChatTab(e, out string why);
SectionChatTabDecl? decl = ChatTab(e, out string why, out string note);
if (decl == null) { Note(ref error, "chatTabs: entry dropped, " + why); continue; }
if (note.Length > 0) Note(ref error, "chatTabs: " + decl.Server + "/" + decl.Id + " " + note);
if (chatTabs.Exists(c => c.Server == decl.Server && c.Id == decl.Id)) { Note(ref error, "chatTabs: duplicate " + decl.Server + "/" + decl.Id); continue; }
chatTabs.Add(decl);
}
@ -334,9 +346,10 @@ internal static class SectionManifestParser
}
/// <summary>One chatTabs entry, or null (with the reason) when a field is bad.</summary>
private static SectionChatTabDecl? ChatTab(JsonElement e, out string why)
private static SectionChatTabDecl? ChatTab(JsonElement e, out string why, out string note)
{
why = string.Empty;
note = string.Empty;
string server = Id(Str(e, "server"));
if (server.Length == 0) { why = "missing or bad server"; return null; }
string id = Id(Str(e, "id"));
@ -363,7 +376,24 @@ internal static class SectionManifestParser
string pattern = globalRoom ? string.Empty : Pattern(Str(r, "pattern"), MaxTextLength);
rules.Add(new SectionChatRuleDecl(action, when, mode, pattern, globalRoom));
}
return new SectionChatTabDecl(server, id, name, Bool(e, "pinned", false), room, rules.ToArray());
SectionNetworkDecl? network = null;
if (e.TryGetProperty("network", out JsonElement net))
{
string kind = net.ValueKind == JsonValueKind.Object ? Str(net, "kind").Trim().ToLowerInvariant() : string.Empty;
string command = net.ValueKind == JsonValueKind.Object && net.TryGetProperty("command", out _) ? Str(net, "command").Trim() : "dn";
if (kind != "dereth") note = "network dropped, kind is not dereth";
else if (!NetworkCommandValid(command)) note = "network dropped, command is not 1-32 of a-z and 0-9";
else network = new SectionNetworkDecl(kind, command);
}
return new SectionChatTabDecl(server, id, name, Bool(e, "pinned", false), room, rules.ToArray()) { Network = network };
}
/// <summary>A network command word: 1-32 characters of a-z and 0-9.</summary>
private static bool NetworkCommandValid(string s)
{
if (s.Length is < 1 or > 32) return false;
foreach (char c in s) if (!(c is >= 'a' and <= 'z' or >= '0' and <= '9')) return false;
return true;
}
/// <summary>A chat rule's when, as RynthChat reads it: "", "chan:&lt;tab&gt;" or "type:&lt;number &gt;= 0&gt;".</summary>

View file

@ -35,6 +35,7 @@ internal static class Program
WindowFrameless();
ManifestBarMenu();
ManifestChatTabs();
ManifestChatTabNetwork();
BarMenuHoverTiming();
BarMenuReload();
StackCheckAfterPluginDraw();
@ -668,6 +669,36 @@ internal static class Program
"17 rules: entry dropped, error set");
}
private static void ManifestChatTabNetwork()
{
const string head = "{ \"chatTabs\": [ { \"server\": \"dw\", \"id\": \"a\", \"name\": \"A\", \"rules\": []";
SectionManifest m = SectionManifestParser.Parse("DW", head + ", \"network\": { \"kind\": \"dereth\", \"command\": \"derethnetwork\" } } ] }", out string err);
Case("manifest: chatTabs network present");
Check(err.Length == 0 && m.ChatTabs.Length == 1 && m.ChatTabs[0].Network == new SectionNetworkDecl("dereth", "derethnetwork"), "kind and command parse: " + err);
Case("manifest: chatTabs network absent");
m = SectionManifestParser.Parse("DW", head + " } ] }", out err);
Check(err.Length == 0 && m.ChatTabs.Length == 1 && m.ChatTabs[0].Network == null, "no network key = null: " + err);
Case("manifest: chatTabs network bad kind");
m = SectionManifestParser.Parse("DW", head + ", \"network\": { \"kind\": \"irc\", \"command\": \"dn\" } } ] }", out err);
Check(m.ChatTabs.Length == 1 && m.ChatTabs[0].Network == null && err.Contains("network"), "key dropped, entry kept, error notes it: " + err);
Case("manifest: chatTabs network bad command");
string[] bad = { "Dn", "d n", "/dn", "", new string('a', 33) };
for (int i = 0; i < bad.Length; i++)
{
m = SectionManifestParser.Parse("DW", head + ", \"network\": { \"kind\": \"dereth\", \"command\": \"" + bad[i] + "\" } } ] }", out err);
Check(m.ChatTabs.Length == 1 && m.ChatTabs[0].Network == null && err.Contains("network"), "key dropped, entry kept, error notes it: '" + bad[i] + "'");
}
m = SectionManifestParser.Parse("DW", head + ", \"network\": { \"kind\": \"dereth\", \"command\": \"" + new string('a', 32) + "\" } } ] }", out err);
Check(err.Length == 0 && m.ChatTabs[0].Network?.Command.Length == 32, "32 characters is allowed");
Case("manifest: chatTabs network default command");
m = SectionManifestParser.Parse("DW", head + ", \"network\": { \"kind\": \"dereth\" } } ] }", out err);
Check(err.Length == 0 && m.ChatTabs[0].Network == new SectionNetworkDecl("dereth", "dn"), "absent command = dn: " + err);
}
private static void ManifestBarMenu()
{
Case("plugin windows: barMenu (v95)");