-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.go
157 lines (134 loc) · 3.47 KB
/
irc.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"github.com/Sirupsen/logrus"
irc "github.com/fluffle/goirc/client"
"regexp"
"strings"
)
const (
NickRegexp = `(?i)[a-z;\[\]\\` + "`" + `_^{}|][a-z0-9;\[\]\\` + "`" + `_^{}|-]*`
)
type CommandHandlerFunc func(*irc.Conn, *Cmd)
type Cmd struct {
Command string
Args []string
*Privmsg
}
type CommandHandler struct {
command string
handlerFunc CommandHandlerFunc
}
func NewCommandHandler(command string, handler CommandHandlerFunc) *CommandHandler {
log.WithFields(logrus.Fields{
"command": command,
}).Debug("Registering new irc command")
return &CommandHandler{
command: command,
handlerFunc: handler,
}
}
func (h CommandHandler) Handle(c *irc.Conn, line *irc.Line) {
msg := parsePrivmsg(line)
if msg.Public() {
h.command = fmt.Sprintf("!%s", h.command)
}
if strings.HasPrefix(msg.Text, h.command) {
log.WithFields(logrus.Fields{
"nick": msg.Nick,
"channel": msg.Channel,
"text": msg.Text,
"command": h.command,
}).Debug("Executing irc command")
h.handlerFunc(c, &Cmd{
Command: h.command,
Args: strings.Split(msg.Text, " ")[1:],
Privmsg: msg})
}
}
type MatchHandlerFunc func(*irc.Conn, *Privmsg, []string)
type MatchHandler struct {
matcher *regexp.Regexp
handlerFunc MatchHandlerFunc
}
func NewMatchHandler(matcher *regexp.Regexp, handler MatchHandlerFunc) *MatchHandler {
log.WithFields(logrus.Fields{
"matcher": matcher,
}).Debug("Registering new irc regex matcher")
return &MatchHandler{
matcher: matcher,
handlerFunc: handler,
}
}
func (h *MatchHandler) Handle(c *irc.Conn, line *irc.Line) {
msg := parsePrivmsg(line)
matches := h.matcher.FindStringSubmatch(msg.Text)
if matches == nil {
return
}
log.WithFields(logrus.Fields{
"nick": msg.Nick,
"channel": msg.Channel,
"text": msg.Text,
"matcher": h.matcher,
"matches": matches,
}).Debug("Executing irc regex matcher")
h.handlerFunc(c, msg, matches)
}
type MatchAllHandlerFunc func(*irc.Conn, *Privmsg, [][]string)
type MatchAllHandler struct {
matcher *regexp.Regexp
handlerFunc MatchAllHandlerFunc
}
func NewMatchAllHandler(matcher *regexp.Regexp, handler MatchAllHandlerFunc) *MatchAllHandler {
log.WithFields(logrus.Fields{
"matcher": matcher,
}).Debug("Registering new irc regex all matcher")
return &MatchAllHandler{
matcher: matcher,
handlerFunc: handler,
}
}
func (h *MatchAllHandler) Handle(c *irc.Conn, line *irc.Line) {
msg := parsePrivmsg(line)
matches := h.matcher.FindAllStringSubmatch(msg.Text, -1)
if matches == nil {
return
}
log.WithFields(logrus.Fields{
"nick": msg.Nick,
"channel": msg.Channel,
"text": msg.Text,
"matcher": h.matcher,
"matches": matches,
}).Debug("Executing irc regex all matcher")
h.handlerFunc(c, msg, matches)
}
type Privmsg struct {
Channel, Text string
*irc.Line
}
func parsePrivmsg(line *irc.Line) *Privmsg {
return &Privmsg{
line.Args[0], // Channel
line.Args[1], // Text
line,
}
}
func (m *Privmsg) respond(includeToNick bool, conn *irc.Conn, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if m.Public() {
if includeToNick {
msg = fmt.Sprintf("%s: %s", m.Nick, msg)
}
conn.Privmsg(m.Channel, msg)
} else {
conn.Privmsg(m.Nick, msg)
}
}
func (m *Privmsg) Respond(conn *irc.Conn, format string, args ...interface{}) {
m.respond(false, conn, format, args...)
}
func (m *Privmsg) RespondToNick(conn *irc.Conn, format string, args ...interface{}) {
m.respond(true, conn, format, args...)
}