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"
|
2014-02-17 08:29:11 +01:00
|
|
|
"strings"
|
2012-12-12 08:12:35 +01:00
|
|
|
"time"
|
2012-04-07 20:44:59 +02:00
|
|
|
)
|
|
|
|
|
2014-02-18 02:58:22 +01:00
|
|
|
func IsNickname(nick string) bool {
|
|
|
|
return NicknameExpr.MatchString(nick)
|
|
|
|
}
|
|
|
|
|
2012-04-07 20:44:59 +02:00
|
|
|
type Client struct {
|
2014-02-14 03:39:33 +01:00
|
|
|
atime time.Time
|
2014-02-12 00:44:58 +01:00
|
|
|
awayMessage string
|
|
|
|
channels ChannelSet
|
2014-02-14 03:59:45 +01:00
|
|
|
ctime time.Time
|
2014-02-17 22:22:35 +01:00
|
|
|
flags map[UserMode]bool
|
2014-02-17 02:23:47 +01:00
|
|
|
friends map[*Client]uint
|
2014-02-18 06:30:14 +01:00
|
|
|
hops uint
|
2014-02-12 00:44:58 +01:00
|
|
|
hostname string
|
|
|
|
idleTimer *time.Timer
|
2014-02-13 22:19:26 +01:00
|
|
|
loginTimer *time.Timer
|
2014-02-12 00:44:58 +01:00
|
|
|
nick string
|
2014-02-15 03:28:36 +01:00
|
|
|
phase Phase
|
2014-02-12 00:44:58 +01:00
|
|
|
quitTimer *time.Timer
|
|
|
|
realname string
|
2014-02-13 22:19:26 +01:00
|
|
|
replies chan Reply
|
2014-02-12 00:44:58 +01:00
|
|
|
server *Server
|
2014-02-14 04:37:16 +01:00
|
|
|
socket *Socket
|
2014-02-12 00:44:58 +01:00
|
|
|
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 {
|
2014-02-14 03:59:45 +01:00
|
|
|
now := time.Now()
|
2012-12-09 21:51:50 +01:00
|
|
|
client := &Client{
|
2014-02-14 03:59:45 +01:00
|
|
|
atime: now,
|
2014-02-09 17:48:11 +01:00
|
|
|
channels: make(ChannelSet),
|
2014-02-14 03:59:45 +01:00
|
|
|
ctime: now,
|
2014-02-17 22:22:35 +01:00
|
|
|
flags: make(map[UserMode]bool),
|
2014-02-17 02:23:47 +01:00
|
|
|
friends: make(map[*Client]uint),
|
2014-02-14 03:11:32 +01:00
|
|
|
hostname: AddrLookupHostname(conn.RemoteAddr()),
|
2014-02-15 03:28:36 +01:00
|
|
|
phase: server.InitPhase(),
|
2014-02-13 22:19:26 +01:00
|
|
|
replies: make(chan Reply),
|
2014-02-09 17:48:11 +01:00
|
|
|
server: server,
|
2014-02-14 04:37:16 +01:00
|
|
|
socket: NewSocket(conn),
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-18 02:58:22 +01:00
|
|
|
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionClosed)
|
2014-02-14 04:37:16 +01:00
|
|
|
go client.readCommands()
|
|
|
|
go client.writeReplies()
|
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-17 07:20:42 +01:00
|
|
|
func (client *Client) readCommands() {
|
|
|
|
for line := range client.socket.Read() {
|
|
|
|
msg, err := ParseCommand(line)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case NotEnoughArgsError:
|
2014-02-17 08:29:11 +01:00
|
|
|
parts := strings.SplitN(line, " ", 2)
|
|
|
|
client.Reply(ErrNeedMoreParams(client.server, parts[0]))
|
2014-02-17 07:20:42 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.SetClient(client)
|
|
|
|
client.server.commands <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) writeReplies() {
|
|
|
|
for reply := range client.replies {
|
|
|
|
if DEBUG_CLIENT {
|
|
|
|
log.Printf("%s ← %s", client, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.socket.Write(reply.Format(client))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 21:13:09 +01:00
|
|
|
func (client *Client) Touch() {
|
2014-02-14 03:39:33 +01:00
|
|
|
client.atime = time.Now()
|
|
|
|
|
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 {
|
|
|
|
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
|
|
|
|
} else {
|
|
|
|
client.idleTimer.Reset(IDLE_TIMEOUT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 03:10:52 +01:00
|
|
|
type ClientIdle struct {
|
|
|
|
BaseCommand
|
|
|
|
}
|
|
|
|
|
2014-02-09 21:13:09 +01:00
|
|
|
func (client *Client) Idle() {
|
|
|
|
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-14 03:39:33 +01:00
|
|
|
|
2014-02-18 03:10:52 +01:00
|
|
|
cmd := &ClientIdle{}
|
|
|
|
cmd.SetClient(client)
|
|
|
|
client.server.commands <- cmd
|
2014-02-09 21:13:09 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 02:58:22 +01:00
|
|
|
func (client *Client) connectionTimeout() {
|
2014-02-09 21:13:09 +01:00
|
|
|
msg := &QuitCommand{
|
|
|
|
message: "connection timeout",
|
|
|
|
}
|
|
|
|
msg.SetClient(client)
|
2014-02-17 07:20:42 +01:00
|
|
|
client.server.commands <- msg
|
2014-02-09 21:13:09 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 02:58:22 +01:00
|
|
|
func (client *Client) connectionClosed() {
|
2014-02-13 18:47:10 +01:00
|
|
|
msg := &QuitCommand{
|
|
|
|
message: "connection closed",
|
|
|
|
}
|
|
|
|
msg.SetClient(client)
|
2014-02-17 07:20:42 +01:00
|
|
|
client.server.commands <- msg
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (client *Client) Destroy() {
|
|
|
|
if DEBUG_CLIENT {
|
|
|
|
log.Printf("%s destroy", client)
|
2012-04-08 08:32:08 +02:00
|
|
|
}
|
2012-04-09 16:57:55 +02:00
|
|
|
|
2014-02-14 05:23:22 +01:00
|
|
|
client.socket.Close()
|
2014-02-13 06:04:50 +01:00
|
|
|
|
2014-02-17 03:16:13 +01:00
|
|
|
client.loginTimer.Stop()
|
|
|
|
|
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
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
client.server.clients.Remove(client)
|
2014-02-14 08:16:07 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
if DEBUG_CLIENT {
|
|
|
|
log.Printf("%s destroyed", client)
|
2014-02-14 05:42:06 +01:00
|
|
|
}
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 04:35:25 +01:00
|
|
|
func (client *Client) Reply(reply Reply) {
|
|
|
|
if client.replies == nil {
|
|
|
|
if DEBUG_CLIENT {
|
|
|
|
log.Printf("%s dropped %s", client, reply)
|
2014-02-14 04:41:51 +01:00
|
|
|
}
|
2014-02-15 04:35:25 +01:00
|
|
|
return
|
2014-02-12 00:33:02 +01:00
|
|
|
}
|
2014-02-15 04:35:25 +01:00
|
|
|
client.replies <- reply
|
2012-12-17 04:13:53 +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-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
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (client *Client) AddFriend(friend *Client) {
|
|
|
|
client.friends[friend] += 1
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (client *Client) RemoveFriend(friend *Client) {
|
|
|
|
client.friends[friend] -= 1
|
|
|
|
if client.friends[friend] <= 0 {
|
|
|
|
delete(client.friends, friend)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (client *Client) ChangeNickname(nickname string) {
|
2014-02-17 02:23:47 +01:00
|
|
|
// Make reply before changing nick.
|
2014-02-17 07:20:42 +01:00
|
|
|
reply := RplNick(client, nickname)
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
client.nick = nickname
|
2014-02-17 02:23:47 +01:00
|
|
|
|
|
|
|
for friend := range client.friends {
|
|
|
|
friend.Reply(reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (client *Client) Quit(message string) {
|
2014-02-17 02:23:47 +01:00
|
|
|
if len(client.friends) > 0 {
|
2014-02-17 07:20:42 +01:00
|
|
|
reply := RplQuit(client, message)
|
2014-02-17 02:23:47 +01:00
|
|
|
for friend := range client.friends {
|
|
|
|
if friend == client {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
friend.Reply(reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for channel := range client.channels {
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.Quit(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:10:48 +01:00
|
|
|
client.Reply(RplError(client.server, client))
|
2014-02-17 02:23:47 +01:00
|
|
|
client.Destroy()
|
|
|
|
}
|