2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
37 FormatMsg = "[%s] <%s> %s\n"
38 FormatMeta = "[%s] * %s %s\n"
41 // Client events going from each of client
42 // They can be either NEW, DEL or unparsed MSG
43 type ClientEvent struct {
49 func (m ClientEvent) String() string {
50 return string(m.eventType) + ": " + m.client.String() + ": " + m.text
53 // Logging in-room events
54 // Intended to tell when, where and who send a message or meta command
55 type LogEvent struct {
62 // Logging events logger itself
63 // Each room's events are written to separate file in logdir
64 // Events include messages, topic and keys changes, joining and leaving
65 func Logger(logdir string, events <-chan LogEvent) {
66 mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
67 perm := os.FileMode(0660)
72 for event := range events {
73 logfile = path.Join(logdir, event.where)
74 fd, err = os.OpenFile(logfile, mode, perm)
76 log.Println("Can not open logfile", logfile, err)
84 _, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
87 log.Println("Error writing to logfile", logfile, err)
92 type StateEvent struct {
98 // Room state events saver
99 // Room states shows that either topic or key has been changed
100 // Each room's state is written to separate file in statedir
101 func StateKeeper(statedir string, events <-chan StateEvent) {
105 for event := range events {
106 fn = path.Join(statedir, event.where)
107 data = event.topic + "\n" + event.key + "\n"
108 err = ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
110 log.Printf("Can not write statefile %s: %v", fn, err)