mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 10:10:08 -08:00
Add regexes for commands.
This commit is contained in:
parent
a427e2bb47
commit
faece3e7f8
5 changed files with 202 additions and 75 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue