This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.go
123 lines (103 loc) · 2.85 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package logger
import (
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/plugins"
"github.com/depado/go-b0tsec/utils"
"github.com/thoj/go-ircevent"
)
const (
middlewareName = "logger"
)
var (
urlRegex, _ = regexp.Compile("^https?:.*$")
// LinkLogger is used to aggregate links.
LinkLogger *log.Logger
// LinkFile is the file associated to the LinkLogger
LinkFile *os.File
//HistoryLogger is used to aggregate all the messages.
HistoryLogger *log.Logger
// HistoryFile is the file associated to the HistoryLogger
HistoryFile *os.File
)
// Middleware is the actual logger.Middleware
type Middleware struct {
Started bool
}
func init() {
plugins.Middlewares[middlewareName] = new(Middleware)
}
// Get actually operates on the message
func (m *Middleware) Get(ib *irc.Connection, from string, to string, message string) {
if !m.Started {
return
}
HistoryLogger.Println(from + " : " + message)
for _, field := range strings.Fields(message) {
if urlRegex.MatchString(field) {
LinkLogger.Println(from + " : " + field)
}
}
}
// Start starts the middleware and returns any occurred error, nil otherwise
func (m *Middleware) Start() error {
if utils.StringInSlice(middlewareName, configuration.Config.Middlewares) {
if err := InitLoggers(); err != nil {
return fmt.Errorf("Error init loggers : %s\n", err)
}
m.Started = true
}
return nil
}
// Stop returns nil when it didn’t encounter any error, the error otherwise
func (m *Middleware) Stop() error {
if !m.Started {
return nil
}
var closeErr error
if err := HistoryFile.Close(); err != nil {
log.Printf("Error closing history file : %s\n", err)
closeErr = fmt.Errorf("error occurred while closing loggers’ files")
}
if err := LinkFile.Close(); err != nil {
log.Printf("closing links file : %s\n", err)
closeErr = fmt.Errorf("error occurred while closing loggers’ files")
}
if closeErr != nil {
return closeErr
}
m.Started = false
return nil
}
// IsStarted returns the state of the middleware
func (m *Middleware) IsStarted() bool {
return m.Started
}
// InitLoggers initialize the loggers to use with the logger middleware.
func InitLoggers() error {
var err error
sc := strings.Replace(configuration.Config.Channel, "#", "", 1)
if err = utils.CheckAndCreateFolder("logs"); err != nil {
return err
}
lf := path.Join("logs", sc)
if err = utils.CheckAndCreateFolder(lf); err != nil {
return err
}
HistoryFile, err := os.OpenFile(path.Join(lf, "history.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
LinkFile, err := os.OpenFile(path.Join(lf, "links.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
LinkLogger = log.New(LinkFile, "", log.Ldate|log.Ltime)
HistoryLogger = log.New(HistoryFile, "", log.Ldate|log.Ltime)
return err
}