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