Cleaned up file handling.
This commit is contained in:
parent
c1d256aaa0
commit
41f9649494
32
daemon.go
32
daemon.go
@ -18,11 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@ -72,29 +70,23 @@ func (daemon *Daemon) SendLusers(client *Client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (daemon *Daemon) SendMotd(client *Client) {
|
func (daemon *Daemon) SendMotd(client *Client) {
|
||||||
if daemon.motd != "" {
|
if len(daemon.motd) == 0 {
|
||||||
fd, err := os.Open(daemon.motd)
|
client.ReplyNicknamed("422", "MOTD File is missing")
|
||||||
if err == nil {
|
return
|
||||||
defer fd.Close()
|
}
|
||||||
motd := []byte{}
|
|
||||||
var err error
|
motd, err := ioutil.ReadFile(daemon.motd)
|
||||||
for err != io.EOF {
|
if err != nil {
|
||||||
buf := make([]byte, 1024)
|
log.Printf("Can not read motd file %s: %v", daemon.motd, err)
|
||||||
_, err = fd.Read(buf)
|
client.ReplyNicknamed("422", "Error reading MOTD File")
|
||||||
motd = append(motd, bytes.TrimRight(buf, "\x00")...)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
|
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("372", "- "+string(s))
|
||||||
}
|
}
|
||||||
client.ReplyNicknamed("376", "End of /MOTD command")
|
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) {
|
func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
|
||||||
|
@ -113,10 +113,10 @@ func TestRegistrationWorkflow(t *testing.T) {
|
|||||||
func TestMotd(t *testing.T) {
|
func TestMotd(t *testing.T) {
|
||||||
fd, err := ioutil.TempFile("", "motd")
|
fd, err := ioutil.TempFile("", "motd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("can not create temporary file")
|
t.Fatalf("can not create temporary file: %v", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(fd.Name())
|
defer os.Remove(fd.Name())
|
||||||
fd.Write([]byte("catched\n"))
|
fd.WriteString("catched\n")
|
||||||
|
|
||||||
conn := NewTestingConn()
|
conn := NewTestingConn()
|
||||||
client := NewClient("foohost", conn)
|
client := NewClient("foohost", conn)
|
||||||
@ -129,7 +129,7 @@ func TestMotd(t *testing.T) {
|
|||||||
if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
|
if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
|
||||||
t.Fatal("MOTD contents", r)
|
t.Fatal("MOTD contents", r)
|
||||||
}
|
}
|
||||||
if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
|
if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
|
||||||
t.Fatal("MOTD end", r)
|
t.Fatalf("MOTD end: got %q, want prefix %q", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
events.go
15
events.go
@ -19,6 +19,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -94,16 +95,12 @@ type StateEvent struct {
|
|||||||
// Room states shows that either topic or key has been changed
|
// Room states shows that either topic or key has been changed
|
||||||
// Each room's state is written to separate file in statedir
|
// Each room's state is written to separate file in statedir
|
||||||
func StateKeeper(statedir string, events <-chan StateEvent) {
|
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 {
|
for event := range events {
|
||||||
state_path := path.Join(statedir, event.where)
|
fn := path.Join(statedir, event.where)
|
||||||
fd, err := os.OpenFile(state_path, mode, perm)
|
data := event.topic + "\n" + event.key + "\n"
|
||||||
|
err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Can not open statefile", state_path, err)
|
log.Printf("Can not write statefile %s: %v", fn, err)
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
fd.WriteString(event.topic + "\n" + event.key + "\n")
|
|
||||||
fd.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
goircd.go
20
goircd.go
@ -18,12 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"flag"
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -77,28 +76,25 @@ func Run() {
|
|||||||
if !path.IsAbs(*statedir) {
|
if !path.IsAbs(*statedir) {
|
||||||
log.Fatalln("Need absolute path for statedir")
|
log.Fatalln("Need absolute path for statedir")
|
||||||
}
|
}
|
||||||
states, err := filepath.Glob(*statedir + "/#*")
|
states, err := filepath.Glob(path.Join(*statedir, "#*"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Can not read statedir", err)
|
log.Fatalln("Can not read statedir", err)
|
||||||
}
|
}
|
||||||
for _, state := range states {
|
for _, state := range states {
|
||||||
fd, err := os.Open(state)
|
buf, err := ioutil.ReadFile(state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Can not open state", state, err)
|
log.Fatalf("Can not read state %s: %v", state, err)
|
||||||
}
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
_, err = fd.Read(buf)
|
|
||||||
fd.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Can not read state", state, err)
|
|
||||||
}
|
}
|
||||||
room, _ := daemon.RoomRegister(path.Base(state))
|
room, _ := daemon.RoomRegister(path.Base(state))
|
||||||
buf = bytes.TrimRight(buf, "\x00")
|
|
||||||
contents := strings.Split(string(buf), "\n")
|
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.topic = contents[0]
|
||||||
room.key = contents[1]
|
room.key = contents[1]
|
||||||
log.Println("Loaded state for room", room.name)
|
log.Println("Loaded state for room", room.name)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
go StateKeeper(*statedir, state_sink)
|
go StateKeeper(*statedir, state_sink)
|
||||||
log.Println(*statedir, "statekeeper initialized")
|
log.Println(*statedir, "statekeeper initialized")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user