From 53e098067a8d818ec862c09cee28812cb75b1248 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Mon, 9 Apr 2012 07:57:55 -0700 Subject: [PATCH] user registration --- src/irc/client.go | 8 ++++++++ src/irc/protocol.go | 9 ++++++++- src/irc/server.go | 47 ++++++++++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/irc/client.go b/src/irc/client.go index 245b75e2..490d95f9 100644 --- a/src/irc/client.go +++ b/src/irc/client.go @@ -19,6 +19,7 @@ type Client struct { username string realname string nick string + registered bool } func NewClient(conn net.Conn) *Client { @@ -45,3 +46,10 @@ func (c *Client) Send(lines ...string) { c.send <- line } } + +func (c *Client) Nick() string { + if c.nick != "" { + return c.nick + } + return "" +} diff --git a/src/irc/protocol.go b/src/irc/protocol.go index 071601be..f32e7d3c 100644 --- a/src/irc/protocol.go +++ b/src/irc/protocol.go @@ -63,7 +63,7 @@ const ( RE_OPER = "(?P\\S+) (?P\\S+)" RE_MODE = "(?P\\S+)(?: (?P[-+][iwroOs]+))*" RE_SERVICE = "(?P\\S+) (?P\\S+) (?P\\S+) (?P\\S+) (?P\\S+) :(?P.+)" - RE_QUIT = "(?P.*)" + RE_QUIT = ":(?P.*)" RE_SQUIT = "(?P\\S+) :(?P.+)" RE_JOIN = "0|(?:(?P\\S+(?:,\\S+)*)(?: (?P\\S+(?:,\\S+)*))?)" RE_PART = "(?P\\S+(?:,\\S+)*)(?: :(?P.+))?" @@ -105,3 +105,10 @@ const ( RE_ISON = "(?P\\S+(?: \\S+)*)" ) +func MessagePong() string { + return "PONG" +} + +func MessageError() string { + return "ERROR :Bye" +} diff --git a/src/irc/server.go b/src/irc/server.go index 2085f647..008fbfe1 100644 --- a/src/irc/server.go +++ b/src/irc/server.go @@ -3,17 +3,17 @@ package irc import ( "log" "net" + "regexp" "strings" ) type Server struct { ch chan Message - users map[string]*Client nicks map[string]*Client } func NewServer() *Server { - server := Server{make(chan Message), make(map[string]*Client), make(map[string]*Client)} + server := Server{make(chan Message), make(map[string]*Client)} go server.Receive() return &server } @@ -39,15 +39,15 @@ func (s *Server) Receive() { log.Printf("C -> S: %s %s", message.command, message.args) switch message.command { case "PING": - message.client.Send("PONG") - case "PASS": - s.PassCommand(message.client, message.args) + message.client.Send(MessagePong()) case "USER": s.UserCommand(message.client, message.args) case "NICK": s.NickCommand(message.client, message.args) + case "QUIT": + s.QuitCommand(message.client, message.args) default: - message.client.Send(ErrUnknownCommand(message.client.nick, message.command)) + message.client.Send(ErrUnknownCommand(message.client.Nick(), message.command)) } } } @@ -58,25 +58,15 @@ func (s *Server) Send(m Message) { // commands -func (s *Server) PassCommand(c *Client, args string) { -} - func (s *Server) UserCommand(c *Client, args string) { parts := strings.SplitN(args, " ", 4) username, _, _, realname := parts[0], parts[1], parts[2], parts[3] - if s.users[username] != nil { + if c.username != "" { c.Send(ErrAlreadyRegistered(c.nick)) return } c.username, c.realname = username, realname - s.users[username] = c - if c.nick != "" { - c.Send( - ReplyWelcome(c.nick, c.username, "localhost"), - ReplyYourHost(c.nick, "irc.jlatt.com"), - ReplyCreated(c.nick, "2012/04/07"), - ReplyMyInfo(c.nick, "irc.jlatt.com")) - } + s.TryRegister(c) } func (s *Server) NickCommand(c *Client, nick string) { @@ -86,4 +76,25 @@ func (s *Server) NickCommand(c *Client, nick string) { } c.nick = nick s.nicks[nick] = c + s.TryRegister(c) +} + +func (s *Server) TryRegister(c *Client) { + if (!c.registered && c.nick != "" && c.username != "") { + c.registered = true + c.Send( + ReplyWelcome(c.Nick(), c.username, "localhost"), + ReplyYourHost(c.Nick(), "irc.jlatt.com"), + ReplyCreated(c.Nick(), "2012/04/07"), + ReplyMyInfo(c.Nick(), "irc.jlatt.com")) + } +} + +func (s *Server) QuitCommand(c *Client, args string) { + re := regexp.MustCompile("^" + RE_QUIT + "$") + matches := re.FindAllStringSubmatch(args, -1) + if matches != nil { + c.Send(MessageError()) + } + delete(s.nicks, c.nick) }