-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
72 lines (60 loc) · 1.6 KB
/
config.go
File metadata and controls
72 lines (60 loc) · 1.6 KB
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
package sdbot
import (
"os"
"regexp"
"strings"
"github.com/BurntSushi/toml"
)
// Config holds the configuration information read from the config.toml file.
type Config struct {
Server string
Port string
Nick string
Password string
MessagesPerSecond float64
Rooms []string
Avatar int
PluginPrefixes []string
PluginSuffixes []string
PluginPrefix *regexp.Regexp
PluginSuffix *regexp.Regexp
CaseInsensitive bool
IgnorePrivateMessages bool
IgnoreChatMessages bool
}
// Reads the config data from toml config file.
func readConfig(path string) *Config {
_, err := os.Stat(path)
if err != nil {
Fatalf("Config file is missing: %s", path)
}
var config Config
_, err = toml.DecodeFile(path, &config)
CheckErr(err)
config.generatePluginPrefixRegexp()
config.generatePluginSuffixRegexp()
if config.MessagesPerSecond == 0 {
config.MessagesPerSecond = 3
}
return &config
}
func (c *Config) generatePluginPrefixRegexp() {
var prefixes []string
for _, prefix := range c.PluginPrefixes {
prefixes = append(prefixes, regexp.QuoteMeta(prefix))
}
regStr := "^(" + strings.Join(prefixes, "|") + ")"
reg, err := regexp.Compile(regStr)
CheckErr(err)
c.PluginPrefix = reg
}
func (c *Config) generatePluginSuffixRegexp() {
var suffixes []string
for _, suffix := range c.PluginSuffixes {
suffixes = append(suffixes, regexp.QuoteMeta(suffix))
}
regStr := "(" + strings.Join(suffixes, "|") + ")$"
reg, err := regexp.Compile(regStr)
CheckErr(err)
c.PluginSuffix = reg
}