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
|
|
|
)
|
|
|
|
|
2013-05-11 23:43:06 +02:00
|
|
|
const (
|
2014-02-05 04:28:24 +01:00
|
|
|
DEBUG_CLIENT = false
|
2013-05-11 23:43:06 +02:00
|
|
|
)
|
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
type ClientCommand interface {
|
|
|
|
Command
|
|
|
|
HandleClient(*Client)
|
|
|
|
}
|
|
|
|
|
2012-04-07 20:44:59 +02:00
|
|
|
type Client struct {
|
2014-02-05 04:28:24 +01:00
|
|
|
atime time.Time
|
|
|
|
away bool
|
|
|
|
channels ChannelSet
|
|
|
|
commands chan<- ClientCommand
|
2012-12-12 08:04:03 +01:00
|
|
|
conn net.Conn
|
2012-12-10 05:24:53 +01:00
|
|
|
hostname string
|
2012-04-08 08:32:08 +02:00
|
|
|
nick string
|
2014-02-05 04:28:24 +01:00
|
|
|
realname string
|
2012-12-12 07:34:22 +01:00
|
|
|
registered bool
|
2012-12-17 04:13:53 +01:00
|
|
|
replies chan<- Reply
|
2014-02-05 04:28:24 +01:00
|
|
|
server *Server
|
|
|
|
serverPass bool
|
|
|
|
username string
|
2012-12-17 04:13:53 +01: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)
|
2014-02-05 04:28:24 +01:00
|
|
|
commands := make(chan ClientCommand)
|
2013-06-03 01:53:06 +02:00
|
|
|
replies := make(chan Reply)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
client := &Client{
|
2014-02-05 04:28:24 +01:00
|
|
|
channels: make(ChannelSet),
|
|
|
|
commands: commands,
|
2012-12-09 21:51:50 +01:00
|
|
|
conn: conn,
|
2014-02-05 04:28:24 +01:00
|
|
|
hostname: LookupHostname(conn.RemoteAddr()),
|
2012-12-15 23:34:20 +01:00
|
|
|
replies: replies,
|
2014-02-05 04:28:24 +01:00
|
|
|
server: server,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
|
|
|
go client.readConn(read)
|
2012-12-15 23:34:20 +01:00
|
|
|
go client.writeConn(write, replies)
|
2014-02-05 04:28:24 +01:00
|
|
|
go client.receiveCommands(commands)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
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 {
|
2012-12-15 23:34:20 +01:00
|
|
|
m, err := ParseCommand(str)
|
2012-12-09 21:51:50 +01:00
|
|
|
if err != nil {
|
2013-05-11 22:55:01 +02:00
|
|
|
if err == NotEnoughArgsError {
|
2014-02-05 04:28:24 +01:00
|
|
|
c.replies <- ErrNeedMoreParams(c.server, str)
|
2013-05-11 22:55:01 +02:00
|
|
|
} else {
|
2014-02-05 04:28:24 +01:00
|
|
|
c.replies <- ErrUnknownCommand(c.server, str)
|
2013-05-11 22:55:01 +02:00
|
|
|
}
|
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
|
|
|
|
2013-06-03 07:07:50 +02:00
|
|
|
m.SetBase(c)
|
2012-12-15 23:34:20 +01:00
|
|
|
c.server.commands <- m
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
|
|
|
|
for reply := range replies {
|
2013-05-11 23:43:06 +02:00
|
|
|
if DEBUG_CLIENT {
|
2013-05-26 22:28:22 +02:00
|
|
|
log.Printf("%s ← %s : %s", c, reply.Source(), reply)
|
2013-05-11 23:43:06 +02:00
|
|
|
}
|
2013-06-03 07:07:50 +02:00
|
|
|
reply.Format(c, write)
|
2012-04-08 08:32:08 +02:00
|
|
|
}
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
2012-04-09 16:57:55 +02:00
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
func (client *Client) receiveCommands(commands <-chan ClientCommand) {
|
|
|
|
for command := range commands {
|
|
|
|
if DEBUG_CLIENT {
|
|
|
|
log.Printf("%s → %s : %s", command.Client(), client, command)
|
|
|
|
}
|
|
|
|
command.HandleClient(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) Replies() chan<- Reply {
|
2012-12-17 04:13:53 +01:00
|
|
|
return c.replies
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) Server() *Server {
|
2012-12-17 04:13:53 +01:00
|
|
|
return c.server
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) Nick() string {
|
2014-02-05 04:28:24 +01:00
|
|
|
if c.HasNick() {
|
2012-04-09 16:57:55 +02:00
|
|
|
return c.nick
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
return "guest"
|
2012-04-09 16:57:55 +02:00
|
|
|
}
|
2012-04-18 05:24:26 +02:00
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) UModeString() string {
|
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
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) HasNick() bool {
|
2014-02-05 04:28:24 +01:00
|
|
|
return c.nick != ""
|
2012-04-18 06:13:12 +02:00
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) HasUsername() bool {
|
2012-04-18 06:13:12 +02:00
|
|
|
return c.username != ""
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) Username() string {
|
|
|
|
if c.HasUsername() {
|
2012-12-12 07:34:22 +01:00
|
|
|
return c.username
|
|
|
|
}
|
2013-05-12 03:28:18 +02:00
|
|
|
return "guest"
|
2012-12-12 07:34:22 +01:00
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02: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
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) Id() string {
|
2012-12-09 07:54:58 +01:00
|
|
|
return c.UserHost()
|
2012-04-18 07:11:35 +02:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) String() string {
|
|
|
|
return c.hostname
|
2013-05-11 22:55:01 +02:00
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (c *Client) PublicId() string {
|
2012-12-15 23:34:20 +01:00
|
|
|
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
|
|
|
|
}
|
2014-02-05 04:28:24 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// commands
|
|
|
|
//
|
|
|
|
|
|
|
|
func (m *PrivMsgCommand) HandleClient(client *Client) {
|
|
|
|
client.replies <- RplPrivMsg(m.Client(), client, m.message)
|
|
|
|
}
|