2015-08-01 17:32:41 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2015-08-02 02:46:58 +00:00
|
|
|
"sync"
|
2015-08-01 17:32:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sorcix/irc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents an IRC Client connection to the server
|
|
|
|
type Client struct {
|
|
|
|
*irc.Conn
|
|
|
|
conn net.Conn
|
|
|
|
Nickname string
|
|
|
|
Name string
|
|
|
|
Host string
|
|
|
|
Username string
|
|
|
|
RealName string
|
|
|
|
|
|
|
|
Prefix *irc.Prefix
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
Server *Server
|
2015-08-01 17:32:41 +00:00
|
|
|
authorized bool
|
|
|
|
|
|
|
|
idleTimer *time.Timer
|
|
|
|
quitTimer *time.Timer
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
AwayMessage string
|
|
|
|
|
|
|
|
channels map[string]*Channel
|
2015-08-08 23:37:17 +00:00
|
|
|
channelMutex sync.RWMutex
|
2015-08-05 15:33:06 +00:00
|
|
|
|
|
|
|
*UserModeSet
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) newClient(ircConn *irc.Conn, conn net.Conn) *Client {
|
2015-08-02 02:46:58 +00:00
|
|
|
client := &Client{Conn: ircConn, conn: conn, Server: s}
|
|
|
|
client.authorized = len(s.Config.Password) == 0
|
2015-08-08 17:14:43 +00:00
|
|
|
client.idleTimer = time.AfterFunc(time.Minute*3, client.idle)
|
2015-08-02 02:46:58 +00:00
|
|
|
client.channels = map[string]*Channel{}
|
2015-08-05 15:33:06 +00:00
|
|
|
client.UserModeSet = NewUserModeSet()
|
2015-08-01 17:32:41 +00:00
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up the IRC client and closes the connection
|
|
|
|
func (c *Client) Close() error {
|
2015-08-02 02:46:58 +00:00
|
|
|
c.Server.RemoveClient(c)
|
|
|
|
c.Server.RemoveClientNick(c)
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping sends an IRC PING command to a client
|
|
|
|
func (c *Client) Ping() {
|
2015-08-05 15:33:06 +00:00
|
|
|
m := irc.Message{Command: irc.PING, Trailing: c.Server.Config.Name}
|
2015-08-01 17:32:41 +00:00
|
|
|
c.Encode(&m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pong sends an IRC PONG command to the client
|
|
|
|
func (c *Client) Pong() {
|
2015-08-05 15:33:06 +00:00
|
|
|
m := irc.Message{Command: irc.PONG, Trailing: c.Server.Config.Name}
|
2015-08-01 17:32:41 +00:00
|
|
|
c.Encode(&m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) handleIncoming() {
|
2015-08-02 02:46:58 +00:00
|
|
|
c.Server.AddClient(c)
|
2015-08-01 17:32:41 +00:00
|
|
|
for {
|
|
|
|
message, err := c.Decode()
|
2015-08-02 12:21:16 +00:00
|
|
|
if err != nil {
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
_, closedError := err.(*net.OpError)
|
|
|
|
if err == io.EOF || err == io.ErrClosedPipe || closedError || strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
return
|
|
|
|
}
|
2015-08-02 12:21:16 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if message == nil || message.Len() == 0 {
|
|
|
|
continue
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.idleTimer.Stop()
|
2015-08-08 17:14:43 +00:00
|
|
|
c.idleTimer = time.AfterFunc(time.Minute*3, c.idle)
|
2015-08-01 17:32:41 +00:00
|
|
|
if c.quitTimer != nil {
|
|
|
|
c.quitTimer.Stop()
|
|
|
|
c.quitTimer = nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
c.Server.CommandsMux.ServeIRC(message, c)
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) idle() {
|
|
|
|
c.Ping()
|
2015-08-08 17:14:43 +00:00
|
|
|
c.quitTimer = time.AfterFunc(time.Minute*3, c.quit)
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) quit() {
|
2015-08-02 02:46:58 +00:00
|
|
|
// Have client leave/part each channel
|
|
|
|
for _, channel := range c.GetChannels() {
|
2015-08-05 15:33:06 +00:00
|
|
|
channel.Quit(c, "Disconnected")
|
2015-08-02 02:46:58 +00:00
|
|
|
}
|
2015-08-01 17:32:41 +00:00
|
|
|
c.Quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quit sends the IRC Quit command and closes the connection
|
|
|
|
func (c *Client) Quit() {
|
2015-08-02 02:46:58 +00:00
|
|
|
m := irc.Message{Prefix: &irc.Prefix{Name: c.Server.Config.Name}, Command: irc.QUIT,
|
2015-08-01 17:32:41 +00:00
|
|
|
Params: []string{c.Nickname}}
|
|
|
|
|
|
|
|
c.Encode(&m)
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Welcome handles initial client connection IRC protocols for a client.
|
|
|
|
// Welcome procedure includes IRC WELCOME, Host Info, and MOTD
|
|
|
|
func (c *Client) Welcome() {
|
|
|
|
|
|
|
|
// Have all client info now
|
2015-08-05 15:33:06 +00:00
|
|
|
c.Prefix = &irc.Prefix{Name: c.Nickname, User: c.Name, Host: c.Host}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m := irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_WELCOME,
|
2015-08-08 00:37:41 +00:00
|
|
|
Params: []string{c.Nickname, "Welcome to the Internet Relay Network", c.Prefix.String()}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
err := c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m = irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_YOURHOST,
|
2015-08-08 00:37:41 +00:00
|
|
|
Params: []string{c.Nickname, fmt.Sprintf("Your host is %s, running version %s", c.Server.Config.Name, c.Server.Config.Version)}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
err = c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m = irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_CREATED,
|
|
|
|
Params: []string{c.Nickname, fmt.Sprintf("This server was created %s", c.Server.created)}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
err = c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m = irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_MYINFO,
|
|
|
|
Params: []string{c.Nickname, fmt.Sprintf("%s - Golang IRC server", c.Server.Config.Name)}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
err = c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-08 00:37:41 +00:00
|
|
|
// Send MOTD
|
|
|
|
c.MOTD()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MOTD returns the Message of the Day of the server to the client
|
|
|
|
func (c *Client) MOTD() {
|
|
|
|
|
|
|
|
if len(c.Server.Config.MOTD) == 0 {
|
|
|
|
m := irc.Message{Prefix: c.Server.Prefix, Command: irc.ERR_NOMOTD, Params: []string{c.Nickname}, Trailing: "MOTD File is missing"}
|
|
|
|
c.Encode(&m)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_MOTDSTART,
|
2015-08-02 02:46:58 +00:00
|
|
|
Params: []string{c.Nickname, fmt.Sprintf("%s - Message of the day", c.Server.Config.Name)}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
2015-08-08 00:37:41 +00:00
|
|
|
err := c.Encode(&m)
|
2015-08-01 17:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m = irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_MOTD,
|
|
|
|
Params: []string{c.Nickname, c.Server.Config.MOTD}}
|
2015-08-01 17:32:41 +00:00
|
|
|
|
|
|
|
err = c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
m = irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_ENDOFMOTD,
|
2015-08-01 17:32:41 +00:00
|
|
|
Params: []string{c.Nickname, "End of MOTD"}}
|
|
|
|
|
|
|
|
err = c.Encode(&m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 02:46:58 +00:00
|
|
|
|
|
|
|
// AddChannel adds a channel to the client's active list
|
|
|
|
func (c *Client) AddChannel(channel *Channel) {
|
|
|
|
c.channelMutex.Lock()
|
|
|
|
defer c.channelMutex.Unlock()
|
|
|
|
c.channels[channel.Name] = channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveChannel removes a channel to the client's active list
|
|
|
|
func (c *Client) RemoveChannel(channel *Channel) {
|
|
|
|
c.channelMutex.Lock()
|
|
|
|
defer c.channelMutex.Unlock()
|
|
|
|
delete(c.channels, channel.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChannels gets a list of channels this client is joined to
|
|
|
|
func (c *Client) GetChannels() map[string]*Channel {
|
|
|
|
return c.channels
|
|
|
|
}
|
2015-08-08 23:37:17 +00:00
|
|
|
|
|
|
|
// UpdateNick updates the clients nicknamae to a new nickname
|
|
|
|
func (c *Client) UpdateNick(newNick string) {
|
|
|
|
oldNick := c.Nickname
|
|
|
|
c.Nickname = newNick
|
|
|
|
|
|
|
|
c.Server.UpdateClientNick(c, oldNick)
|
|
|
|
c.channelMutex.RLock()
|
|
|
|
defer c.channelMutex.RUnlock()
|
2015-08-09 02:19:02 +00:00
|
|
|
|
|
|
|
// Notify all people that should know (people on channels with this client)
|
|
|
|
m := irc.Message{Prefix: c.Prefix, Command: irc.NICK, Trailing: c.Nickname}
|
|
|
|
notified := map[string]interface{}{} // Just notify people once
|
|
|
|
c.Encode(&m)
|
|
|
|
notified[c.Nickname] = nil
|
|
|
|
|
2015-08-08 23:37:17 +00:00
|
|
|
for _, channel := range c.channels {
|
2015-08-09 02:19:02 +00:00
|
|
|
|
2015-08-08 23:37:17 +00:00
|
|
|
channel.UpdateMemberNick(c, oldNick)
|
2015-08-09 02:19:02 +00:00
|
|
|
|
|
|
|
for client := range channel.members {
|
|
|
|
cl, ok := c.Server.GetClientByNick(client)
|
|
|
|
_, alreadyNotified := notified[client]
|
|
|
|
if ok && !alreadyNotified {
|
|
|
|
cl.Encode(&m)
|
|
|
|
notified[client] = nil
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Prefix.Name = newNick
|
|
|
|
}
|