]> Cypherpunks.ru repositories - goircd.git/blobdiff - daemon.go
Fix several races
[goircd.git] / daemon.go
index be9a84e51c33b8ee0e6f6878dceda35bdb7ec1f8..420ec9121d366440e9ce1fc0ab50e62958c4d025 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -18,11 +18,10 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
-       "bytes"
        "fmt"
-       "io"
+       "io/ioutil"
        "log"
-       "os"
+       "net"
        "regexp"
        "sort"
        "strings"
@@ -40,9 +39,11 @@ var (
 )
 
 type Daemon struct {
+       Verbose              bool
        hostname             string
        motd                 string
        clients              map[*Client]bool
+       client_aliveness     map[*Client]*ClientAlivenessState
        rooms                map[string]*Room
        room_sinks           map[*Room]chan ClientEvent
        last_aliveness_check time.Time
@@ -53,6 +54,7 @@ type Daemon struct {
 func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon {
        daemon := Daemon{hostname: hostname, motd: motd}
        daemon.clients = make(map[*Client]bool)
+       daemon.client_aliveness = make(map[*Client]*ClientAlivenessState)
        daemon.rooms = make(map[string]*Room)
        daemon.room_sinks = make(map[*Room]chan ClientEvent)
        daemon.log_sink = log_sink
@@ -71,29 +73,23 @@ func (daemon *Daemon) SendLusers(client *Client) {
 }
 
 func (daemon *Daemon) SendMotd(client *Client) {
-       if daemon.motd != "" {
-               fd, err := os.Open(daemon.motd)
-               if err == nil {
-                       defer fd.Close()
-                       motd := []byte{}
-                       var err error
-                       for err != io.EOF {
-                               buf := make([]byte, 1024)
-                               _, err = fd.Read(buf)
-                               motd = append(motd, bytes.TrimRight(buf, "\x00")...)
-                       }
+       if len(daemon.motd) == 0 {
+               client.ReplyNicknamed("422", "MOTD File is missing")
+               return
+       }
 
-                       client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
-                       for _, s := range bytes.Split(bytes.TrimRight(motd, "\n"), []byte("\n")) {
-                               client.ReplyNicknamed("372", "- "+string(s))
-                       }
-                       client.ReplyNicknamed("376", "End of /MOTD command")
-                       return
-               } else {
-                       log.Println("Can not open motd file", daemon.motd, err)
-               }
+       motd, err := ioutil.ReadFile(daemon.motd)
+       if err != nil {
+               log.Printf("Can not read motd file %s: %v", daemon.motd, err)
+               client.ReplyNicknamed("422", "Error reading MOTD File")
+               return
+       }
+
+       client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
+       for _, s := range strings.Split(strings.Trim(string(motd), "\n"), "\n") {
+               client.ReplyNicknamed("372", "- "+string(s))
        }
-       client.ReplyNicknamed("422", "MOTD File is missing")
+       client.ReplyNicknamed("376", "End of /MOTD command")
 }
 
 func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
@@ -101,11 +97,17 @@ func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
                nickname = strings.ToLower(nickname)
                found := false
                for c := range daemon.clients {
-                       if c.nickname != nickname {
+                       if strings.ToLower(c.nickname) != nickname {
                                continue
                        }
                        found = true
-                       client.ReplyNicknamed("311", c.nickname, c.username, c.conn.RemoteAddr().String(), "*", c.realname)
+                       h := c.conn.RemoteAddr().String()
+                       h, _, err := net.SplitHostPort(h)
+                       if err != nil {
+                               log.Printf("Can't parse RemoteAddr %q: %v", h, err)
+                               h = "Unknown"
+                       }
+                       client.ReplyNicknamed("311", c.nickname, c.username, h, "*", c.realname)
                        client.ReplyNicknamed("312", c.nickname, daemon.hostname, daemon.hostname)
                        subscriptions := []string{}
                        for _, room := range daemon.rooms {
@@ -158,8 +160,8 @@ func (daemon *Daemon) ClientRegister(client *Client, command string, cols []stri
                        return
                }
                nickname := cols[1]
-               for client := range daemon.clients {
-                       if client.nickname == nickname {
+               for existing_client := range daemon.clients {
+                       if existing_client.nickname == nickname {
                                client.ReplyParts("433", "*", nickname, "Nickname is already in use")
                                return
                        }
@@ -197,6 +199,7 @@ func (daemon *Daemon) ClientRegister(client *Client, command string, cols []stri
 // to corresponding daemon's places and start room's processor goroutine.
 func (daemon *Daemon) RoomRegister(name string) (*Room, chan<- ClientEvent) {
        room_new := NewRoom(daemon.hostname, name, daemon.log_sink, daemon.state_sink)
+       room_new.Verbose = daemon.Verbose
        room_sink := make(chan ClientEvent)
        daemon.rooms[name] = room_new
        daemon.room_sinks[room_new] = room_sink
@@ -254,20 +257,25 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
 
 func (daemon *Daemon) Processor(events <-chan ClientEvent) {
        for event := range events {
+               now := time.Now()
+               client := event.client
 
                // Check for clients aliveness
-               now := time.Now()
                if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) {
                        for c := range daemon.clients {
-                               if c.timestamp.Add(PING_TIMEOUT).Before(now) {
+                               aliveness, alive := daemon.client_aliveness[c]
+                               if !alive {
+                                       continue
+                               }
+                               if aliveness.timestamp.Add(PING_TIMEOUT).Before(now) {
                                        log.Println(c, "ping timeout")
                                        c.conn.Close()
                                        continue
                                }
-                               if !c.ping_sent && c.timestamp.Add(PING_THRESHOLD).Before(now) {
+                               if !aliveness.ping_sent && aliveness.timestamp.Add(PING_THRESHOLD).Before(now) {
                                        if c.registered {
                                                c.Msg("PING :" + daemon.hostname)
-                                               c.ping_sent = true
+                                               aliveness.ping_sent = true
                                        } else {
                                                log.Println(c, "ping timeout")
                                                c.conn.Close()
@@ -277,26 +285,30 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                        daemon.last_aliveness_check = now
                }
 
-               client := event.client
                switch event.event_type {
                case EVENT_NEW:
                        daemon.clients[client] = true
+                       daemon.client_aliveness[client] = &ClientAlivenessState{ping_sent: false, timestamp: now}
                case EVENT_DEL:
                        delete(daemon.clients, client)
+                       delete(daemon.client_aliveness, client)
                        for _, room_sink := range daemon.room_sinks {
                                room_sink <- event
                        }
                case EVENT_MSG:
                        cols := strings.SplitN(event.text, " ", 2)
                        command := strings.ToUpper(cols[0])
-                       log.Println(client, "command", command)
+                       if daemon.Verbose {
+                               log.Println(client, "command", command)
+                       }
                        if command == "QUIT" {
                                delete(daemon.clients, client)
+                               delete(daemon.client_aliveness, client)
                                client.conn.Close()
                                continue
                        }
                        if !client.registered {
-                               go daemon.ClientRegister(client, command, cols)
+                               daemon.ClientRegister(client, command, cols)
                                continue
                        }
                        switch command {
@@ -307,7 +319,7 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                        client.ReplyNotEnoughParameters("JOIN")
                                        continue
                                }
-                               go daemon.HandlerJoin(client, cols[1])
+                               daemon.HandlerJoin(client, cols[1])
                        case "LIST":
                                daemon.SendList(client, cols)
                        case "LUSERS":
@@ -374,7 +386,7 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                target := strings.ToLower(cols[0])
                                for c := range daemon.clients {
                                        if c.nickname == target {
-                                               msg = fmt.Sprintf(":%s %s %s :%s", client, command, c.nickname, cols[1])
+                                               msg = fmt.Sprintf(":%s %s %s %s", client, command, c.nickname, cols[1])
                                                c.Msg(msg)
                                                break
                                        }
@@ -429,5 +441,9 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                client.ReplyNicknamed("421", command, "Unknown command")
                        }
                }
+               if aliveness, alive := daemon.client_aliveness[client]; alive {
+                       aliveness.timestamp = now
+                       aliveness.ping_sent = false
+               }
        }
 }