summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorPratyush Desai2024-09-28 19:43:10 +0200
committerPratyush Desai2024-09-28 19:43:10 +0200
commita475bc2f428c6fb6905f3f2896b5477a6e361f32 (patch)
tree3ba1171b3795f90ecabea7d217b56643dff2b3b9 /main.go
parentf4a9607770cc4f84431332ab4cdb7e292d037275 (diff)
parent0a90b6e483bcf6d57030cc7f26f2a3b1a819b37e (diff)
downloadwatbot-a475bc2f428c6fb6905f3f2896b5477a6e361f32.tar.gz
watbot-a475bc2f428c6fb6905f3f2896b5477a6e361f32.tar.bz2
watbot-a475bc2f428c6fb6905f3f2896b5477a6e361f32.zip
Merge pull request 'Implement configuration file + automatic channel joining' (#14) from config into master
Reviewed-on: https://git.com.de/LibertaCasa/watbot/pulls/14
Diffstat (limited to 'main.go')
-rw-r--r--main.go117
1 files changed, 95 insertions, 22 deletions
diff --git a/main.go b/main.go
index e817bcc..c242571 100644
--- a/main.go
+++ b/main.go
@@ -1,40 +1,113 @@
package main
-import "fmt"
-import "crypto/tls"
+import (
+ "crypto/tls"
+ "errors"
+ "fmt"
+ "log"
+ "os"
-import "github.com/go-irc/irc"
-import "github.com/namsral/flag"
+ "flag"
+ "github.com/go-irc/irc"
-import "git.circuitco.de/self/watbot/wat"
+ "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"`
+ 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() {
- pass := flag.String("pass", "", "password")
+ var configPathArg string
+ flag.StringVar(&configPathArg, "config", "config.yaml", "Path to configuration file")
flag.Parse()
- fmt.Printf("PASS len %d\n", len(*pass))
- config := irc.ClientConfig{
- Nick: "watt",
- Pass: *pass,
- User: "wat",
- Name: "wat",
+ 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{
- PermittedChannels: []string{
- "#lucy",
- "#sweden",
- },
- IgnoredHosts: []string{
- "tripsit/user/creatonez",
- },
+ AutoJoinChannels: config.Channels.Join,
+ PermittedChannels: config.Channels.Permitted,
+ IgnoredHosts: config.Ignores.Hosts,
+ AdminHosts: config.Admins.Hosts,
}
tcpConf := &tls.Config{
- InsecureSkipVerify: true,
+ InsecureSkipVerify: !config.Server.TlsVerify,
}
- conn, err := tls.Dial("tcp", "127.0.0.1:6697", tcpConf)
+ 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(&config, &watConfig, conn)
+ wwat := wat.NewWatBot(&ircConfig, &watConfig, conn)
wwat.Run()
}