3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

mutex server commands

This commit is contained in:
Jeremy Latt 2014-02-13 20:08:16 -08:00
parent 42ff4410ad
commit a9eae872c9

View File

@ -9,14 +9,16 @@ import (
"log" "log"
"net" "net"
"os" "os"
"sync"
"time" "time"
) )
type Server struct { type Server struct {
channels ChannelNameMap channels ChannelNameMap
commands chan<- Command commands chan Command
ctime time.Time ctime time.Time
motdFile string motdFile string
mutex *sync.Mutex
name string name string
operators map[string]string operators map[string]string
password string password string
@ -24,13 +26,13 @@ type Server struct {
} }
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
commands := make(chan Command)
server := &Server{ server := &Server{
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: commands, commands: make(chan Command),
ctime: time.Now(), ctime: time.Now(),
motdFile: config.MOTD, motdFile: config.MOTD,
mutex: &sync.Mutex{},
name: config.Name, name: config.Name,
operators: make(map[string]string), operators: make(map[string]string),
password: config.Password, password: config.Password,
@ -40,7 +42,7 @@ func NewServer(config *Config) *Server {
server.operators[opConf.Name] = opConf.Password server.operators[opConf.Name] = opConf.Password
} }
go server.receiveCommands(commands) go server.receiveCommands()
for _, listenerConf := range config.Listeners { for _, listenerConf := range config.Listeners {
go server.listen(listenerConf) go server.listen(listenerConf)
@ -49,8 +51,8 @@ func NewServer(config *Config) *Server {
return server return server
} }
func (server *Server) receiveCommands(commands <-chan Command) { func (server *Server) receiveCommands() {
for command := range commands { for command := range server.commands {
if DEBUG_SERVER { if DEBUG_SERVER {
log.Printf("%s → %s %+v", command.Client(), server, command) log.Printf("%s → %s %+v", command.Client(), server, command)
} }
@ -70,6 +72,12 @@ func (server *Server) receiveCommands(commands <-chan Command) {
} }
} }
func (server *Server) Command(command Command) {
server.mutex.Lock()
server.commands <- command
server.mutex.Unlock()
}
func (server *Server) Authorize(client *Client, command Command) bool { func (server *Server) Authorize(client *Client, command Command) bool {
if client.authorized { if client.authorized {
return true return true
@ -83,7 +91,7 @@ func (server *Server) Authorize(client *Client, command Command) bool {
switch command.(type) { switch command.(type) {
case *PassCommand, *CapCommand, *ProxyCommand: case *PassCommand, *CapCommand, *ProxyCommand:
// no-op // no-op
default: default: // any other commands void authorization
return false return false
} }