This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
/
main.go
268 lines (230 loc) · 7.97 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"strings"
// YAML
// IRC bot
"github.com/StalkR/goircbot/bot"
// Logs
"github.com/fluffle/goirc/client"
"github.com/fluffle/goirc/logging"
glogging "github.com/fluffle/goirc/logging/glog"
// Plugins
adminplugin "github.com/StalkR/goircbot/plugins/admin"
"github.com/icedream/vpnbot/irc/autojoin"
"github.com/icedream/vpnbot/irc/bots"
"github.com/icedream/vpnbot/irc/dumptempban"
"github.com/icedream/vpnbot/irc/isupport"
"github.com/icedream/vpnbot/irc/mode"
"github.com/icedream/vpnbot/irc/nickserv"
"github.com/icedream/vpnbot/irc/tempban"
"github.com/icedream/vpnbot/irc/vpnbot"
"github.com/icedream/vpnbot/irc/whois"
)
// Program version, build server changes this at compile time to represent
// the `git describe` output of the current commit.
var version = "dev"
// Loaded configuration
var loadedConfiguration = defaultConfiguration
// Command line flags
var configPath = flag.String("config", "config.yml",
"Path to the configuration file. Configuration file must be in YAML format.")
var migratePath = flag.String("migrate", "",
"If given with a path, will migrate from version 1 of VPN bot to the new configuration format.")
var generateDefault = flag.Bool("generate", false,
"Generates a default configuration and saves it at the path given via -config.")
var makeTempBans = flag.Bool("maketempbans", false,
"Causes the bot to only connect shortly to dump all its previously set bans to temporary bans. This is useful for V1 migration.")
// Logger
var logger glogging.GLogger
// The main program logic.
func main() {
// Load configuration path from flags
flag.Parse()
// Initialize the logger
logger = glogging.GLogger{}
logging.SetLogger(logger)
// Check if we're supposed to generate a default config
if *generateDefault {
logger.Debug("Saving default configuration...")
if err := defaultConfiguration.Save(*configPath); err != nil {
logger.Error("Failed at saving default configuration: %v", err)
os.Exit(1)
}
logger.Info("Saved default configuration.")
os.Exit(0)
}
// Check if we're supposed to migrate an old config
if *migratePath != "" {
logger.Debug("Migrating old configuration...")
if c, err := LoadV1Config(*migratePath); err != nil {
logger.Error("Failed to load old configuration: %v", err)
os.Exit(1)
} else {
newC := c.Migrate()
if err := newC.Save(*configPath); err != nil {
logger.Error("Migration failed: %v", err)
os.Exit(1)
}
if err := newC.Validate(); err != nil {
logger.Warn("Migration successful but found errors while "+
"validating the new configuration, you should fix this before "+
"running the bot: %v", err)
os.Exit(2)
}
}
logger.Info("Migration successful.")
os.Exit(0)
}
// Load configuration from configuration path
if c, err := Load(*configPath); err != nil {
logger.Error("Can't load configuration from %v: %v\n", *configPath, err)
os.Exit(1)
} else {
loadedConfiguration = c
}
logger.Debug("Loaded configuration will be printed below.")
logger.Debug("%#v", loadedConfiguration)
// Validate configuration
if err := loadedConfiguration.Validate(); err != nil {
logger.Error("The configuration is invalid: %v\n", err)
os.Exit(2)
}
// Now initialize the bot
logger.Info("Initializing vpnbot %v...", version)
b := bot.NewBot(
loadedConfiguration.Server.Address,
loadedConfiguration.Server.SSL,
loadedConfiguration.Nick,
loadedConfiguration.Ident,
[]string{})
b.Conn().Config().Version = fmt.Sprintf("vpnbot/%v", version)
b.Conn().Config().Recover = func(conn *client.Conn, line *client.Line) {
if err := recover(); err != nil {
logging.Error("An internal error occurred: %v\n%v",
err, string(debug.Stack()))
}
}
b.Conn().Config().Pass = loadedConfiguration.Server.Password
if loadedConfiguration.Name != "" {
b.Conn().Config().Me.Name = loadedConfiguration.Name
}
// Load plugins
// TODO - Move this into its own little intelligent loader struct, maybe.
isupportPlugin := isupport.Register(b)
modePlugin := mode.Register(b, isupportPlugin)
nickservPlugin := nickserv.Register(b, modePlugin)
nickservPlugin.Username = loadedConfiguration.NickServ.Username
nickservPlugin.Password = loadedConfiguration.NickServ.Password
nickservPlugin.Channels = loadedConfiguration.Channels
switch {
case *makeTempBans: // Run in tempban dumping mode
// Prepare channels to let us know about dumped bans
doneChan := make(map[string]chan interface{})
for _, channel := range loadedConfiguration.Channels {
doneChan[strings.ToLower(channel)] = make(chan interface{}, 1)
}
// Load the tempban dumping plugin
dumptempbanPlugin := dumptempban.Register(b, isupportPlugin, modePlugin)
dumptempbanPlugin.DumpedBansFunc = func(target string, num int, err error) {
if err != nil {
logging.Error("Failed to dump bans for %v: %v", target, err)
} else {
logging.Info("Dumped %v bans for %v successfully.", num, target)
}
if done, ok := doneChan[strings.ToLower(target)]; ok {
done <- nil
}
}
// Start up the bot asynchronously
go b.Run()
// Wait for all channels to be done
for _, done := range doneChan {
<-done
}
b.Quit("Ban dumping done.")
default: // Run normally
// Load plugins
autojoin.Register(b)
adminplugin.Register(b, loadedConfiguration.Admins)
bots.Register(b, isupportPlugin)
tempbanPlugin := tempban.Register(b, isupportPlugin, modePlugin)
tempbanPlugin.OldHostmasks = loadedConfiguration.OldHostmasks
whoisPlugin := whois.Register(b, isupportPlugin)
vpnbotPlugin := vpnbot.Register(b, whoisPlugin, isupportPlugin,
tempbanPlugin, nickservPlugin)
vpnbotPlugin.Admins = loadedConfiguration.Admins
// This is to update the configuration when the bot joins channels
b.HandleFunc("join",
func(c *client.Conn, line *client.Line) {
// Arguments: [ <channel> ]
// Make sure this is about us
if line.Nick != c.Me().Nick {
return
}
// I don't think method calls are a good idea in a loop
channel := line.Target()
// See if we already had this channel saved
for _, savedChannel := range loadedConfiguration.Channels {
if strings.EqualFold(savedChannel, channel) {
return // Channel already saved
}
}
// Store this channel
logger.Info("Adding %v to configured channels", channel)
loadedConfiguration.Channels = append(
loadedConfiguration.Channels, channel)
// And save to configuration file!
loadedConfiguration.Save(*configPath)
})
b.HandleFunc("kick",
func(c *client.Conn, line *client.Line) {
// Arguments: [ <channel>, <nick>, <reason> ]
// Make sure this is about us
if line.Args[1] != c.Me().Nick {
return
}
// I don't think method calls are a good idea in a loop
channel := line.Target()
for index, savedChannel := range loadedConfiguration.Channels {
if strings.EqualFold(savedChannel, channel) {
// Delete the channel
logger.Info("Removing %v from configured channels", savedChannel)
loadedConfiguration.Channels = append(
loadedConfiguration.Channels[0:index],
loadedConfiguration.Channels[index+1:]...)
// And save to configuration file!
loadedConfiguration.Save(*configPath)
return
}
}
})
b.HandleFunc("part",
func(c *client.Conn, line *client.Line) {
// Arguments: [ <channel> (, <reason>) ]
// Make sure this is about us
if line.Nick != c.Me().Nick {
return
}
// I don't think method calls are a good idea in a loop
channel := line.Target()
for index, savedChannel := range loadedConfiguration.Channels {
if strings.EqualFold(savedChannel, channel) {
// Delete the channel
logger.Info("Removing %v from configured channels", savedChannel)
loadedConfiguration.Channels = append(
loadedConfiguration.Channels[0:index],
loadedConfiguration.Channels[index+1:]...)
// And save to configuration file!
loadedConfiguration.Save(*configPath)
return
}
}
})
// Run the bot
b.Run()
}
}