3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 07:29:31 +01:00

clean up general command and name handling

This commit is contained in:
Jeremy Latt 2014-02-17 17:58:22 -08:00
parent a55b912941
commit 1a7f56a903
5 changed files with 48 additions and 29 deletions

View File

@ -15,14 +15,7 @@ type Channel struct {
} }
func IsChannel(target string) bool { func IsChannel(target string) bool {
if target == "" { return ChannelNameExpr.MatchString(target)
return false
}
switch target[0] {
case '&', '#', '+', '!':
return true
}
return false
} }
// NewChannel creates a new channel from a `Server` and a `name` // NewChannel creates a new channel from a `Server` and a `name`

View File

@ -8,6 +8,10 @@ import (
"time" "time"
) )
func IsNickname(nick string) bool {
return NicknameExpr.MatchString(nick)
}
type Client struct { type Client struct {
atime time.Time atime time.Time
awayMessage string awayMessage string
@ -43,8 +47,7 @@ func NewClient(server *Server, conn net.Conn) *Client {
socket: NewSocket(conn), socket: NewSocket(conn),
} }
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.ConnectionClosed) client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionClosed)
go client.readCommands() go client.readCommands()
go client.writeReplies() go client.writeReplies()
@ -94,7 +97,7 @@ func (client *Client) Touch() {
func (client *Client) Idle() { func (client *Client) Idle() {
if client.quitTimer == nil { if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.ConnectionTimeout) client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
} else { } else {
client.quitTimer.Reset(QUIT_TIMEOUT) client.quitTimer.Reset(QUIT_TIMEOUT)
} }
@ -102,7 +105,7 @@ func (client *Client) Idle() {
client.Reply(RplPing(client.server, client)) client.Reply(RplPing(client.server, client))
} }
func (client *Client) ConnectionTimeout() { func (client *Client) connectionTimeout() {
msg := &QuitCommand{ msg := &QuitCommand{
message: "connection timeout", message: "connection timeout",
} }
@ -110,7 +113,7 @@ func (client *Client) ConnectionTimeout() {
client.server.commands <- msg client.server.commands <- msg
} }
func (client *Client) ConnectionClosed() { func (client *Client) connectionClosed() {
msg := &QuitCommand{ msg := &QuitCommand{
message: "connection closed", message: "connection closed",
} }

View File

@ -2,16 +2,24 @@ package irc
import ( import (
"errors" "errors"
"regexp"
"time" "time"
) )
var ( var (
// debugging flags
DEBUG_NET = false DEBUG_NET = false
DEBUG_CLIENT = false DEBUG_CLIENT = false
DEBUG_CHANNEL = false DEBUG_CHANNEL = false
DEBUG_SERVER = false DEBUG_SERVER = false
// errors
ErrAlreadyDestroyed = errors.New("already destroyed") ErrAlreadyDestroyed = errors.New("already destroyed")
// regexps
ChannelNameExpr = regexp.MustCompile(`^[&!#+][[:word:]]{1,63}$`)
NicknameExpr = regexp.MustCompile(
"^[[:alpha:]\\[\\]{}^`][[:word:]\\[\\]{}^`]{1,31}$")
) )
const ( const (

View File

@ -433,3 +433,8 @@ func ErrNoMOTD(server *Server) Reply {
func ErrNoNicknameGiven(server *Server) Reply { func ErrNoNicknameGiven(server *Server) Reply {
return NewNumericReply(server, ERR_NONICKNAMEGIVEN, ":No nickname given") return NewNumericReply(server, ERR_NONICKNAMEGIVEN, ":No nickname given")
} }
func ErrErroneusNickname(server *Server, nick string) Reply {
return NewNumericReply(server, ERR_ERRONEUSNICKNAME,
"%s :Erroneous nickname", nick)
}

View File

@ -54,37 +54,37 @@ func (server *Server) ReceiveCommands() {
case conn := <-server.conns: case conn := <-server.conns:
NewClient(server, conn) NewClient(server, conn)
case command := <-server.commands: case cmd := <-server.commands:
client := cmd.Client()
if DEBUG_SERVER { if DEBUG_SERVER {
log.Printf("%s → %s %+v", command.Client(), server, command) log.Printf("%s → %s %s", client, server, cmd)
} }
client := command.Client()
switch client.phase { switch client.phase {
case Authorization: case Authorization:
authCommand, ok := command.(AuthServerCommand) authCmd, ok := cmd.(AuthServerCommand)
if !ok { if !ok {
client.Destroy() client.Destroy()
continue continue
} }
authCommand.HandleAuthServer(server) authCmd.HandleAuthServer(server)
case Registration: case Registration:
regCommand, ok := command.(RegServerCommand) regCmd, ok := cmd.(RegServerCommand)
if !ok { if !ok {
client.Destroy() client.Destroy()
continue continue
} }
regCommand.HandleRegServer(server) regCmd.HandleRegServer(server)
default: default:
serverCommand, ok := command.(ServerCommand) srvCmd, ok := cmd.(ServerCommand)
if !ok { if !ok {
client.Reply(ErrUnknownCommand(server, command.Code())) client.Reply(ErrUnknownCommand(server, cmd.Code()))
continue continue
} }
client.Touch() client.Touch()
serverCommand.HandleServer(server) srvCmd.HandleServer(server)
} }
} }
} }
@ -264,11 +264,16 @@ func (m *NickCommand) HandleRegServer(s *Server) {
return return
} }
if s.clients[m.nickname] != nil { if s.clients.Get(m.nickname) != nil {
client.Reply(ErrNickNameInUse(s, m.nickname)) client.Reply(ErrNickNameInUse(s, m.nickname))
return return
} }
if !IsNickname(m.nickname) {
client.Reply(ErrErroneusNickname(s, m.nickname))
return
}
client.ChangeNickname(m.nickname) client.ChangeNickname(m.nickname)
s.clients.Add(client) s.clients.Add(client)
s.tryRegister(client) s.tryRegister(client)
@ -304,7 +309,7 @@ func (msg *NickCommand) HandleServer(server *Server) {
return return
} }
if server.clients[msg.nickname] != nil { if server.clients.Get(msg.nickname) != nil {
client.Reply(ErrNickNameInUse(server, msg.nickname)) client.Reply(ErrNickNameInUse(server, msg.nickname))
return return
} }
@ -395,7 +400,12 @@ func (msg *PrivMsgCommand) HandleServer(server *Server) {
func (m *ModeCommand) HandleServer(s *Server) { func (m *ModeCommand) HandleServer(s *Server) {
client := m.Client() client := m.Client()
target := s.clients[m.nickname] target := s.clients.Get(m.nickname)
if target == nil {
client.Reply(ErrNoSuchNick(s, m.nickname))
return
}
if client != target && !client.flags[Operator] { if client != target && !client.flags[Operator] {
client.Reply(ErrUsersDontMatch(s)) client.Reply(ErrUsersDontMatch(s))
@ -521,8 +531,8 @@ func (msg *IsOnCommand) HandleServer(server *Server) {
ison := make([]string, 0) ison := make([]string, 0)
for _, nick := range msg.nicks { for _, nick := range msg.nicks {
if _, ok := server.clients[nick]; ok { if iclient := server.clients.Get(nick); iclient != nil {
ison = append(ison, nick) ison = append(ison, iclient.Nick())
} }
} }
@ -546,7 +556,7 @@ func (msg *NoticeCommand) HandleServer(server *Server) {
return return
} }
target := server.clients[msg.target] target := server.clients.Get(msg.target)
if target == nil { if target == nil {
client.Reply(ErrNoSuchNick(server, msg.target)) client.Reply(ErrNoSuchNick(server, msg.target))
return return