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

user registration

This commit is contained in:
Jeremy Latt 2012-04-09 07:57:55 -07:00
parent faece3e7f8
commit 53e098067a
3 changed files with 45 additions and 19 deletions

View File

@ -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 "<guest>"
}

View File

@ -63,7 +63,7 @@ const (
RE_OPER = "(?P<name>\\S+) (?P<password>\\S+)"
RE_MODE = "(?P<nickname>\\S+)(?: (?P<mode>[-+][iwroOs]+))*"
RE_SERVICE = "(?P<nickname>\\S+) (?P<reserved1>\\S+) (?P<distribution>\\S+) (?P<type>\\S+) (?P<reserved2>\\S+) :(?P<info>.+)"
RE_QUIT = "(?P<message>.*)"
RE_QUIT = ":(?P<message>.*)"
RE_SQUIT = "(?P<server>\\S+) :(?P<comment>.+)"
RE_JOIN = "0|(?:(?P<channels>\\S+(?:,\\S+)*)(?: (?P<keys>\\S+(?:,\\S+)*))?)"
RE_PART = "(?P<channels>\\S+(?:,\\S+)*)(?: :(?P<message>.+))?"
@ -105,3 +105,10 @@ const (
RE_ISON = "(?P<nicknames>\\S+(?: \\S+)*)"
)
func MessagePong() string {
return "PONG"
}
func MessageError() string {
return "ERROR :Bye"
}

View File

@ -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)
}