Cleaned up file handling.

This commit is contained in:
Thomas Habets 2014-06-08 03:16:33 +02:00
parent c1d256aaa0
commit 41f9649494
4 changed files with 37 additions and 52 deletions

View File

@ -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
}
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 bytes.Split(bytes.TrimRight(motd, "\n"), []byte("\n")) {
for _, s := range strings.Split(strings.Trim(string(motd), "\n"), "\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)
}
}
client.ReplyNicknamed("422", "MOTD File is missing")
}
func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {

View 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)
}
}

View File

@ -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
}
fd.WriteString(event.topic + "\n" + event.key + "\n")
fd.Close()
log.Printf("Can not write statefile %s: %v", fn, err)
}
}
}

View File

@ -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,28 +76,25 @@ 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")
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")
}