2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2012-04-18 07:11:35 +02:00
|
|
|
"fmt"
|
2016-06-30 11:28:34 +02:00
|
|
|
"log"
|
2012-04-07 20:44:59 +02:00
|
|
|
"net"
|
2016-06-30 07:35:34 +02:00
|
|
|
"strconv"
|
2012-12-12 08:12:35 +01:00
|
|
|
"time"
|
2016-06-17 14:17:42 +02:00
|
|
|
|
|
|
|
"github.com/DanielOaks/girc-go/ircmsg"
|
2016-06-30 11:28:34 +02:00
|
|
|
"github.com/DanielOaks/go-ident"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
2014-03-13 01:52:25 +01:00
|
|
|
const (
|
2016-07-02 11:12:00 +02:00
|
|
|
IDLE_TIMEOUT = time.Minute + time.Second*30 // how long before a client is considered idle
|
|
|
|
QUIT_TIMEOUT = time.Minute // how long after idle before a client is kicked
|
|
|
|
IdentTimeoutSeconds = 8
|
2016-06-30 07:35:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
TIMEOUT_STATED_SECONDS = strconv.Itoa(int((IDLE_TIMEOUT + QUIT_TIMEOUT).Seconds()))
|
2014-03-13 01:52:25 +01:00
|
|
|
)
|
|
|
|
|
2012-04-07 20:44:59 +02:00
|
|
|
type Client struct {
|
2016-06-19 06:55:24 +02:00
|
|
|
atime time.Time
|
|
|
|
authorized bool
|
|
|
|
awayMessage string
|
|
|
|
capabilities CapabilitySet
|
|
|
|
capState CapState
|
|
|
|
channels ChannelSet
|
|
|
|
ctime time.Time
|
|
|
|
flags map[UserMode]bool
|
2016-06-22 14:04:13 +02:00
|
|
|
isQuitting bool
|
2016-06-19 06:55:24 +02:00
|
|
|
hasQuit bool
|
|
|
|
hops uint
|
|
|
|
hostname Name
|
|
|
|
idleTimer *time.Timer
|
|
|
|
nick Name
|
|
|
|
nickString string // cache for nick string since it's used with most numerics
|
2016-06-19 07:43:16 +02:00
|
|
|
nickMaskString string // cache for nickmask string since it's used with lots of replies
|
2016-06-19 06:55:24 +02:00
|
|
|
quitTimer *time.Timer
|
2016-06-19 13:59:18 +02:00
|
|
|
realname string
|
2016-08-14 03:59:33 +02:00
|
|
|
accountName string
|
2016-06-19 06:55:24 +02:00
|
|
|
registered bool
|
|
|
|
server *Server
|
|
|
|
socket *Socket
|
|
|
|
username Name
|
|
|
|
isDestroyed bool
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 17:09:07 +02:00
|
|
|
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
|
2014-02-14 03:59:45 +01:00
|
|
|
now := time.Now()
|
2016-06-15 13:21:45 +02:00
|
|
|
socket := NewSocket(conn)
|
2012-12-09 21:51:50 +01:00
|
|
|
client := &Client{
|
2014-03-01 04:21:33 +01:00
|
|
|
atime: now,
|
2014-03-07 05:09:29 +01:00
|
|
|
authorized: server.password == nil,
|
2014-03-01 04:21:33 +01:00
|
|
|
capState: CapNone,
|
|
|
|
capabilities: make(CapabilitySet),
|
|
|
|
channels: make(ChannelSet),
|
|
|
|
ctime: now,
|
|
|
|
flags: make(map[UserMode]bool),
|
|
|
|
server: server,
|
2016-06-15 13:21:45 +02:00
|
|
|
socket: &socket,
|
2016-08-14 03:59:33 +02:00
|
|
|
accountName: "*", // * is used until actual account name is set
|
2016-06-20 14:53:45 +02:00
|
|
|
nickString: "*", // * is used until actual nick is given
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2016-06-28 17:09:07 +02:00
|
|
|
if isTLS {
|
|
|
|
client.flags[TLS] = true
|
|
|
|
}
|
2016-06-30 11:28:34 +02:00
|
|
|
if server.checkIdent {
|
|
|
|
_, serverPortString, err := net.SplitHostPort(conn.LocalAddr().String())
|
|
|
|
serverPort, _ := strconv.Atoi(serverPortString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
clientHost, clientPortString, err := net.SplitHostPort(conn.RemoteAddr().String())
|
|
|
|
clientPort, _ := strconv.Atoi(clientPortString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Notice("*** Looking up your username")
|
2016-07-02 11:12:00 +02:00
|
|
|
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
|
2016-06-30 11:28:34 +02:00
|
|
|
if err == nil {
|
|
|
|
username := resp.Identifier
|
|
|
|
//TODO(dan): replace this with IsUsername/IsIRCName?
|
|
|
|
if Name(username).IsNickname() {
|
|
|
|
client.Notice("*** Found your username")
|
|
|
|
client.username = Name(username)
|
|
|
|
// we don't need to updateNickMask here since nickMask is not used for anything yet
|
|
|
|
} else {
|
|
|
|
client.Notice("*** Got a malformed username, ignoring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client.Notice("*** Could not find your username")
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 17:49:52 +02:00
|
|
|
client.Touch()
|
2014-02-24 07:21:39 +01:00
|
|
|
go client.run()
|
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
|
|
|
}
|
|
|
|
|
2014-02-24 07:21:39 +01:00
|
|
|
//
|
|
|
|
// command goroutine
|
|
|
|
//
|
|
|
|
|
|
|
|
func (client *Client) run() {
|
2014-04-15 17:49:52 +02:00
|
|
|
var err error
|
2016-06-17 14:17:42 +02:00
|
|
|
var isExiting bool
|
2014-04-15 17:49:52 +02:00
|
|
|
var line string
|
2016-06-17 14:17:42 +02:00
|
|
|
var msg ircmsg.IrcMessage
|
2014-04-15 17:49:52 +02:00
|
|
|
|
|
|
|
// Set the hostname for this client. The client may later send a PROXY
|
|
|
|
// command from stunnel that sets the hostname to something more accurate.
|
2016-04-21 08:39:31 +02:00
|
|
|
client.hostname = AddrLookupHostname(client.socket.conn.RemoteAddr())
|
2014-04-15 17:49:52 +02:00
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
//TODO(dan): Make this a socketreactor from ircbnc
|
|
|
|
for {
|
|
|
|
line, err = client.socket.Read()
|
|
|
|
if err != nil {
|
|
|
|
client.Quit("connection closed")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-06-19 02:01:30 +02:00
|
|
|
msg, err = ircmsg.ParseLine(line)
|
2016-06-17 14:17:42 +02:00
|
|
|
if err != nil {
|
2016-06-20 14:53:45 +02:00
|
|
|
client.Quit("received malformed line")
|
2016-06-17 14:17:42 +02:00
|
|
|
break
|
2014-02-24 07:21:39 +01:00
|
|
|
}
|
2014-04-15 17:49:52 +02:00
|
|
|
|
2016-06-19 02:01:30 +02:00
|
|
|
cmd, exists := Commands[msg.Command]
|
|
|
|
if !exists {
|
2016-06-20 14:53:45 +02:00
|
|
|
client.Send(nil, client.server.nameString, ERR_UNKNOWNCOMMAND, client.nickString, msg.Command, "Unknown command")
|
|
|
|
continue
|
2016-06-19 02:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isExiting = cmd.Run(client.server, client, msg)
|
2016-06-22 14:04:13 +02:00
|
|
|
if isExiting || client.isQuitting {
|
2016-06-17 14:17:42 +02:00
|
|
|
break
|
|
|
|
}
|
2014-02-24 07:21:39 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
// ensure client connection gets closed
|
2016-06-19 02:01:30 +02:00
|
|
|
client.destroy()
|
2014-04-15 17:49:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
//
|
2014-04-15 17:49:52 +02:00
|
|
|
// quit timer goroutine
|
2016-06-17 14:17:42 +02:00
|
|
|
//
|
2014-04-15 17:49:52 +02:00
|
|
|
|
2014-03-01 04:21:33 +01:00
|
|
|
func (client *Client) connectionTimeout() {
|
2016-06-30 07:35:34 +02:00
|
|
|
client.Quit(fmt.Sprintf("Ping timeout: %s seconds", TIMEOUT_STATED_SECONDS))
|
2016-06-22 14:04:13 +02:00
|
|
|
client.isQuitting = true
|
2014-03-01 04:21:33 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 22:25:21 +01:00
|
|
|
//
|
|
|
|
// idle timer goroutine
|
|
|
|
//
|
|
|
|
|
|
|
|
func (client *Client) connectionIdle() {
|
|
|
|
client.server.idle <- client
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// server goroutine
|
|
|
|
//
|
|
|
|
|
|
|
|
func (client *Client) Active() {
|
2014-02-14 03:39:33 +01:00
|
|
|
client.atime = time.Now()
|
2014-02-18 22:25:21 +01:00
|
|
|
}
|
2014-02-14 03:39:33 +01:00
|
|
|
|
2014-02-18 22:25:21 +01:00
|
|
|
func (client *Client) Touch() {
|
2014-02-09 21:13:09 +01:00
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
2014-02-14 03:39:33 +01:00
|
|
|
|
2014-02-09 21:13:09 +01:00
|
|
|
if client.idleTimer == nil {
|
2014-02-18 20:22:56 +01:00
|
|
|
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
|
2014-02-09 21:13:09 +01:00
|
|
|
} else {
|
|
|
|
client.idleTimer.Reset(IDLE_TIMEOUT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Idle() {
|
2016-06-19 07:37:29 +02:00
|
|
|
client.Send(nil, "", "PING", client.nickString)
|
2014-02-18 20:22:56 +01:00
|
|
|
|
2014-02-09 21:13:09 +01:00
|
|
|
if client.quitTimer == nil {
|
2014-02-18 02:58:22 +01:00
|
|
|
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
|
2014-02-09 21:13:09 +01:00
|
|
|
} else {
|
|
|
|
client.quitTimer.Reset(QUIT_TIMEOUT)
|
|
|
|
}
|
2014-02-18 20:22:56 +01:00
|
|
|
}
|
2014-02-14 03:39:33 +01:00
|
|
|
|
2014-02-18 22:25:21 +01:00
|
|
|
func (client *Client) Register() {
|
2014-03-13 01:52:25 +01:00
|
|
|
if client.registered {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.registered = true
|
2014-02-18 22:25:21 +01:00
|
|
|
client.Touch()
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 00:25:32 +01:00
|
|
|
func (client *Client) IdleTime() time.Duration {
|
|
|
|
return time.Since(client.atime)
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:56:06 +01:00
|
|
|
func (client *Client) SignonTime() int64 {
|
|
|
|
return client.ctime.Unix()
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:08:57 +01:00
|
|
|
func (client *Client) IdleSeconds() uint64 {
|
|
|
|
return uint64(client.IdleTime().Seconds())
|
|
|
|
}
|
|
|
|
|
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 17:53:06 +01:00
|
|
|
// <mode>
|
|
|
|
func (c *Client) ModeString() (str string) {
|
2014-02-17 22:22:35 +01:00
|
|
|
for flag := range c.flags {
|
|
|
|
str += flag.String()
|
2014-02-09 19:07:40 +01:00
|
|
|
}
|
|
|
|
|
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-03-09 21:45:36 +01:00
|
|
|
func (c *Client) UserHost() Name {
|
2014-02-13 03:14:19 +01:00
|
|
|
username := "*"
|
|
|
|
if c.HasUsername() {
|
2014-03-09 21:45:36 +01:00
|
|
|
username = c.username.String()
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
return Name(fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname))
|
2012-12-12 07:34:22 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (c *Client) Nick() Name {
|
2014-02-13 03:14:19 +01:00
|
|
|
if c.HasNick() {
|
|
|
|
return c.nick
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
return Name("*")
|
2012-04-18 06:13:12 +02:00
|
|
|
}
|
2012-04-18 07:11:35 +02:00
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (c *Client) Id() Name {
|
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-03-09 21:45:36 +01:00
|
|
|
return c.Id().String()
|
2013-05-11 22:55:01 +02:00
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
// Friends refers to clients that share a channel with this client.
|
2014-02-19 00:28:20 +01:00
|
|
|
func (client *Client) Friends() ClientSet {
|
|
|
|
friends := make(ClientSet)
|
|
|
|
friends.Add(client)
|
|
|
|
for channel := range client.channels {
|
|
|
|
for member := range channel.members {
|
|
|
|
friends.Add(member)
|
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
2014-02-19 00:28:20 +01:00
|
|
|
return friends
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 07:37:29 +02:00
|
|
|
func (client *Client) updateNickMask() {
|
|
|
|
client.nickString = client.nick.String()
|
|
|
|
client.nickMaskString = fmt.Sprintf("%s!%s@%s", client.nickString, client.username, client.hostname)
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (client *Client) SetNickname(nickname Name) {
|
2014-03-17 20:11:35 +01:00
|
|
|
if client.HasNick() {
|
|
|
|
Log.error.Printf("%s nickname already set!", client)
|
|
|
|
return
|
|
|
|
}
|
2014-02-19 00:36:58 +01:00
|
|
|
client.nick = nickname
|
2016-06-19 07:37:29 +02:00
|
|
|
client.updateNickMask()
|
2014-02-19 00:36:58 +01:00
|
|
|
client.server.clients.Add(client)
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (client *Client) ChangeNickname(nickname Name) {
|
2016-06-19 07:37:29 +02:00
|
|
|
origNickMask := client.nickMaskString
|
2014-02-19 00:36:58 +01:00
|
|
|
client.server.clients.Remove(client)
|
2014-03-06 22:55:25 +01:00
|
|
|
client.server.whoWas.Append(client)
|
2014-02-17 07:20:42 +01:00
|
|
|
client.nick = nickname
|
2016-06-19 07:37:29 +02:00
|
|
|
client.updateNickMask()
|
2014-02-19 00:36:58 +01:00
|
|
|
client.server.clients.Add(client)
|
2016-06-19 07:37:29 +02:00
|
|
|
client.Send(nil, origNickMask, "NICK", nickname.String())
|
2014-02-19 00:28:20 +01:00
|
|
|
for friend := range client.Friends() {
|
2016-06-19 07:37:29 +02:00
|
|
|
friend.Send(nil, origNickMask, "NICK", nickname.String())
|
2014-02-22 20:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:07:25 +02:00
|
|
|
func (client *Client) Reply(reply string) error {
|
2016-06-15 13:21:45 +02:00
|
|
|
//TODO(dan): We'll be passing around real message objects instead of raw strings
|
|
|
|
return client.socket.WriteLine(reply)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 02:01:30 +02:00
|
|
|
func (client *Client) Quit(message string) {
|
2016-06-19 06:55:24 +02:00
|
|
|
client.Send(nil, client.nickMaskString, "QUIT", message)
|
|
|
|
client.Send(nil, client.nickMaskString, "ERROR", message)
|
2016-06-17 14:17:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) destroy() {
|
|
|
|
if client.isDestroyed {
|
2014-02-18 22:25:21 +01:00
|
|
|
return
|
|
|
|
}
|
2014-02-20 03:46:46 +01:00
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
client.isDestroyed = true
|
2014-03-06 22:55:25 +01:00
|
|
|
client.server.whoWas.Append(client)
|
2014-02-19 00:28:20 +01:00
|
|
|
friends := client.Friends()
|
|
|
|
friends.Remove(client)
|
2016-06-17 14:17:42 +02:00
|
|
|
|
|
|
|
// clean up channels
|
|
|
|
for channel := range client.channels {
|
|
|
|
channel.Quit(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up server
|
|
|
|
client.server.clients.Remove(client)
|
|
|
|
|
|
|
|
// clean up self
|
|
|
|
if client.idleTimer != nil {
|
|
|
|
client.idleTimer.Stop()
|
|
|
|
}
|
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
client.socket.Close()
|
2016-06-19 02:01:30 +02:00
|
|
|
for friend := range client.Friends() {
|
|
|
|
//TODO(dan): store quit message in user, if exists use that instead here
|
2016-06-19 06:55:24 +02:00
|
|
|
friend.Send(nil, client.nickMaskString, "QUIT", "Exited")
|
2016-06-19 02:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-18 22:25:21 +01:00
|
|
|
|
2016-06-19 02:01:30 +02:00
|
|
|
// Send sends an IRC line to the client.
|
|
|
|
func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
|
2016-08-13 14:04:21 +02:00
|
|
|
// attach server-time
|
|
|
|
if client.capabilities[ServerTime] {
|
|
|
|
if tags == nil {
|
|
|
|
tags = ircmsg.MakeTags("time", time.Now().Format(time.RFC3339))
|
|
|
|
} else {
|
|
|
|
(*tags)["time"] = ircmsg.MakeTagValue(time.Now().Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send out the message
|
2016-06-19 02:01:30 +02:00
|
|
|
ircmsg := ircmsg.MakeMessage(tags, prefix, command, params...)
|
|
|
|
line, err := ircmsg.Line()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
2016-06-19 02:01:30 +02:00
|
|
|
client.socket.Write(line)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notice sends the client a notice from the server.
|
|
|
|
func (client *Client) Notice(text string) {
|
2016-06-19 07:43:16 +02:00
|
|
|
client.Send(nil, client.server.nameString, "NOTICE", client.nickString, text)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|