2012-04-07 20:44:59 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2012-04-18 07:11:35 +02:00
|
|
|
"fmt"
|
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
|
|
|
)
|
|
|
|
|
2014-03-13 01:52:25 +01:00
|
|
|
const (
|
|
|
|
LOGIN_TIMEOUT = time.Minute / 2 // how long the client has to login
|
|
|
|
IDLE_TIMEOUT = time.Minute // how long before a client is considered idle
|
|
|
|
QUIT_TIMEOUT = time.Minute // how long after idle before a client is kicked
|
|
|
|
)
|
|
|
|
|
2012-04-07 20:44:59 +02:00
|
|
|
type Client struct {
|
2014-03-23 06:48:53 +01:00
|
|
|
atime time.Time
|
|
|
|
authorized bool
|
|
|
|
awayMessage Text
|
|
|
|
capabilities CapabilitySet
|
|
|
|
capState CapState
|
|
|
|
channels ChannelSet
|
|
|
|
commands chan Command
|
|
|
|
ctime time.Time
|
|
|
|
flags map[UserMode]bool
|
|
|
|
hasQuit bool
|
|
|
|
hops uint
|
|
|
|
hostname Name
|
|
|
|
idleTimer *time.Timer
|
|
|
|
loginTimer *time.Timer
|
|
|
|
nick Name
|
|
|
|
quitTimer *time.Timer
|
|
|
|
realname Text
|
|
|
|
registered bool
|
|
|
|
server *Server
|
|
|
|
socket *Socket
|
|
|
|
username Name
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:03:33 +01:00
|
|
|
func NewClient(server *Server, conn net.Conn) *Client {
|
2014-02-14 03:59:45 +01:00
|
|
|
now := time.Now()
|
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),
|
2014-03-08 23:24:17 +01:00
|
|
|
commands: make(chan Command),
|
2014-03-01 04:21:33 +01:00
|
|
|
ctime: now,
|
|
|
|
flags: make(map[UserMode]bool),
|
|
|
|
server: server,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2014-02-24 07:21:39 +01:00
|
|
|
client.socket = NewSocket(conn, client.commands)
|
2014-02-18 22:25:21 +01:00
|
|
|
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout)
|
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
|
|
|
|
//
|
|
|
|
|
2014-03-28 00:49:22 +01:00
|
|
|
func (client *Client) send(command Command) {
|
|
|
|
command.SetClient(client)
|
|
|
|
client.server.commands <- command
|
|
|
|
}
|
|
|
|
|
2014-02-24 07:21:39 +01:00
|
|
|
func (client *Client) run() {
|
2014-03-28 00:49:22 +01:00
|
|
|
client.send(&ProxyCommand{
|
|
|
|
hostname: AddrLookupHostname(client.socket.conn.RemoteAddr()),
|
|
|
|
})
|
2014-02-24 07:21:39 +01:00
|
|
|
|
2014-03-28 00:49:22 +01:00
|
|
|
for command := range client.commands {
|
2014-02-24 18:41:09 +01:00
|
|
|
checkPass, ok := command.(checkPasswordCommand)
|
|
|
|
if ok {
|
|
|
|
checkPass.LoadPassword(client.server)
|
2014-03-15 23:12:29 +01:00
|
|
|
// Block the client thread while handling a potentially expensive
|
|
|
|
// password bcrypt operation. Since the server is single-threaded
|
|
|
|
// for commands, we don't want the server to perform the bcrypt,
|
|
|
|
// blocking anyone else from sending commands until it
|
|
|
|
// completes. This could be a form of DoS if handled naively.
|
2014-02-24 18:41:09 +01:00
|
|
|
checkPass.CheckPassword()
|
2014-02-24 07:21:39 +01:00
|
|
|
}
|
2014-02-24 18:41:09 +01:00
|
|
|
|
2014-03-28 00:49:22 +01:00
|
|
|
client.send(command)
|
2014-02-24 07:21:39 +01:00
|
|
|
}
|
2014-03-28 00:49:22 +01:00
|
|
|
|
|
|
|
client.send(&QuitCommand{
|
|
|
|
message: "connection closed",
|
|
|
|
})
|
2014-02-24 07:21:39 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 04:21:33 +01:00
|
|
|
func (client *Client) connectionTimeout() {
|
|
|
|
client.commands <- &QuitCommand{
|
|
|
|
message: "connection timeout",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2014-02-22 20:40:32 +01:00
|
|
|
client.Reply(RplPing(client))
|
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.loginTimer.Stop()
|
|
|
|
client.Touch()
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 03:46:46 +01:00
|
|
|
func (client *Client) destroy() {
|
2014-02-18 08:58:02 +01:00
|
|
|
// clean up channels
|
|
|
|
|
|
|
|
for channel := range client.channels {
|
|
|
|
channel.Quit(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up server
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
client.server.clients.Remove(client)
|
2014-02-14 08:16:07 +01:00
|
|
|
|
2014-02-23 20:08:01 +01:00
|
|
|
// clean up self
|
|
|
|
|
|
|
|
if client.loginTimer != nil {
|
|
|
|
client.loginTimer.Stop()
|
|
|
|
}
|
|
|
|
if client.idleTimer != nil {
|
|
|
|
client.idleTimer.Stop()
|
|
|
|
}
|
|
|
|
if client.quitTimer != nil {
|
|
|
|
client.quitTimer.Stop()
|
|
|
|
}
|
|
|
|
|
2014-02-24 02:04:24 +01:00
|
|
|
client.socket.Close()
|
|
|
|
|
2014-03-09 00:01:15 +01:00
|
|
|
Log.debug.Printf("%s: destroyed", client)
|
2014-02-09 02:10:04 +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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
client.server.clients.Add(client)
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (client *Client) ChangeNickname(nickname Name) {
|
2014-02-19 00:36:58 +01:00
|
|
|
// Make reply before changing nick to capture original source id.
|
2014-02-17 07:20:42 +01:00
|
|
|
reply := RplNick(client, nickname)
|
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
|
2014-02-19 00:36:58 +01:00
|
|
|
client.server.clients.Add(client)
|
2014-02-19 00:28:20 +01:00
|
|
|
for friend := range client.Friends() {
|
2014-02-22 20:40:32 +01:00
|
|
|
friend.Reply(reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 21:54:48 +01:00
|
|
|
func (client *Client) Reply(reply string, args ...interface{}) {
|
|
|
|
if len(args) > 0 {
|
|
|
|
reply = fmt.Sprintf(reply, args...)
|
|
|
|
}
|
2014-02-24 02:04:24 +01:00
|
|
|
client.socket.Write(reply)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (client *Client) Quit(message Text) {
|
2014-02-18 22:25:21 +01:00
|
|
|
if client.hasQuit {
|
|
|
|
return
|
|
|
|
}
|
2014-02-20 03:46:46 +01:00
|
|
|
|
|
|
|
client.hasQuit = true
|
2014-03-28 00:49:22 +01:00
|
|
|
client.Reply(RplError("quit"))
|
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)
|
2014-02-20 03:46:46 +01:00
|
|
|
client.destroy()
|
2014-02-18 22:25:21 +01:00
|
|
|
|
2014-02-19 00:28:20 +01:00
|
|
|
if len(friends) > 0 {
|
2014-02-17 07:20:42 +01:00
|
|
|
reply := RplQuit(client, message)
|
2014-02-19 00:28:20 +01:00
|
|
|
for friend := range friends {
|
2014-02-22 20:40:32 +01:00
|
|
|
friend.Reply(reply)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|