]> Cypherpunks.ru repositories - goircd.git/commitdiff
Cleaned up file handling.
authorThomas Habets <thomas@habets.se>
Sun, 8 Jun 2014 01:16:33 +0000 (03:16 +0200)
committerThomas Habets <thomas@habets.se>
Sun, 8 Jun 2014 01:16:33 +0000 (03:16 +0200)
daemon.go
daemon_test.go
events.go
goircd.go

index 1f142c2c13f9917c8e986627ff3529f560fd45aa..1f979ce6ea4f7c4dd295c5f4924b8ab477bdd845 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -18,11 +18,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
-       "bytes"
        "fmt"
-       "io"
+       "io/ioutil"
        "log"
-       "os"
        "regexp"
        "sort"
        "strings"
@@ -72,29 +70,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) {
index 1c86f351d819816682b095d063d5f98116471d35..9c9db3de436923a43540debbc7d582374db58a43 100644 (file)
@@ -113,10 +113,10 @@ func TestRegistrationWorkflow(t *testing.T) {
 func TestMotd(t *testing.T) {
        fd, err := ioutil.TempFile("", "motd")
        if err != nil {
-               t.Fatal("can not create temporary file")
+               t.Fatalf("can not create temporary file: %v", err)
        }
        defer os.Remove(fd.Name())
-       fd.Write([]byte("catched\n"))
+       fd.WriteString("catched\n")
 
        conn := NewTestingConn()
        client := NewClient("foohost", conn)
@@ -129,7 +129,7 @@ func TestMotd(t *testing.T) {
        if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
                t.Fatal("MOTD contents", r)
        }
-       if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
-               t.Fatal("MOTD end", r)
+       if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
+               t.Fatalf("MOTD end: got %q, want prefix %q", got, want)
        }
 }
index 95760e8a6ceeaf0efe139c5d3e8d295d685c7b13..0921069d2cf21748e6bcd34fae076dff206cc44e 100644 (file)
--- a/events.go
+++ b/events.go
@@ -19,6 +19,7 @@ package main
 
 import (
        "fmt"
+       "io/ioutil"
        "log"
        "os"
        "path"
@@ -94,16 +95,12 @@ type StateEvent struct {
 // Room states shows that either topic or key has been changed
 // Each room's state is written to separate file in statedir
 func StateKeeper(statedir string, events <-chan StateEvent) {
-       mode := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
-       perm := os.FileMode(0660)
        for event := range events {
-               state_path := path.Join(statedir, event.where)
-               fd, err := os.OpenFile(state_path, mode, perm)
+               fn := path.Join(statedir, event.where)
+               data := event.topic + "\n" + event.key + "\n"
+               err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
                if err != nil {
-                       log.Println("Can not open statefile", state_path, err)
-                       continue
+                       log.Printf("Can not write statefile %s: %v", fn, err)
                }
-               fd.WriteString(event.topic + "\n" + event.key + "\n")
-               fd.Close()
        }
 }
index e1768048f5858ac01be79a9955b9b0f4544048af..9fdabc0357d1a7430a422aee6c919284b936ce28 100644 (file)
--- a/goircd.go
+++ b/goircd.go
@@ -18,12 +18,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
-       "bytes"
        "crypto/tls"
        "flag"
+       "io/ioutil"
        "log"
        "net"
-       "os"
        "path"
        "path/filepath"
        "strings"
@@ -77,27 +76,24 @@ func Run() {
                if !path.IsAbs(*statedir) {
                        log.Fatalln("Need absolute path for statedir")
                }
-               states, err := filepath.Glob(*statedir + "/#*")
+               states, err := filepath.Glob(path.Join(*statedir, "#*"))
                if err != nil {
                        log.Fatalln("Can not read statedir", err)
                }
                for _, state := range states {
-                       fd, err := os.Open(state)
+                       buf, err := ioutil.ReadFile(state)
                        if err != nil {
-                               log.Fatalln("Can not open state", state, err)
-                       }
-                       buf := make([]byte, 1024)
-                       _, err = fd.Read(buf)
-                       fd.Close()
-                       if err != nil {
-                               log.Fatalln("Can not read state", state, err)
+                               log.Fatalf("Can not read state %s: %v", state, err)
                        }
                        room, _ := daemon.RoomRegister(path.Base(state))
-                       buf = bytes.TrimRight(buf, "\x00")
                        contents := strings.Split(string(buf), "\n")
-                       room.topic = contents[0]
-                       room.key = contents[1]
-                       log.Println("Loaded state for room", room.name)
+                       if len(contents) < 2 {
+                               log.Printf("State corrupted for %s: %q", room.name, contents)
+                       } else {
+                               room.topic = contents[0]
+                               room.key = contents[1]
+                               log.Println("Loaded state for room", room.name)
+                       }
                }
                go StateKeeper(*statedir, state_sink)
                log.Println(*statedir, "statekeeper initialized")