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

Add regexes for commands.

This commit is contained in:
Jeremy Latt 2012-04-07 23:32:08 -07:00
parent a427e2bb47
commit faece3e7f8
5 changed files with 202 additions and 75 deletions

View File

@ -1,5 +1,5 @@
package main package main
// http://tools.ietf.org/html/rfc1459 // http://tools.ietf.org/html/rfc2812
import ( import (
"irc" "irc"
@ -7,5 +7,5 @@ import (
func main() { func main() {
server := irc.NewServer() server := irc.NewServer()
server.Listen(":6697") server.Listen(":6667")
} }

View File

@ -1,28 +1,47 @@
package irc package irc
import ( import (
"log"
"net" "net"
"strings"
) )
type Message struct {
command string
args string
client *Client
}
type Client struct { type Client struct {
conn net.Conn addr net.Addr
ch chan Message send chan string
recv chan string
username string
realname string
nick string
} }
func NewClient(conn net.Conn) *Client { func NewClient(conn net.Conn) *Client {
return &Client{conn, NewMessageChan(NewStringChan(conn))} client := new(Client)
client.addr = conn.RemoteAddr()
client.send = StringWriteChan(conn)
client.recv = StringReadChan(conn)
return client
} }
// Write messages from the client to the server. // Adapt `chan string` to a `chan Message`.
func (c *Client) Communicate(server chan Message) { func (c *Client) Communicate(server *Server) {
for message := range c.ch { go func() {
message.client = c for str := range c.recv {
server <- message parts := strings.SplitN(str, " ", 2)
server.Send(Message{parts[0], parts[1], c})
} }
c.Close() }()
} }
func (c *Client) Close() { func (c *Client) Send(lines ...string) {
c.conn.Close() for _, line := range lines {
log.Printf("C <- S: %s", line)
c.send <- line
}
} }

View File

@ -3,43 +3,42 @@ package irc
import ( import (
"bufio" "bufio"
"log" "log"
"strings"
"net" "net"
) )
// Adapt `net.Conn` to a `chan string`. // Adapt `net.Conn` to a `chan string`.
func NewStringChan(conn net.Conn) chan string { func StringReadChan(conn net.Conn) chan string {
ch := make(chan string) ch := make(chan string)
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)) reader := bufio.NewReader(conn)
done := make(chan bool)
go func() {
<- done
close(ch)
}()
// conn -> ch
go func() { go func() {
for { for {
line, err := rw.ReadString('\n') line, err := reader.ReadString('\n')
if (line != "") {
ch <- strings.TrimSpace(line)
}
if err != nil { if err != nil {
log.Print("StringChan[read]: %v", err) log.Print("StringReadChan[read]: ", err)
break break
} }
ch <- line
} }
done <- true close(ch)
}() }()
return ch
}
// ch -> conn func StringWriteChan(conn net.Conn) chan string {
ch := make(chan string)
writer := bufio.NewWriter(conn)
go func() { go func() {
for str := range ch { for str := range ch {
if _, err := rw.WriteString(str + "\r\n"); err != nil { if _, err := writer.WriteString(str + "\r\n"); err != nil {
log.Print("StringChan[write]: %v", err) log.Print("StringWriteChan[write]: ", err)
break break
} }
rw.Flush() writer.Flush()
} }
done <- true close(ch)
}() }()
return ch return ch

View File

@ -1,43 +1,107 @@
package irc package irc
import ( import (
"fmt"
) )
type Message struct { const (
line string VERSION = "goircd-1"
client *Client )
const (
RPL_WELCOME = "001"
RPL_YOURHOST = "002"
RPL_CREATED = "003"
RPL_MYINFO = "004"
RPL_NONE = "300"
)
func ReplyWelcome(nick string, user string, host string) string {
return fmt.Sprintf("%s %s Welcome to the Internet Relay Network %s!%s@%s", RPL_WELCOME, nick, nick, user, host)
} }
func (m *Message) Encode() string { func ReplyYourHost(nick string, server string) string {
return m.line return fmt.Sprintf("%s %s Your host is %s, running version %s", RPL_YOURHOST, nick, server, VERSION)
}
func ReplyCreated(nick string, created string) string {
return fmt.Sprintf("%s %s This server was created %s", RPL_CREATED, nick, created)
}
func ReplyMyInfo(nick string, servername string) string {
return fmt.Sprintf("%s %s %s %s <user modes> <channel modes>", RPL_MYINFO, nick, servername, VERSION)
}
const (
ERR_NOSUCHNICK = "401"
ERR_NOSUCHSERVER = "402"
ERR_NOSUCHCHANNEL = "403"
ERR_UNKNOWNCOMMAND = "421"
ERR_NICKNAMEINUSE = "433"
ERR_NEEDMOREPARAMS = "461"
ERR_ALREADYREGISTRED = "462"
ERR_USERSDONTMATCH = "502"
)
func ErrAlreadyRegistered(nick string) string {
return fmt.Sprintf("%s %s :You may not reregister", ERR_ALREADYREGISTRED, nick)
}
func ErrNickNameInUse(nick string) string {
return fmt.Sprintf("%s %s :Nickname is already in use", ERR_NICKNAMEINUSE, nick)
}
func ErrUnknownCommand(nick string, command string) string {
return fmt.Sprintf("%s %s %s :Unknown command", ERR_UNKNOWNCOMMAND, nick, command)
} }
// Adapt `chan string` to a `chan Message`. const (
func NewMessageChan(strch chan string) chan Message { RE_PASS = "(?P<password>\\S+)"
msgch := make(chan Message) RE_NICK = "(?P<nickname>\\S+)"
RE_USER = "(?P<user>\\S+) (?P<mode>\\d) (?:\\S+) :(?P<realname>.+)"
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_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>.+))?"
RE_MODE_CH = "(?P<channel>\\S+)(?: (?P<mode>[-+][iwroOs]+))*" // XXX incomplete
RE_TOPIC = "(?P<channel>\\S+)(?: :(?P<topic>.+))?"
RE_NAMES = "(?:(?P<channels>\\S+(?:,\\S+)*)(?: (?P<target>\\S+))?)?"
RE_LIST = "(?:(?P<channels>\\S+(?:,\\S+)*)(?: (?P<target>\\S+))?)?"
RE_INVITE = "(?P<nickname>\\S+) (?P<channel>\\S+)"
RE_KICK = "(?P<channels>\\S+(?:,\\S+)*) (?P<users>\\S+(?:,\\S+))(?: :(?P<comment>.+))?"
RE_PRIVMSG = "(?P<target>\\S+) :(?P<text>.+)"
RE_NOTICE = "(?P<target>\\S+) :(?P<text>.+)"
RE_MOTD = "(?P<target>\\S+)?"
RE_LUSERS = "(?:(?P<mask>\\S+)(?: (?P<target>\\S+))?)?"
RE_VERSION = "(?P<target>\\S+)?"
RE_STATS = "(?:(?P<query>\\S+)(?: (?P<target>\\S+))?)?"
RE_LINKS = "(?:(?P<remote>\\S+) )?(?P<mask>\\S+)"
RE_TIME = "(?P<target>\\S+)?"
RE_CONNECT = "(?P<target>\\S+) (?P<port>\\d+)(?: (?P<remote>\\S+))?"
RE_TRACE = "(?P<target>\\S+)?"
RE_ADMIN = "(?P<target>\\S+)?"
RE_INFO = "(?P<target>\\S+)?"
RE_SERVLIST = "" // XXX
RE_SQUERY = "" // XXX
RE_WHO = "" // XXX
RE_WHOIS = "" // XXX
RE_WHOWAS = "" // XXX
RE_KILL = "(?P<nickname>\\S+) :(?P<comment>.+)"
RE_PING = "(?P<server1>\\S+)(?: (?P<server2>\\S+))?"
RE_PONG = "(?P<server1>\\S+)(?: (?P<server2>\\S+))?"
RE_ERROR = ":(?P<error>.+)"
RE_AWAY = ":(?P<text>.+)"
RE_REHASH = ""
RE_DIE = ""
RE_RESTART = ""
RE_SUMMON = "(?P<user>\\S+)(?: (?P<target>\\S+)(?: (?P<channel>\\S+))?)?"
RE_USERS = "(?P<target>\\S+)?"
RE_WALLOPS = ":(?P<text>.+)"
RE_USERHOST = "(?P<nicknames>\\S+(?: \\S+)*)"
RE_ISON = "(?P<nicknames>\\S+(?: \\S+)*)"
)
done := make(chan bool)
go func() {
<- done
close(msgch)
}()
// str -> msg
go func() {
for str := range strch {
msgch <- Message{str, nil}
}
done <- true
}()
// msg -> str
go func() {
for message := range msgch {
strch <- message.Encode()
}
done <- true
}()
return msgch
}

View File

@ -3,14 +3,17 @@ package irc
import ( import (
"log" "log"
"net" "net"
"strings"
) )
type Server struct { type Server struct {
ch chan Message ch chan Message
users map[string]*Client
nicks map[string]*Client
} }
func NewServer() *Server { func NewServer() *Server {
server := Server{make(chan Message)} server := Server{make(chan Message), make(map[string]*Client), make(map[string]*Client)}
go server.Receive() go server.Receive()
return &server return &server
} }
@ -18,27 +21,69 @@ func NewServer() *Server {
func (s *Server) Listen(addr string) { func (s *Server) Listen(addr string) {
listener, err := net.Listen("tcp", addr) listener, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
log.Fatal("Server.Listen: %v", err) log.Fatal("Server.Listen: ", err)
} }
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Print("Server.Listen: %v", err) log.Print("Server.Listen: ", err)
continue continue
} }
client := NewClient(conn) go NewClient(conn).Communicate(s)
go client.Communicate(s.ch)
} }
} }
func (s *Server) Receive() { func (s *Server) Receive() {
for message := range s.ch { for message := range s.ch {
log.Print("Server.Receive: %v", message.line) log.Printf("C -> S: %s %s", message.command, message.args)
message.client.ch <- Message{"pong: " + message.line, nil} switch message.command {
case "PING":
message.client.Send("PONG")
case "PASS":
s.PassCommand(message.client, message.args)
case "USER":
s.UserCommand(message.client, message.args)
case "NICK":
s.NickCommand(message.client, message.args)
default:
message.client.Send(ErrUnknownCommand(message.client.nick, message.command))
}
} }
} }
func (s *Server) Close() { func (s *Server) Send(m Message) {
close(s.ch) s.ch <- m
}
// 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 {
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"))
}
}
func (s *Server) NickCommand(c *Client, nick string) {
if s.nicks[nick] != nil {
c.Send(ErrNickNameInUse(nick))
return
}
c.nick = nick
s.nicks[nick] = c
} }