chat, main: Add /rename op command

This commit is contained in:
Andrey Petrov 2020-06-24 12:36:02 -04:00
parent 3c4d90cfc1
commit 36843252b4
2 changed files with 55 additions and 11 deletions

View File

@ -23,6 +23,8 @@ var ErrUserClosed = errors.New("user closed")
type User struct {
Identifier
Ignored *set.Set
OnChange func()
colorIdx int
joined time.Time
msg chan Message
@ -72,12 +74,20 @@ func (u *User) SetConfig(cfg UserConfig) {
u.mu.Lock()
u.config = cfg
u.mu.Unlock()
if u.OnChange != nil {
u.OnChange()
}
}
// Rename the user with a new Identifier.
func (u *User) SetID(id string) {
u.Identifier.SetID(id)
u.setColorIdx(rand.Int())
if u.OnChange != nil {
u.OnChange()
}
}
// ReplyTo returns the last user that messaged this user.

56
host.go
View File

@ -13,6 +13,7 @@ import (
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/internal/humantime"
"github.com/shazow/ssh-chat/internal/sanitize"
"github.com/shazow/ssh-chat/sshd"
)
@ -92,6 +93,10 @@ func (h *Host) isOp(conn sshd.Connection) bool {
func (h *Host) Connect(term *sshd.Terminal) {
id := NewIdentity(term.Conn)
user := message.NewUserScreen(id, term)
user.OnChange = func() {
term.SetPrompt(GetPrompt(user))
user.SetHighlight(user.Name())
}
cfg := user.Config()
apiMode := strings.ToLower(term.Term()) == "bot"
@ -216,17 +221,6 @@ func (h *Host) Connect(term *sshd.Terminal) {
// Skip the remaining rendering workarounds
continue
}
cmd := m.Command()
if cmd == "/nick" || cmd == "/theme" {
// Hijack /nick command to update terminal synchronously. Wouldn't
// work if we use h.room.Send(m) above.
//
// FIXME: This is hacky, how do we improve the API to allow for
// this? Chat module shouldn't know about terminals.
term.SetPrompt(GetPrompt(user))
user.SetHighlight(user.Name())
}
}
err = h.Leave(user)
@ -619,4 +613,44 @@ func (h *Host) InitCommands(c *chat.Commands) {
return nil
},
})
c.Add(chat.Command{
Op: true,
Prefix: "/rename",
PrefixHelp: "USER NEW_NAME [SYMBOL]",
Help: "Rename USER to NEW_NAME, add optional SYMBOL prefix",
Handler: func(room *chat.Room, msg message.CommandMsg) error {
if !room.IsOp(msg.From()) {
return errors.New("must be op")
}
args := msg.Args()
if len(args) < 2 {
return errors.New("must specify user and new name")
}
member, ok := room.MemberByID(args[0])
if !ok {
return errors.New("user not found")
}
oldID := member.ID()
newID := sanitize.Name(args[1])
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
}
body := fmt.Sprintf("%s was renamed by %s.", oldID, msg.From().Name())
room.Send(message.NewAnnounceMsg(body))
return nil
},
})
}