2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2012-04-18 07:11:35 +02:00
|
|
|
"fmt"
|
2012-12-12 07:34:22 +01:00
|
|
|
"log"
|
2012-04-07 20:44:59 +02:00
|
|
|
"net"
|
2012-12-12 08:12:35 +01:00
|
|
|
"time"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2012-12-12 08:04:03 +01:00
|
|
|
conn net.Conn
|
|
|
|
send chan<- Reply
|
2012-04-08 08:32:08 +02:00
|
|
|
username string
|
|
|
|
realname string
|
2012-12-10 05:24:53 +01:00
|
|
|
hostname string
|
2012-04-08 08:32:08 +02:00
|
|
|
nick string
|
2012-12-10 05:24:53 +01:00
|
|
|
serverPass bool
|
2012-12-12 07:34:22 +01:00
|
|
|
registered bool
|
2012-12-12 08:04:03 +01:00
|
|
|
away bool
|
|
|
|
wallOps bool
|
|
|
|
server *Server
|
|
|
|
channels ChannelSet
|
2012-12-12 08:12:35 +01:00
|
|
|
atime time.Time
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
type ClientSet map[*Client]bool
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
func NewClient(server *Server, conn net.Conn) *Client {
|
2012-12-13 08:27:17 +01:00
|
|
|
read := StringReadChan(conn)
|
|
|
|
write := StringWriteChan(conn)
|
|
|
|
send := make(chan Reply)
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
client := &Client{
|
|
|
|
channels: make(ChannelSet),
|
|
|
|
conn: conn,
|
|
|
|
hostname: LookupHostname(conn.RemoteAddr()),
|
|
|
|
server: server,
|
2012-12-13 08:27:17 +01:00
|
|
|
send: send,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
|
|
|
// Connect the conn to the server.
|
|
|
|
go client.readConn(read)
|
|
|
|
|
|
|
|
// Connect the reply channel to the conn.
|
|
|
|
go client.writeConn(write, send)
|
|
|
|
|
2012-04-08 08:32:08 +02:00
|
|
|
return client
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
func (c *Client) readConn(recv <-chan string) {
|
|
|
|
for str := range recv {
|
|
|
|
log.Printf("%s > %s", c.Id(), str)
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
m, err := ParseMessage(str)
|
|
|
|
if err != nil {
|
|
|
|
// TODO handle error
|
2012-12-13 08:27:17 +01:00
|
|
|
continue
|
2012-04-08 08:32:08 +02:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
|
|
|
m.SetClient(c)
|
|
|
|
c.server.recv <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) writeConn(write chan<- string, send <-chan Reply) {
|
|
|
|
for reply := range send {
|
|
|
|
replyStr := reply.String(c)
|
|
|
|
log.Printf("%s < %s", c.Id(), replyStr)
|
|
|
|
write <- replyStr
|
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
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
return "*"
|
2012-04-09 16:57:55 +02:00
|
|
|
}
|
2012-04-18 05:24:26 +02:00
|
|
|
|
|
|
|
func (c *Client) UModeString() string {
|
2012-12-12 07:54:57 +01:00
|
|
|
if c.wallOps {
|
2012-12-12 08:04:03 +01:00
|
|
|
return "+w"
|
2012-12-12 07:54:57 +01:00
|
|
|
}
|
2012-12-12 08:04:03 +01:00
|
|
|
return ""
|
2012-04-18 05:24:26 +02:00
|
|
|
}
|
2012-04-18 06:13:12 +02:00
|
|
|
|
|
|
|
func (c *Client) HasNick() bool {
|
|
|
|
return c.nick != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) HasUser() bool {
|
|
|
|
return c.username != ""
|
|
|
|
}
|
|
|
|
|
2012-12-12 07:34:22 +01:00
|
|
|
func (c *Client) Username() string {
|
|
|
|
if c.HasUser() {
|
|
|
|
return c.username
|
|
|
|
}
|
|
|
|
return "*"
|
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
func (c *Client) UserHost() string {
|
2012-12-12 07:34:22 +01:00
|
|
|
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
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
func (c *Client) Id() string {
|
|
|
|
return c.UserHost()
|
2012-04-18 07:11:35 +02:00
|
|
|
}
|