From 71a0376962633e102d6e20484b70065f148aad63 Mon Sep 17 00:00:00 2001 From: nato Date: Sun, 5 Feb 2023 15:11:17 -0800 Subject: [PATCH] Host.go: move /nick command and add nick registration check. --- auth.go | 5 +++++ chat/command.go | 30 --------------------------- host.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/auth.go b/auth.go index 05c1bcf..e54bcca 100644 --- a/auth.go +++ b/auth.go @@ -109,6 +109,11 @@ func (a *Auth) Keyname(key ssh.PublicKey) string { return name } +func (a *Auth) KeynameFingerprint(keyname string) string { + fingerprint := a.fingerprintsByKeyname[keyname] + return fingerprint +} + // SetPassphrase enables passphrase authentication with the given passphrase. // If an empty passphrase is given, disable passphrase authentication. func (a *Auth) SetPassphrase(passphrase string) { diff --git a/chat/command.go b/chat/command.go index 6a8fcd2..5d27cf1 100644 --- a/chat/command.go +++ b/chat/command.go @@ -142,36 +142,6 @@ func InitCommands(c *Commands) { }) c.Alias("/exit", "/quit") - c.Add(Command{ - Prefix: "/nick", - PrefixHelp: "NAME", - Help: "Rename yourself.", - Handler: func(room *Room, msg message.CommandMsg) error { - args := msg.Args() - if len(args) != 1 { - return ErrMissingArg - } - u := msg.From() - - member, ok := room.MemberByID(u.ID()) - if !ok { - return errors.New("failed to find member") - } - - oldID := member.ID() - newID := sanitize.Name(args[0]) - if newID == oldID { - return errors.New("new name is the same as the original") - } - member.SetID(newID) - err := room.Rename(oldID, member) - if err != nil { - member.SetID(oldID) - return err - } - return nil - }, - }) c.Add(Command{ Prefix: "/names", diff --git a/host.go b/host.go index 0da784e..17b32df 100644 --- a/host.go +++ b/host.go @@ -387,6 +387,60 @@ func (h *Host) InitCommands(c *chat.Commands) { }, }) + c.Add(chat.Command{ + Prefix: "/nick", + PrefixHelp: "NAME", + Help: "Rename yourself.", + Handler: func(room *chat.Room, msg message.CommandMsg) error { + args := msg.Args() + if len(args) != 1 { + return errors.New("missing argument") + } + u := msg.From() + + member, ok := room.MemberByID(u.ID()) + if !ok { + return errors.New("failed to find member") + } + + oldID := member.ID() + newID := sanitize.Name(args[0]) + + if newID == oldID { + return errors.New("new name is the same as the original") + } + + // check nick registration + if h.auth.keynamesMode { + identity, identified := member.Identifier.(*Identity) + if identified { + identity.SetSymbol(strings.Replace(identity.symbol, "✓", "", -1 )) + } + registeredFingerprint := h.auth.KeynameFingerprint(newID) + if registeredFingerprint != "" { + if ! identified { + errors.New("this nick is registered to different key") + } + fingerprint := sshd.Fingerprint(identity.PublicKey()) + if registeredFingerprint != fingerprint { + return errors.New("this nick is registered to different key") + } + if identified { + identity.SetSymbol("✓"+ identity.symbol) + } + } + } + + member.SetID(newID) + err := room.Rename(oldID, member) + if err != nil { + member.SetID(oldID) + return err + } + return nil + }, + }) + c.Add(chat.Command{ Prefix: "/reply", PrefixHelp: "MESSAGE",