|
1
|
package ergo |
|
2
|
|
|
3
|
import ( |
|
4
|
"bytes" |
|
5
|
"text/template" |
|
6
|
|
|
7
|
"github.com/conflicthq/scuttlebot/internal/config" |
|
8
|
"golang.org/x/crypto/bcrypt" |
|
9
|
) |
|
10
|
|
|
11
|
var ircdTemplate = template.Must(template.New("ircd").Parse(`# Generated by scuttlebot — do not edit manually. |
|
12
|
network: |
|
13
|
name: {{.NetworkName}} |
|
14
|
|
|
15
|
server: |
|
16
|
name: {{.ServerName}} |
|
17
|
listeners: |
|
18
|
"{{.IRCAddr}}": {} |
|
19
|
{{- if .TLSDomain}} |
|
20
|
"{{.TLSAddr}}": |
|
21
|
tls: |
|
22
|
autocert: true |
|
23
|
min-tls-version: 1.2 |
|
24
|
{{- end}} |
|
25
|
casemapping: ascii |
|
26
|
enforce-utf8: true |
|
27
|
max-sendq: 96k |
|
28
|
relaymsg: |
|
29
|
enabled: true |
|
30
|
separators: / |
|
31
|
available-to-chanops: false |
|
32
|
ip-cloaking: |
|
33
|
enabled: false |
|
34
|
lookup-hostnames: false |
|
35
|
|
|
36
|
datastore: |
|
37
|
path: ./ircd.db |
|
38
|
autoupgrade: true |
|
39
|
{{- if .HistoryEnabled}} |
|
40
|
{{- if .PostgresDSN}} |
|
41
|
postgresql: |
|
42
|
enabled: true |
|
43
|
dsn: "{{.PostgresDSN}}" |
|
44
|
{{- else if .MySQLEnabled}} |
|
45
|
mysql: |
|
46
|
enabled: true |
|
47
|
host: "{{.MySQL.Host}}" |
|
48
|
port: {{.MySQL.Port}} |
|
49
|
user: "{{.MySQL.User}}" |
|
50
|
password: "{{.MySQL.Password}}" |
|
51
|
history-database: "{{.MySQL.Database}}" |
|
52
|
{{- end}} |
|
53
|
{{- end}} |
|
54
|
|
|
55
|
accounts: |
|
56
|
registration: |
|
57
|
enabled: true |
|
58
|
allow-before-connect: true |
|
59
|
throttling: |
|
60
|
enabled: false |
|
61
|
authentication-enabled: true |
|
62
|
require-sasl: |
|
63
|
enabled: {{.RequireSASL}} |
|
64
|
|
|
65
|
channels: |
|
66
|
registration: |
|
67
|
enabled: true |
|
68
|
default-modes: "{{.DefaultChannelModes}}" |
|
69
|
|
|
70
|
history: |
|
71
|
enabled: {{.HistoryEnabled}} |
|
72
|
{{- if .HistoryEnabled}} |
|
73
|
channel-length: 2048 |
|
74
|
client-length: 256 |
|
75
|
autoresize-window: 3d |
|
76
|
autoreplay-on-join: 0 |
|
77
|
chathistory-maxmessages: 1000 |
|
78
|
znc-maxmessages: 2048 |
|
79
|
restrictions: |
|
80
|
expire-time: 7d |
|
81
|
query-cutoff: none |
|
82
|
persistent: |
|
83
|
enabled: {{.HistoryEnabled}} |
|
84
|
unregistered-channels: false |
|
85
|
registered-channels: opt-in |
|
86
|
direct-messages: opt-in |
|
87
|
{{- end}} |
|
88
|
|
|
89
|
opers: |
|
90
|
scuttlebot: |
|
91
|
class: server-admin |
|
92
|
whois-line: is a scuttlebot system operator |
|
93
|
password: "{{.OperPassHash}}" |
|
94
|
|
|
95
|
oper-classes: |
|
96
|
server-admin: |
|
97
|
title: Server Admin |
|
98
|
capabilities: |
|
99
|
- samode |
|
100
|
- chanreg |
|
101
|
|
|
102
|
api: |
|
103
|
enabled: true |
|
104
|
listener: "{{.APIAddr}}" |
|
105
|
bearer-tokens: |
|
106
|
- "{{.APIToken}}" |
|
107
|
|
|
108
|
logging: |
|
109
|
- method: stderr |
|
110
|
type: "* -userinput -useroutput" |
|
111
|
level: warn |
|
112
|
|
|
113
|
limits: |
|
114
|
nicklen: 32 |
|
115
|
channellen: 64 |
|
116
|
awaylen: 200 |
|
117
|
kicklen: 255 |
|
118
|
topiclen: 512 |
|
119
|
|
|
120
|
`)) |
|
121
|
|
|
122
|
type ircdTemplateData struct { |
|
123
|
NetworkName string |
|
124
|
ServerName string |
|
125
|
IRCAddr string |
|
126
|
TLSDomain string |
|
127
|
TLSAddr string |
|
128
|
DataDir string |
|
129
|
APIAddr string |
|
130
|
APIToken string |
|
131
|
OperPassHash string // bcrypt hash of APIToken for oper password |
|
132
|
HistoryEnabled bool |
|
133
|
PostgresDSN string |
|
134
|
MySQLEnabled bool |
|
135
|
MySQL config.MySQLConfig |
|
136
|
RequireSASL bool |
|
137
|
DefaultChannelModes string |
|
138
|
} |
|
139
|
|
|
140
|
// GenerateConfig renders an ircd.yaml from the given ErgoConfig. |
|
141
|
func GenerateConfig(cfg config.ErgoConfig) ([]byte, error) { |
|
142
|
operHash, _ := bcrypt.GenerateFromPassword([]byte(cfg.APIToken), bcrypt.MinCost) |
|
143
|
data := ircdTemplateData{ |
|
144
|
NetworkName: cfg.NetworkName, |
|
145
|
ServerName: cfg.ServerName, |
|
146
|
IRCAddr: cfg.IRCAddr, |
|
147
|
TLSDomain: cfg.TLSDomain, |
|
148
|
TLSAddr: cfg.TLSAddr, |
|
149
|
DataDir: cfg.DataDir, |
|
150
|
APIAddr: cfg.APIAddr, |
|
151
|
APIToken: cfg.APIToken, |
|
152
|
OperPassHash: string(operHash), |
|
153
|
HistoryEnabled: cfg.History.Enabled, |
|
154
|
PostgresDSN: cfg.History.PostgresDSN, |
|
155
|
MySQLEnabled: cfg.History.MySQL.Host != "" && cfg.History.PostgresDSN == "", |
|
156
|
MySQL: cfg.History.MySQL, |
|
157
|
RequireSASL: cfg.RequireSASL, |
|
158
|
DefaultChannelModes: cfg.DefaultChannelModes, |
|
159
|
} |
|
160
|
|
|
161
|
var buf bytes.Buffer |
|
162
|
if err := ircdTemplate.Execute(&buf, data); err != nil { |
|
163
|
return nil, err |
|
164
|
} |
|
165
|
return buf.Bytes(), nil |
|
166
|
} |
|
167
|
|