3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 02:32:39 +01:00

server: Store MOTD in memory rather than reading it from disk every time. Fixes #3

This commit is contained in:
Daniel Oaks 2016-04-13 15:49:30 +10:00
parent c97472582d
commit 8dc2732137

View File

@ -31,7 +31,7 @@ type Server struct {
ctime time.Time
db *sql.DB
idle chan *Client
motdFile string
motdLines []string
name Name
newConns chan net.Conn
operators map[Name][]byte
@ -55,7 +55,6 @@ func NewServer(config *Config) *Server {
ctime: time.Now(),
db: OpenDB(config.Server.Database),
idle: make(chan *Client),
motdFile: config.Server.MOTD,
name: NewName(config.Server.Name),
newConns: make(chan net.Conn),
operators: config.Operators(),
@ -64,6 +63,24 @@ func NewServer(config *Config) *Server {
theaters: config.Theaters(),
}
if config.Server.MOTD != "" {
file, err := os.Open(config.Server.MOTD)
if err == nil {
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimRight(line, "\r\n")
server.motdLines = append(server.motdLines, line)
}
}
}
if config.Server.Password != "" {
server.password = config.Server.PasswordBytes()
}
@ -283,27 +300,13 @@ func (s *Server) tryRegister(c *Client) {
}
func (server *Server) MOTD(client *Client) {
if server.motdFile == "" {
if len(server.motdLines) < 1 {
client.ErrNoMOTD()
return
}
file, err := os.Open(server.motdFile)
if err != nil {
client.ErrNoMOTD()
return
}
defer file.Close()
client.RplMOTDStart()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimRight(line, "\r\n")
for _, line := range server.motdLines {
client.RplMOTD(line)
}
client.RplMOTDEnd()