Give the telnet window the rest of what N1MM's has

The packet window showed traffic and took a command line, and everything else
about the cluster lived in a dialog under Config. It is now the window N1MM has,
with the same five tabs.

Telnet shows the traffic with spot lines in green, what went out in blue and
lines from a preferred spotter in bold. Double clicking a spot line, or "Jump to
this spot", puts the radio there with the call in the entry window. Scrolling
stops while the pointer is over the traffic. The client keeps the last two
hundred lines, so a window opened mid-contest is not blank.

Clusters keeps the operator's nodes with their ports, passwords and after-login
commands, connects and disconnects, and holds the logon settings. Download
fetches the published list of telnet nodes from NG3K — around fifty, with the
sysop's call and a note about each — and clicking one fills the boxes in. N1MM
downloads its list from its own web service, which asks the operator to opt in
to data collection and is N1MM's to run, so this reads a public page instead.
ClusterList takes any page with telnet:// links in a table; a page it cannot
read leaves the stored list alone.

Filters decide which spots reach the bandmap: bands, modes, beacons, busted
calls, stations outside the call history file, blacklisted spotters and calls,
spots from outside your country, continent or a list of prefixes, and how long a
spot stays on the map. A busted spot is a call the callsign database has never
heard that is one character away from one it knows; a call nothing resembles is
kept, because that is what a new station looks like. Nothing is filtered out of
the traffic itself — the operator sees everything the node sends.

Buttons edits the twelve command buttons. A button takes what N1MM's takes: the
message macros, several commands separated by semicolons, or {CONN} and the name
of a favourite, which connects to that node instead of sending anything. The
label takes the macros too. Right-clicking a button opens the editor.

Config ▸ Cluster now opens this window on the Clusters tab rather than a dialog
of its own, which is where N1MM keeps those settings.

Three things that could take the program down while a cluster was connected:

Settings.Load read a null where the property is not nullable. A file that names
a key with a null value — one written before the property existed and then
edited — put that null straight through, because the property's own default only
runs when the key is missing. Opening the telnet window then threw on the first
list it touched. Every null is now put back to the default the property
declares, walking into the stored records and the lists of them.

Bandmap was written from the cluster's thread and read from the window's, so a
dictionary could be modified while a window enumerated it. Every method locks
now.

ClusterClient disposed its token source while its own loop still used it, and
the retry delay sat outside the catch, so a disconnect faulted the loop task.

Along the way the message macros were checked against N1MM's function-key
documentation, and several were wrong. {LOGGEDCALL} is N1MM's {LASTCALL}, the
serial is #, and there is no {MYZONE}; {NAME} and {GRIDSQUARE} stand for the
other station's name and grid, not ours; {OTHERMHZ} is the radio the operator is
not on. The single-character macros * and ! were missing. Added from the same
table: {LASTCALL}, {PREVNR}, {NAMEANDSPACE}, {CHNAME}, {GRID}, the two grid
bearings and the grid distance, {FREQ}, {FREQROUND}, the other-radio
frequencies, {TIMESTAMP} and {TIME2}. Frequencies are formatted the way N1MM
formats them, with R for the decimal point on CW. The macros that pass a station
to the other band take the second radio as a new argument, and stand for nothing
at a one-radio station.

Left out, and written down: saving spots to a database, which N1MM keeps in its
admin database rather than in the log file the two programs share; the
special-calls list; the two-character busted check, which is a few hundred
thousand lookups per spot against a few hundred for one character; and N1MM's
action macros, which need a different shape than an expander that returns a
string.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-28 07:33:55 +00:00
parent 648da1918a
commit d9f9d880f2
40 changed files with 2278 additions and 272 deletions

View File

@@ -0,0 +1,291 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Threading;
using Nonemm.App.Configuration;
using Nonemm.Core;
using Nonemm.Session;
using Nonemm.Spotting;
namespace Nonemm.App.Windows;
/// The cluster node's traffic as it arrived, a line to send commands on, and
/// the tabs that say which node to talk to and which spots to keep. Double
/// clicking a spot line puts the radio on it.
public sealed partial class TelnetWindow : RefreshableWindow
{
private const int LinesKept = 500;
private static readonly IBrush SpotColour = Verdicts.NewMultiplier;
private static readonly IBrush SentColour = Verdicts.Worth;
private static readonly IBrush NoticeColour = Verdicts.Dupe;
private readonly AppSession session;
private readonly Action<Frequency, string> tune;
private readonly List<string> history = [];
private int recalled = -1;
private bool paused;
public TelnetWindow(AppSession session, Action<Frequency, string> tune)
{
this.session = session;
this.tune = tune;
InitializeComponent();
LoadButtons();
LoadButtonRows();
LoadClusters();
LoadFilters();
Listen(session.Cluster);
Closed += (_, _) => Forget(session.Cluster);
Refresh();
}
/// Config ▸ Cluster opens the window here, because this is where the node
/// is chosen.
public void ShowClusters() => Tabs.SelectedIndex = 1;
public void ShowButtons() => Tabs.SelectedIndex = 3;
public override void Refresh()
{
ClusterClient? cluster = session.Cluster;
StateText.Text = cluster is null
? "not connected — pick a node on the Clusters tab"
: cluster.IsConnected
? $"connected to {cluster.Host}:{cluster.Port}"
: $"connecting to {cluster.Host}:{cluster.Port}…";
ReconnectButton.Content = cluster is null ? "Connect" : "Reconnect";
}
/// The traffic so far is put up first, so a window opened mid-contest is
/// not blank. Taking it before subscribing keeps a line from appearing
/// twice.
private void Listen(ClusterClient? cluster)
{
if (cluster is null)
{
return;
}
IReadOnlyList<ClusterLine> already = cluster.Recent;
cluster.LineArrived += OnLineArrived;
cluster.LineSent += OnLineSent;
cluster.ConnectionChanged += OnConnectionChanged;
foreach (ClusterLine line in already)
{
if (line.WasSent)
{
OnLineSent(cluster, line.Text);
}
else
{
OnLineArrived(cluster, line.Text);
}
}
}
private void Forget(ClusterClient? cluster)
{
if (cluster is null)
{
return;
}
cluster.LineArrived -= OnLineArrived;
cluster.LineSent -= OnLineSent;
cluster.ConnectionChanged -= OnConnectionChanged;
}
private void OnLineArrived(object? sender, string line)
{
if (line.StartsWith("***", StringComparison.Ordinal))
{
Show(line, NoticeColour);
return;
}
if (SpotLine.Parse(line, DateTime.UtcNow) is not { } spot)
{
Show(line, Foreground);
return;
}
Show(line, SpotColour, IsPreferred(spot.Spotter));
}
/// A spotter the operator listed on the Filters tab. N1MM matches on the
/// start of the call, so `W3LPL` also covers `W3LPL-#`.
private bool IsPreferred(string spotter) =>
session.Settings.SpotFilter.PreferredSpotters.Any(one =>
one.Length > 0 && spotter.StartsWith(one, StringComparison.OrdinalIgnoreCase));
private void OnLineSent(object? sender, string line) => Show($"> {line}", SentColour);
private void OnConnectionChanged(object? sender, bool connected) =>
Dispatcher.UIThread.Post(Refresh);
/// `strong` marks the lines from a preferred spotter.
private void Show(string line, IBrush? colour, bool strong = false) => Dispatcher.UIThread.Post(() =>
{
while (Traffic.Items.Count >= LinesKept)
{
Traffic.Items.RemoveAt(0);
}
Traffic.Items.Add(new TextBlock
{
Text = line,
Foreground = colour,
FontWeight = strong ? FontWeight.Bold : FontWeight.Normal,
});
if (!paused && Traffic.Items.Count > 0)
{
Traffic.ScrollIntoView(Traffic.Items.Count - 1);
}
Refresh();
});
/// Scrolling stops while the pointer is over the traffic, so a line can be
/// read or copied while the node keeps sending.
private void OnTrafficEntered(object? sender, PointerEventArgs e)
{
paused = true;
PausedText.IsVisible = true;
}
private void OnTrafficExited(object? sender, PointerEventArgs e)
{
paused = false;
PausedText.IsVisible = false;
if (Traffic.Items.Count > 0)
{
Traffic.ScrollIntoView(Traffic.Items.Count - 1);
}
}
/// The label takes the macros as well, expanded once here, which is what
/// N1MM does when it loads its buttons.
private void LoadButtons()
{
ButtonRow.Children.Clear();
foreach (StoredTelnetButton stored in Buttons())
{
Button button = new()
{
Content = Expanded(stored.Label),
FontSize = 11,
Padding = new Avalonia.Thickness(6, 3),
Margin = new Avalonia.Thickness(0, 0, 3, 3),
Tag = stored,
};
ToolTip.SetTip(button, $"{stored.Command}\nright-click to edit the buttons");
button.Click += OnButtonPressed;
button.PointerReleased += OnButtonRightClick;
ButtonRow.Children.Add(button);
}
}
/// Right-clicking a button opens the editor, as it does in N1MM.
private void OnButtonRightClick(object? sender, PointerReleasedEventArgs e)
{
if (e.InitialPressMouseButton == MouseButton.Right)
{
ShowButtons();
}
}
private IReadOnlyList<StoredTelnetButton> Buttons() =>
session.Settings.TelnetButtons.Count > 0
? session.Settings.TelnetButtons
: StoredTelnetButton.Default;
/// A button's text is N1MM's: the message macros, and `{CONN}node` to
/// connect to a favourite instead of sending anything. Several commands are
/// separated by semicolons and go out one after the other.
private async void OnButtonPressed(object? sender, RoutedEventArgs e)
{
if (sender is not Button { Tag: StoredTelnetButton stored })
{
return;
}
if (ConnectToFavourite(stored.Command))
{
return;
}
foreach (string command in Expanded(stored.Command).Split(';', StringSplitOptions.RemoveEmptyEntries))
{
await SendAsync(command.Trim());
}
}
/// The same macros the function keys take. With no contest open there is
/// nothing to fill them from, so the text goes out as it was typed.
private string Expanded(string text) =>
session.Position is { } position
? MessageExpander.Expand(text, position, session.Other(position))
: text;
private async void OnSend(object? sender, RoutedEventArgs e)
{
if (CommandBox.Text is not { Length: > 0 } command)
{
return;
}
history.Add(command);
recalled = -1;
CommandBox.Text = "";
// what is typed goes out as it was typed, the way N1MM sends it
await SendAsync(command);
}
/// Up and down walk back through what has been typed, the way a terminal
/// does.
private void OnCommandKey(object? sender, KeyEventArgs e)
{
if (history.Count == 0 || (e.Key != Key.Up && e.Key != Key.Down))
{
return;
}
recalled = e.Key == Key.Up
? Math.Min(recalled + 1, history.Count - 1)
: Math.Max(recalled - 1, -1);
CommandBox.Text = recalled < 0 ? "" : history[history.Count - 1 - recalled];
CommandBox.CaretIndex = CommandBox.Text.Length;
e.Handled = true;
}
private async Task SendAsync(string command)
{
if (session.Cluster is not { } cluster)
{
Show("*** not connected to a node", NoticeColour);
return;
}
await cluster.SendAsync(command);
}
private void OnReconnect(object? sender, RoutedEventArgs e) => Connect();
/// Tunes the radio to the spot on the selected line, which is what N1MM's
/// "Jump to this spot" does.
private void OnJumpToSpot(object? sender, RoutedEventArgs e)
{
if (Traffic.SelectedItem is not TextBlock { Text: { } line })
{
return;
}
if (SpotLine.Parse(line, DateTime.UtcNow) is not { } spot)
{
Show("*** that line is not a spot", NoticeColour);
return;
}
tune(spot.Frequency, spot.Call.Text);
}
private async void OnCopyLine(object? sender, RoutedEventArgs e)
{
if (Traffic.SelectedItem is TextBlock { Text: { } line } && Clipboard is { } clipboard)
{
await clipboard.SetTextAsync(line);
}
}
private void OnClearTraffic(object? sender, RoutedEventArgs e) => Traffic.Items.Clear();
}