summaryrefslogtreecommitdiffstats
path: root/main.go
blob: fb71444f0158a73b5824de0b47207300214713c7 (plain)
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
package main

import (
	"crypto/tls"
	"errors"
	"fmt"
	"log"
	"os"

	"flag"
	"github.com/go-irc/irc"

	"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"`
	Bots            struct {
		Hosts []string		`yaml:"hosts"`
		Games wat.BotGameConfig	`yaml:"games"`
	} `yaml:"bots"`
	Admins		struct {
		Hosts []string		`yaml:"hosts"`
	}				`yaml:"admins"`
	Channels 	struct {
		Join      []string	`yaml:"join"`
		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
}

func main() {
	var configPathArg string
	flag.StringVar(&configPathArg, "config", "config.yaml", "Path to configuration file")
	flag.Parse()
	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,
	}
	watConfig := wat.WatConfig{
		AutoJoinChannels:  config.Channels.Join,
		PermittedChannels: config.Channels.Permitted,
		IgnoredHosts:      config.Ignores.Hosts,
		AdminHosts:        config.Admins.Hosts,
		BotHosts:          config.Bots.Hosts,
		BotGames:          config.Bots.Games,
	}
	tcpConf := &tls.Config{
		InsecureSkipVerify: !config.Server.TlsVerify,
	}
	conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port), tcpConf)
	if err != nil {
		fmt.Println("err " + err.Error())
		return
	}
	wwat := wat.NewWatBot(&ircConfig, &watConfig, conn)
	wwat.Run()
}