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