]> Cypherpunks.ru repositories - goircd.git/blobdiff - events.go
Replace Makefile with redo, keep version in source code
[goircd.git] / events.go
index 6fdf1e79b3318052d319878457459d9ca22ccc55..283b5f76d5039ba03fa0e1f2b2cbefced7f877cd 100644 (file)
--- a/events.go
+++ b/events.go
@@ -1,11 +1,10 @@
 /*
 goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
 
 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 <http://www.gnu.org/licenses/>.
 */
+
 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
@@ -60,21 +68,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 +104,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()
        }
 }