3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/src/irc/client.go

56 lines
927 B
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
2012-04-08 08:32:08 +02:00
"log"
2012-04-07 20:44:59 +02:00
"net"
2012-04-08 08:32:08 +02:00
"strings"
2012-04-07 20:44:59 +02:00
)
2012-04-08 08:32:08 +02:00
type Message struct {
command string
args string
client *Client
}
2012-04-07 20:44:59 +02:00
type Client struct {
2012-04-08 08:32:08 +02:00
addr net.Addr
send chan string
recv chan string
username string
realname string
nick string
2012-04-09 16:57:55 +02:00
registered bool
2012-04-07 20:44:59 +02:00
}
func NewClient(conn net.Conn) *Client {
2012-04-08 08:32:08 +02:00
client := new(Client)
client.addr = conn.RemoteAddr()
client.send = StringWriteChan(conn)
client.recv = StringReadChan(conn)
return client
2012-04-07 20:44:59 +02:00
}
2012-04-08 08:32:08 +02:00
// 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})
}
}()
2012-04-07 20:44:59 +02:00
}
2012-04-08 08:32:08 +02:00
func (c *Client) Send(lines ...string) {
for _, line := range lines {
log.Printf("C <- S: %s", line)
c.send <- line
}
2012-04-07 20:44:59 +02:00
}
2012-04-09 16:57:55 +02:00
func (c *Client) Nick() string {
if c.nick != "" {
return c.nick
}
return "<guest>"
}