X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=events.go;h=283b5f76d5039ba03fa0e1f2b2cbefced7f877cd;hp=95760e8a6ceeaf0efe139c5d3e8d295d685c7b13;hb=def58d0f4944397faa8cdd4a9cf3515125e0b548;hpb=de49f5eeba0117dd09fcdc290ef99ba6a5e2656d diff --git a/events.go b/events.go index 95760e8..283b5f7 100644 --- a/events.go +++ b/events.go @@ -1,11 +1,10 @@ /* goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014 Sergey Matveev +Copyright (C) 2014-2020 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 -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -15,10 +14,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 +27,33 @@ 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 + EventTerm = iota + EventTick = iota + FormatMsg = "[%s] <%s> %s\n" + FormatMeta = "[%s] * %s %s\n" +) + +var ( + logSink chan LogEvent = make(chan LogEvent) + stateSink chan StateEvent = make(chan StateEvent) ) // 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 @@ -64,17 +72,20 @@ 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() @@ -94,16 +105,15 @@ 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) + 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() } }