X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=events.go;h=843e8b59d8b407d2e96de3e334d77afab4219d80;hp=6fdf1e79b3318052d319878457459d9ca22ccc55;hb=ad68cf29f652c5bfa7822a10000e9da658fbf1d5;hpb=b2f90fb412218973608b24a32eebadc60de9878a diff --git a/events.go b/events.go index 6fdf1e7..843e8b5 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,7 +62,7 @@ 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 @@ -72,9 +74,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() @@ -93,17 +95,13 @@ 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) { 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() } }