This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
147 lines (119 loc) · 3.26 KB
/
config.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
package main
import (
"errors"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
type InviteBehavior string
const (
// Only accept invite and automatically join the channel once. When this
// value is set, the configuration will fall back to "Ignore" once a channel
// is joined.
InviteBehaviorOnce InviteBehavior = "once"
// Ignore all invitations.
InviteBehaviorIgnore = "ignore"
// Always automatically join channels on invite.
InviteBehaviorAlways = "always"
)
type ServerConfig struct {
Address string "Address"
Password string "Password"
SSL bool "SSL"
}
type NickServConfig struct {
// The username to use when authenticating with NickServ.
Username string "Username,omitempty"
// The password to use when authenticating with NickServ.
Password string "Password,omitempty"
}
type Config struct {
// The main nickname the bot uses.
Nick string "Nick"
// The ident (username) the bot uses.
Ident string "Ident"
// The real name the bot uses.
Name string "Name"
// The full exact hostmask of admins that can control the bot.
Admins []string "Admins"
// Determines how the bot should handle invitations.
AutoJoinOnInvite InviteBehavior "AutoJoinOnInvite"
// The channels to join automatically. This field will be changed by the
// bot when it joins channels on invitation.
Channels []string "Channels"
// Old hostmasks from which to migrate old leftover bans.
OldHostmasks []string "OldHostmasks"
// The server to connect to.
Server ServerConfig "Server"
// Details regarding NickServ authentication
NickServ NickServConfig "NickServ"
}
func Load(configPath string) (c Config, err error) {
logger.Debug("Reading from %v...", configPath)
contents, err := ioutil.ReadFile(configPath)
if err != nil {
return
}
logger.Debug("Parsing configuration...")
c = defaultConfiguration
err = yaml.Unmarshal(contents, &c)
return
}
func (c Config) Save(configPath string) error {
contents, err := yaml.Marshal(&c)
if err != nil {
return err
}
return ioutil.WriteFile(configPath, contents, 0640)
}
func (c Config) Validate() error {
if c.Nick == "" {
return errors.New("You need to set a nickname in the configuration.")
}
if c.Ident == "" {
return errors.New("You need to set an ident in the configuration.")
}
if c.Server.Address == "" {
return errors.New("You need to set a server address in the configuration.")
}
// Admins
if c.Admins == nil {
c.Admins = []string{}
}
// AutoJoinOnInvite
c.AutoJoinOnInvite = InviteBehavior(strings.ToLower(string(c.AutoJoinOnInvite)))
switch c.AutoJoinOnInvite {
case InviteBehaviorAlways:
case InviteBehaviorIgnore:
case InviteBehaviorOnce:
case "":
c.AutoJoinOnInvite = InviteBehaviorOnce
default:
return errors.New("AutoJoinOnInvite must be set to either always, ignore or once (default).")
}
// Channels
if c.Channels == nil {
c.Channels = []string{}
}
// OldHostmasks
if c.OldHostmasks == nil {
c.OldHostmasks = []string{}
}
return nil
}
// Default configuration
var defaultConfiguration = Config{
Nick: "vpn",
Ident: "vpn",
Admins: []string{},
AutoJoinOnInvite: InviteBehaviorOnce,
Channels: []string{"#vpnbot"},
Server: ServerConfig{
Address: "",
SSL: false,
},
NickServ: NickServConfig{
Username: "",
Password: "",
},
}