2014-05-11 16:18:55 +00:00
|
|
|
/*
|
|
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
2018-03-25 14:57:05 +00:00
|
|
|
Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-05-09 15:27:06 +00:00
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-08 01:16:33 +00:00
|
|
|
"io/ioutil"
|
2014-05-11 16:18:55 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-08-09 12:42:19 +00:00
|
|
|
EventNew = iota
|
|
|
|
EventDel = iota
|
|
|
|
EventMsg = iota
|
|
|
|
EventTopic = iota
|
|
|
|
EventWho = iota
|
|
|
|
EventMode = iota
|
2015-10-11 08:12:55 +00:00
|
|
|
EventTerm = iota
|
|
|
|
EventTick = iota
|
2014-08-09 12:42:19 +00:00
|
|
|
FormatMsg = "[%s] <%s> %s\n"
|
|
|
|
FormatMeta = "[%s] * %s %s\n"
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
var (
|
|
|
|
logSink chan LogEvent = make(chan LogEvent)
|
|
|
|
stateSink chan StateEvent = make(chan StateEvent)
|
|
|
|
)
|
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
// Client events going from each of client
|
|
|
|
// They can be either NEW, DEL or unparsed MSG
|
|
|
|
type ClientEvent struct {
|
2014-08-09 12:42:19 +00:00
|
|
|
client *Client
|
|
|
|
eventType int
|
|
|
|
text string
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m ClientEvent) String() string {
|
2014-08-09 12:42:19 +00:00
|
|
|
return string(m.eventType) + ": " + m.client.String() + ": " + m.text
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logging in-room events
|
|
|
|
// Intended to tell when, where and who send a message or meta command
|
|
|
|
type LogEvent struct {
|
|
|
|
where string
|
|
|
|
who string
|
|
|
|
what string
|
|
|
|
meta bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logging events logger itself
|
|
|
|
// Each room's events are written to separate file in logdir
|
|
|
|
// Events include messages, topic and keys changes, joining and leaving
|
2014-06-08 00:21:06 +00:00
|
|
|
func Logger(logdir string, events <-chan LogEvent) {
|
2014-05-11 16:18:55 +00:00
|
|
|
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
|
|
|
perm := os.FileMode(0660)
|
|
|
|
var format string
|
2015-10-06 07:59:49 +00:00
|
|
|
var logfile string
|
|
|
|
var fd *os.File
|
|
|
|
var err error
|
2014-05-11 16:18:55 +00:00
|
|
|
for event := range events {
|
2015-10-11 08:12:55 +00:00
|
|
|
logfile = path.Join(logdir, event.where+".log")
|
2015-10-06 07:59:49 +00:00
|
|
|
fd, err = os.OpenFile(logfile, mode, perm)
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Can not open logfile", logfile, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if event.meta {
|
2014-08-09 12:42:19 +00:00
|
|
|
format = FormatMeta
|
2014-05-11 16:18:55 +00:00
|
|
|
} else {
|
2014-08-09 12:42:19 +00:00
|
|
|
format = FormatMsg
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
_, err = fd.WriteString(fmt.Sprintf(format, time.Now(), event.who, event.what))
|
|
|
|
fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error writing to logfile", logfile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StateEvent struct {
|
|
|
|
where string
|
|
|
|
topic string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Room state events saver
|
|
|
|
// Room states shows that either topic or key has been changed
|
|
|
|
// Each room's state is written to separate file in statedir
|
2014-06-08 00:21:06 +00:00
|
|
|
func StateKeeper(statedir string, events <-chan StateEvent) {
|
2015-10-06 07:59:49 +00:00
|
|
|
var fn string
|
|
|
|
var data string
|
|
|
|
var err error
|
2014-05-11 16:18:55 +00:00
|
|
|
for event := range events {
|
2015-10-06 07:59:49 +00:00
|
|
|
fn = path.Join(statedir, event.where)
|
|
|
|
data = event.topic + "\n" + event.key + "\n"
|
|
|
|
err = ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
2014-06-08 01:16:33 +00:00
|
|
|
log.Printf("Can not write statefile %s: %v", fn, err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|