Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions irc/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"log"
"strings"

"github.com/sparques/kiss"
)

type serverCommand func(s *Server, user *User, args []string) (quit bool)
Expand Down Expand Up @@ -35,8 +33,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
}

Expand Down Expand Up @@ -81,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
Expand Down Expand Up @@ -225,7 +237,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
}
Expand All @@ -247,40 +261,36 @@ 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 <rx> [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))

return false
}

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
}
Expand Down
49 changes: 48 additions & 1 deletion irc/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -175,3 +175,50 @@ 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())
}
}

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")
}
}
Loading