X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=daemon.go;h=d8cc3b8bc2729d87af875dc7ea30ffd06b389c7b;hb=afbf2b40d4f5accd041a3052ab58718958c8da1c;hp=2cb215390b1ce0bcada4cd0f63d302834f40697f;hpb=54d2fcb9cbab1e5693b83b9c506bc0aca74dd90c;p=goircd.git diff --git a/daemon.go b/daemon.go index 2cb2153..d8cc3b8 100644 --- a/daemon.go +++ b/daemon.go @@ -18,11 +18,10 @@ along with this program. If not, see . package main import ( - "bytes" "fmt" - "io" + "io/ioutil" "log" - "os" + "net" "regexp" "sort" "strings" @@ -40,6 +39,7 @@ var ( ) type Daemon struct { + Verbose bool hostname string motd string clients map[*Client]bool @@ -71,29 +71,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) { @@ -105,7 +99,13 @@ func (daemon *Daemon) SendWhois(client *Client, nicknames []string) { 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 { @@ -197,6 +197,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 @@ -289,7 +290,9 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) { 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) client.conn.Close()