2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 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, version 3 of the License.
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.
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/>.
38 FormatMsg = "[%s] <%s> %s\n"
39 FormatMeta = "[%s] * %s %s\n"
43 logSink chan LogEvent = make(chan LogEvent)
44 stateSink chan StateEvent = make(chan StateEvent)
47 // Client events going from each of client
48 // They can be either NEW, DEL or unparsed MSG
49 type ClientEvent struct {
55 func (m ClientEvent) String() string {
56 return string(m.eventType) + ": " + m.client.String() + ": " + m.text
59 // Logging in-room events
60 // Intended to tell when, where and who send a message or meta command
61 type LogEvent struct {
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)
78 for event := range events {
79 logfile = path.Join(logdir, event.where+".log")
80 fd, err = os.OpenFile(logfile, mode, perm)
82 log.Println("Can not open logfile", logfile, err)
90 _, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
93 log.Println("Error writing to logfile", logfile, err)
98 type StateEvent struct {
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) {
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))
116 log.Printf("Can not write statefile %s: %v", fn, err)