2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2014-02-12 00:00:19 +01:00
|
|
|
"bufio"
|
2012-04-18 07:11:35 +02:00
|
|
|
"fmt"
|
2014-02-12 00:00:19 +01:00
|
|
|
"io"
|
2012-12-12 07:34:22 +01:00
|
|
|
"log"
|
2012-04-07 20:44:59 +02:00
|
|
|
"net"
|
2014-02-12 00:00:19 +01:00
|
|
|
"strings"
|
2012-12-12 08:12:35 +01:00
|
|
|
"time"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2014-02-12 00:44:58 +01:00
|
|
|
away bool
|
|
|
|
awayMessage string
|
|
|
|
channels ChannelSet
|
|
|
|
conn net.Conn
|
2014-02-13 06:04:50 +01:00
|
|
|
destroyed bool
|
2014-02-12 00:44:58 +01:00
|
|
|
hostname string
|
|
|
|
idleTimer *time.Timer
|
|
|
|
invisible bool
|
|
|
|
nick string
|
|
|
|
operator bool
|
|
|
|
quitTimer *time.Timer
|
|
|
|
realname string
|
|
|
|
recv *bufio.Reader
|
|
|
|
registered bool
|
|
|
|
replies chan<- Reply
|
|
|
|
send *bufio.Writer
|
|
|
|
server *Server
|
|
|
|
serverPass bool
|
|
|
|
username string
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
func NewClient(server *Server, conn net.Conn) *Client {
|
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-09 17:48:11 +01:00
|
|
|
channels: make(ChannelSet),
|
|
|
|
conn: conn,
|
2014-02-11 04:39:53 +01:00
|
|
|
hostname: AddrLookupHostname(conn.RemoteAddr()),
|
2014-02-12 00:00:19 +01:00
|
|
|
recv: bufio.NewReader(conn),
|
2014-02-09 17:48:11 +01:00
|
|
|
replies: replies,
|
2014-02-12 00:00:19 +01:00
|
|
|
send: bufio.NewWriter(conn),
|
2014-02-09 17:48:11 +01:00
|
|
|
server: server,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-12 00:00:19 +01:00
|
|
|
go client.readConn()
|
|
|
|
go client.writeConn(replies)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-09 21:13:09 +01:00
|
|
|
client.Touch()
|
2012-04-08 08:32:08 +02:00
|
|
|
return client
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:13:09 +01: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 {
|
2014-02-13 18:47:10 +01:00
|
|
|
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.ConnectionTimeout)
|
2014-02-09 21:13:09 +01:00
|
|
|
} else {
|
|
|
|
client.quitTimer.Reset(QUIT_TIMEOUT)
|
|
|
|
}
|
|
|
|
client.Reply(RplPing(client.server, client))
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:47:10 +01:00
|
|
|
func (client *Client) ConnectionTimeout() {
|
2014-02-09 21:13:09 +01:00
|
|
|
msg := &QuitCommand{
|
|
|
|
message: "connection timeout",
|
|
|
|
}
|
|
|
|
msg.SetClient(client)
|
|
|
|
client.server.commands <- msg
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:47:10 +01:00
|
|
|
func (client *Client) ConnectionClosed() {
|
|
|
|
msg := &QuitCommand{
|
|
|
|
message: "connection closed",
|
|
|
|
}
|
|
|
|
msg.SetClient(client)
|
|
|
|
client.server.commands <- msg
|
|
|
|
}
|
|
|
|
|
2014-02-12 00:00:19 +01:00
|
|
|
func (c *Client) readConn() {
|
|
|
|
for {
|
|
|
|
line, err := c.recv.ReadString('\n')
|
|
|
|
if err != nil {
|
2014-02-13 06:18:02 +01:00
|
|
|
if DEBUG_NET {
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Printf("%s → %s closed", c.conn.RemoteAddr(), c.conn.LocalAddr())
|
|
|
|
} else {
|
|
|
|
log.Printf("%s → %s error: %s", c.conn.RemoteAddr(), c.conn.LocalAddr(), err)
|
|
|
|
}
|
2014-02-12 00:00:19 +01:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2014-02-13 06:18:02 +01:00
|
|
|
|
2014-02-12 00:00:19 +01:00
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s → %s %s", c.conn.RemoteAddr(), c.conn.LocalAddr(), line)
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ParseCommand(line)
|
2012-12-09 21:51:50 +01:00
|
|
|
if err != nil {
|
2014-02-13 06:18:02 +01:00
|
|
|
switch err {
|
|
|
|
case NotEnoughArgsError:
|
2014-02-12 00:00:19 +01:00
|
|
|
c.Reply(ErrNeedMoreParams(c.server, line))
|
2014-02-13 06:18:02 +01:00
|
|
|
default:
|
2014-02-12 00:00:19 +01:00
|
|
|
c.Reply(ErrUnknownCommand(c.server, line))
|
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
|
|
|
|
2014-02-09 08:51:51 +01:00
|
|
|
m.SetClient(c)
|
2012-12-15 23:34:20 +01:00
|
|
|
c.server.commands <- m
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2014-02-13 18:47:10 +01:00
|
|
|
c.ConnectionClosed()
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 00:00:19 +01:00
|
|
|
func (client *Client) maybeLogWriteError(err error) bool {
|
|
|
|
if err != nil {
|
2014-02-13 06:18:02 +01:00
|
|
|
if DEBUG_NET {
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Printf("%s ← %s closed", client.conn.RemoteAddr(), client.conn.LocalAddr())
|
|
|
|
} else {
|
|
|
|
log.Printf("%s ← %s error: %s", client.conn.RemoteAddr(), client.conn.LocalAddr(), err)
|
|
|
|
}
|
2014-02-12 00:00:19 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) writeConn(replies <-chan Reply) {
|
2012-12-15 23:34:20 +01:00
|
|
|
for reply := range replies {
|
2013-05-11 23:43:06 +02:00
|
|
|
if DEBUG_CLIENT {
|
2014-02-12 00:00:19 +01:00
|
|
|
log.Printf("%s ← %s %s", client, reply.Source(), reply)
|
|
|
|
}
|
|
|
|
for _, str := range reply.Format(client) {
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s ← %s %s", client.conn.RemoteAddr(), client.conn.LocalAddr(), str)
|
|
|
|
}
|
|
|
|
if _, err := client.send.WriteString(str); client.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, err := client.send.WriteString(CRLF); client.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := client.send.Flush(); client.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
2013-05-11 23:43:06 +02:00
|
|
|
}
|
2012-04-08 08:32:08 +02:00
|
|
|
}
|
2014-02-13 18:47:10 +01:00
|
|
|
client.ConnectionClosed()
|
2012-04-07 20:44:59 +02:00
|
|
|
}
|
2012-04-09 16:57:55 +02:00
|
|
|
|
2014-02-13 06:04:50 +01:00
|
|
|
func (client *Client) Destroy() {
|
|
|
|
if client.destroyed {
|
|
|
|
return
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|
2014-02-10 04:06:30 +01:00
|
|
|
|
2014-02-09 17:48:11 +01:00
|
|
|
client.conn.Close()
|
2014-02-13 03:33:08 +01:00
|
|
|
|
2014-02-13 06:04:50 +01:00
|
|
|
close(client.replies)
|
2014-02-13 18:35:59 +01:00
|
|
|
client.replies = nil
|
2014-02-13 06:04:50 +01:00
|
|
|
|
2014-02-10 04:06:30 +01:00
|
|
|
if client.idleTimer != nil {
|
|
|
|
client.idleTimer.Stop()
|
|
|
|
}
|
2014-02-13 03:33:08 +01:00
|
|
|
|
2014-02-10 04:06:30 +01:00
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
2014-02-13 03:33:08 +01:00
|
|
|
|
|
|
|
// clear channel list
|
|
|
|
client.channels = make(ChannelSet)
|
|
|
|
|
2014-02-13 06:04:50 +01:00
|
|
|
client.destroyed = true
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 06:04:50 +01:00
|
|
|
func (client *Client) Reply(replies ...Reply) {
|
2014-02-13 18:35:59 +01:00
|
|
|
if client.replies == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-02-12 00:33:02 +01:00
|
|
|
for _, reply := range replies {
|
|
|
|
client.replies <- reply
|
|
|
|
}
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 02:10:04 +01:00
|
|
|
func (client *Client) HasNick() bool {
|
|
|
|
return client.nick != ""
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 02:10:04 +01:00
|
|
|
func (client *Client) HasUsername() bool {
|
|
|
|
return client.username != ""
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:43:59 +01:00
|
|
|
func (client *Client) InterestedClients() ClientSet {
|
2014-02-09 02:10:04 +01:00
|
|
|
clients := make(ClientSet)
|
2014-02-09 02:43:59 +01:00
|
|
|
for channel := range client.channels {
|
|
|
|
for member := range channel.members {
|
2014-02-11 23:32:17 +01:00
|
|
|
clients.Add(member)
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
2012-04-09 16:57:55 +02:00
|
|
|
}
|
2014-02-09 02:10:04 +01:00
|
|
|
return clients
|
2012-04-09 16:57:55 +02:00
|
|
|
}
|
2012-04-18 05:24:26 +02:00
|
|
|
|
2014-02-09 17:53:06 +01:00
|
|
|
// <mode>
|
|
|
|
func (c *Client) ModeString() (str string) {
|
2014-02-08 23:20:23 +01:00
|
|
|
if c.invisible {
|
2014-02-09 17:53:06 +01:00
|
|
|
str += Invisible.String()
|
2014-02-08 23:20:23 +01:00
|
|
|
}
|
2014-02-09 19:07:40 +01:00
|
|
|
if c.operator {
|
|
|
|
str += Operator.String()
|
|
|
|
}
|
|
|
|
|
2014-02-09 17:53:06 +01:00
|
|
|
if len(str) > 0 {
|
|
|
|
str = "+" + str
|
|
|
|
}
|
|
|
|
return
|
2012-04-18 05:24:26 +02:00
|
|
|
}
|
2012-04-18 06:13:12 +02:00
|
|
|
|
2014-02-09 02:10:04 +01:00
|
|
|
func (c *Client) UserHost() string {
|
2014-02-13 03:14:19 +01:00
|
|
|
username := "*"
|
|
|
|
if c.HasUsername() {
|
|
|
|
username = c.username
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
2014-02-13 03:14:19 +01:00
|
|
|
return fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname)
|
2012-12-12 07:34:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 02:10:04 +01:00
|
|
|
func (c *Client) Nick() string {
|
2014-02-13 03:14:19 +01:00
|
|
|
if c.HasNick() {
|
|
|
|
return c.nick
|
|
|
|
}
|
|
|
|
return "*"
|
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 {
|
2014-02-08 22:18:11 +01:00
|
|
|
return c.UserHost()
|
2013-05-11 22:55:01 +02:00
|
|
|
}
|