3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +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 ctime time.Time
db *sql.DB db *sql.DB
idle chan *Client idle chan *Client
motdFile string motdLines []string
name Name name Name
newConns chan net.Conn newConns chan net.Conn
operators map[Name][]byte operators map[Name][]byte
@ -55,7 +55,6 @@ func NewServer(config *Config) *Server {
ctime: time.Now(), ctime: time.Now(),
db: OpenDB(config.Server.Database), db: OpenDB(config.Server.Database),
idle: make(chan *Client), idle: make(chan *Client),
motdFile: config.Server.MOTD,
name: NewName(config.Server.Name), name: NewName(config.Server.Name),
newConns: make(chan net.Conn), newConns: make(chan net.Conn),
operators: config.Operators(), operators: config.Operators(),
@ -64,6 +63,24 @@ func NewServer(config *Config) *Server {
theaters: config.Theaters(), 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 != "" { if config.Server.Password != "" {
server.password = config.Server.PasswordBytes() server.password = config.Server.PasswordBytes()
} }
@ -283,27 +300,13 @@ func (s *Server) tryRegister(c *Client) {
} }
func (server *Server) MOTD(client *Client) { func (server *Server) MOTD(client *Client) {
if server.motdFile == "" { if len(server.motdLines) < 1 {
client.ErrNoMOTD() client.ErrNoMOTD()
return return
} }
file, err := os.Open(server.motdFile)
if err != nil {
client.ErrNoMOTD()
return
}
defer file.Close()
client.RplMOTDStart() client.RplMOTDStart()
reader := bufio.NewReader(file) for _, line := range server.motdLines {
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimRight(line, "\r\n")
client.RplMOTD(line) client.RplMOTD(line)
} }
client.RplMOTDEnd() client.RplMOTDEnd()