2018-10-20 01:40:15 +02:00
|
|
|
package main
|
|
|
|
|
2024-09-21 16:06:04 +02:00
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-10-25 02:04:35 +02:00
|
|
|
|
2024-09-21 16:06:04 +02:00
|
|
|
"flag"
|
|
|
|
"github.com/go-irc/irc"
|
2018-10-20 01:58:30 +02:00
|
|
|
|
2024-09-21 16:06:04 +02:00
|
|
|
"git.circuitco.de/self/watbot/wat"
|
|
|
|
"github.com/creasty/defaults"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Watbot watConfig `yaml:"watbot"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type watConfig struct {
|
|
|
|
Nick string `yaml:"nick"`
|
|
|
|
Pass string `yaml:"pass"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Name string `yaml:"name"`
|
2024-09-22 17:42:49 +02:00
|
|
|
Bots struct {
|
|
|
|
Hosts []string `yaml:"hosts"`
|
|
|
|
Games wat.BotGameConfig `yaml:"games"`
|
|
|
|
} `yaml:"bots"`
|
2024-09-21 16:06:04 +02:00
|
|
|
Admins struct {
|
|
|
|
Hosts []string `yaml:"hosts"`
|
|
|
|
} `yaml:"admins"`
|
|
|
|
Channels struct {
|
2024-09-21 18:40:29 +02:00
|
|
|
Join []string `yaml:"join"`
|
2024-09-21 16:06:04 +02:00
|
|
|
Permitted []string `yaml:"permitted"`
|
|
|
|
} `yaml:"channels"`
|
|
|
|
Ignores struct {
|
|
|
|
Hosts []string `yaml:"hosts"`
|
|
|
|
} `yaml:"ignores"`
|
|
|
|
Server struct {
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port int `default:"6697" yaml:"port"`
|
|
|
|
TlsVerify bool `default:"true" yaml:"tls_verify"`
|
|
|
|
} `yaml:"server"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfig(configPath string) (*watConfig, error) {
|
|
|
|
allConfig := Config{}
|
|
|
|
|
|
|
|
buffer, err := os.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not read configuration file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(buffer, &allConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not parse configuration file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &allConfig.Watbot
|
|
|
|
defaults.Set(config)
|
|
|
|
|
|
|
|
if config.Server.Host == "" {
|
|
|
|
return nil, errors.New("Shall I play wattery to know where to connect to?")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Name != "" && config.Nick == "" {
|
|
|
|
config.Nick = config.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Nick != "" && config.User == "" {
|
|
|
|
config.User = config.Nick
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Name == "" || config.Nick == "" || config.User == "" {
|
|
|
|
return nil, errors.New("Don't know who I am.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
2018-10-20 01:40:15 +02:00
|
|
|
|
|
|
|
func main() {
|
2024-09-21 16:06:04 +02:00
|
|
|
var configPathArg string
|
|
|
|
flag.StringVar(&configPathArg, "config", "config.yaml", "Path to configuration file")
|
2018-10-20 01:58:30 +02:00
|
|
|
flag.Parse()
|
2024-09-21 16:06:04 +02:00
|
|
|
log.Println("Starting with configuration:", configPathArg)
|
|
|
|
|
|
|
|
config, err := readConfig(configPathArg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ircConfig := irc.ClientConfig{
|
|
|
|
Nick: config.Nick,
|
|
|
|
Pass: config.Pass,
|
|
|
|
User: config.User,
|
|
|
|
Name: config.Name,
|
2018-10-20 01:40:15 +02:00
|
|
|
}
|
2019-01-04 10:55:40 +01:00
|
|
|
watConfig := wat.WatConfig{
|
2024-09-21 18:40:29 +02:00
|
|
|
AutoJoinChannels: config.Channels.Join,
|
2024-09-21 16:06:04 +02:00
|
|
|
PermittedChannels: config.Channels.Permitted,
|
|
|
|
IgnoredHosts: config.Ignores.Hosts,
|
|
|
|
AdminHosts: config.Admins.Hosts,
|
2024-09-22 17:42:49 +02:00
|
|
|
BotHosts: config.Bots.Hosts,
|
|
|
|
BotGames: config.Bots.Games,
|
2019-01-04 10:55:40 +01:00
|
|
|
}
|
2018-10-21 13:57:23 +02:00
|
|
|
tcpConf := &tls.Config{
|
2024-09-21 16:06:04 +02:00
|
|
|
InsecureSkipVerify: !config.Server.TlsVerify,
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
2024-09-21 16:06:04 +02:00
|
|
|
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port), tcpConf)
|
2018-10-21 13:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("err " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-09-21 16:06:04 +02:00
|
|
|
wwat := wat.NewWatBot(&ircConfig, &watConfig, conn)
|
2018-10-21 13:57:23 +02:00
|
|
|
wwat.Run()
|
2018-10-20 01:40:15 +02:00
|
|
|
}
|