1
0
Fork 0
forked from External/ergo

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,28 +1,47 @@
package irc
import (
"log"
"net"
"strings"
)
type Message struct {
command string
args string
client *Client
}
type Client struct {
conn net.Conn
ch chan Message
addr net.Addr
send chan string
recv chan string
username string
realname string
nick string
}
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.
func (c *Client) Communicate(server chan Message) {
for message := range c.ch {
message.client = c
server <- message
// Adapt `chan string` to a `chan Message`.
func (c *Client) Communicate(server *Server) {
go func() {
for str := range c.recv {
parts := strings.SplitN(str, " ", 2)
server.Send(Message{parts[0], parts[1], c})
}
}()
}
func (c *Client) Send(lines ...string) {
for _, line := range lines {
log.Printf("C <- S: %s", line)
c.send <- line
}
c.Close()
}
func (c *Client) Close() {
c.conn.Close()
}