]> Cypherpunks.ru repositories - goircd.git/blob - events.go
Mention TLS and -verbose options in documentation
[goircd.git] / events.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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 package main
19
20 import (
21         "fmt"
22         "io/ioutil"
23         "log"
24         "os"
25         "path"
26         "time"
27 )
28
29 const (
30         EVENT_NEW   = iota
31         EVENT_DEL   = iota
32         EVENT_MSG   = iota
33         EVENT_TOPIC = iota
34         EVENT_WHO   = iota
35         EVENT_MODE  = iota
36         FORMAT_MSG  = "[%s] <%s> %s\n"
37         FORMAT_META = "[%s] * %s %s\n"
38 )
39
40 // Client events going from each of client
41 // They can be either NEW, DEL or unparsed MSG
42 type ClientEvent struct {
43         client     *Client
44         event_type int
45         text       string
46 }
47
48 func (m ClientEvent) String() string {
49         return string(m.event_type) + ": " + m.client.String() + ": " + m.text
50 }
51
52 // Logging in-room events
53 // Intended to tell when, where and who send a message or meta command
54 type LogEvent struct {
55         where string
56         who   string
57         what  string
58         meta  bool
59 }
60
61 // Logging events logger itself
62 // Each room's events are written to separate file in logdir
63 // Events include messages, topic and keys changes, joining and leaving
64 func Logger(logdir string, events <-chan LogEvent) {
65         mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
66         perm := os.FileMode(0660)
67         var format string
68         for event := range events {
69                 logfile := path.Join(logdir, event.where)
70                 fd, err := os.OpenFile(logfile, mode, perm)
71                 if err != nil {
72                         log.Println("Can not open logfile", logfile, err)
73                         continue
74                 }
75                 if event.meta {
76                         format = FORMAT_META
77                 } else {
78                         format = FORMAT_MSG
79                 }
80                 _, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
81                 fd.Close()
82                 if err != nil {
83                         log.Println("Error writing to logfile", logfile, err)
84                 }
85         }
86 }
87
88 type StateEvent struct {
89         where string
90         topic string
91         key   string
92 }
93
94 // Room state events saver
95 // Room states shows that either topic or key has been changed
96 // Each room's state is written to separate file in statedir
97 func StateKeeper(statedir string, events <-chan StateEvent) {
98         for event := range events {
99                 fn := path.Join(statedir, event.where)
100                 data := event.topic + "\n" + event.key + "\n"
101                 err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
102                 if err != nil {
103                         log.Printf("Can not write statefile %s: %v", fn, err)
104                 }
105         }
106 }