all repos — paprika @ 3a6f4cb1fc53b08fb810de6293a22718bb8ce9ef

go rewrite of taigabot

use yaml instead :(
Anthony DeDominic adedomin@gmail.com
Sun, 21 Nov 2021 17:41:51 -0500
commit

3a6f4cb1fc53b08fb810de6293a22718bb8ce9ef

parent

6056e2af58b42f154b2d9bf203b954d315bafaaf

9 files changed, 67 insertions(+), 132 deletions(-)

jump to
M config/config.goconfig/config.go

@@ -1,23 +1,26 @@

package config import ( + "fmt" + "io" "log" "os" - "strings" - "github.com/adedomin/indenttext" + "gopkg.in/yaml.v3" ) -var ( - Nick = "paprika" - Pass = "" - Host = "irc.rizon.net:6667" - Sasl = "" - Tls = false - ChanJoinStr = "JOIN #taigobot-test" - DbPath = "" - ApiKeys = make(map[string]string) -) +type Config struct { + Nick string + Pass string + Host string + Sasl string + Tls bool + Channels []string + DbPath string + ApiKeys map[string]string +} + +var C Config func init() { configPath := ""

@@ -57,53 +60,14 @@ }

} defer file.Close() - firstChannel := true - var chanList strings.Builder - err = indenttext.Parse(file, func (parents []string, item string, typeof indenttext.ItemType) bool { - if len(parents) == 1 && typeof == indenttext.Value { - switch parents[0] { - case "nick": - Nick = item - case "pass": - Pass = item - case "host": - Host = item - case "sasl": - Sasl = item - case "tls": - if item == "true" { - Tls = true - } else { - Tls = false - } - case "channels": - if firstChannel { - chanList.WriteString(item) - firstChannel = false - } else { - chanList.WriteByte(',') - chanList.WriteString(item) - } - case "db-path": - DbPath = item - } - } else if len(parents) == 2 && typeof == indenttext.Value { - if parents[0] == "api-keys" { - ApiKeys[parents[1]] = item - } - } - return false - }) + data, err := io.ReadAll(file) + err = yaml.Unmarshal(data, &C) if err != nil { log.Fatal(err) } - if chanList.Len() > 0 { - ChanJoinStr = splitChannelList(chanList.String()) - } - - if DbPath == "" { - DbPath = getDbPath() + if C.DbPath == "" { + C.DbPath = getDbPath() } }
M config/confpath.goconfig/confpath.go

@@ -7,23 +7,23 @@ "os"

"path" ) -//go:embed example.conf +//go:embed example.yaml var exampleConfig []byte func configPaths() []string { var configChoices []string // from most desirable to least desirable config path if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" { - configChoices = append(configChoices, path.Join(xdgConfigHome, "paprika.conf")) + configChoices = append(configChoices, path.Join(xdgConfigHome, "paprika.yml")) } if home := os.Getenv("HOME"); home != "" { - configChoices = append(configChoices, path.Join(home, ".config", "paprika.conf")) + configChoices = append(configChoices, path.Join(home, ".config", "paprika.yml")) } systemdConfigDir := os.Getenv("CONFIGURATION_DIRECTORY") if systemdConfigDir != "" { - configChoices = append(configChoices, path.Join(systemdConfigDir, "paprika.conf")) + configChoices = append(configChoices, path.Join(systemdConfigDir, "paprika.yml")) } else { - configChoices = append(configChoices, path.Join("/etc", "paprika.conf")) + configChoices = append(configChoices, path.Join("/etc", "paprika.yml")) } return configChoices }

@@ -52,7 +52,7 @@ }

} if file == nil { log.Print("Error: Failed to create config file.") - for i, _ := range configChoices { + for i := range configChoices { log.Printf("- reason: %s", errs[i]) } os.Exit(1)

@@ -82,7 +82,7 @@ }

} log.Print("Error: Could not open any config paths.") - for i, _ := range configChoices { + for i := range configChoices { log.Printf("- %s", errs[i]) } os.Exit(1)
D config/example.conf

@@ -1,37 +0,0 @@

-# IRCd Parameters -nick: paprika1234 -# make sure to add the port at the end with a colon -host: irc.rizon.net:6667 -# use the word true or false for this field -tls: false - -# Sever password, sent as "PASS :%s", pass -# NOTE (xyz:\n:) is declaring a key with no values. -pass: - # put your pass here or make it a name: value pair -: - -# SASL password (cert support tbd) for nickserv identification -sasl: - # put your pass here or make it a name: value pair -: - -# database, and other state, directory -# defaults to XDG_DATA_HOME or -# STATE_DIRECTORY if using StateDirectory= in a systemd service unit file -db-path: - # e.g. /var/lib/mybot/state -: - -# a newline delimited list of channels -channels: - # the (#) is a comment, use content start marker (') to escape it. - # for syntax-highlighting reasons, you can terminate content with a (') as well. - '#taigobot-test' -: - -# : by itself terminates a list -# API Keys are stored in a map by IDENT: value -api-keys: - test: fdsafdasfdsafdsa -:
A config/example.yml

@@ -0,0 +1,26 @@

+# IRCd Parameters +nick: paprika1234 +# make sure to add the port at the end with a colon +host: irc.rizon.net:6667 +# use the word true or false for this field +tls: false + +# Sever password, sent as ("PASS :%s", pass) +#pass: yourpass + +# SASL password (cert support tbd) for nickserv identification +#sasl: mynickserv-pass + +# database, and other state, directory +# defaults to XDG_DATA_HOME or +# STATE_DIRECTORY if using StateDirectory= in a systemd service unit file +#db-path: /var/lib/mybot/state + +# a newline delimited list of channels +channels: + - '#taigobot-test' + +# : by itself terminates a list +# API Keys are stored in a map by IDENT: value +#api-keys: +# - somesite: somekey
M config/helpers.goconfig/helpers.go

@@ -8,20 +8,14 @@ "path"

"strings" ) -func splitChannelList(channels string) string { - if len(channels) /* len() on string is bytelen */ + 5 /* for "JOIN " */ <= 510 { - return "JOIN " + channels + "\r\n" - } - - // channel lenghts longer than 510bytes have to be split - chans := strings.Split(channels, ",") +func SplitChannelList(channels []string) string { lineSize := 0 first := true var ret strings.Builder // Splits configured list of channels into a safe set of commands - for _, channel := range chans { + for _, channel := range channels { if lineSize + len(channel) > 510 { lineSize = 0 first = true
M database/db.godatabase/db.go

@@ -16,7 +16,7 @@ }

func Open() (*badger.DB, error) { db, err := badger.Open( - badger.DefaultOptions(path.Join(config.DbPath, "badger")), + badger.DefaultOptions(path.Join(config.C.DbPath, "badger")), ) if err != nil { return nil, err
M go.modgo.mod

@@ -1,6 +1,6 @@

module git.icyphox.sh/paprika -go 1.17 +go 1.16 require ( github.com/dgraph-io/badger/v3 v3.2103.2

@@ -9,19 +9,4 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2

gopkg.in/irc.v3 v3.1.4 ) -require ( - github.com/adedomin/indenttext v0.0.0-20211119223124-29d4292dd20b - github.com/cespare/xxhash v1.1.0 // indirect - github.com/cespare/xxhash/v2 v2.1.1 // indirect - github.com/dgraph-io/ristretto v0.1.0 // indirect - github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect - github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect - github.com/golang/protobuf v1.3.1 // indirect - github.com/golang/snappy v0.0.3 // indirect - github.com/google/flatbuffers v1.12.1 // indirect - github.com/klauspost/compress v1.12.3 // indirect - github.com/pkg/errors v0.9.1 // indirect - go.opencensus.io v0.22.5 // indirect - golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect -) +require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
M go.sumgo.sum

@@ -2,8 +2,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=

github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/adedomin/indenttext v0.0.0-20211119223124-29d4292dd20b h1:8LTaSMTmm2VPw860DZtPSRHokEna95eEOFIfdvsKwi8= -github.com/adedomin/indenttext v0.0.0-20211119223124-29d4292dd20b/go.mod h1:0W4Kq5zZA0pHRMal7C+rcxF5bQeFQl75XwlV6SBJXeU= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=

@@ -48,8 +46,10 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=

github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=

@@ -139,10 +139,13 @@ google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=

google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/irc.v3 v3.1.4 h1:DYGMRFbtseXEh+NadmMUFzMraqyuUj4I3iWYFEzDZPc= gopkg.in/irc.v3 v3.1.4/go.mod h1:shO2gz8+PVeS+4E6GAny88Z0YVVQSxQghdrMVGQsR9s= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
M main.gomain.go

@@ -102,7 +102,7 @@

func ircHandler(c *irc.Client, m *irc.Message) { switch m.Command { case "001": - c.Write(config.ChanJoinStr) + c.Write(config.SplitChannelList(config.C.Channels)) case "PRIVMSG": if isCTCPmessage(m) { handleCTCPMessage(c, m)

@@ -113,17 +113,17 @@ }

} func main() { - conn, err := net.Dial("tcp", config.Host) + conn, err := net.Dial("tcp", config.C.Host) if err != nil { log.Fatal(err) } defer conn.Close() ircConfig := irc.ClientConfig{ - Nick: config.Nick, - Pass: config.Pass, + Nick: config.C.Nick, + Pass: config.C.Pass, User: "paprikabot", - Name: config.Nick, + Name: config.C.Nick, Handler: irc.HandlerFunc(ircHandler), }