forked from nnev/frank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrank.go
211 lines (173 loc) · 5.3 KB
/
frank.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
parser "github.com/husio/irc"
"github.com/robustirc/bridge/robustsession"
)
var (
network = flag.String("network", "", `DNS name to connect to (e.g. "robustirc.net"). The _robustirc._tcp SRV record must be present.`)
tlsCAFile = flag.String("tls_ca_file", "", "Use the specified file as trusted CA instead of the system CAs. Useful for testing.")
channels = flag.String("channels", "", "channels the bot should join. Space separated.")
nick = flag.String("nick", "frank", "nickname of the bot")
admins = flag.String("admins", "xeen", "users who can control the bot. Space separated.")
nickserv_password = flag.String("nickserv_password", "", "password used to identify with nickserv. No action is taken if password is blank or not set.")
verbose = flag.Bool("verbose", false, "enable to get very detailed logs")
)
type Message *parser.Message
var session *robustsession.RobustSession
func setupFlags() {
flag.Parse()
if *network == "" {
log.Fatal("You must specify -network")
}
}
func setupSession() {
var err error
session, err = robustsession.Create(*network, *tlsCAFile)
if err != nil {
log.Fatal("Could not create RobustIRC session: %v", err)
}
log.Printf("Created RobustSession for %s. Session id: %s", *nick, session.SessionId())
}
func setupKeepalive() {
// TODO: only if no other traffic
go func() {
keepaliveToNetwork := time.After(1 * time.Minute)
for {
<-keepaliveToNetwork
session.PostMessage("PING keepalive")
keepaliveToNetwork = time.After(1 * time.Minute)
}
}()
}
func setupSignalHandler() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-signalChan
log.Printf("Exiting due to signal %q", sig)
kill()
}()
}
func setupSessionErrorHandler() {
go func() {
err := <-session.Errors
log.Fatal("RobustIRC session error: %v", err)
}()
}
func setupJoinChannels() {
for _, channel := range strings.Split(*channels, " ") {
Join(channel)
}
}
func kill() {
log.Printf("Deleting Session. Goodbye.")
if err := session.Delete(*nick + " says goodbye"); err != nil {
log.Fatalf("Could not properly delete RobustIRC session: %v", err)
}
os.Exit(int(syscall.SIGTERM) | 0x80)
}
func boot() {
Post(fmt.Sprintf("NICK %s", *nick))
Post(fmt.Sprintf("USER bot 0 * :%s von Bötterich", *nick))
if *nickserv_password == "" {
setupJoinChannels()
return
}
nickserv := make(chan bool, 1)
listener := ListenerAdd("nickserv auth detector", func(parsed Message) {
// PREFIX=services.robustirc.net COMMAND=MODE PARAMS=[frank2] TRAILING=+r
is_me := Target(parsed) == *nick
is_plus_r := strings.HasPrefix(parsed.Trailing, "+") && strings.Contains(parsed.Trailing, "r")
if parsed.Command == "MODE" && is_me && is_plus_r {
nickserv <- true
}
})
log.Printf("NICKSERV: Authenticating…")
Privmsg("nickserv", "identify "+*nickserv_password)
go func() {
select {
case <-nickserv:
log.Printf("NICKSERV: auth successful")
case <-time.After(10 * time.Second):
log.Printf("NICKSERV: auth failed. No response within 10s, joining channels anyway. Maybe check the password, i.e. “/msg frank msg nickserv identify <pass>” and watch the logs.")
}
listener.Remove()
setupJoinChannels()
}()
}
func parse(msg string) {
defer func() {
if r := recover(); r != nil {
log.Printf("parser broken: %v\nMessage that caused this: %s", r, msg)
}
}()
if strings.TrimSpace(msg) == "" {
return
}
parsed, err := parser.ParseLine(msg)
if err != nil {
log.Fatal("Could not parse IRC message: %v", err)
return
}
// Work around incorrect parsing when a single word PRIVMSG is not formatted
// as a trailing message, see: github.com/robustirc/robustirc/issues/129
// We should probably wait for github.com/sorcix/irc/issues/26 to be fixed and
// then use that parsing library in favor of the lesser/unmaintained
// husio/irc.
if parsed.Trailing == "" && len(parsed.Params) == 2 {
parsed.Trailing = parsed.Params[1]
parsed.Params = parsed.Params[:1]
}
if parsed.Command == "PONG" {
return
}
listenersRun(parsed)
}
func main() {
listenersReset()
setupFlags()
setupSession()
setupSignalHandler()
setupKeepalive()
setupSessionErrorHandler()
boot()
go TopicChanger()
go Rss()
ListenerAdd("help", runnerHelp)
ListenerAdd("admin", runnerAdmin)
ListenerAdd("highlight", runnerHighlight)
ListenerAdd("karma", runnerKarma)
ListenerAdd("invite", runnerInvite)
ListenerAdd("lmgtfy", runnerLmgtfy)
ListenerAdd("urifind", runnerUrifind)
ListenerAdd("raumbang", runnerRaumbang)
ListenerAdd("greeter", runnerGreet)
// Keep this last, so that other runners can access the name lists
ListenerAdd("updateMembers", runnerMembers)
if *verbose {
ListenerAdd("verbose debugger", func(parsed Message) {
log.Printf("< PREFIX=%s COMMAND=%s PARAMS=%s TRAILING=%s", parsed.Prefix, parsed.Command, parsed.Params, parsed.Trailing)
})
}
ListenerAdd("nickname checker", func(parsed Message) {
if parsed.Command == ERR_NICKNAMEINUSE {
log.Printf("Nickname is already in use. Sleeping for a minute before restarting.")
listenersReset()
time.Sleep(time.Minute)
log.Printf("Killing now due to nickname being in use")
kill()
}
})
for {
msg := <-session.Messages
parse(msg)
}
}