]> Cypherpunks.ru repositories - goircd.git/blob - events.go
Raise copyright years
[goircd.git] / events.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "fmt"
22         "io/ioutil"
23         "log"
24         "os"
25         "path"
26         "time"
27 )
28
29 const (
30         EventNew   = iota
31         EventDel   = iota
32         EventMsg   = iota
33         EventTopic = iota
34         EventWho   = iota
35         EventMode  = iota
36         EventTerm  = iota
37         EventTick  = iota
38         FormatMsg  = "[%s] <%s> %s\n"
39         FormatMeta = "[%s] * %s %s\n"
40 )
41
42 var (
43         logSink   chan LogEvent   = make(chan LogEvent)
44         stateSink chan StateEvent = make(chan StateEvent)
45 )
46
47 // Client events going from each of client
48 // They can be either NEW, DEL or unparsed MSG
49 type ClientEvent struct {
50         client    *Client
51         eventType int
52         text      string
53 }
54
55 func (m ClientEvent) String() string {
56         return string(m.eventType) + ": " + m.client.String() + ": " + m.text
57 }
58
59 // Logging in-room events
60 // Intended to tell when, where and who send a message or meta command
61 type LogEvent struct {
62         where string
63         who   string
64         what  string
65         meta  bool
66 }
67
68 // Logging events logger itself
69 // Each room's events are written to separate file in logdir
70 // Events include messages, topic and keys changes, joining and leaving
71 func Logger(logdir string, events <-chan LogEvent) {
72         mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
73         perm := os.FileMode(0660)
74         var format string
75         var logfile string
76         var fd *os.File
77         var err error
78         for event := range events {
79                 logfile = path.Join(logdir, event.where+".log")
80                 fd, err = os.OpenFile(logfile, mode, perm)
81                 if err != nil {
82                         log.Println("Can not open logfile", logfile, err)
83                         continue
84                 }
85                 if event.meta {
86                         format = FormatMeta
87                 } else {
88                         format = FormatMsg
89                 }
90                 _, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
91                 fd.Close()
92                 if err != nil {
93                         log.Println("Error writing to logfile", logfile, err)
94                 }
95         }
96 }
97
98 type StateEvent struct {
99         where string
100         topic string
101         key   string
102 }
103
104 // Room state events saver
105 // Room states shows that either topic or key has been changed
106 // Each room's state is written to separate file in statedir
107 func StateKeeper(statedir string, events <-chan StateEvent) {
108         var fn string
109         var data string
110         var err error
111         for event := range events {
112                 fn = path.Join(statedir, event.where)
113                 data = event.topic + "\n" + event.key + "\n"
114                 err = ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
115                 if err != nil {
116                         log.Printf("Can not write statefile %s: %v", fn, err)
117                 }
118         }
119 }