3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 10:42:52 +01:00

support USER from both RFCs 1459 and 2812

This commit is contained in:
Jeremy Latt 2014-02-17 22:10:48 -08:00
parent bb64d38684
commit baf83e73d2
3 changed files with 71 additions and 20 deletions

View File

@ -254,5 +254,6 @@ func (client *Client) Quit(message string) {
channel.Quit(client)
}
client.Reply(RplError(client.server, client))
client.Destroy()
}

View File

@ -39,7 +39,7 @@ var (
PROXY: NewProxyCommand,
QUIT: NewQuitCommand,
TOPIC: NewTopicCommand,
USER: NewUserMsgCommand,
USER: NewUserCommand,
WHO: NewWhoCommand,
WHOIS: NewWhoisCommand,
}
@ -223,34 +223,68 @@ func NewNickCommand(args []string) (editableCommand, error) {
}, nil
}
// USER <user> <mode> <unused> <realname>
type UserMsgCommand struct {
type UserCommand struct {
BaseCommand
user string
mode uint8
unused string
username string
realname string
}
func (cmd *UserMsgCommand) String() string {
return fmt.Sprintf("USER(user=%s, mode=%o, unused=%s, realname=%s)",
cmd.user, cmd.mode, cmd.unused, cmd.realname)
// USER <username> <hostname> <servername> <realname>
type RFC1459UserCommand struct {
UserCommand
hostname string
servername string
}
func NewUserMsgCommand(args []string) (editableCommand, error) {
func (cmd *RFC1459UserCommand) String() string {
return fmt.Sprintf("USER(username=%s, hostname=%s, servername=%s, realname=%s)",
cmd.username, cmd.hostname, cmd.servername, cmd.realname)
}
// USER <user> <mode> <unused> <realname>
type RFC2812UserCommand struct {
UserCommand
mode uint8
unused string
}
func (cmd *RFC2812UserCommand) String() string {
return fmt.Sprintf("USER(username=%s, mode=%d, unused=%s, realname=%s)",
cmd.username, cmd.mode, cmd.unused, cmd.realname)
}
func (cmd *RFC2812UserCommand) Flags() []UserMode {
flags := make([]UserMode, 0)
if (cmd.mode & 4) == 4 {
flags = append(flags, WallOps)
}
if (cmd.mode & 8) == 8 {
flags = append(flags, Invisible)
}
return flags
}
func NewUserCommand(args []string) (editableCommand, error) {
if len(args) != 4 {
return nil, NotEnoughArgsError
}
msg := &UserMsgCommand{
user: args[0],
unused: args[2],
realname: args[3],
}
mode, err := strconv.ParseUint(args[1], 10, 8)
if err == nil {
msg.mode = uint8(mode)
msg := &RFC2812UserCommand{
mode: uint8(mode),
unused: args[2],
}
msg.username = args[0]
msg.realname = args[3]
return msg, nil
}
msg := &RFC1459UserCommand{
hostname: args[1],
servername: args[2],
}
msg.username = args[0]
msg.realname = args[3]
return msg, nil
}

View File

@ -283,9 +283,25 @@ func (m *NickCommand) HandleRegServer(s *Server) {
s.tryRegister(client)
}
func (msg *UserMsgCommand) HandleRegServer(server *Server) {
func (msg *RFC1459UserCommand) HandleRegServer(server *Server) {
msg.HandleRegServer2(server)
}
func (msg *RFC2812UserCommand) HandleRegServer(server *Server) {
client := msg.Client()
client.username, client.realname = msg.user, msg.realname
flags := msg.Flags()
if len(flags) > 0 {
for _, mode := range msg.Flags() {
client.flags[mode] = true
}
client.Reply(RplUModeIs(server, client))
}
msg.HandleRegServer2(server)
}
func (msg *UserCommand) HandleRegServer2(server *Server) {
client := msg.Client()
client.username, client.realname = msg.username, msg.realname
server.tryRegister(client)
}
@ -323,7 +339,7 @@ func (msg *NickCommand) HandleServer(server *Server) {
server.clients.Add(client)
}
func (m *UserMsgCommand) HandleServer(s *Server) {
func (m *UserCommand) HandleServer(s *Server) {
m.Client().Reply(ErrAlreadyRegistered(s))
}