2020-04-03 16:59:17 +00:00
|
|
|
package soju
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type messageLogger struct {
|
|
|
|
conn *upstreamConn
|
|
|
|
entity string
|
|
|
|
|
2020-04-07 19:50:12 +00:00
|
|
|
path string
|
|
|
|
file *os.File
|
2020-04-03 16:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMessageLogger(uc *upstreamConn, entity string) *messageLogger {
|
|
|
|
return &messageLogger{
|
|
|
|
conn: uc,
|
|
|
|
entity: entity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:50:12 +00:00
|
|
|
func logPath(network *network, entity string, t time.Time) string {
|
|
|
|
user := network.user
|
|
|
|
srv := user.srv
|
|
|
|
|
|
|
|
// TODO: handle/forbid network/entity names with illegal path characters
|
|
|
|
year, month, day := t.Date()
|
|
|
|
filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
|
|
|
return filepath.Join(srv.LogPath, user.Username, network.GetName(), entity, filename)
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:59:17 +00:00
|
|
|
func (ml *messageLogger) Append(msg *irc.Message) error {
|
|
|
|
s := formatMessage(msg)
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:48:23 +00:00
|
|
|
// TODO: parse time from msg.Tags["time"], if available
|
|
|
|
|
2020-04-03 16:59:17 +00:00
|
|
|
// TODO: enforce maximum open file handles (LRU cache of file handles)
|
|
|
|
// TODO: handle non-monotonic clock behaviour
|
|
|
|
now := time.Now()
|
2020-04-07 19:50:12 +00:00
|
|
|
path := logPath(ml.conn.network, ml.entity, now)
|
|
|
|
if ml.path != path {
|
2020-04-03 16:59:17 +00:00
|
|
|
if ml.file != nil {
|
|
|
|
ml.file.Close()
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:50:12 +00:00
|
|
|
dir := filepath.Dir(path)
|
2020-04-03 16:59:17 +00:00
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return fmt.Errorf("failed to create logs directory %q: %v", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open log file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:50:12 +00:00
|
|
|
ml.path = path
|
2020-04-03 16:59:17 +00:00
|
|
|
ml.file = f
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s)
|
|
|
|
if err != nil {
|
2020-04-07 19:50:12 +00:00
|
|
|
return fmt.Errorf("failed to log message to %q: %v", ml.path, err)
|
2020-04-03 16:59:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *messageLogger) Close() error {
|
|
|
|
if ml.file == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ml.file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatMessage formats a message log line. It assumes a well-formed IRC
|
|
|
|
// message.
|
|
|
|
func formatMessage(msg *irc.Message) string {
|
|
|
|
switch strings.ToUpper(msg.Command) {
|
|
|
|
case "NICK":
|
|
|
|
return fmt.Sprintf("*** %s is now known as %s", msg.Prefix.Name, msg.Params[0])
|
|
|
|
case "JOIN":
|
|
|
|
return fmt.Sprintf("*** Joins: %s (%s@%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host)
|
|
|
|
case "PART":
|
|
|
|
var reason string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
reason = msg.Params[1]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*** Parts: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
|
|
|
case "KICK":
|
|
|
|
nick := msg.Params[1]
|
|
|
|
var reason string
|
|
|
|
if len(msg.Params) > 2 {
|
|
|
|
reason = msg.Params[2]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*** %s was kicked by %s (%s)", nick, msg.Prefix.Name, reason)
|
|
|
|
case "QUIT":
|
|
|
|
var reason string
|
|
|
|
if len(msg.Params) > 0 {
|
|
|
|
reason = msg.Params[0]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*** Quits: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
2020-04-06 16:49:39 +00:00
|
|
|
case "TOPIC":
|
|
|
|
var topic string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
topic = msg.Params[1]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*** %s changes topic to '%s'", msg.Prefix.Name, topic)
|
2020-04-03 16:59:17 +00:00
|
|
|
case "MODE":
|
|
|
|
return fmt.Sprintf("*** %s sets mode: %s", msg.Prefix.Name, strings.Join(msg.Params[1:], " "))
|
2020-04-06 16:45:30 +00:00
|
|
|
case "NOTICE":
|
|
|
|
return fmt.Sprintf("-%s- %s", msg.Prefix.Name, msg.Params[1])
|
|
|
|
case "PRIVMSG":
|
2020-04-03 16:59:17 +00:00
|
|
|
return fmt.Sprintf("<%s> %s", msg.Prefix.Name, msg.Params[1])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|