-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
157 lines (134 loc) · 3.73 KB
/
main.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"
"log"
"regexp"
"strings"
"time"
"crypto/tls"
"foubot2/configuration"
"foubot2/status"
irc "github.com/thoj/go-ircevent"
"net"
)
const botChannel = configuration.BotChannel
const botNick = configuration.BotNick
const botPswd = configuration.BotPswd
const servertls = configuration.ServerTLS
func handleMessages(event *irc.Event, irc *irc.Connection) {
target := event.Nick
prefix := ""
if event.Arguments[0] == botChannel {
prefix = fmt.Sprintf("%s: ", target)
target = botChannel
}
command := strings.Split(event.Arguments[1], " ")[0]
if command == "!vox" && configuration.BotAutoVoice {
irc.Mode(botChannel, "+v", event.Nick)
irc.Privmsg(target, fmt.Sprintf("%sAlrity then!", prefix))
return
}
if command == "!status" {
status := ledsign.GetSwitchStatus()
if status {
irc.Privmsg(target, fmt.Sprintf("%sThe lab is currently OPEN.", prefix))
} else {
irc.Privmsg(target, fmt.Sprintf("%sSadly, the lab is currently CLOSED.", prefix))
}
return
}
match, _ := regexp.MatchString(botNick, event.Arguments[1])
if match {
irc.Privmsg(target, fmt.Sprintf("%su wot m8?", prefix))
return
}
if event.Arguments[0] != botChannel {
irc.Privmsg(target, fmt.Sprintf("%sVa?", prefix))
return
}
}
func handleJoin(event *irc.Event, irc *irc.Connection) {
go func() {
time.Sleep(time.Minute * 5)
irc.Mode(botChannel, "+v", event.Nick)
}()
}
func handleNick(event *irc.Event, irc *irc.Connection) {
go func() {
time.Sleep(time.Minute * 5)
irc.Mode(botChannel, "+v", event.Nick)
}()
}
func handlePart(event *irc.Event, irc *irc.Connection) {
go func() {
time.Sleep(time.Minute * 5)
irc.Mode(botChannel, "+v", event.Nick)
}()
}
func connectOnce() {
irccon := irc.IRC(botNick, "foubot2")
irccon.VerboseCallbackHandler = false
irccon.Debug = false
irccon.UseTLS = true
host, _, err := net.SplitHostPort(servertls)
if err != nil {
log.Panicf("parse IRC server: %s\n", err)
}
irccon.TLSConfig = &tls.Config{ServerName: host}
if botPswd != "" {
irccon.UseSASL = true
irccon.SASLLogin = botNick
irccon.SASLPassword = botPswd
}
var button *ledsign.SWITCHSTATE
defer func() {
if button != nil {
button.CloseSwitchStatus()
}
}()
irccon.AddCallback("001", func(e *irc.Event) {
log.Printf("Got welcome, joining %s", botChannel)
irccon.Join(botChannel)
})
irccon.AddCallback("332", func(e *irc.Event) {
log.Printf("Got topic, starting status goroutine")
button = ledsign.NewSwitchStatus(e.Arguments[2], irccon)
})
irccon.AddCallback("PRIVMSG", func(e *irc.Event) { handleMessages(e, irccon) })
if configuration.BotAutoVoice {
irccon.AddCallback("JOIN", func(e *irc.Event) { handleJoin(e, irccon) })
irccon.AddCallback("NICK", func(e *irc.Event) { handleNick(e, irccon) })
irccon.AddCallback("PART", func(e *irc.Event) { handlePart(e, irccon) })
}
// Do not use irccon.Loop() - it doesn't reconnect reliably when using SASL:
// https://github.com/thoj/go-ircevent/issues/112#issuecomment-2562001261
// This specific code pattern observed to:
// 1) reconnect reliably
// 2) not leak goroutines
err = irccon.Connect(servertls)
defer func() {
// Workaround for https://github.com/thoj/go-ircevent/issues/112#issuecomment-2569796268:
// If Connect() fails early (eg. from Dial), irccon.Error is not yet
// created and Disconnect can deadlock. Make sure Error exists in all
// cases.
irccon.Lock()
if irccon.Error == nil {
irccon.Error = make(chan error, 10)
}
irccon.Unlock()
irccon.Disconnect()
fmt.Printf("IRC disconnected\n")
}()
if err != nil {
fmt.Printf("Connect error: %s\n", err)
return
}
err = <-irccon.ErrorChan()
fmt.Printf("Error, disconnected: %s\n", err)
}
func main() {
for {
connectOnce()
time.Sleep(60 * time.Second)
}
}