ergo/src/irc/client.go

72 lines
1.2 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"
2012-04-18 06:13:12 +02:00
"strings"
2012-04-07 20:44:59 +02:00
)
type Client struct {
2012-04-18 05:24:26 +02:00
conn net.Conn
send chan<- string
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
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)
2012-04-18 05:24:26 +02:00
client.conn = conn
2012-04-08 08:32:08 +02:00
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 chan<- *ClientMessage) {
for str := range c.recv {
m := ParseMessage(str)
if m != nil {
server <- &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 "<guest>"
}
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) Hostname() string {
addr := c.conn.RemoteAddr().String()
index := strings.LastIndex(addr, ":")
if index != -1 {
return addr[0:index]
}
return addr
}
2012-04-18 07:11:35 +02:00
func (c *Client) UserHost() string {
return fmt.Sprintf("%s!%s@%s", c.nick, c.username, c.Hostname())
}