ircd/irc/client.go

395 lines
9.9 KiB
Go
Raw Normal View History

// 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 18:44:59 +00:00
package irc
import (
2012-04-18 05:11:35 +00:00
"fmt"
"log"
2012-04-07 18:44:59 +00:00
"net"
"strconv"
2012-12-12 07:12:35 +00:00
"time"
"github.com/DanielOaks/girc-go/ircmsg"
"github.com/DanielOaks/go-ident"
2012-04-07 18:44:59 +00:00
)
const (
2016-07-02 09:12:00 +00: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
)
var (
TIMEOUT_STATED_SECONDS = strconv.Itoa(int((IDLE_TIMEOUT + QUIT_TIMEOUT).Seconds()))
)
2012-04-07 18:44:59 +00:00
type Client struct {
account *ClientAccount
atime time.Time
authorized bool
awayMessage string
capabilities CapabilitySet
capState CapState
certfp string
channels ChannelSet
ctime time.Time
flags map[UserMode]bool
isDestroyed bool
isQuitting bool
hasQuit bool
hops uint
hostname string
idleTimer *time.Timer
nick string
nickCasefolded string
nickMaskString string // cache for nickmask string since it's used with lots of replies
nickMaskCasefolded string
quitTimer *time.Timer
realname string
registered bool
saslInProgress bool
saslMechanism string
saslValue string
server *Server
socket *Socket
username string
}
2016-06-28 15:09:07 +00:00
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
2014-02-14 02:59:45 +00:00
now := time.Now()
2016-06-15 11:21:45 +00:00
socket := NewSocket(conn)
2012-12-09 20:51:50 +00:00
client := &Client{
atime: now,
authorized: server.password == nil,
capState: CapNone,
capabilities: make(CapabilitySet),
channels: make(ChannelSet),
ctime: now,
flags: make(map[UserMode]bool),
server: server,
socket: &socket,
account: &NoAccount,
nick: "*", // * is used until actual nick is given
nickCasefolded: "*",
nickMaskString: "*", // * is used until actual nick is given
2012-12-09 20:51:50 +00:00
}
2016-06-28 15:09:07 +00:00
if isTLS {
client.flags[TLS] = true
2016-09-07 11:32:58 +00:00
// error is not useful to us here anyways so we can ignore it
client.certfp, _ = client.socket.CertFP()
2016-06-28 15:09:07 +00: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 09:12:00 +00:00
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
if err == nil {
username := resp.Identifier
_, err := CasefoldName(username) // ensure it's a valid username
if err == nil {
client.Notice("*** Found your username")
client.username = 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")
}
}
client.Touch()
2014-02-24 06:21:39 +00:00
go client.run()
2012-12-13 07:27:17 +00:00
2012-04-08 06:32:08 +00:00
return client
2012-04-07 18:44:59 +00:00
}
2014-02-24 06:21:39 +00:00
//
// command goroutine
//
func (client *Client) run() {
var err error
var isExiting bool
var line string
var msg ircmsg.IrcMessage
// Set the hostname for this client. The client may later send a PROXY
// command from stunnel that sets the hostname to something more accurate.
client.hostname = AddrLookupHostname(client.socket.conn.RemoteAddr())
//TODO(dan): Make this a socketreactor from ircbnc
for {
line, err = client.socket.Read()
if err != nil {
client.Quit("connection closed")
break
}
msg, err = ircmsg.ParseLine(line)
if err != nil {
client.Quit("received malformed line")
break
2014-02-24 06:21:39 +00:00
}
cmd, exists := Commands[msg.Command]
if !exists {
client.Send(nil, client.server.name, ERR_UNKNOWNCOMMAND, client.nick, msg.Command, "Unknown command")
continue
}
isExiting = cmd.Run(client.server, client, msg)
2016-06-22 12:04:13 +00:00
if isExiting || client.isQuitting {
break
}
2014-02-24 06:21:39 +00:00
}
// ensure client connection gets closed
client.destroy()
}
//
// quit timer goroutine
//
func (client *Client) connectionTimeout() {
client.Quit(fmt.Sprintf("Ping timeout: %s seconds", TIMEOUT_STATED_SECONDS))
2016-06-22 12:04:13 +00:00
client.isQuitting = true
}
2014-02-18 21:25:21 +00:00
//
// idle timer goroutine
//
func (client *Client) connectionIdle() {
client.server.idle <- client
}
//
// server goroutine
//
func (client *Client) Active() {
client.atime = time.Now()
2014-02-18 21:25:21 +00:00
}
2014-02-18 21:25:21 +00:00
func (client *Client) Touch() {
2014-02-09 20:13:09 +00:00
if client.quitTimer != nil {
client.quitTimer.Stop()
}
2014-02-09 20:13:09 +00:00
if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
2014-02-09 20:13:09 +00:00
} else {
client.idleTimer.Reset(IDLE_TIMEOUT)
}
}
func (client *Client) Idle() {
client.Send(nil, "", "PING", client.nick)
2014-02-09 20:13:09 +00:00
if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
2014-02-09 20:13:09 +00:00
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
}
2014-02-18 21:25:21 +00:00
func (client *Client) Register() {
if client.registered {
return
}
client.registered = true
2014-02-18 21:25:21 +00:00
client.Touch()
2012-12-13 07:27:17 +00:00
}
2014-02-17 23:25:32 +00:00
func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime)
}
2014-02-18 03:56:06 +00:00
func (client *Client) SignonTime() int64 {
return client.ctime.Unix()
}
2014-02-18 03:08:57 +00:00
func (client *Client) IdleSeconds() uint64 {
return uint64(client.IdleTime().Seconds())
}
func (client *Client) HasNick() bool {
return client.nick != "" && client.nick != "*"
}
func (client *Client) HasUsername() bool {
return client.username != "" && client.username != "*"
}
2014-02-09 16:53:06 +00:00
// <mode>
func (c *Client) ModeString() (str string) {
2016-09-07 11:50:42 +00:00
str = "+"
2014-02-17 21:22:35 +00:00
for flag := range c.flags {
str += flag.String()
2014-02-09 18:07:40 +00:00
}
2014-02-09 16:53:06 +00:00
return
2012-04-18 03:24:26 +00:00
}
2012-04-18 04:13:12 +00:00
func (c *Client) UserHost() string {
return fmt.Sprintf("%s!%s@%s", c.nick, c.username, c.hostname)
2012-04-18 04:13:12 +00:00
}
2012-04-18 05:11:35 +00:00
func (c *Client) Id() string {
return c.UserHost()
2012-04-18 05:11:35 +00:00
}
// Friends refers to clients that share a channel with this client.
2014-02-18 23:28:20 +00: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-18 23:28:20 +00:00
return friends
}
2016-06-19 05:37:29 +00:00
func (client *Client) updateNickMask() {
casefoldedName, err := CasefoldName(client.nick)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Nick [%s] couldn't be casefolded... this should never happen.", client.nick))
}
client.nickCasefolded = casefoldedName
client.nickMaskString = fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname)
nickMaskCasefolded, err := Casefold(client.nickMaskString)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Nickmask [%s] couldn't be casefolded... this should never happen.", client.nickMaskString))
}
client.nickMaskCasefolded = nickMaskCasefolded
2016-06-19 05:37:29 +00:00
}
func (client *Client) SetNickname(nickname string) {
if client.HasNick() {
Log.error.Printf("%s nickname already set!", client.nickMaskString)
return
}
2014-02-18 23:36:58 +00:00
client.nick = nickname
2016-06-19 05:37:29 +00:00
client.updateNickMask()
2014-02-18 23:36:58 +00:00
client.server.clients.Add(client)
}
func (client *Client) ChangeNickname(nickname string) {
2016-06-19 05:37:29 +00:00
origNickMask := client.nickMaskString
2014-02-18 23:36:58 +00:00
client.server.clients.Remove(client)
client.server.whoWas.Append(client)
client.nick = nickname
2016-06-19 05:37:29 +00:00
client.updateNickMask()
2014-02-18 23:36:58 +00:00
client.server.clients.Add(client)
client.Send(nil, origNickMask, "NICK", nickname)
2014-02-18 23:28:20 +00:00
for friend := range client.Friends() {
friend.Send(nil, origNickMask, "NICK", nickname)
2014-02-22 19:40:32 +00:00
}
}
func (client *Client) Reply(reply string) error {
2016-06-15 11:21:45 +00:00
//TODO(dan): We'll be passing around real message objects instead of raw strings
return client.socket.WriteLine(reply)
}
func (client *Client) Quit(message string) {
2016-06-19 04:55:24 +00:00
client.Send(nil, client.nickMaskString, "QUIT", message)
client.Send(nil, client.nickMaskString, "ERROR", message)
}
func (client *Client) destroy() {
if client.isDestroyed {
2014-02-18 21:25:21 +00:00
return
}
client.isDestroyed = true
client.server.whoWas.Append(client)
2014-02-18 23:28:20 +00:00
friends := client.Friends()
friends.Remove(client)
// 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()
for friend := range client.Friends() {
//TODO(dan): store quit message in user, if exists use that instead here
2016-06-19 04:55:24 +00:00
friend.Send(nil, client.nickMaskString, "QUIT", "Exited")
}
}
2014-02-18 21:25:21 +00:00
2016-09-12 01:25:31 +00:00
// SendFromClient sends an IRC line coming from a specific client.
// Adds account-tag to the line as well.
func (client *Client) SendFromClient(from *Client, tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
// attach account-tag
if client.capabilities[AccountTag] && from.account != &NoAccount {
if tags == nil {
tags = ircmsg.MakeTags("account", from.account.Name)
} else {
(*tags)["account"] = ircmsg.MakeTagValue(from.account.Name)
}
}
return client.Send(tags, prefix, command, params...)
}
// 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 12:04:21 +00:00
// attach server-time
if client.capabilities[ServerTime] {
if tags == nil {
2016-09-07 11:48:03 +00:00
tags = ircmsg.MakeTags("time", time.Now().Format("2006-01-02T15:04:05.999Z"))
2016-08-13 12:04:21 +00:00
} else {
2016-09-07 11:48:03 +00:00
(*tags)["time"] = ircmsg.MakeTagValue(time.Now().Format("2006-01-02T15:04:05.999Z"))
2016-08-13 12:04:21 +00:00
}
}
// send out the message
message := ircmsg.MakeMessage(tags, prefix, command, params...)
line, err := message.Line()
if err != nil {
// try not to fail quietly - especially useful when running tests, as a note to dig deeper
message = ircmsg.MakeMessage(nil, client.server.name, ERR_UNKNOWNERROR, "*", "Error assembling message for sending")
line, _ := message.Line()
client.socket.Write(line)
return err
}
client.socket.Write(line)
return nil
}
// Notice sends the client a notice from the server.
func (client *Client) Notice(text string) {
client.Send(nil, client.server.name, "NOTICE", client.nick, text)
}