mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-13 05:42:07 +03:00
Host.go: move /nick command and add nick registration check.
This commit is contained in:
parent
fedad0f216
commit
71a0376962
5
auth.go
5
auth.go
@ -109,6 +109,11 @@ func (a *Auth) Keyname(key ssh.PublicKey) string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Auth) KeynameFingerprint(keyname string) string {
|
||||||
|
fingerprint := a.fingerprintsByKeyname[keyname]
|
||||||
|
return fingerprint
|
||||||
|
}
|
||||||
|
|
||||||
// SetPassphrase enables passphrase authentication with the given passphrase.
|
// SetPassphrase enables passphrase authentication with the given passphrase.
|
||||||
// If an empty passphrase is given, disable passphrase authentication.
|
// If an empty passphrase is given, disable passphrase authentication.
|
||||||
func (a *Auth) SetPassphrase(passphrase string) {
|
func (a *Auth) SetPassphrase(passphrase string) {
|
||||||
|
@ -142,36 +142,6 @@ func InitCommands(c *Commands) {
|
|||||||
})
|
})
|
||||||
c.Alias("/exit", "/quit")
|
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{
|
c.Add(Command{
|
||||||
Prefix: "/names",
|
Prefix: "/names",
|
||||||
|
54
host.go
54
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{
|
c.Add(chat.Command{
|
||||||
Prefix: "/reply",
|
Prefix: "/reply",
|
||||||
PrefixHelp: "MESSAGE",
|
PrefixHelp: "MESSAGE",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user