X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=events.go;h=30444dad06d0d18aeacc07bd7551fcb390c8756e;hb=5c79c4235ec745fa47c1f176657287dab4f61ed5;hp=95760e8a6ceeaf0efe139c5d3e8d295d685c7b13;hpb=de49f5eeba0117dd09fcdc290ef99ba6a5e2656d;p=goircd.git diff --git a/events.go b/events.go index 95760e8..30444da 100644 --- a/events.go +++ b/events.go @@ -19,6 +19,7 @@ package main import ( "fmt" + "io/ioutil" "log" "os" "path" @@ -26,26 +27,26 @@ import ( ) const ( - EVENT_NEW = iota - EVENT_DEL = iota - EVENT_MSG = iota - EVENT_TOPIC = iota - EVENT_WHO = iota - EVENT_MODE = iota - FORMAT_MSG = "[%s] <%s> %s\n" - FORMAT_META = "[%s] * %s %s\n" + EventNew = iota + EventDel = iota + EventMsg = iota + EventTopic = iota + EventWho = iota + EventMode = iota + FormatMsg = "[%s] <%s> %s\n" + FormatMeta = "[%s] * %s %s\n" ) // Client events going from each of client // They can be either NEW, DEL or unparsed MSG type ClientEvent struct { - client *Client - event_type int - text string + client *Client + eventType int + text string } func (m ClientEvent) String() string { - return string(m.event_type) + ": " + m.client.String() + ": " + m.text + return string(m.eventType) + ": " + m.client.String() + ": " + m.text } // Logging in-room events @@ -72,9 +73,9 @@ func Logger(logdir string, events <-chan LogEvent) { continue } if event.meta { - format = FORMAT_META + format = FormatMeta } else { - format = FORMAT_MSG + format = FormatMsg } _, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what)) fd.Close() @@ -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() } }