Generalize mail templates from a fixed pair to an operator-managed list

ArchiveKind was a hardcoded two-value enum (ssh_access/config_update),
wired through FilenameParser's regex, MailTemplateSet's two fixed
slots, and SettingsForm's two fixed tabs. Operators wanted arbitrary
custom templates (own file-name suffix, subject, body) without a code
change per new archive kind ssh-access-tui might start producing.

Replaced with MailTemplateDefinition (DisplayName/FileSuffix/Subject/
BodyHtml) in an open-ended list: FilenameParser now accepts any
"{login}_{suffix}.7z" and ArchiveScanner matches the suffix against
the configured list; SendJob.Kind/SendLogEntry.Kind became plain
strings; SettingsForm gained a template list (+/- buttons, editor
panel) replacing the two static tabs. ssh_access/config_update are now
just the two default entries in that list, not special-cased.

Handles the two backward-compat edges that matter for a tool already
in real use: old config.json's object-shaped "templates" migrates to
the new list shape transparently on first load (DataContractJsonSerializer
turned out to silently produce an empty list rather than throw on the
old shape, so detection is structural, not exception-based), and old
send-log.csv rows with PascalCase Kind values (SshAccess/ConfigUpdate)
still dedupe correctly against the new snake_case suffixes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
redlineoandClaude Sonnet 5 committed 2026-07-24 11:26:31 +03:00
1 parent cb3b273741
commit 804b9610ca
16 files changed
+904 -288

No files matched your search

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
@@ -7,9 +9,16 @@ using System.Text;
namespace BulkArchiveMailer.Core
{
/// <summary>One entry of AppSettingsData.Templates - the JSON shape of a MailTemplateDefinition.</summary>
[DataContract]
public sealed class TemplateData
public sealed class MailTemplateData
{
[DataMember(Name = "displayName")]
public string DisplayName { get; set; } = "";
[DataMember(Name = "fileSuffix")]
public string FileSuffix { get; set; } = "";
[DataMember(Name = "subject")]
public string Subject { get; set; } = "";
@@ -17,16 +26,6 @@ namespace BulkArchiveMailer.Core
public string BodyHtml { get; set; } = "";
}
[DataContract]
public sealed class TemplateSetData
{
[DataMember(Name = "ssh_access")]
public TemplateData SshAccess { get; set; } = new TemplateData();
[DataMember(Name = "config_update")]
public TemplateData ConfigUpdate { get; set; } = new TemplateData();
}
/// <summary>Shape of %APPDATA%\BulkArchiveMailer\config.json (and of the embedded config.default.json that seeds it).</summary>
[DataContract]
public sealed class AppSettingsData
@@ -70,38 +69,57 @@ namespace BulkArchiveMailer.Core
[DataMember(Name = "fromAccountSmtpAddress")]
public string FromAccountSmtpAddress { get; set; } = "";
/// <summary>
/// Arbitrary-length list of configured templates - replaces the old
/// fixed ssh_access/config_update pair. Each entry's FileSuffix is
/// expected to be non-empty and unique among the list; enforcing
/// that is SettingsForm's job at save time (see its btnSave_Click),
/// not this class's.
/// </summary>
[DataMember(Name = "templates")]
public TemplateSetData Templates { get; set; } = new TemplateSetData();
public List<MailTemplateData> Templates { get; set; } = new List<MailTemplateData>();
public MailTemplateSet ToTemplateSet()
public List<MailTemplateDefinition> ToTemplateList()
{
return new MailTemplateSet
var list = new List<MailTemplateDefinition>();
if (Templates == null)
{
SshAccess = new MailTemplate
return list;
}
foreach (var t in Templates)
{
list.Add(new MailTemplateDefinition
{
Subject = Templates?.SshAccess?.Subject ?? "",
BodyHtml = Templates?.SshAccess?.BodyHtml ?? "",
},
ConfigUpdate = new MailTemplate
{
Subject = Templates?.ConfigUpdate?.Subject ?? "",
BodyHtml = Templates?.ConfigUpdate?.BodyHtml ?? "",
},
};
DisplayName = t?.DisplayName ?? "",
FileSuffix = t?.FileSuffix ?? "",
Subject = t?.Subject ?? "",
BodyHtml = t?.BodyHtml ?? "",
});
}
return list;
}
public void SetTemplate(ArchiveKind kind, MailTemplate template)
/// <summary>Replaces the whole template list - used by SettingsForm's Save, which always writes back the entire in-memory list rather than one slot at a time (there are no fixed slots anymore).</summary>
public void SetTemplates(List<MailTemplateDefinition> templates)
{
Templates = Templates ?? new TemplateSetData();
var data = new TemplateData { Subject = template?.Subject ?? "", BodyHtml = template?.BodyHtml ?? "" };
if (kind == ArchiveKind.SshAccess)
var list = new List<MailTemplateData>();
if (templates != null)
{
Templates.SshAccess = data;
}
else
{
Templates.ConfigUpdate = data;
foreach (var t in templates)
{
list.Add(new MailTemplateData
{
DisplayName = t?.DisplayName ?? "",
FileSuffix = t?.FileSuffix ?? "",
Subject = t?.Subject ?? "",
BodyHtml = t?.BodyHtml ?? "",
});
}
}
Templates = list;
}
}
@@ -126,6 +144,57 @@ namespace BulkArchiveMailer.Core
{
private const string EmbeddedDefaultResourceName = "BulkArchiveMailer.Core.config.default.json";
/// <summary>
/// Shape of a config.json written by versions v1.0.0-v1.0.4, before
/// ArchiveKind/the fixed ssh_access+config_update pair was replaced
/// by an open-ended template list - back then "templates" was a
/// JSON OBJECT with two named keys, not an array. Private/nested:
/// only ever used as a one-time fallback parse target inside
/// Load()'s migration handling below, never exposed outside this
/// file.
/// </summary>
[DataContract]
private sealed class LegacyTemplateData
{
[DataMember(Name = "subject")]
public string Subject { get; set; } = "";
[DataMember(Name = "bodyHtml")]
public string BodyHtml { get; set; } = "";
}
[DataContract]
private sealed class LegacyTemplateSetData
{
[DataMember(Name = "ssh_access")]
public LegacyTemplateData SshAccess { get; set; } = new LegacyTemplateData();
[DataMember(Name = "config_update")]
public LegacyTemplateData ConfigUpdate { get; set; } = new LegacyTemplateData();
}
[DataContract]
private sealed class LegacyAppSettingsData
{
[DataMember(Name = "archiveFolder")]
public string ArchiveFolder { get; set; } = "";
[DataMember(Name = "emailDomain")]
public string EmailDomain { get; set; } = "redlab.lo";
[DataMember(Name = "testModeDefault")]
public bool TestModeDefault { get; set; } = true;
[DataMember(Name = "signatureHtmlPath")]
public string SignatureHtmlPath { get; set; } = "";
[DataMember(Name = "fromAccountSmtpAddress")]
public string FromAccountSmtpAddress { get; set; } = "";
[DataMember(Name = "templates")]
public LegacyTemplateSetData Templates { get; set; } = new LegacyTemplateSetData();
}
public static string ConfigDirectory =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "BulkArchiveMailer");
@@ -158,6 +227,14 @@ namespace BulkArchiveMailer.Core
/// never silently overwritten - the DataContractJsonSerializer
/// exception propagates so the caller can warn the operator
/// instead of quietly discarding their edits.
///
/// If the file on disk turns out to be in the pre-v1.0.5 legacy
/// shape (object "templates" instead of a list - see
/// LegacyAppSettingsData), Deserialize below transparently
/// migrates it in memory; this method additionally re-persists the
/// migrated result immediately (Save), so every later Load() goes
/// through the fast/primary parse path instead of re-migrating on
/// every single load.
/// </summary>
public static AppSettingsData Load()
{
@@ -168,7 +245,14 @@ namespace BulkArchiveMailer.Core
}
var json = File.ReadAllText(ConfigFilePath, Encoding.UTF8);
return Deserialize(json);
var data = DeserializeWithLegacyFallback(json, out var wasLegacyMigration);
if (wasLegacyMigration)
{
Save(data);
}
return data;
}
public static void Save(AppSettingsData data)
@@ -182,16 +266,134 @@ namespace BulkArchiveMailer.Core
File.WriteAllText(ConfigFilePath, Serialize(data), new UTF8Encoding(false));
}
private static AppSettingsData Deserialize(string json)
/// <summary>
/// Pure (no disk I/O) parse of a config.json's text into
/// AppSettingsData. DataContractJsonSerializer turns out to be far
/// too lenient to detect the legacy shape by catching an exception
/// from a straight `ReadObject(typeof(AppSettingsData))` call -
/// given the old object-shaped "templates", it does NOT throw, it
/// just silently produces an EMPTY List&lt;MailTemplateData&gt;
/// (confirmed while writing the migration test for this method -
/// see AppSettingsTests). So detecting "is this the legacy shape"
/// has to happen explicitly, before deciding which type to parse
/// into - see IsLegacyTemplatesShape below, which inspects the
/// JSON structurally (via the same JsonReaderWriterFactory
/// DataContractJsonSerializer itself uses under the hood) rather
/// than relying on a parse exception that never comes.
/// </summary>
private static AppSettingsData Deserialize(string json) => DeserializeWithLegacyFallback(json, out _);
private static AppSettingsData DeserializeWithLegacyFallback(string json, out bool wasLegacyMigration)
{
wasLegacyMigration = false;
if (IsLegacyTemplatesShape(json))
{
try
{
var legacy = DeserializeAs<LegacyAppSettingsData>(json);
wasLegacyMigration = true;
return ConvertLegacyToCurrent(legacy);
}
catch
{
// Looked legacy-shaped ("templates" is a JSON object)
// but doesn't actually parse as LegacyAppSettingsData
// either - fall through to the normal parse attempt
// below and let ITS exception (if any) describe what's
// actually wrong, rather than masking it with a legacy
// parse failure that was never the real problem.
}
}
return DeserializeAs<AppSettingsData>(json);
}
private static T DeserializeAs<T>(string json)
{
var bytes = Encoding.UTF8.GetBytes(json);
using (var ms = new MemoryStream(bytes))
{
var serializer = new DataContractJsonSerializer(typeof(AppSettingsData));
return (AppSettingsData)serializer.ReadObject(ms);
var serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
}
/// <summary>
/// True if the top-level "templates" member of this JSON text is a
/// JSON OBJECT (the pre-v1.0.5 shape: {"ssh_access": {...},
/// "config_update": {...}}) rather than a JSON ARRAY (the current
/// List&lt;MailTemplateData&gt; shape). Inspects the JSON via
/// JsonReaderWriterFactory/XElement - the same JSON-to-XML mapping
/// DataContractJsonSerializer itself is built on - specifically so
/// this check reflects the real wire shape rather than a hand-rolled
/// regex/text scan. Returns false (never throws) for anything that
/// isn't even parseable as JSON at all; DeserializeWithLegacyFallback's
/// normal AppSettingsData parse attempt is what surfaces a
/// genuinely malformed file's exception to the caller.
/// </summary>
private static bool IsLegacyTemplatesShape(string json)
{
try
{
var bytes = Encoding.UTF8.GetBytes(json ?? "");
using (var reader = JsonReaderWriterFactory.CreateJsonReader(bytes, System.Xml.XmlDictionaryReaderQuotas.Max))
{
var root = System.Xml.Linq.XElement.Load(reader);
var templatesElement = root.Element("templates");
var typeAttr = templatesElement?.Attribute("type");
return typeAttr != null && string.Equals(typeAttr.Value, "object", StringComparison.OrdinalIgnoreCase);
}
}
catch
{
return false;
}
}
/// <summary>
/// Converts a pre-v1.0.5 config.json (ArchiveKind-based, exactly
/// two named templates) into the current shape: two list entries
/// with FileSuffix "ssh_access"/"config_update" (same text the old
/// JSON keys already used) carrying over the operator's saved
/// Subject/BodyHtml. DisplayName did not exist in the old shape, so
/// it is filled in from the embedded defaults instead.
/// </summary>
private static AppSettingsData ConvertLegacyToCurrent(LegacyAppSettingsData legacy)
{
var defaults = LoadDefaults();
string DefaultDisplayName(string suffix) =>
defaults.Templates?.FirstOrDefault(t => string.Equals(t?.FileSuffix, suffix, StringComparison.OrdinalIgnoreCase))?.DisplayName ?? "";
var migrated = new AppSettingsData
{
ArchiveFolder = legacy?.ArchiveFolder ?? "",
EmailDomain = string.IsNullOrEmpty(legacy?.EmailDomain) ? "redlab.lo" : legacy.EmailDomain,
TestModeDefault = legacy?.TestModeDefault ?? true,
SignatureHtmlPath = legacy?.SignatureHtmlPath ?? "",
FromAccountSmtpAddress = legacy?.FromAccountSmtpAddress ?? "",
Templates = new List<MailTemplateData>
{
new MailTemplateData
{
DisplayName = DefaultDisplayName("ssh_access"),
FileSuffix = "ssh_access",
Subject = legacy?.Templates?.SshAccess?.Subject ?? "",
BodyHtml = legacy?.Templates?.SshAccess?.BodyHtml ?? "",
},
new MailTemplateData
{
DisplayName = DefaultDisplayName("config_update"),
FileSuffix = "config_update",
Subject = legacy?.Templates?.ConfigUpdate?.Subject ?? "",
BodyHtml = legacy?.Templates?.ConfigUpdate?.BodyHtml ?? "",
},
},
};
return migrated;
}
private static string Serialize(AppSettingsData data)
{
using (var ms = new MemoryStream())