3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 03:02:48 +01:00

Merge pull request #26 from jlatt/oper-nick

ONICK command to allow operators to change users' nicks
This commit is contained in:
Jeremy Latt 2014-03-18 14:47:54 -07:00
commit 946a97e4d4
5 changed files with 116 additions and 66 deletions

View File

@ -227,6 +227,10 @@ func (client *Client) Friends() ClientSet {
}
func (client *Client) SetNickname(nickname Name) {
if client.HasNick() {
Log.error.Printf("%s nickname already set!", client)
return
}
client.nick = nickname
client.server.clients.Add(client)
}

View File

@ -40,6 +40,7 @@ var (
NAMES: NewNamesCommand,
NICK: NewNickCommand,
NOTICE: NewNoticeCommand,
ONICK: NewOperNickCommand,
OPER: NewOperCommand,
PART: NewPartCommand,
PASS: NewPassCommand,
@ -228,15 +229,6 @@ func NewPassCommand(args []string) (Command, error) {
// NICK <nickname>
type NickCommand struct {
BaseCommand
nickname Name
}
func (m *NickCommand) String() string {
return fmt.Sprintf("NICK(nickname=%s)", m.nickname)
}
func NewNickCommand(args []string) (Command, error) {
if len(args) != 1 {
return nil, NotEnoughArgsError
@ -1006,3 +998,14 @@ func NewWhoWasCommand(args []string) (Command, error) {
}
return cmd, nil
}
func NewOperNickCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
return &OperNickCommand{
target: NewName(args[0]),
nick: NewName(args[1]),
}, nil
}

View File

@ -21,6 +21,7 @@ const (
NAMES StringCode = "NAMES"
NICK StringCode = "NICK"
NOTICE StringCode = "NOTICE"
ONICK StringCode = "ONICK"
OPER StringCode = "OPER"
PART StringCode = "PART"
PASS StringCode = "PASS"

99
irc/nickname.go Normal file
View File

@ -0,0 +1,99 @@
package irc
import (
"fmt"
)
type NickCommand struct {
BaseCommand
nickname Name
}
func (m *NickCommand) String() string {
return fmt.Sprintf("NICK(nickname=%s)", m.nickname)
}
func (m *NickCommand) HandleRegServer(s *Server) {
client := m.Client()
if !client.authorized {
client.ErrPasswdMismatch()
client.Quit("bad password")
return
}
if client.capState == CapNegotiating {
client.capState = CapNegotiated
}
if m.nickname == "" {
client.ErrNoNicknameGiven()
return
}
if s.clients.Get(m.nickname) != nil {
client.ErrNickNameInUse(m.nickname)
return
}
if !m.nickname.IsNickname() {
client.ErrErroneusNickname(m.nickname)
return
}
client.SetNickname(m.nickname)
s.tryRegister(client)
}
func (msg *NickCommand) HandleServer(server *Server) {
client := msg.Client()
if msg.nickname == "" {
client.ErrNoNicknameGiven()
return
}
if !msg.nickname.IsNickname() {
client.ErrErroneusNickname(msg.nickname)
return
}
if msg.nickname == client.nick {
return
}
target := server.clients.Get(msg.nickname)
if (target != nil) && (target != client) {
client.ErrNickNameInUse(msg.nickname)
return
}
client.ChangeNickname(msg.nickname)
}
type OperNickCommand struct {
BaseCommand
target Name
nick Name
}
func (msg *OperNickCommand) HandleServer(server *Server) {
client := msg.Client()
if !client.flags[Operator] {
client.ErrNoPrivileges()
return
}
if !msg.nick.IsNickname() {
client.ErrErroneusNickname(msg.nick)
return
}
target := server.clients.Get(msg.target)
if target == nil {
client.ErrNoSuchNick(msg.target)
return
}
target.ChangeNickname(msg.nick)
}

View File

@ -289,37 +289,6 @@ func (msg *ProxyCommand) HandleRegServer(server *Server) {
msg.Client().hostname = msg.hostname
}
func (m *NickCommand) HandleRegServer(s *Server) {
client := m.Client()
if !client.authorized {
client.ErrPasswdMismatch()
client.Quit("bad password")
return
}
if client.capState == CapNegotiating {
client.capState = CapNegotiated
}
if m.nickname == "" {
client.ErrNoNicknameGiven()
return
}
if s.clients.Get(m.nickname) != nil {
client.ErrNickNameInUse(m.nickname)
return
}
if !m.nickname.IsNickname() {
client.ErrErroneusNickname(m.nickname)
return
}
client.SetNickname(m.nickname)
s.tryRegister(client)
}
func (msg *RFC1459UserCommand) HandleRegServer(server *Server) {
client := msg.Client()
if !client.authorized {
@ -380,32 +349,6 @@ func (m *PongCommand) HandleServer(s *Server) {
// no-op
}
func (msg *NickCommand) HandleServer(server *Server) {
client := msg.Client()
if msg.nickname == "" {
client.ErrNoNicknameGiven()
return
}
if !msg.nickname.IsNickname() {
client.ErrErroneusNickname(msg.nickname)
return
}
if msg.nickname == client.nick {
return
}
target := server.clients.Get(msg.nickname)
if (target != nil) && (target != client) {
client.ErrNickNameInUse(msg.nickname)
return
}
client.ChangeNickname(msg.nickname)
}
func (m *UserCommand) HandleServer(s *Server) {
m.Client().ErrAlreadyRegistered()
}