2015-08-01 17:32:41 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sorcix/irc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents an IRC server
|
|
|
|
type Server struct {
|
2015-08-02 02:46:58 +00:00
|
|
|
Config ServerConfig
|
2015-08-01 17:32:41 +00:00
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
clients map[net.Addr]*Client
|
2015-08-01 17:32:41 +00:00
|
|
|
clientMutex sync.RWMutex
|
|
|
|
|
2015-08-02 02:46:58 +00:00
|
|
|
clientsByNick map[string]*Client
|
2015-08-01 17:32:41 +00:00
|
|
|
clientByNickMutex sync.RWMutex
|
|
|
|
|
|
|
|
Prefix *irc.Prefix
|
|
|
|
CommandsMux CommandsMux
|
|
|
|
created time.Time
|
2015-08-02 02:46:58 +00:00
|
|
|
|
|
|
|
channels map[string]*Channel
|
|
|
|
channelMutex sync.RWMutex
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerConfig contains configuration data for seeding a server
|
|
|
|
type ServerConfig struct {
|
|
|
|
Name string
|
|
|
|
MOTD string
|
2015-08-08 00:37:41 +00:00
|
|
|
Version string
|
2015-08-01 17:32:41 +00:00
|
|
|
TLSConfig *tls.Config
|
|
|
|
Addr string
|
|
|
|
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates and returns a new Server based on the provided config
|
|
|
|
func NewServer(config ServerConfig) *Server {
|
|
|
|
s := Server{}
|
2015-08-02 02:46:58 +00:00
|
|
|
s.Config = config
|
|
|
|
s.clients = map[net.Addr]*Client{}
|
2015-08-01 17:32:41 +00:00
|
|
|
s.CommandsMux = NewCommandsMux()
|
|
|
|
s.created = time.Now()
|
2015-08-02 02:46:58 +00:00
|
|
|
s.clientsByNick = map[string]*Client{}
|
2015-08-01 17:32:41 +00:00
|
|
|
s.Prefix = &irc.Prefix{Name: config.Name}
|
2015-08-02 02:46:58 +00:00
|
|
|
s.channels = map[string]*Channel{}
|
2015-08-08 00:37:41 +00:00
|
|
|
if len(s.Config.Name) == 0 {
|
|
|
|
s.Config.Name = "localhost"
|
|
|
|
}
|
|
|
|
if len(s.Config.Version) == 0 {
|
|
|
|
s.Config.Version = "1.0"
|
|
|
|
}
|
2015-08-01 17:32:41 +00:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddClient adds a new Client
|
|
|
|
func (s *Server) AddClient(client *Client) {
|
|
|
|
s.clientMutex.Lock()
|
|
|
|
defer s.clientMutex.Unlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
s.clients[client.conn.RemoteAddr()] = client
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveClient removes a client
|
|
|
|
func (s *Server) RemoveClient(client *Client) {
|
|
|
|
s.clientMutex.Lock()
|
|
|
|
defer s.clientMutex.Unlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
delete(s.clients, client.conn.RemoteAddr())
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClient finds a client by its address and returns it
|
|
|
|
func (s *Server) GetClient(addr net.Addr) *Client {
|
|
|
|
s.clientMutex.RLock()
|
|
|
|
defer s.clientMutex.RUnlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
return s.clients[addr]
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddClientNick adds a client based on its nickname
|
|
|
|
func (s *Server) AddClientNick(client *Client) {
|
|
|
|
s.clientByNickMutex.Lock()
|
|
|
|
defer s.clientByNickMutex.Unlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
s.clientsByNick[client.Nickname] = client
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveClientNick removes a client based on its nickname
|
|
|
|
func (s *Server) RemoveClientNick(client *Client) {
|
|
|
|
s.clientByNickMutex.Lock()
|
|
|
|
defer s.clientByNickMutex.Unlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
delete(s.clientsByNick, client.Nickname)
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateClientNick updates the nickname of a client as it is stored by the server
|
|
|
|
func (s *Server) UpdateClientNick(client *Client, oldNick string) {
|
|
|
|
s.clientByNickMutex.Lock()
|
|
|
|
defer s.clientByNickMutex.Unlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
delete(s.clientsByNick, oldNick)
|
|
|
|
s.clientsByNick[client.Nickname] = client
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientByNick returns a client with the corresponding nickname
|
2015-08-02 02:46:58 +00:00
|
|
|
func (s *Server) GetClientByNick(nick string) (*Client, bool) {
|
2015-08-01 17:32:41 +00:00
|
|
|
s.clientByNickMutex.RLock()
|
|
|
|
defer s.clientByNickMutex.RUnlock()
|
2015-08-02 02:46:58 +00:00
|
|
|
c, ok := s.clientsByNick[nick]
|
|
|
|
return c, ok
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the server listening on the configured port
|
|
|
|
func (s *Server) Start() {
|
|
|
|
var listener net.Listener
|
|
|
|
var err error
|
2015-08-02 02:46:58 +00:00
|
|
|
if s.Config.TLSConfig != nil {
|
|
|
|
listener, err = tls.Listen("tcp", s.Config.Addr, s.Config.TLSConfig)
|
2015-08-01 17:32:41 +00:00
|
|
|
} else {
|
2015-08-02 02:46:58 +00:00
|
|
|
listener, err = net.Listen("tcp", s.Config.Addr)
|
2015-08-01 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error starting listner", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error accepting connection", err.Error())
|
|
|
|
//return
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ircConn := irc.NewConn(conn)
|
|
|
|
client := s.newClient(ircConn, conn)
|
|
|
|
|
|
|
|
defer client.Close()
|
|
|
|
go func() {
|
|
|
|
fmt.Println("Incoming connection from:", conn.RemoteAddr())
|
|
|
|
client.handleIncoming()
|
|
|
|
fmt.Println("Disconnected with:", conn.RemoteAddr())
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 02:46:58 +00:00
|
|
|
|
|
|
|
// AddChannel adds an active channel
|
|
|
|
func (s *Server) AddChannel(channel *Channel) {
|
|
|
|
s.channelMutex.Lock()
|
|
|
|
defer s.channelMutex.Unlock()
|
|
|
|
s.channels[channel.Name] = channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveChannel removes a channel from the active listing
|
|
|
|
func (s *Server) RemoveChannel(channel *Channel) {
|
|
|
|
s.channelMutex.Lock()
|
|
|
|
defer s.channelMutex.Unlock()
|
|
|
|
delete(s.channels, channel.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChannel finds and returns an active channel with a matching name if it exists
|
|
|
|
func (s *Server) GetChannel(channelName string) (*Channel, bool) {
|
|
|
|
s.channelMutex.RLock()
|
|
|
|
defer s.channelMutex.RUnlock()
|
|
|
|
c, ok := s.channels[channelName]
|
|
|
|
return c, ok
|
|
|
|
}
|