[215] | 1 | package soju
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "fmt"
|
---|
| 5 | "os"
|
---|
| 6 | "path/filepath"
|
---|
| 7 | "strings"
|
---|
| 8 | "time"
|
---|
| 9 |
|
---|
| 10 | "gopkg.in/irc.v3"
|
---|
| 11 | )
|
---|
| 12 |
|
---|
| 13 | type messageLogger struct {
|
---|
[248] | 14 | network *network
|
---|
| 15 | entity string
|
---|
[215] | 16 |
|
---|
[247] | 17 | path string
|
---|
| 18 | file *os.File
|
---|
[215] | 19 | }
|
---|
| 20 |
|
---|
[248] | 21 | func newMessageLogger(network *network, entity string) *messageLogger {
|
---|
[215] | 22 | return &messageLogger{
|
---|
[248] | 23 | network: network,
|
---|
| 24 | entity: entity,
|
---|
[215] | 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[247] | 28 | func logPath(network *network, entity string, t time.Time) string {
|
---|
| 29 | user := network.user
|
---|
| 30 | srv := user.srv
|
---|
| 31 |
|
---|
| 32 | // TODO: handle/forbid network/entity names with illegal path characters
|
---|
| 33 | year, month, day := t.Date()
|
---|
| 34 | filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
---|
| 35 | return filepath.Join(srv.LogPath, user.Username, network.GetName(), entity, filename)
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[215] | 38 | func (ml *messageLogger) Append(msg *irc.Message) error {
|
---|
| 39 | s := formatMessage(msg)
|
---|
| 40 | if s == "" {
|
---|
| 41 | return nil
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[216] | 44 | // TODO: parse time from msg.Tags["time"], if available
|
---|
| 45 |
|
---|
[215] | 46 | // TODO: enforce maximum open file handles (LRU cache of file handles)
|
---|
| 47 | // TODO: handle non-monotonic clock behaviour
|
---|
| 48 | now := time.Now()
|
---|
[248] | 49 | path := logPath(ml.network, ml.entity, now)
|
---|
[247] | 50 | if ml.path != path {
|
---|
[215] | 51 | if ml.file != nil {
|
---|
| 52 | ml.file.Close()
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[247] | 55 | dir := filepath.Dir(path)
|
---|
[215] | 56 | if err := os.MkdirAll(dir, 0700); err != nil {
|
---|
| 57 | return fmt.Errorf("failed to create logs directory %q: %v", dir, err)
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
---|
| 61 | if err != nil {
|
---|
| 62 | return fmt.Errorf("failed to open log file %q: %v", path, err)
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[247] | 65 | ml.path = path
|
---|
[215] | 66 | ml.file = f
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | _, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s)
|
---|
| 70 | if err != nil {
|
---|
[247] | 71 | return fmt.Errorf("failed to log message to %q: %v", ml.path, err)
|
---|
[215] | 72 | }
|
---|
| 73 | return nil
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | func (ml *messageLogger) Close() error {
|
---|
| 77 | if ml.file == nil {
|
---|
| 78 | return nil
|
---|
| 79 | }
|
---|
| 80 | return ml.file.Close()
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | // formatMessage formats a message log line. It assumes a well-formed IRC
|
---|
| 84 | // message.
|
---|
| 85 | func formatMessage(msg *irc.Message) string {
|
---|
| 86 | switch strings.ToUpper(msg.Command) {
|
---|
| 87 | case "NICK":
|
---|
| 88 | return fmt.Sprintf("*** %s is now known as %s", msg.Prefix.Name, msg.Params[0])
|
---|
| 89 | case "JOIN":
|
---|
| 90 | return fmt.Sprintf("*** Joins: %s (%s@%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host)
|
---|
| 91 | case "PART":
|
---|
| 92 | var reason string
|
---|
| 93 | if len(msg.Params) > 1 {
|
---|
| 94 | reason = msg.Params[1]
|
---|
| 95 | }
|
---|
| 96 | return fmt.Sprintf("*** Parts: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
| 97 | case "KICK":
|
---|
| 98 | nick := msg.Params[1]
|
---|
| 99 | var reason string
|
---|
| 100 | if len(msg.Params) > 2 {
|
---|
| 101 | reason = msg.Params[2]
|
---|
| 102 | }
|
---|
| 103 | return fmt.Sprintf("*** %s was kicked by %s (%s)", nick, msg.Prefix.Name, reason)
|
---|
| 104 | case "QUIT":
|
---|
| 105 | var reason string
|
---|
| 106 | if len(msg.Params) > 0 {
|
---|
| 107 | reason = msg.Params[0]
|
---|
| 108 | }
|
---|
| 109 | return fmt.Sprintf("*** Quits: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
[235] | 110 | case "TOPIC":
|
---|
| 111 | var topic string
|
---|
| 112 | if len(msg.Params) > 1 {
|
---|
| 113 | topic = msg.Params[1]
|
---|
| 114 | }
|
---|
| 115 | return fmt.Sprintf("*** %s changes topic to '%s'", msg.Prefix.Name, topic)
|
---|
[215] | 116 | case "MODE":
|
---|
| 117 | return fmt.Sprintf("*** %s sets mode: %s", msg.Prefix.Name, strings.Join(msg.Params[1:], " "))
|
---|
[234] | 118 | case "NOTICE":
|
---|
| 119 | return fmt.Sprintf("-%s- %s", msg.Prefix.Name, msg.Params[1])
|
---|
| 120 | case "PRIVMSG":
|
---|
[215] | 121 | return fmt.Sprintf("<%s> %s", msg.Prefix.Name, msg.Params[1])
|
---|
| 122 | default:
|
---|
| 123 | return ""
|
---|
| 124 | }
|
---|
| 125 | }
|
---|