X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=events.go;h=bc659a6c9eeb0e8302d6e55abecb492245622707;hp=6fdf1e79b3318052d319878457459d9ca22ccc55;hb=186ec4b4bd91f9fd101b2f62d30c0f2bd4ed50ce;hpb=b2f90fb412218973608b24a32eebadc60de9878a diff --git a/events.go b/events.go index 6fdf1e7..bc659a6 100644 --- a/events.go +++ b/events.go @@ -1,6 +1,6 @@ /* goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014 Sergey Matveev +Copyright (C) 2014-2015 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,10 +15,12 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ + package main import ( "fmt" + "io/ioutil" "log" "os" "path" @@ -26,26 +28,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 @@ -60,21 +62,24 @@ type LogEvent struct { // Logging events logger itself // Each room's events are written to separate file in logdir // Events include messages, topic and keys changes, joining and leaving -func Logger(logdir string, events chan LogEvent) { +func Logger(logdir string, events <-chan LogEvent) { mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND perm := os.FileMode(0660) var format string + var logfile string + var fd *os.File + var err error for event := range events { - logfile := path.Join(logdir, event.where) - fd, err := os.OpenFile(logfile, mode, perm) + logfile = path.Join(logdir, event.where + ".log") + fd, err = os.OpenFile(logfile, mode, perm) if err != nil { log.Println("Can not open logfile", logfile, err) 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() @@ -93,17 +98,16 @@ type StateEvent struct { // Room state events saver // 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) +func StateKeeper(statedir string, events <-chan StateEvent) { + var fn string + var data string + var err error 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() } }