2012-04-07 11:44:59 -07:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2012-04-17 22:11:35 -07:00
|
|
|
"fmt"
|
2012-12-11 22:34:22 -08:00
|
|
|
"log"
|
2012-04-07 11:44:59 -07:00
|
|
|
"net"
|
2012-12-11 23:12:35 -08:00
|
|
|
"time"
|
2012-04-07 11:44:59 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2014-02-04 19:28:24 -08:00
|
|
|
away bool
|
|
|
|
channels ChannelSet
|
2012-12-11 23:04:03 -08:00
|
|
|
conn net.Conn
|
2012-12-09 20:24:53 -08:00
|
|
|
hostname string
|
2014-02-08 14:20:23 -08:00
|
|
|
invisible bool
|
2012-04-07 23:32:08 -07:00
|
|
|
nick string
|
2014-02-09 10:07:40 -08:00
|
|
|
operator bool
|
2014-02-04 19:28:24 -08:00
|
|
|
realname string
|
2012-12-11 22:34:22 -08:00
|
|
|
registered bool
|
2012-12-16 19:13:53 -08:00
|
|
|
replies chan<- Reply
|
2014-02-04 19:28:24 -08:00
|
|
|
server *Server
|
|
|
|
serverPass bool
|
|
|
|
username string
|
2014-02-09 12:13:09 -08:00
|
|
|
idleTimer *time.Timer
|
|
|
|
quitTimer *time.Timer
|
2012-12-16 19:13:53 -08:00
|
|
|
}
|
|
|
|
|
2012-12-09 12:51:50 -08:00
|
|
|
func NewClient(server *Server, conn net.Conn) *Client {
|
2012-12-12 23:27:17 -08:00
|
|
|
read := StringReadChan(conn)
|
|
|
|
write := StringWriteChan(conn)
|
2013-06-02 16:53:06 -07:00
|
|
|
replies := make(chan Reply)
|
2012-12-12 23:27:17 -08:00
|
|
|
|
2012-12-09 12:51:50 -08:00
|
|
|
client := &Client{
|
2014-02-09 08:48:11 -08:00
|
|
|
channels: make(ChannelSet),
|
|
|
|
conn: conn,
|
|
|
|
hostname: LookupHostname(conn.RemoteAddr()),
|
|
|
|
replies: replies,
|
|
|
|
server: server,
|
2012-12-09 12:51:50 -08:00
|
|
|
}
|
2012-12-12 23:27:17 -08:00
|
|
|
|
|
|
|
go client.readConn(read)
|
2012-12-15 14:34:20 -08:00
|
|
|
go client.writeConn(write, replies)
|
2012-12-12 23:27:17 -08:00
|
|
|
|
2014-02-09 12:13:09 -08:00
|
|
|
client.Touch()
|
2012-04-07 23:32:08 -07:00
|
|
|
return client
|
2012-04-07 11:44:59 -07:00
|
|
|
}
|
|
|
|
|
2014-02-09 12:13:09 -08:00
|
|
|
func (client *Client) Touch() {
|
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
|
|
|
if client.idleTimer == nil {
|
|
|
|
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
|
|
|
|
} else {
|
|
|
|
client.idleTimer.Reset(IDLE_TIMEOUT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Idle() {
|
|
|
|
if client.quitTimer == nil {
|
|
|
|
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.Quit)
|
|
|
|
} else {
|
|
|
|
client.quitTimer.Reset(QUIT_TIMEOUT)
|
|
|
|
}
|
|
|
|
client.Reply(RplPing(client.server, client))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Quit() {
|
|
|
|
msg := &QuitCommand{
|
|
|
|
message: "connection timeout",
|
|
|
|
}
|
|
|
|
msg.SetClient(client)
|
|
|
|
client.server.commands <- msg
|
|
|
|
}
|
|
|
|
|
2012-12-12 23:27:17 -08:00
|
|
|
func (c *Client) readConn(recv <-chan string) {
|
|
|
|
for str := range recv {
|
2012-12-15 14:34:20 -08:00
|
|
|
m, err := ParseCommand(str)
|
2012-12-09 12:51:50 -08:00
|
|
|
if err != nil {
|
2013-05-11 13:55:01 -07:00
|
|
|
if err == NotEnoughArgsError {
|
2014-02-09 08:48:11 -08:00
|
|
|
c.Reply(ErrNeedMoreParams(c.server, str))
|
2013-05-11 13:55:01 -07:00
|
|
|
} else {
|
2014-02-09 08:48:11 -08:00
|
|
|
c.Reply(ErrUnknownCommand(c.server, str))
|
2013-05-11 13:55:01 -07:00
|
|
|
}
|
2012-12-12 23:27:17 -08:00
|
|
|
continue
|
2012-04-07 23:32:08 -07:00
|
|
|
}
|
2012-12-12 23:27:17 -08:00
|
|
|
|
2014-02-08 23:51:51 -08:00
|
|
|
m.SetClient(c)
|
2012-12-15 14:34:20 -08:00
|
|
|
c.server.commands <- m
|
2012-12-12 23:27:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 14:34:20 -08:00
|
|
|
func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
|
|
|
|
for reply := range replies {
|
2013-05-11 14:43:06 -07:00
|
|
|
if DEBUG_CLIENT {
|
2013-05-26 13:28:22 -07:00
|
|
|
log.Printf("%s ← %s : %s", c, reply.Source(), reply)
|
2013-05-11 14:43:06 -07:00
|
|
|
}
|
2013-06-02 22:07:50 -07:00
|
|
|
reply.Format(c, write)
|
2012-04-07 23:32:08 -07:00
|
|
|
}
|
2012-04-07 11:44:59 -07:00
|
|
|
}
|
2012-04-09 07:57:55 -07:00
|
|
|
|
2014-02-09 08:48:11 -08:00
|
|
|
func (client *Client) Destroy() error {
|
|
|
|
if client.replies == nil {
|
|
|
|
return ErrAlreadyDestroyed
|
|
|
|
}
|
2014-02-09 19:06:30 -08:00
|
|
|
|
2014-02-08 23:15:05 -08:00
|
|
|
close(client.replies)
|
2014-02-09 08:48:11 -08:00
|
|
|
client.replies = nil
|
|
|
|
client.conn.Close()
|
2014-02-09 19:06:30 -08:00
|
|
|
if client.idleTimer != nil {
|
|
|
|
client.idleTimer.Stop()
|
|
|
|
}
|
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
2014-02-09 08:48:11 -08:00
|
|
|
return nil
|
2014-02-08 17:10:04 -08:00
|
|
|
}
|
|
|
|
|
2014-02-09 08:48:11 -08:00
|
|
|
func (client *Client) Reply(reply Reply) error {
|
|
|
|
if client.replies == nil {
|
|
|
|
return ErrAlreadyDestroyed
|
|
|
|
}
|
|
|
|
client.replies <- reply
|
|
|
|
return nil
|
2012-12-16 19:13:53 -08:00
|
|
|
}
|
|
|
|
|
2014-02-08 17:10:04 -08:00
|
|
|
func (client *Client) HasNick() bool {
|
|
|
|
return client.nick != ""
|
2012-12-16 19:13:53 -08:00
|
|
|
}
|
|
|
|
|
2014-02-08 17:10:04 -08:00
|
|
|
func (client *Client) HasUsername() bool {
|
|
|
|
return client.username != ""
|
|
|
|
}
|
|
|
|
|
2014-02-08 17:43:59 -08:00
|
|
|
func (client *Client) InterestedClients() ClientSet {
|
2014-02-08 17:10:04 -08:00
|
|
|
clients := make(ClientSet)
|
2014-02-08 17:43:59 -08:00
|
|
|
for channel := range client.channels {
|
|
|
|
for member := range channel.members {
|
|
|
|
clients[member] = true
|
2014-02-08 17:10:04 -08:00
|
|
|
}
|
2012-04-09 07:57:55 -07:00
|
|
|
}
|
2014-02-08 17:10:04 -08:00
|
|
|
return clients
|
2012-04-09 07:57:55 -07:00
|
|
|
}
|
2012-04-17 20:24:26 -07:00
|
|
|
|
2014-02-09 08:53:06 -08:00
|
|
|
// <mode>
|
|
|
|
func (c *Client) ModeString() (str string) {
|
2014-02-08 14:20:23 -08:00
|
|
|
if c.invisible {
|
2014-02-09 08:53:06 -08:00
|
|
|
str += Invisible.String()
|
2014-02-08 14:20:23 -08:00
|
|
|
}
|
2014-02-09 10:07:40 -08:00
|
|
|
if c.operator {
|
|
|
|
str += Operator.String()
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:53:06 -08:00
|
|
|
if len(str) > 0 {
|
|
|
|
str = "+" + str
|
|
|
|
}
|
|
|
|
return
|
2012-04-17 20:24:26 -07:00
|
|
|
}
|
2012-04-17 21:13:12 -07:00
|
|
|
|
2014-02-08 17:10:04 -08:00
|
|
|
func (c *Client) UserHost() string {
|
|
|
|
nick := c.nick
|
|
|
|
if nick == "" {
|
|
|
|
nick = "*"
|
|
|
|
}
|
|
|
|
username := c.username
|
|
|
|
if username == "" {
|
|
|
|
username = "*"
|
2012-12-11 22:34:22 -08:00
|
|
|
}
|
2014-02-08 17:10:04 -08:00
|
|
|
return fmt.Sprintf("%s!%s@%s", nick, username, c.hostname)
|
2012-12-11 22:34:22 -08:00
|
|
|
}
|
|
|
|
|
2014-02-08 17:10:04 -08:00
|
|
|
func (c *Client) Nick() string {
|
|
|
|
return c.nick
|
2012-04-17 21:13:12 -07:00
|
|
|
}
|
2012-04-17 22:11:35 -07:00
|
|
|
|
2013-05-11 18:28:18 -07:00
|
|
|
func (c *Client) Id() string {
|
2012-12-08 22:54:58 -08:00
|
|
|
return c.UserHost()
|
2012-04-17 22:11:35 -07:00
|
|
|
}
|
2012-12-15 14:34:20 -08:00
|
|
|
|
2013-05-11 18:28:18 -07:00
|
|
|
func (c *Client) String() string {
|
2014-02-08 13:18:11 -08:00
|
|
|
return c.UserHost()
|
2013-05-11 13:55:01 -07:00
|
|
|
}
|