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

88 lines
1.5 KiB
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
2012-04-18 07:11:35 +02:00
"fmt"
2012-04-07 20:44:59 +02:00
"net"
)
type Client struct {
2012-04-18 05:24:26 +02:00
conn net.Conn
hostname string
send chan<- Reply
recv <-chan string
2012-04-08 08:32:08 +02:00
username string
realname string
nick string
2012-04-09 16:57:55 +02:00
registered bool
2012-04-18 05:24:26 +02:00
invisible bool
channels ChannelSet
2012-12-09 21:51:50 +01:00
server *Server
2012-04-07 20:44:59 +02:00
}
type ClientSet map[*Client]bool
2012-12-09 21:51:50 +01:00
func NewClient(server *Server, conn net.Conn) *Client {
client := &Client{
channels: make(ChannelSet),
conn: conn,
hostname: LookupHostname(conn.RemoteAddr()),
recv: StringReadChan(conn),
server: server,
}
client.SetReplyToStringChan()
2012-04-08 08:32:08 +02:00
return client
2012-04-07 20:44:59 +02:00
}
func (c *Client) SetReplyToStringChan() {
send := make(chan Reply)
write := StringWriteChan(c.conn)
go func() {
for reply := range send {
write <- reply.String(c)
}
}()
c.send = send
}
2012-04-08 08:32:08 +02:00
// Adapt `chan string` to a `chan Message`.
2012-12-09 21:51:50 +01:00
func (c *Client) Communicate() {
for str := range c.recv {
2012-12-09 21:51:50 +01:00
m, err := ParseMessage(str)
if err != nil {
// TODO handle error
return
2012-04-08 08:32:08 +02:00
}
2012-12-09 21:51:50 +01:00
c.server.recv <- &ClientMessage{c, m}
2012-04-08 08:32:08 +02:00
}
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 "*"
2012-04-09 16:57:55 +02:00
}
2012-04-18 05:24:26 +02:00
func (c *Client) UModeString() string {
if c.invisible {
return "+i"
}
return ""
}
2012-04-18 06:13:12 +02:00
func (c *Client) HasNick() bool {
return c.nick != ""
}
func (c *Client) HasUser() bool {
return c.username != ""
}
func (c *Client) UserHost() string {
return fmt.Sprintf("%s!%s@%s", c.nick, c.username, c.hostname)
2012-04-18 06:13:12 +02:00
}
2012-04-18 07:11:35 +02:00
func (c *Client) Id() string {
return c.UserHost()
2012-04-18 07:11:35 +02:00
}