From 9cc8e3eb4858a4eca3071f40a13259c45ece940a Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:16 -0400 Subject: [PATCH 01/14] only answer CAP subcommands that expect a reply Every CAP subcommand got "CAP * LS", including CAP END. Some clients answer an unsolicited LS with another CAP END, so the two loop forever. Reply to LS/LIST, NAK any REQ, and stay silent on END. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands.go | 13 +++++++++++-- irc/commands_test.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/irc/commands.go b/irc/commands.go index 2988a57..5ba5ec8 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -35,8 +35,17 @@ var cmdSet = map[string]serverCommand{ } func capabilities(s *Server, user *User, args []string) (quit bool) { - // We don't support much... - s.reply(user, "CAP", "*", "LS", "") + // We don't support any capabilities. Only answer the subcommands that + // expect a reply; answering CAP END makes some clients loop. + if len(args) < 2 { + return + } + switch sub := strings.ToUpper(args[1]); sub { + case "LS", "LIST": + s.reply(user, "CAP", replyNick(user), sub, "") + case "REQ": + s.reply(user, "CAP", replyNick(user), "NAK", strings.Join(args[2:], " ")) + } return } diff --git a/irc/commands_test.go b/irc/commands_test.go index 82d2e40..8fe44e1 100644 --- a/irc/commands_test.go +++ b/irc/commands_test.go @@ -175,3 +175,20 @@ func TestNickChangeBroadcastsToChannelMembers(t *testing.T) { t.Fatalf("other nick-change message = %q, want %q", otherOut.String(), want) } } + +func TestCapEndGetsNoReply(t *testing.T) { + server := NewServer() + server.Name = "hamirc" + var out bytes.Buffer + user := NewUser("", &out) + + server.handleCommand(user, "CAP LS 302") + if want := ":hamirc CAP * LS :\r\n"; out.String() != want { + t.Fatalf("CAP LS reply = %q, want %q", out.String(), want) + } + out.Reset() + server.handleCommand(user, "CAP END") + if out.Len() != 0 { + t.Fatalf("CAP END got reply %q", out.String()) + } +} From 8cb1c051cd65aad30fe70b1889b8b378381fd1af Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:18 -0400 Subject: [PATCH 02/14] always echo NICK changes to the user The NICK message only went to channel members, so a user in no channels never learned their new nick. Their client then ignored its own JOIN and showed the NAMES list without the channel. Also allow case-only nick changes: the user collided with themselves, and the channel map update deleted the key it had just written. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands_test.go | 30 ++++++++++++++++++++++++++++++ irc/server.go | 8 +++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/irc/commands_test.go b/irc/commands_test.go index 8fe44e1..03f38a7 100644 --- a/irc/commands_test.go +++ b/irc/commands_test.go @@ -192,3 +192,33 @@ func TestCapEndGetsNoReply(t *testing.T) { t.Fatalf("CAP END got reply %q", out.String()) } } + +func TestNickChangeOutsideChannelsEchoesToUser(t *testing.T) { + server := NewServer() + var out bytes.Buffer + user := NewUser("Maple", &out) + user.local = true + server.Users[nickKey(user.Nick)] = user + + nick(server, user, []string{"NICK", "Cedar"}) + if want := ":Maple NICK :Cedar\r\n"; out.String() != want { + t.Fatalf("nick-change message = %q, want %q", out.String(), want) + } +} + +func TestNickCaseChangeKeepsChannelMembership(t *testing.T) { + server := NewServer() + user := NewUser("Maple", &bytes.Buffer{}) + user.local = true + server.Users[nickKey(user.Nick)] = user + channel := server.Channel("#hamirc") + channel.Users[nickKey(user.Nick)] = user + + nick(server, user, []string{"NICK", "MAPLE"}) + if user.Nick != "MAPLE" { + t.Fatalf("nick = %q, want MAPLE", user.Nick) + } + if channel.Users[nickKey(user.Nick)] != user { + t.Fatal("case-only nick change dropped user from channel") + } +} diff --git a/irc/server.go b/irc/server.go index 7629685..69f5b40 100644 --- a/irc/server.go +++ b/irc/server.go @@ -533,7 +533,7 @@ func (s *Server) changeNick(user *User, newNick string) { // Check if the new nickname is already in use // allow person to snag a remote user though existingUser, ok := s.Users[newNickLower] - if ok && existingUser.Local() { + if ok && existingUser != user && existingUser.Local() { s.Unlock() s.reply(user, ERR_NICKNAMEINUSE, replyNick(user), newNick, "Nickname is already in use") return @@ -542,7 +542,9 @@ func (s *Server) changeNick(user *User, newNick string) { // Update the server's user list oldNick := user.Nick user.Nick = newNick - var recipients []*User + // The user must always see their own NICK, or their client keeps the + // old nick and won't recognize its own JOINs. + recipients := []*User{user} if oldNickLower != "" { delete(s.Users, oldNickLower) s.Users[newNickLower] = user @@ -552,8 +554,8 @@ func (s *Server) changeNick(user *User, newNick string) { for _, ch := range s.Channels { if _, ok := ch.Users[oldNickLower]; ok { - ch.Users[newNickLower] = user delete(ch.Users, oldNickLower) + ch.Users[newNickLower] = user for _, chUser := range ch.Users { recipients = append(recipients, chUser) } From 7865ddf942c0696724f35f1b89f8cb47bb91179b Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:19 -0400 Subject: [PATCH 03/14] serialize writes to a user connection User is written from its own connection goroutine, the TNC goroutine and other users' goroutines, but bufio.Writer is not safe for that. NAMES and USERHOST were also written in pieces, so a concurrent line could splice into the middle of them. Lock User.Write and write each line in one call. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 15 +++++---------- irc/user.go | 6 +++++- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/irc/server.go b/irc/server.go index 69f5b40..433dc64 100644 --- a/irc/server.go +++ b/irc/server.go @@ -699,12 +699,8 @@ func (s *Server) joinChannel(user *User, channelName string) { s.reply(user, RPL_TOPIC, user.Nick, channelName, topic) } - fmt.Fprintf(user, ":%s 353 %s = %s :", s.Name, user.Nick, channelName) - - for _, name := range names { - fmt.Fprintf(user, "%s ", name) - } - fmt.Fprintf(user, "\r\n") + // one Write per line so concurrent writers can't splice into it + fmt.Fprintf(user, ":%s 353 %s = %s :%s\r\n", s.Name, user.Nick, channelName, strings.Join(names, " ")) s.reply(user, RPL_ENDOFNAMES, user.Nick, channelName, "End of /NAMES list") } @@ -735,16 +731,15 @@ func (s *Server) userHost(user *User, nicks []string) { defer s.Unlock() //:irc.example.com 302 Sparques :Nick1=-user1@host1 Nick2=+user2@host2 - fmt.Fprintf(user, ":%s 302 %s :", s.Name, user.Nick) - + var replies []string for _, nick := range nicks { u, ok := s.Users[nickKey(nick)] if !ok { continue } - fmt.Fprintf(user, "%s=-%s@%s ", nick, u.Callsign, strings.ReplaceAll(u.RealName, " ", "_")) + replies = append(replies, fmt.Sprintf("%s=-%s@%s", nick, u.Callsign, strings.ReplaceAll(u.RealName, " ", "_"))) } - fmt.Fprintf(user, "\r\n") + fmt.Fprintf(user, ":%s 302 %s :%s\r\n", s.Name, user.Nick, strings.Join(replies, " ")) } func (s *Server) quit(user *User, reason string) { diff --git a/irc/user.go b/irc/user.go index ddc9b54..361b675 100644 --- a/irc/user.go +++ b/irc/user.go @@ -7,6 +7,7 @@ import ( "io" "net" "strings" + "sync" "time" ) @@ -22,7 +23,8 @@ type User struct { conn net.Conn local bool - buf *bufio.Writer + bufMu sync.Mutex // User is written from several goroutines + buf *bufio.Writer } func NewUser(nick string, wr io.Writer) *User { @@ -35,6 +37,8 @@ func NewUser(nick string, wr io.Writer) *User { // Write writes to the user, in discrete lines, buffering if we did // not get a line-feed. func (u *User) Write(buf []byte) (n int, err error) { + u.bufMu.Lock() + defer u.bufMu.Unlock() var n2 int for len(buf) > 0 { i := bytes.Index(buf, []byte{'\n'}) From e067b074b8cf5d6adead0d0c93b52c03c5eaad84 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:21 -0400 Subject: [PATCH 04/14] make user IDs parse with any real name Parse required exactly three fields split on '!' and '@'. A real name that was empty or contained '@' or '!' made receivers drop every transmission from that user. Split on the first '!' and '@' instead, and have ID() replace '!'/'@' and use '*' for an empty host so stations that have not updated can still parse it. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands_test.go | 2 +- irc/server_test.go | 20 +++++++++++++++++--- irc/user.go | 24 +++++++++++++----------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/irc/commands_test.go b/irc/commands_test.go index 03f38a7..56770d9 100644 --- a/irc/commands_test.go +++ b/irc/commands_test.go @@ -84,7 +84,7 @@ func TestPartBroadcastsAndRemovesUser(t *testing.T) { if _, ok := channel.Users[nickKey(sender.Nick)]; ok { t.Fatal("sender was not removed from channel") } - want := ":Maple!N0CALL@ PART #hamirc :done\r\n" + want := ":Maple!N0CALL@* PART #hamirc :done\r\n" if senderOut.String() != want { t.Fatalf("sender PART message = %q, want %q", senderOut.String(), want) } diff --git a/irc/server_test.go b/irc/server_test.go index e74c240..6bd64de 100644 --- a/irc/server_test.go +++ b/irc/server_test.go @@ -295,7 +295,7 @@ func TestSendLocalMessageWithoutTNCStillDeliversLocally(t *testing.T) { server.send(sender, "NOTICE", "Birch", "copy") - want := ":Maple!N0CALL@ NOTICE Birch :copy\r\n" + want := ":Maple!N0CALL@* NOTICE Birch :copy\r\n" if recipientOut.String() != want { t.Fatalf("recipient = %q, want %q", recipientOut.String(), want) } @@ -332,7 +332,7 @@ func TestJoinChannelBroadcastsAndSendsNames(t *testing.T) { server.joinChannel(joining, "#hamirc") - joinLine := ":Maple!N0CALL@ JOIN :#hamirc\r\n" + joinLine := ":Maple!N0CALL@* JOIN :#hamirc\r\n" if !strings.Contains(joiningOut.String(), joinLine) { t.Fatalf("joining user did not receive JOIN line: %q", joiningOut.String()) } @@ -368,7 +368,7 @@ func TestPartChannelBroadcastsAndRemovesRemoteUser(t *testing.T) { t.Fatal("remote user was not removed from channel") } - want := ":Maple!N0CALL@ PART #hamirc :73\r\n" + want := ":Maple!N0CALL@* PART #hamirc :73\r\n" if remoteOut.String() != want { t.Fatalf("remote PART message = %q, want %q", remoteOut.String(), want) } @@ -509,3 +509,17 @@ func TestListChannelsIncludesClientOnStartAndEnd(t *testing.T) { t.Fatalf("LIST end reply missing client nick: %q", got) } } + +func TestUserIDRoundTripsThroughParse(t *testing.T) { + for _, realName := range []string{"", "Joe joe@example.com", "Hi!"} { + sent := NewUser("Maple", io.Discard) + sent.Callsign = "N0CALL" + sent.RealName = realName + + got := NewUser("", io.Discard) + got.Parse(sent.ID()) + if got.Nick != "Maple" || got.Callsign != "N0CALL" { + t.Fatalf("Parse(%q) = nick %q callsign %q", sent.ID(), got.Nick, got.Callsign) + } + } +} diff --git a/irc/user.go b/irc/user.go index 361b675..ea891a6 100644 --- a/irc/user.go +++ b/irc/user.go @@ -64,7 +64,13 @@ func (u *User) Write(buf []byte) (n int, err error) { // ID generates a user id in the form of !@. ID converts spaces to underscores // for Real Name. func (u *User) ID() string { - return fmt.Sprintf("%s!%s@%s", u.Nick, u.Callsign, strings.Join(strings.Fields(u.RealName), "_")) + // Receivers need exactly one '!' and one '@' and a non-empty host. + host := strings.Join(strings.Fields(u.RealName), "_") + host = strings.NewReplacer("!", "_", "@", "_").Replace(host) + if host == "" { + host = "*" + } + return fmt.Sprintf("%s!%s@%s", u.Nick, u.Callsign, host) } // Parse breaks apart a nick!user@host identifier into its constituent parts and @@ -75,20 +81,16 @@ func (u *User) ID() string { func (u *User) Parse(id string) { // More rigorous nick/id validation can be added if malformed traffic // becomes a practical issue. - fields := strings.FieldsFunc(id, func(r rune) bool { - if r == '!' || r == '@' { - return true - } - return false - }) + nick, rest, ok1 := strings.Cut(id, "!") + callsign, realName, ok2 := strings.Cut(rest, "@") // don't populate anything if we can't do this simple bit of parsing // chances are we were sent junk or the message was garbled - if len(fields) != 3 { + if !ok1 || !ok2 || nick == "" || callsign == "" { return } - u.Nick = fields[0] - u.Callsign = fields[1] - u.RealName = fields[2] + u.Nick = nick + u.Callsign = callsign + u.RealName = realName } // Local returns true if the user is connected via TCP. This is used From ef5ad10edfd8aca4080a727be791fcd8458c1ba7 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:22 -0400 Subject: [PATCH 05/14] don't purge local users from channels userPurge compared LastSeen, which stays zero for a local user who has not spoken, so idle local users were removed from channels within an hour while their client still showed them joined. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/irc/server.go b/irc/server.go index 433dc64..b6449e4 100644 --- a/irc/server.go +++ b/irc/server.go @@ -117,7 +117,9 @@ func (s *Server) userPurge() { for _, ch := range s.Channels { ch.Lock() for nick, user := range ch.Users { - if time.Since(user.LastSeen) > s.UserPurgeAfter { + // local users stay until they part or disconnect; their client + // still shows them as joined + if !user.Local() && time.Since(user.LastSeen) > s.UserPurgeAfter { delete(ch.Users, nick) } } From 29f5c394f9a5c715da87a2b2adbe09a09ac49c54 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:24 -0400 Subject: [PATCH 06/14] keep radio users separate from local users with the same nick A radio message whose nick matched a local user was attributed to the local user: it was hidden from them and retransmitted, which could loop between two stations using the same nick. Leave the radio user untracked in that case, and compare users by pointer in send() and partChannel(). Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 22 +++++++++++----------- irc/server_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/irc/server.go b/irc/server.go index b6449e4..f647b3a 100644 --- a/irc/server.go +++ b/irc/server.go @@ -333,17 +333,16 @@ func (s *Server) handleTNC() { continue } // add user to server if not previously seen - if existingUser := s.Nick(incomingUser.Nick); existingUser == nil { - s.Lock() - if existingUser = s.Users[nickKey(incomingUser.Nick)]; existingUser == nil { - s.Users[nickKey(incomingUser.Nick)] = incomingUser - } else { - incomingUser = existingUser - } - s.Unlock() - } else { + s.Lock() + if existingUser := s.Users[nickKey(incomingUser.Nick)]; existingUser == nil { + s.Users[nickKey(incomingUser.Nick)] = incomingUser + } else if !existingUser.Local() { incomingUser = existingUser } + // If a local user holds this nick, the radio user stays untracked. + // Reusing the local user would make the message look like ours and + // send() would retransmit it. + s.Unlock() // do user-level ban check here? @@ -638,7 +637,7 @@ func (s *Server) send(sender *User, cmd, target, msg string) { return } for _, u := range ch.Users { - if u.Nick == sender.Nick && cmd != "PART" { + if u == sender && cmd != "PART" { continue } if slices.Contains(u.partedChannels, target) { @@ -713,7 +712,8 @@ func (s *Server) partChannel(user *User, channelName, reason string) bool { s.Unlock() return false } - if _, ok := ch.Users[nickKey(user.Nick)]; !ok { + // compare pointers: a local user may share a nick with a radio user + if ch.Users[nickKey(user.Nick)] != user { s.Unlock() return false } diff --git a/irc/server_test.go b/irc/server_test.go index 6bd64de..a6b0e8e 100644 --- a/irc/server_test.go +++ b/irc/server_test.go @@ -523,3 +523,22 @@ func TestUserIDRoundTripsThroughParse(t *testing.T) { } } } + +func TestHandleTNCNickCollisionDeliversToLocalUser(t *testing.T) { + server := NewServer() + var out bytes.Buffer + local := NewUser("Maple", &out) + local.local = true + local.Callsign = "N0CALL" + server.Users[nickKey(local.Nick)] = local + server.Channel("#hamirc").Users[nickKey(local.Nick)] = local + + runSingleTNCFrame(t, server, ":Maple!K1ABC@Far_Away PRIVMSG #hamirc :hello") + + if server.Nick("Maple") != local { + t.Fatal("radio user replaced local user") + } + if want := ":Maple!K1ABC@Far_Away PRIVMSG #hamirc :hello\r\n"; out.String() != want { + t.Fatalf("local user got %q, want %q", out.String(), want) + } +} From 0c27fc015a4cba03a6f9e8cb083f34cd63b22480 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:26 -0400 Subject: [PATCH 07/14] fix map races on channel membership and parted channels Channel.Nick read ch.Users under the channel lock while writers hold the server lock, which can crash with a concurrent map access. The partedChannels append in PART also ran without a lock. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands.go | 2 ++ irc/server.go | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/irc/commands.go b/irc/commands.go index 5ba5ec8..dab102f 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -234,7 +234,9 @@ func part(s *Server, user *User, args []string) (quit bool) { s.Unlock() s.partChannel(user, chName, reason) + s.Lock() user.partedChannels = append(user.partedChannels, channelKey(chName)) + s.Unlock() } return } diff --git a/irc/server.go b/irc/server.go index f647b3a..8c0437b 100644 --- a/irc/server.go +++ b/irc/server.go @@ -361,7 +361,10 @@ func (s *Server) handleTNC() { ch := s.Channel(args[2]) // add user to channel if not already there - if nil == ch.Nick(incomingUser.Nick) { + s.Lock() + _, inChannel := ch.Users[nickKey(incomingUser.Nick)] + s.Unlock() + if !inChannel { s.joinChannel(incomingUser, ch.Name) } From 73914fd2fc956a86ee825ea4a285cde44964b22a Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:27 -0400 Subject: [PATCH 08/14] match parted channels case-insensitively partedChannels holds channel keys, but send() compared them against the target as typed, so a parted user still got messages for mixed-case channels. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/irc/server.go b/irc/server.go index 8c0437b..9eccece 100644 --- a/irc/server.go +++ b/irc/server.go @@ -643,7 +643,7 @@ func (s *Server) send(sender *User, cmd, target, msg string) { if u == sender && cmd != "PART" { continue } - if slices.Contains(u.partedChannels, target) { + if slices.Contains(u.partedChannels, channelKey(target)) { continue } recipients = append(recipients, u) From 09ef0207908302e3a47730c5cb5029736704d36e Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:29 -0400 Subject: [PATCH 09/14] log TNC transmit errors Write errors to the TNC were ignored, so a failed transmission looked like a message that did nothing. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/irc/server.go b/irc/server.go index 9eccece..0390e81 100644 --- a/irc/server.go +++ b/irc/server.go @@ -627,10 +627,7 @@ func (s *Server) send(sender *User, cmd, target, msg string) { sender.LastSeen = time.Now() senderID := sender.ID() - var tncWriter io.Writer - if sender.Local() && s.tnc != nil { - tncWriter = s.tnc.Port(uint8(s.tncport)) - } + transmit := sender.Local() && s.tnc != nil var recipients []*User if strings.HasPrefix(target, "#") { @@ -654,8 +651,8 @@ func (s *Server) send(sender *User, cmd, target, msg string) { s.Unlock() // Transmit local messages via radio after releasing the server lock. - if tncWriter != nil { - fmt.Fprintf(tncWriter, ":%s %s %s :%s", senderID, cmd, target, msg) + if transmit { + s.transmit(":%s %s %s :%s", senderID, cmd, target, msg) } for _, recipient := range recipients { @@ -663,6 +660,13 @@ func (s *Server) send(sender *User, cmd, target, msg string) { } } +// transmit sends one line over the radio. +func (s *Server) transmit(format string, args ...any) { + if _, err := fmt.Fprintf(s.tnc.Port(uint8(s.tncport)), format, args...); err != nil { + log.Printf("error transmitting to TNC: %v", err) + } +} + func (s *Server) Notice(sender *User, target string, msg string) { s.send(sender, "NOTICE", target, msg) } @@ -826,10 +830,7 @@ func (s *Server) setTopic(user *User, ch *Channel, topic string) { } chName := ch.Name userID := user.ID() - var tncWriter io.Writer - if user.Local() && s.tnc != nil { - tncWriter = s.tnc.Port(uint8(s.tncport)) - } + transmit := user.Local() && s.tnc != nil s.Unlock() for _, recipient := range recipients { @@ -837,7 +838,7 @@ func (s *Server) setTopic(user *User, ch *Channel, topic string) { } // also push out topic change - if tncWriter != nil { - fmt.Fprintf(tncWriter, ":%s %s %s :%s", userID, "TOPIC", chName, topic) + if transmit { + s.transmit(":%s %s %s :%s", userID, "TOPIC", chName, topic) } } From af1f84080453bc2591caac2105ecfbcd4cae41b7 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:30 -0400 Subject: [PATCH 10/14] time out SetHardware replies FREQUENCY, SETHARDWARE and -sethardware blocked forever waiting for a reply that most TNCs, direwolf included, never send. That froze the client, or the whole server at startup. Wait 2s instead. Also: SETHARDWARE no longer sends its own command name in the frame, and both commands reject missing arguments. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands.go | 36 +++++++++++++++--------------------- irc/server.go | 24 ++++++++++++++++++++++++ main.go | 9 +++------ 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/irc/commands.go b/irc/commands.go index dab102f..44fab47 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -4,8 +4,6 @@ import ( "fmt" "log" "strings" - - "github.com/sparques/kiss" ) type serverCommand func(s *Server, user *User, args []string) (quit bool) @@ -258,23 +256,18 @@ func frequency(s *Server, user *User, args []string) (quit bool) { case 3: rx, tx = args[1], args[2] default: - // do error - log.Printf("Frequency called with %d args", len(args)) + s.reply(user, ERR_NEEDMOREPARAMS, replyNick(user), "FREQUENCY", "Usage: FREQUENCY [tx]") + return } - frame := []byte(fmt.Sprintf("AT+DMOSETGROUP=1,%s,%s,0000,0,0000", tx, rx)) - - s.CommandPort().Write(kiss.WithCommand(kiss.FrameTypeSetHardware, frame)) - reply := make([]byte, 1024) - n, err := s.CommandPort().Read(reply) + frame := fmt.Sprintf("AT+DMOSETGROUP=1,%s,%s,0000,0,0000", tx, rx) + reply, err := s.SetHardware(frame) if err != nil { - log.Printf("Error reading SetHardware response: %v", err) - + log.Printf("SetHardware %s: %v", frame, err) + s.reply(user, "FREQUENCY", fmt.Sprintf("Failed to change frequency: %v", err)) return } - - // s.reply(user, "FREQUENCY", fmt.Sprintf("Failed to change Frequency: %v", )) - log.Printf("SetHardware: %s; Reply: %s", frame, string(reply[:n])) + log.Printf("SetHardware: %s; Reply: %s", frame, reply) s.reply(user, "FREQUENCY", fmt.Sprintf("Changed Frequency: RX: %s; TX: %s", rx, tx)) @@ -282,16 +275,17 @@ func frequency(s *Server, user *User, args []string) (quit bool) { } func sethardware(s *Server, user *User, args []string) (quit bool) { - cmd := strings.Join(args, " ") - s.CommandPort().Write(kiss.WithCommand(kiss.FrameTypeSetHardware, []byte(cmd))) - reply := make([]byte, 1024) - n, err := s.CommandPort().Read(reply) + if len(args) < 2 { + s.reply(user, ERR_NEEDMOREPARAMS, replyNick(user), "SETHARDWARE", "Not enough parameters") + return + } + reply, err := s.SetHardware(strings.Join(args[1:], " ")) if err != nil { - log.Printf("Error reading SetHardware response: %v", err) - return false + s.reply(user, "SETHARDWARE", err.Error()) + return } - s.reply(user, "SETHARDWARE", string(reply[:n])) + s.reply(user, "SETHARDWARE", reply) return } diff --git a/irc/server.go b/irc/server.go index 0390e81..7c3b491 100644 --- a/irc/server.go +++ b/irc/server.go @@ -105,6 +105,30 @@ func (s *Server) CommandPort() io.ReadWriter { return s.tnc.CommandPort(uint8(s.tncport)) } +// SetHardware sends a KISS SetHardware frame and waits briefly for the TNC's +// reply. Many TNCs (direwolf included) never answer, and an unbounded read +// would hang the caller. +// ponytail: a timed-out read stays parked and eats the next reply; add a reply router if that bites. +func (s *Server) SetHardware(cmd string) (string, error) { + port := s.CommandPort() + if _, err := port.Write(kiss.WithCommand(kiss.FrameTypeSetHardware, []byte(cmd))); err != nil { + return "", err + } + replies := make(chan string, 1) + go func() { + buf := make([]byte, 1024) + if n, err := port.Read(buf); err == nil { + replies <- string(buf[:min(n, len(buf))]) + } + }() + select { + case reply := <-replies: + return reply, nil + case <-time.After(2 * time.Second): + return "", errors.New("no reply from TNC") + } +} + func (s *Server) debugf(format string, v ...any) { if s.Debug { log.Printf(format, v...) diff --git a/main.go b/main.go index cb7ff34..f8fd785 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "syscall" "github.com/sparques/hamirc/irc" - "github.com/sparques/kiss" ) var ( @@ -79,14 +78,12 @@ func main() { if len(sethardware) > 0 { for _, frame := range sethardware { - server.CommandPort().Write(kiss.WithCommand(kiss.FrameTypeSetHardware, []byte(frame))) - reply := make([]byte, 1024) - n, err := server.CommandPort().Read(reply) + reply, err := server.SetHardware(frame) if err != nil { - log.Printf("Error reading SetHardware resposne: %v", err) + log.Printf("SetHardware %s: %v", frame, err) continue } - log.Printf("SetHardware: %s; Reply: %s", frame, string(reply[:n])) + log.Printf("SetHardware: %s; Reply: %s", frame, reply) } } From fe840ef10825b409b5533b88591ccb7e7eb3dd9f Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:32 -0400 Subject: [PATCH 11/14] send the connect announce after registration The announce went out when the TCP connection opened, before NICK and USER, so it carried no callsign. That is unidentified traffic, and receivers dropped it anyway. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/irc/server.go b/irc/server.go index 7c3b491..09797a1 100644 --- a/irc/server.go +++ b/irc/server.go @@ -440,11 +440,6 @@ func (s *Server) handleConnection(conn net.Conn) { user.local = true user.conn = conn - // send a notice when local user connects - if s.Announce { - fmt.Fprintf(s.tnc.Port(uint8(s.tncport)), ":%s %s %s :%s", user.String(), "NOTICE", "#hamirc", "hamirc - IRC for Amateur Radio - https://github.com/sparques/hamirc") - } - // Handle commands for scanner.Scan() { if s.handleCommand(user, strings.TrimSpace(scanner.Text())) { @@ -482,6 +477,11 @@ func (s *Server) acceptUser(user *User) { s.Users[nickKey(user.Nick)] = user s.Unlock() + // Announce after registration so the transmission carries the callsign. + if s.Announce && s.tnc != nil { + s.transmit(":%s NOTICE #hamirc :hamirc - IRC for Amateur Radio - https://github.com/sparques/hamirc", user.ID()) + } + s.motd(user) } From 9dabbc2b0ed12b181fdff7b42b5af661ad6a09d3 Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:33 -0400 Subject: [PATCH 12/14] send QUIT once, locally, in standard form QUIT went through send() once per channel, producing ":id QUIT #chan :reason" (clients read #chan as the reason) and transmitting each copy over the air, where receivers discard QUIT. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/irc/server.go b/irc/server.go index 09797a1..fe73ad0 100644 --- a/irc/server.go +++ b/irc/server.go @@ -777,16 +777,23 @@ func (s *Server) userHost(user *User, nicks []string) { func (s *Server) quit(user *User, reason string) { s.Lock() - channels := make([]string, 0, len(s.Channels)) + var recipients []*User for _, ch := range s.Channels { - if _, ok := ch.Users[nickKey(user.Nick)]; ok { - channels = append(channels, ch.Name) + if ch.Users[nickKey(user.Nick)] != user { + continue + } + for _, u := range ch.Users { + if u != user { + recipients = append(recipients, u) + } } } + userID := user.ID() s.Unlock() - for _, channel := range channels { - s.send(user, "QUIT", channel, reason) + // Local only: radio stations drop QUIT, so transmitting it wastes airtime. + for _, recipient := range uniqueUsers(recipients) { + fmt.Fprintf(recipient, ":%s QUIT :%s\r\n", userID, reason) } } From 3e92661db3ea339ae76b8d9e0a82817c2795072a Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:35 -0400 Subject: [PATCH 13/14] broadcast topic changes as TOPIC messages Channel members got an unsolicited 332 numeric instead of a TOPIC message, which clients don't treat as a change. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/server.go | 4 ++-- irc/server_test.go | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/irc/server.go b/irc/server.go index fe73ad0..1394041 100644 --- a/irc/server.go +++ b/irc/server.go @@ -865,11 +865,11 @@ func (s *Server) setTopic(user *User, ch *Channel, topic string) { s.Unlock() for _, recipient := range recipients { - s.reply(recipient, RPL_TOPIC, recipient.Nick, chName, topic) + fmt.Fprintf(recipient, ":%s TOPIC %s :%s\r\n", userID, chName, topic) } // also push out topic change if transmit { - s.transmit(":%s %s %s :%s", userID, "TOPIC", chName, topic) + s.transmit(":%s TOPIC %s :%s", userID, chName, topic) } } diff --git a/irc/server_test.go b/irc/server_test.go index a6b0e8e..48b1d0f 100644 --- a/irc/server_test.go +++ b/irc/server_test.go @@ -477,13 +477,12 @@ func TestSetTopicLocalWithoutTNCBroadcastsLocally(t *testing.T) { if channel.Topic != "check in" { t.Fatalf("topic = %q, want check in", channel.Topic) } - wantSetter := ":hamirc 332 Maple #hamirc :check in\r\n" - if setterOut.String() != wantSetter { - t.Fatalf("setter topic reply = %q, want %q", setterOut.String(), wantSetter) + want := ":Maple!N0CALL@* TOPIC #hamirc :check in\r\n" + if setterOut.String() != want { + t.Fatalf("setter topic message = %q, want %q", setterOut.String(), want) } - wantOther := ":hamirc 332 Birch #hamirc :check in\r\n" - if otherOut.String() != wantOther { - t.Fatalf("other topic reply = %q, want %q", otherOut.String(), wantOther) + if otherOut.String() != want { + t.Fatalf("other topic message = %q, want %q", otherOut.String(), want) } } From 4e88510be85cad92159c1a37a81c69659a02784d Mon Sep 17 00:00:00 2001 From: Andrew Rodland Date: Sun, 27 Sep 2026 00:54:35 -0400 Subject: [PATCH 14/14] accept JOIN keys and reject channels without # "JOIN #chan key" was refused as missing parameters. Channels without a leading # were created, but messages to them were routed as private messages and went nowhere. Co-Authored-By: Claude Opus 5.5 (1M context) --- irc/commands.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/irc/commands.go b/irc/commands.go index 44fab47..ecef5d7 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -88,11 +88,16 @@ func user(s *Server, user *User, args []string) (quit bool) { } func join(s *Server, user *User, args []string) (quit bool) { - if len(args) != 2 { + if len(args) < 2 { s.reply(user, ERR_NEEDMOREPARAMS, replyNick(user), "JOIN", "Not enough parameters") return } + // channel keys (args[2]) are accepted and ignored for _, ch := range strings.Split(args[1], ",") { + if !strings.HasPrefix(ch, "#") { + s.reply(user, ERR_NOSUCHCHANNEL, user.Nick, ch, "Channel names must start with #") + continue + } s.joinChannel(user, ch) } return